Source Code Cross Referenced for CleanUpDuplicateUsers.java in  » Project-Management » XPlanner-0.7b7 » com » technoetic » xplanner » upgrade » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Project Management » XPlanner 0.7b7 » com.technoetic.xplanner.upgrade 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.