Last active
August 29, 2015 14:09
-
-
Save pedrosan7os/d919e4f7e90b9c91b59b to your computer and use it in GitHub Desktop.
ClearDeletedGroupRelations
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 pt.ist.fenix.webapp; | |
import java.lang.reflect.InvocationTargetException; | |
import java.lang.reflect.Method; | |
import java.sql.Connection; | |
import java.sql.ResultSet; | |
import java.sql.Statement; | |
import java.util.ArrayList; | |
import java.util.HashMap; | |
import java.util.List; | |
import java.util.Map; | |
import net.sourceforge.fenixedu.domain.accessControl.AcademicAuthorizationGroup; | |
import net.sourceforge.fenixedu.domain.accessControl.DepartmentPresidentStrategy; | |
import net.sourceforge.fenixedu.domain.accessControl.PersistentCampusEmployeeGroup; | |
import net.sourceforge.fenixedu.domain.accessControl.PersistentDelegateStudentsGroup; | |
import net.sourceforge.fenixedu.domain.accessControl.PersistentDelegatesGroup; | |
import net.sourceforge.fenixedu.domain.accessControl.PersistentManagersOfUnitSiteGroup; | |
import net.sourceforge.fenixedu.domain.accessControl.PersistentMembersLinkGroup; | |
import net.sourceforge.fenixedu.domain.accessControl.PersistentResearchAuthorGroup; | |
import net.sourceforge.fenixedu.domain.accessControl.PersistentRoleGroup; | |
import net.sourceforge.fenixedu.domain.accessControl.PersistentVigilancyGroup; | |
import net.sourceforge.fenixedu.domain.accessControl.academicAdministration.AcademicOperationType; | |
import net.sourceforge.fenixedu.domain.person.RoleType; | |
import net.sourceforge.fenixedu.domain.util.email.Recipient; | |
import net.sourceforge.fenixedu.domain.util.email.Sender; | |
import net.sourceforge.fenixedu.util.ConnectionManager; | |
import org.fenixedu.bennu.core.domain.groups.PersistentDifferenceGroup; | |
import org.fenixedu.bennu.core.domain.groups.PersistentDifferenceGroup_Base; | |
import org.fenixedu.bennu.core.domain.groups.PersistentGroup; | |
import org.fenixedu.bennu.core.domain.groups.PersistentIntersectionGroup; | |
import org.fenixedu.bennu.core.domain.groups.PersistentUnionGroup; | |
import org.fenixedu.bennu.core.groups.DifferenceGroup; | |
import org.fenixedu.bennu.core.groups.Group; | |
import org.fenixedu.bennu.core.groups.IntersectionGroup; | |
import org.fenixedu.bennu.core.groups.NobodyGroup; | |
import org.fenixedu.bennu.core.groups.UnionGroup; | |
import org.fenixedu.bennu.portal.domain.MenuItem; | |
import org.fenixedu.bennu.scheduler.custom.CustomTask; | |
import pt.ist.fenixframework.DomainObject; | |
import pt.ist.fenixframework.FenixFramework; | |
public class ClearDeletedGroupRelations extends CustomTask { | |
@Override | |
public void runTask() throws Exception { | |
// Removed entirely | |
cleanupRelations(PersistentDelegatesGroup.class, NobodyGroup.get()); | |
cleanupRelations(PersistentDelegateStudentsGroup.class, NobodyGroup.get()); | |
cleanupRelations(PersistentResearchAuthorGroup.class, NobodyGroup.get()); | |
cleanupRelations(AcademicAuthorizationGroup.get(AcademicOperationType.MANAGE_CONTRIBUTORS).toPersistentGroup(), | |
NobodyGroup.get()); | |
// Optional removes | |
cleanupRelations(PersistentManagersOfUnitSiteGroup.class, NobodyGroup.get()); // Extracted to fenixedu-learning | |
cleanupRelations(PersistentVigilancyGroup.class, NobodyGroup.get()); // Extracted to fenixedu-ist-vigilancies | |
cleanupRelations(PersistentCampusEmployeeGroup.class, NobodyGroup.get()); //Extracted to fenixedu-ist-giaf-contracts | |
cleanupRelations(PersistentMembersLinkGroup.class, NobodyGroup.get()); //Extracted to fenixedu-ist-legacy | |
cleanupRelations(new DepartmentPresidentStrategy().toPersistentGroup(), NobodyGroup.get()); //Extracted to fenixedu-ist-giaf-contracts | |
Map<RoleType, Group> roles = new HashMap<>(); | |
roles.put(RoleType.ADIST_INSTITUCIONAL_PROJECTS_MANAGER, NobodyGroup.get()); | |
roles.put(RoleType.ADIST_PROJECTS_MANAGER, NobodyGroup.get()); | |
roles.put(RoleType.ADMINISTRATOR, NobodyGroup.get()); | |
roles.put(RoleType.CMS_MANAGER, NobodyGroup.get()); | |
roles.put(RoleType.CONTACT_ADMIN, NobodyGroup.get()); | |
roles.put(RoleType.CREDITS_MANAGER, NobodyGroup.get()); | |
roles.put(RoleType.DEGREE_ADMINISTRATIVE_OFFICE, NobodyGroup.get()); | |
roles.put(RoleType.DELEGATE, NobodyGroup.get()); | |
roles.put(RoleType.DEVELOPER, NobodyGroup.get()); | |
roles.put(RoleType.IDENTIFICATION_CARD_MANAGER, NobodyGroup.get()); | |
roles.put(RoleType.INSTITUCIONAL_PROJECTS_MANAGER, NobodyGroup.get()); | |
roles.put(RoleType.ISTID_INSTITUCIONAL_PROJECTS_MANAGER, NobodyGroup.get()); | |
roles.put(RoleType.ISTID_PROJECTS_MANAGER, NobodyGroup.get()); | |
roles.put(RoleType.IT_PROJECTS_MANAGER, NobodyGroup.get()); | |
roles.put(RoleType.MASTER_DEGREE_CANDIDATE, NobodyGroup.get()); | |
roles.put(RoleType.PARKING_MANAGER, NobodyGroup.get()); | |
roles.put(RoleType.PERSONNEL_SECTION, NobodyGroup.get()); | |
roles.put(RoleType.PROJECTS_MANAGER, NobodyGroup.get()); | |
roles.put(RoleType.SEMINARIES_COORDINATOR, NobodyGroup.get()); | |
roles.put(RoleType.TREASURY, NobodyGroup.get()); | |
roles.put(RoleType.RESOURCE_MANAGER, NobodyGroup.get()); | |
roles.put(RoleType.WEBSITE_MANAGER, NobodyGroup.get()); | |
roles.put(RoleType.DEPARTMENT_ADMINISTRATIVE_OFFICE, NobodyGroup.get()); | |
roles.put(RoleType.DEPARTMENT_CREDITS_MANAGER, NobodyGroup.get()); | |
roles.put(RoleType.EMPLOYEE, NobodyGroup.get()); | |
roles.put(RoleType.EXAM_COORDINATOR, NobodyGroup.get()); | |
roles.put(RoleType.GRANT_OWNER, NobodyGroup.get()); | |
roles.put(RoleType.TUTORSHIP, NobodyGroup.get()); | |
List<String> oids = findOids(PersistentRoleGroup.class); | |
for (String oid : oids) { | |
PersistentRoleGroup group = FenixFramework.getDomainObject(oid); | |
if (roles.containsKey(group.getRole().getRoleType())) { | |
taskLog("Cleaning %s\n", group.expression()); | |
cleanupRelations(group, roles.get(group.getRole().getRoleType())); | |
} | |
} | |
} | |
private void cleanupRelations(Class<? extends DomainObject> type, Group replacement) throws Exception { | |
List<String> oids = findOids(type); | |
taskLog("There are %s entries for %s\n", oids.size(), type.getName()); | |
for (String oid : oids) { | |
PersistentGroup group = FenixFramework.getDomainObject(oid); | |
cleanupRelations(group, replacement); | |
} | |
} | |
private void cleanupRelations(PersistentGroup group, Group replacement) { | |
int senders = 0; | |
int recipients = 0; | |
int menus = 0; | |
for (Sender sender : group.getSenderAsMembersSet()) { | |
sender.setMembers(replacement); | |
senders++; | |
} | |
for (Recipient recipient : group.getRecipientAsMembersSet()) { | |
recipient.setMembers(replacement); | |
recipients++; | |
for (final Sender sender : recipient.getSendersSet()) { | |
sender.removeRecipients(recipient); | |
if (sender.getRecipientsSet().isEmpty()) { | |
sender.setMembers(replacement); | |
senders++; | |
} | |
} | |
} | |
for (MenuItem menu : group.getMenuItemSet()) { | |
if (replacement instanceof NobodyGroup) { | |
menu.delete(); | |
} else { | |
menu.setAccessGroup(replacement); | |
} | |
menus++; | |
} | |
for (PersistentUnionGroup union : group.getUnionsSet()) { | |
cleanupRelations(union, replace(union, group, replacement)); | |
} | |
for (PersistentIntersectionGroup intersection : group.getIntersectionsSet()) { | |
cleanupRelations(intersection, replace(intersection, group, replacement)); | |
} | |
for (PersistentDifferenceGroup difference : group.getDifferenceAtFirstSet()) { | |
cleanupRelations(difference, replaceFirst(difference, group, replacement)); | |
} | |
for (PersistentDifferenceGroup difference : group.getDifferenceAtRestSet()) { | |
cleanupRelations(difference, replaceRest(difference, group, replacement)); | |
} | |
if (senders > 0 || recipients > 0) { | |
taskLog("\tGroup: %s - %s: %d menus %d senders and %d recipients deleted\n", group.getExternalId(), | |
group.expression(), menus, senders, recipients); | |
} | |
} | |
private Group replace(PersistentUnionGroup composition, PersistentGroup group, Group replacement) { | |
return UnionGroup.of(composition.getChildrenSet().stream().map(g -> g.equals(group) ? replacement : g.toGroup())); | |
} | |
private Group replace(PersistentIntersectionGroup composition, PersistentGroup group, Group replacement) { | |
return IntersectionGroup.of(composition.getChildrenSet().stream().map(g -> g.equals(group) ? replacement : g.toGroup())); | |
} | |
private Group replaceFirst(PersistentDifferenceGroup composition, PersistentGroup group, Group replacement) { | |
return DifferenceGroup.between(replacement, composition.getRestSet().stream().map(PersistentGroup::toGroup)); | |
} | |
private Group replaceRest(PersistentDifferenceGroup composition, PersistentGroup group, Group replacement) { | |
return DifferenceGroup.between(firstAtDifference(composition), | |
composition.getRestSet().stream().map(g -> g.equals(group) ? replacement : g.toGroup())); | |
} | |
//Hack because getFirst should be visible but it's not. | |
private Group firstAtDifference(PersistentDifferenceGroup composition) { | |
try { | |
Method method = PersistentDifferenceGroup_Base.class.getDeclaredMethod("getFirst"); | |
method.setAccessible(true); | |
return ((PersistentGroup) method.invoke(composition)).toGroup(); | |
} catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | |
| InvocationTargetException e) { | |
e.printStackTrace(); | |
return NobodyGroup.get(); | |
} | |
} | |
private List<String> findOids(Class<? extends DomainObject> type) throws Exception { | |
Connection connection = ConnectionManager.getCurrentSQLConnection(); | |
try (Statement stmt = connection.createStatement()) { | |
ResultSet rs = | |
stmt.executeQuery("SELECT OID FROM `PERSISTENT_GROUP` WHERE ((OID >> 32) & 0xffff) = (SELECT DOMAIN_CLASS_ID from FF$DOMAIN_CLASS_INFO where DOMAIN_CLASS_NAME = '" | |
+ type.getName() + "')"); | |
List<String> oids = new ArrayList<>(); | |
while (rs.next()) { | |
oids.add(rs.getString("OID")); | |
} | |
rs.close(); | |
return oids; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment