001: package com.technoetic.xplanner.upgrade;
002:
003: import java.lang.reflect.InvocationTargetException;
004: import java.lang.reflect.Method;
005: import java.sql.SQLException;
006: import java.util.ArrayList;
007: import java.util.HashMap;
008: import java.util.Iterator;
009: import java.util.List;
010: import java.util.Map;
011: import java.util.Vector;
012:
013: import net.sf.hibernate.HibernateException;
014: import org.apache.log4j.Logger;
015:
016: import com.technoetic.xplanner.domain.Integration;
017: import com.technoetic.xplanner.domain.Person;
018: import com.technoetic.xplanner.domain.RoleAssociation;
019: import com.technoetic.xplanner.domain.Task;
020: import com.technoetic.xplanner.domain.TimeEntry;
021: import com.technoetic.xplanner.domain.UserStory;
022: import com.technoetic.xplanner.history.HistoricalEvent;
023: import com.technoetic.xplanner.security.SecurityHelper;
024: import com.technoetic.xplanner.security.auth.Permission;
025: import com.technoetic.xplanner.util.LogUtil;
026:
027: public class CleanUpDuplicateUsers extends
028: HibernateMigrationTaskSupport {
029: private static final Logger LOG = LogUtil.getLogger();
030: public static boolean isCaseSensitive = false;
031: public static boolean readCaseSensitiveValueFromProperties = true;
032: List userList = null;
033:
034: public CleanUpDuplicateUsers() {
035: super ("cleanup_duplicate_users", 5);
036: }
037:
038: protected void setUp() throws Exception {
039: super .setUp();
040: userList = session
041: .find("select person from Person person order by id asc");
042: }
043:
044: protected void migrate() {
045: initCaseSensitive();
046: LOG
047: .debug("==============================================================");
048: LOG.debug("The userId casesensitive property is set to: "
049: + isCaseSensitive);
050:
051: Map usersToBeCleand = getUserIdsToBeDeletedMap();
052: LOG
053: .debug("==============================================================");
054: LOG.debug("There are " + usersToBeCleand.size()
055: + " users to be cleaned up.");
056: //cleanUp.printUsersToBeCleaned(usersToBeCleand);
057: Iterator iterator = usersToBeCleand.keySet().iterator();
058: while (iterator.hasNext()) {
059: Person userToBeCleaned = (Person) iterator.next();
060: Vector usersToBeDeleted = (Vector) usersToBeCleand
061: .get(userToBeCleaned);
062: LOG
063: .debug("==============================================================");
064: LOG.debug("Valid user: " + userToBeCleaned);
065: for (int a = 0; a < usersToBeDeleted.size(); a++) {
066: LOG.debug(" Invalid user: "
067: + (Person) usersToBeDeleted.get(a));
068: cleanUser(userToBeCleaned, (Person) usersToBeDeleted
069: .get(a));
070: }
071: }
072: }
073:
074: private void printUsersToBeCleaned(Map usersToBeCleand) {
075: Iterator it = usersToBeCleand.keySet().iterator();
076: while (it.hasNext()) {
077: Person personToBeCleaned = (Person) it.next();
078: //Should us ArrayList instead
079: Vector v = (Vector) usersToBeCleand.get(personToBeCleaned);
080: LOG.info("id=" + personToBeCleaned.getId() + ", "
081: + personToBeCleaned.getUserId());
082: for (int i = 0; i < v.size(); i++) {
083: Person personToBeDeleted = (Person) v.get(i);
084: LOG.info(" id=" + personToBeDeleted.getId()
085: + ", userId=" + personToBeDeleted.getUserId());
086: }
087: }
088:
089: List list = null;
090: try {
091: list = session
092: .find("select person from Person person where userId like 'defun%'");
093: } catch (HibernateException e) {
094: e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
095: }
096:
097: Iterator iter = list.iterator();
098: LOG.info("Number of defunc users: " + list.size());
099: while (iter.hasNext()) {
100: Person p = (Person) iter.next();
101: LOG.info(" id=" + p.getId() + ", userId=" + p.getUserId());
102: }
103:
104: }
105:
106: private void cleanUser(Person userToBeCleaned,
107: Person userToBeDeleted) {
108: try {
109: changeObject(Permission.class,
110: "select permission from Permission permission where permission.principalId="
111: + userToBeDeleted.getId(),
112: "setPrincipalId", userToBeCleaned.getId());
113: changeObject(Permission.class,
114: "select permission from Permission permission where permission.resourceId="
115: + userToBeDeleted.getId(), "setResourceId",
116: userToBeCleaned.getId());
117: changeObject(HistoricalEvent.class,
118: "select history from HistoricalEvent history where history.personId="
119: + userToBeDeleted.getId(), "setPersonId",
120: userToBeCleaned.getId());
121: changeObject(HistoricalEvent.class,
122: "select history from HistoricalEvent history where history.targetObjectId="
123: + userToBeDeleted.getId(),
124: "setTargetObjectId", userToBeCleaned.getId());
125: changeObject(Integration.class,
126: "select integration from Integration integration where integration.personId="
127: + userToBeDeleted.getId(), "setPersonId",
128: userToBeCleaned.getId());
129: changeObject(TimeEntry.class,
130: "select time_entry from TimeEntry time_entry where time_entry.person1Id="
131: + userToBeDeleted.getId(), "setPerson1Id",
132: userToBeCleaned.getId());
133: changeObject(TimeEntry.class,
134: "select time_entry from TimeEntry time_entry where time_entry.person2Id="
135: + userToBeDeleted.getId(), "setPerson2Id",
136: userToBeCleaned.getId());
137: changeObject(Task.class,
138: "select task from Task task where task.acceptorId="
139: + userToBeDeleted.getId(), "setAcceptorId",
140: userToBeCleaned.getId());
141: changeObject(UserStory.class,
142: "select userStory from UserStory userStory where userStory.trackerId="
143: + userToBeDeleted.getId(), "setTrackerId",
144: userToBeCleaned.getId());
145:
146: changeCustomerInUserStory(userToBeCleaned, userToBeDeleted);
147: changeRoleAssociation(userToBeCleaned, userToBeDeleted);
148: session.delete(userToBeDeleted);
149: session.flush();
150: session.connection().commit();
151: LOG.debug(" Invalid user deleted: " + userToBeDeleted);
152: } catch (HibernateException e) {
153: e.printStackTrace();
154: } catch (SQLException e) {
155: e.printStackTrace();
156: }
157: }
158:
159: private void changeCustomerInUserStory(Person userToBeCleaned,
160: Person userToBeDeleted) throws HibernateException {
161: List list = session
162: .find("select userStory from UserStory userStory where userStory.customer="
163: + userToBeDeleted.getId());
164: Iterator iterator = list.iterator();
165: while (iterator.hasNext()) {
166: UserStory object = (UserStory) iterator.next();
167: LOG.debug(" Object to be changed: " + object);
168: object.setCustomer(userToBeCleaned);
169: session.update(object);
170: LOG.debug(" Object changed to: " + object);
171: }
172: }
173:
174: private void changeRoleAssociation(Person userToBeCleaned,
175: Person userToBeDeleted) throws HibernateException {
176:
177: List listAssociation = session
178: .find("select roleAssociation from RoleAssociation roleAssociation where"
179: + " roleAssociation.personId="
180: + userToBeCleaned.getId()
181: + " or roleAssociation.personId="
182: + userToBeDeleted.getId());
183: Iterator listAssociationItarator = listAssociation.iterator();
184: while (listAssociationItarator.hasNext()) {
185: RoleAssociation roleAssociationToBeDeleted = (RoleAssociation) listAssociationItarator
186: .next();
187: RoleAssociation roleAssociationToBeSaved = new RoleAssociation(
188: roleAssociationToBeDeleted.getProjectId(),
189: userToBeCleaned.getId(), roleAssociationToBeDeleted
190: .getRoleId());
191: List list = session.find("select roleAssociation "
192: + " from RoleAssociation roleAssociation "
193: + " where role_id="
194: + roleAssociationToBeSaved.getRoleId()
195: + " and person_id="
196: + roleAssociationToBeSaved.getPersonId()
197: + " and project_id="
198: + roleAssociationToBeSaved.getProjectId());
199: if (list.isEmpty()) {
200: session.delete(roleAssociationToBeDeleted);
201: session.saveOrUpdate(roleAssociationToBeSaved);
202: }
203: }
204: }
205:
206: private void changeObject(Class clazz, String query, String method,
207: int userId) throws HibernateException {
208: try {
209: Method m = clazz.getDeclaredMethod(method,
210: new Class[] { int.class });
211: List list = session.find(query);
212: Iterator it = list.iterator();
213: while (it.hasNext()) {
214: Object object = (Object) it.next();
215: LOG.debug(" Object to be changed: " + object);
216: m.invoke(object, new Object[] { new Integer(userId) });
217: session.update(object);
218: LOG.debug(" Object changed to: " + object);
219: }
220: } catch (NoSuchMethodException e) {
221: e.printStackTrace();
222: } catch (IllegalAccessException e) {
223: e.printStackTrace();
224: } catch (InvocationTargetException e) {
225: e.printStackTrace();
226: }
227:
228: }
229:
230: private void initCaseSensitive() {
231: if (readCaseSensitiveValueFromProperties) {
232: isCaseSensitive = SecurityHelper
233: .isAuthenticationCaseSensitive();
234: }
235: }
236:
237: private Map getUserIdsToBeDeletedMap() {
238: Map usersToBeCleand = new HashMap();
239: ArrayList proceedIds = new ArrayList();
240: for (int i = 0; i < userList.size(); i++) {
241: Person personToBeCleaned = (Person) userList.get(i);
242: Vector usersToBeDeleted = new Vector();
243: if (!personToBeCleaned.isHidden()) {
244: for (int ii = 0; ii < userList.size(); ii++) {
245: Person personToCompare = (Person) userList.get(ii);
246: if (!proceedIds.contains(new Integer(
247: personToBeCleaned.getId()))
248: && !proceedIds.contains(new Integer(
249: personToCompare.getId()))
250: && personToBeCleaned.getId() != personToCompare
251: .getId()) {
252: if (isCaseSensitive) {
253: if (personToBeCleaned.getUserId().equals(
254: personToCompare.getUserId())) {
255: usersToBeDeleted.add(personToCompare);
256: proceedIds.add(new Integer(
257: personToCompare.getId()));
258: }
259: } else {
260: if (personToBeCleaned
261: .getUserId()
262: .equalsIgnoreCase(
263: personToCompare.getUserId())) {
264: usersToBeDeleted.add(personToCompare);
265: proceedIds.add(new Integer(
266: personToCompare.getId()));
267: }
268: }
269: }
270: }
271: proceedIds.add(new Integer(personToBeCleaned.getId()));
272: }
273: if (usersToBeDeleted.size() > 0) {
274: usersToBeCleand
275: .put(personToBeCleaned, usersToBeDeleted);
276: }
277: }
278: return usersToBeCleand;
279: }
280:
281: public static void main(String[] arg) throws Exception {
282: new CleanUpDuplicateUsers().run();
283:
284: }
285: }
|