Skip to content

Instantly share code, notes, and snippets.

@JoaoRoxoNeves
Last active August 29, 2015 14:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save JoaoRoxoNeves/d1f887bab813dc0dc084 to your computer and use it in GitHub Desktop.
Save JoaoRoxoNeves/d1f887bab813dc0dc084 to your computer and use it in GitHub Desktop.
package org.fenixedu.academic.task;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import org.fenixedu.academic.domain.candidacy.Ingression;
import org.fenixedu.academic.domain.candidacy.StudentCandidacy;
import org.fenixedu.academic.domain.student.Registration;
import org.fenixedu.academic.util.ConnectionManager;
import org.fenixedu.bennu.core.domain.Bennu;
import org.fenixedu.bennu.scheduler.custom.CustomTask;
import org.fenixedu.commons.i18n.LocalizedString;
import pt.ist.fenixframework.Atomic.TxMode;
public class DumpIngressions extends CustomTask {
@Override
public void runTask() throws Exception {
int classId = findNextClassId("org.fenixedu.academic.domain.candidacy.IngressionType");
StringBuilder builder = new StringBuilder();
long oid = ((long) classId << 32);
Map<Ingression, Long> oidMap = new HashMap<>();
for (Ingression ingression : Ingression.values()) {
String code = ingression.getName();
String description = new LocalizedString(Locale.getDefault(), ingression.getFullDescription()).json().toString();
int hasEntryPhase = is(ingression.hasEntryPhase());
int isExternalCourseChange = is(ingression == Ingression.MCE);
int isInternalCourseChange = is(ingression == Ingression.MCI);
int isTransfer = is(ingression == Ingression.TRF);
int isMiddleAndSuperiorCourses = is(ingression == Ingression.CEA02);
int isOver23 = is(ingression == Ingression.CM23);
int isInternal2ndCycleAccess = is(ingression == Ingression.CIA2C);
int isIsolatedCurricularUnits = is(ingression == Ingression.STC);
int isInternal3rdCycleAccess = is(ingression == Ingression.CIA3C);
int isDirectAccessFrom1stCycle = is(ingression == Ingression.DA1C);
int isHandicappedContingent = is(ingression == Ingression.CNA07);
int isFirstCycleAttribution = is(ingression == Ingression.AG1C);
int isReIngression = is(ingression == Ingression.RI);
String rootId = Bennu.getInstance().getExternalId();
oidMap.put(ingression, ++oid);
out(builder,
"INSERT INTO INGRESSION_TYPE (OID, CODE, DESCRIPTION, HAS_ENTRY_PHASE , IS_EXTERNAL_DEGREE_CHANGE, IS_INTERNAL_DEGREE_CHANGE, IS_TRANSFER, IS_MIDDLE_AND_SUPERIOR_COURSES, IS_OVER23, IS_INTERNAL2ND_CYCLE_ACCESS , IS_ISOLATED_CURRICULAR_UNITS , IS_INTERNAL3RD_CYCLE_ACCESS, IS_DIRECT_ACCESS_FROM1ST_CYCLE, IS_HANDICAPPED_CONTINGENT, IS_FIRST_CYCLE_ATTRIBUTION, IS_RE_INGRESSION, OID_ROOT_DOMAIN_OBJECT) "
+ "VALUES (%s, '%s', '%s', %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s); -- %s\n", oid,
code, description, hasEntryPhase, isExternalCourseChange, isInternalCourseChange, isTransfer,
isMiddleAndSuperiorCourses, isOver23, isInternal2ndCycleAccess, isIsolatedCurricularUnits,
isInternal3rdCycleAccess, isDirectAccessFrom1stCycle, isHandicappedContingent, isFirstCycleAttribution,
isReIngression, rootId, code);
}
for (Registration registration : Bennu.getInstance().getRegistrationsSet()) {
if (registration.getIngression() != null) {
out(builder, "UPDATE REGISTRATION SET OID_INGRESSION_TYPE = '%s' WHERE OID = %s;\n",
oidMap.get(registration.getIngression()), registration.getExternalId());
}
}
Set<StudentCandidacy> collect =
Bennu.getInstance().getCandidaciesSet().stream().filter(candidacy -> candidacy instanceof StudentCandidacy)
.map(StudentCandidacy.class::cast).collect(Collectors.toSet());
for (StudentCandidacy studentCandidacy : collect) {
if (studentCandidacy.getIngression() != null) {
out(builder, "UPDATE CANDIDACY SET OID_INGRESSION_TYPE = '%s' WHERE OID = %s;\n",
oidMap.get(studentCandidacy.getIngression()), studentCandidacy.getExternalId());
}
}
output("dumpIngressions.sql", builder.toString().getBytes());
taskLog(builder.toString());
}
private static void out(StringBuilder builder, String format, Object... args) {
builder.append(String.format(format, args));
}
private static final int is(boolean bool) {
return bool ? 1 : 0;
}
private int findNextClassId(String type) throws SQLException {
int classId;
try (Statement stmt = ConnectionManager.getCurrentSQLConnection().createStatement()) {
try (ResultSet rs = stmt.executeQuery("select MAX(DOMAIN_CLASS_ID) from FF$DOMAIN_CLASS_INFO")) {
rs.next();
classId = rs.getInt(1) + 1;
}
}
try (PreparedStatement stmt =
ConnectionManager.getCurrentSQLConnection().prepareStatement(
"INSERT INTO FF$DOMAIN_CLASS_INFO (DOMAIN_CLASS_ID, DOMAIN_CLASS_NAME) VALUES (?,?)")) {
stmt.setInt(1, classId);
stmt.setString(2, type);
stmt.executeUpdate();
}
return classId;
}
@Override
public TxMode getTxMode() {
return TxMode.READ;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment