-
-
Save jcarvalho/90205db88544e340dc2e 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.Map; | |
import org.fenixedu.academic.domain.student.StudentStatuteType; | |
import org.fenixedu.academic.util.Bundle; | |
import org.fenixedu.academic.util.ConnectionManager; | |
import org.fenixedu.bennu.core.domain.Bennu; | |
import org.fenixedu.bennu.core.i18n.BundleUtil; | |
import org.fenixedu.bennu.scheduler.custom.CustomTask; | |
import org.fenixedu.commons.i18n.LocalizedString; | |
import pt.ist.fenixframework.Atomic.TxMode; | |
public class DumpStatutes extends CustomTask { | |
@Override | |
public void runTask() throws Exception { | |
int classId = findNextClassId("org.fenixedu.academic.domain.student.StatuteType"); | |
StringBuilder builder = new StringBuilder(); | |
long oid = ((long) classId << 32); | |
Map<StudentStatuteType, Long> oidMap = new HashMap<>(); | |
for (StudentStatuteType enumType : StudentStatuteType.values()) { | |
LocalizedString typeName = BundleUtil.getLocalizedString(Bundle.ENUMERATION, "StudentStatuteType." + enumType.name()); | |
int grantsWorkingStudentStatute = is(enumType == StudentStatuteType.WORKING_STUDENT); | |
int associativeLeaderStatute = is(enumType == StudentStatuteType.ASSOCIATIVE_LEADER); | |
int specialSeasonGrantedByRequest = is(enumType == StudentStatuteType.SPECIAL_SEASON_GRANTED_BY_REQUEST); | |
int grantOwnerStatute = is(enumType == StudentStatuteType.SAS_GRANT_OWNER); | |
int seniorStatute = is(enumType == StudentStatuteType.SENIOR); | |
int handicappedStatute = is(enumType == StudentStatuteType.HANDICAPPED); | |
int specialSeasonGranted = is(enumType.isSpecialSeasonGranted()); | |
int active = is(true); | |
int explicitCreation = is(enumType.isExplicitCreation()); | |
int visible = is(enumType.isVisible()); | |
long myId = ++oid; | |
oidMap.put(enumType, myId); | |
out(builder, | |
"INSERT INTO STATUTE_TYPE (OID, CODE, NAME, WORKING_STUDENT_STATUTE, ASSOCIATIVE_LEADER_STATUTE, " | |
+ "SPECIAL_SEASON_GRANTED_BY_REQUEST, GRANT_OWNER_STATUTE, SENIOR_STATUTE, HANDICAPPED_STATUTE, SPECIAL_SEASON_GRANTED, ACTIVE, EXPLICIT_CREATION, VISIBLE, OID_ROOT_DOMAIN_OBJECT) " | |
+ "VALUES (%s, '%s', '%s', %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s); -- %s\n", myId, | |
enumType.name(), typeName.json().toString(), grantsWorkingStudentStatute, associativeLeaderStatute, | |
specialSeasonGrantedByRequest, grantOwnerStatute, seniorStatute, handicappedStatute, specialSeasonGranted, | |
active, explicitCreation, visible, Bennu.getInstance().getExternalId(), enumType.name()); | |
} | |
oidMap.forEach((type, id) -> { | |
out(builder, "UPDATE STUDENT_STATUTE SET OID_TYPE = %s WHERE STATUTE_TYPE = '%s';\n", id, type.name()); | |
}); | |
output("statutes.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