-
-
Save jcarvalho/cbda742415f67129bc39 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 org.fenixedu.academic.domain.candidacy.Ingression; | |
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); | |
} | |
oidMap.forEach((ingression, id) -> { | |
out(builder, "UPDATE REGISTRATION SET OID_INGRESSION_TYPE = '%s' WHERE INGRESSION = '%s';\n", id, ingression.name()); | |
out(builder, "UPDATE CANDIDACY SET OID_INGRESSION_TYPE = '%s' WHERE INGRESSION = '%s';\n", id, ingression.name()); | |
}); | |
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