Skip to content

Instantly share code, notes, and snippets.

@pedrosan7os
Last active August 29, 2015 14:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pedrosan7os/f07a9023a7cb7897e019 to your computer and use it in GitHub Desktop.
Save pedrosan7os/f07a9023a7cb7897e019 to your computer and use it in GitHub Desktop.
ExportTeacherAuthorizations
package pt.ist.fenix.task;
import java.util.Collection;
import java.util.Comparator;
import java.util.List;
import java.util.SortedSet;
import java.util.TreeSet;
import java.util.stream.Collectors;
import net.sourceforge.fenixedu.domain.AplicaTeacherAuthorization;
import net.sourceforge.fenixedu.domain.Department;
import net.sourceforge.fenixedu.domain.Employee;
import net.sourceforge.fenixedu.domain.ExecutionSemester;
import net.sourceforge.fenixedu.domain.ExternalTeacherAuthorization;
import net.sourceforge.fenixedu.domain.Teacher;
import net.sourceforge.fenixedu.domain.TeacherAuthorization;
import net.sourceforge.fenixedu.domain.organizationalStructure.AccountabilityTypeEnum;
import net.sourceforge.fenixedu.domain.organizationalStructure.DepartmentUnit;
import net.sourceforge.fenixedu.domain.organizationalStructure.EmployeeContract;
import net.sourceforge.fenixedu.domain.organizationalStructure.Unit;
import net.sourceforge.fenixedu.domain.personnelSection.contracts.GiafProfessionalData;
import net.sourceforge.fenixedu.domain.personnelSection.contracts.PersonContractSituation;
import net.sourceforge.fenixedu.domain.personnelSection.contracts.PersonProfessionalData;
import net.sourceforge.fenixedu.domain.personnelSection.contracts.ProfessionalCategory;
import net.sourceforge.fenixedu.domain.teacher.CategoryType;
import org.fenixedu.bennu.core.domain.Bennu;
import org.fenixedu.bennu.scheduler.custom.CustomTask;
import org.joda.time.DateTime;
import org.joda.time.Interval;
import org.joda.time.LocalDate;
import pt.ist.fenixframework.Atomic.TxMode;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
public class ExportTeacherAuthorizations extends CustomTask {
@Override
public TxMode getTxMode() {
return TxMode.READ;
}
@Override
public void runTask() throws Exception {
JsonArray auths = new JsonArray();
for (Teacher teacher : Bennu.getInstance().getTeachersSet()) {
export(auths, teacher);
}
output("auths.json", new GsonBuilder().setPrettyPrinting().create().toJson(auths).getBytes());
}
private void export(JsonArray array, Teacher teacher) {
Employee employee = teacher.getPerson().getEmployee();
if (employee != null) {
PersonProfessionalData professionalData = teacher.getPerson().getPersonProfessionalData();
if (professionalData != null) {
GiafProfessionalData giafProfessionalData = professionalData.getGiafProfessionalData();
SortedSet<PersonContractSituation> validPersonContractSituations =
new TreeSet<PersonContractSituation>(new Comparator<PersonContractSituation>() {
@Override
public int compare(PersonContractSituation c1, PersonContractSituation c2) {
int compare = c1.getBeginDate().compareTo(c2.getBeginDate());
return compare == 0 ? c1.getExternalId().compareTo(c2.getExternalId()) : compare;
}
});
validPersonContractSituations.addAll(giafProfessionalData.getValidPersonContractSituations());
for (final ExecutionSemester semester : Bennu.getInstance().getExecutionPeriodsSet()) {
Department department = getDepartment(employee, semester);
if (department != null) {
List<PersonContractSituation> collect =
validPersonContractSituations
.stream()
.filter(c -> {
DateTime start = c.getBeginDate().toDateTimeAtStartOfDay();
DateTime end =
c.getEndDate() != null ? c.getEndDate().toDateTimeAtStartOfDay() : new DateTime(
Long.MAX_VALUE);
Interval contractInterval = new Interval(start, end);
ProfessionalCategory professionalCategory = c.getProfessionalCategory();
return professionalCategory != null && professionalCategory.getCategoryType() != null
&& professionalCategory.getCategoryType().equals(CategoryType.TEACHER)
&& semester.getAcademicInterval().overlaps(contractInterval);
}).collect(Collectors.toList());
if (!collect.isEmpty()) {
PersonContractSituation personContractSituation = collect.get(collect.size() - 1);
export(array, teacher, department, semester, personContractSituation.getProfessionalCategory(),
personContractSituation.getEndDate());
}
}
}
}
}
for (TeacherAuthorization authorization : teacher.getAuthorizationSet()) {
if (authorization instanceof ExternalTeacherAuthorization) {
export(array, (ExternalTeacherAuthorization) authorization);
} else {
export(array, (AplicaTeacherAuthorization) authorization);
}
}
}
private Department getDepartment(Employee employee, ExecutionSemester semester) {
Collection<EmployeeContract> contracts =
(Collection<EmployeeContract>) employee.getPerson().getParentAccountabilities(
AccountabilityTypeEnum.WORKING_CONTRACT, EmployeeContract.class);
EmployeeContract first = null;
for (EmployeeContract contract : contracts) {
if (contract.belongsToPeriod(semester.getBeginDateYearMonthDay(), semester.getEndDateYearMonthDay())) {
if (first == null || first.getBeginDate().isAfter(contract.getBeginDate())) {
first = contract;
}
}
}
if (first != null) {
return getEmployeeDepartmentUnit(first.getUnit());
}
return null;
}
private Department getEmployeeDepartmentUnit(Unit unit) {
Collection<Unit> parentUnits = unit.getParentUnits();
if (unitDepartment(unit)) {
return ((DepartmentUnit) unit).getDepartment();
} else if (!parentUnits.isEmpty()) {
for (Unit parentUnit : parentUnits) {
if (unitDepartment(parentUnit)) {
return ((DepartmentUnit) parentUnit).getDepartment();
} else if (parentUnit.hasAnyParentUnits()) {
Department department = getEmployeeDepartmentUnit(parentUnit);
if (department != null) {
return department;
}
}
}
}
return null;
}
private boolean unitDepartment(Unit unit) {
return unit.isDepartmentUnit() && ((DepartmentUnit) unit).getDepartment() != null;
}
private void export(JsonArray auths, Teacher teacher, Department department, ExecutionSemester semester,
ProfessionalCategory category, LocalDate localDate) {
JsonObject auth = new JsonObject();
auth.addProperty("contracted", true);
auth.addProperty("teacher", teacher.getPerson().getUsername());
auth.addProperty("department", department.getExternalId());
auth.addProperty("semester", semester.getExternalId());
auth.add("category", exportCategory(category));
auth.addProperty("lessonHours", teacher.getMandatoryLessonHours(semester));
if (localDate != null && semester.getAcademicInterval().contains(localDate.toDateTimeAtStartOfDay())) {
auth.addProperty("revokeTime", localDate.toDateTimeAtStartOfDay().toString());
}
auths.add(auth);
}
private void export(JsonArray auths, ExternalTeacherAuthorization authorization) {
JsonObject auth = new JsonObject();
auth.addProperty("contracted", false);
auth.addProperty("teacher", authorization.getTeacher().getPerson().getUsername());
auth.addProperty("department", authorization.getDepartment().getExternalId());
auth.addProperty("semester", authorization.getExecutionSemester().getExternalId());
auth.add("category", exportCategory(authorization.getProfessionalCategory()));
auth.addProperty("lessonHours", authorization.getLessonHours());
auth.addProperty("authorizer", authorization.getAuthorizer().getUsername());
if (!authorization.getActive()) {
auth.addProperty("revoker", authorization.getRevoker().getUsername());
auth.addProperty("revokeTime", authorization.getUnactiveTime().toString());
}
auths.add(auth);
}
private JsonElement exportCategory(ProfessionalCategory category) {
JsonObject json = new JsonObject();
json.add("name", category.getName().toLocalizedString().json());
json.addProperty("weight", category.getWeight());
return json;
}
private void export(JsonArray array, AplicaTeacherAuthorization authorization) {
taskLog("aplica auth");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment