KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > cowsultants > itracker > web > actions > ImportDataProcessAction


1 /*
2  * This software was designed and created by Jason Carroll.
3  * Copyright (c) 2002, 2003, 2004 Jason Carroll.
4  * The author can be reached at jcarroll@cowsultants.com
5  * ITracker website: http://www.cowsultants.com
6  * ITracker forums: http://www.cowsultants.com/phpBB/index.php
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it only under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  */

18
19 package cowsultants.itracker.web.actions;
20
21 import java.io.*;
22 import java.rmi.*;
23 import java.util.*;
24 import javax.ejb.*;
25 import javax.rmi.*;
26 import javax.naming.*;
27 import javax.servlet.*;
28 import javax.servlet.http.*;
29
30 import org.apache.commons.beanutils.*;
31 import org.apache.struts.action.*;
32 import org.apache.struts.upload.*;
33 import org.apache.struts.util.*;
34 import org.apache.struts.validator.*;
35
36 import cowsultants.itracker.ejb.client.exceptions.*;
37 import cowsultants.itracker.ejb.client.interfaces.*;
38 import cowsultants.itracker.ejb.client.models.*;
39 import cowsultants.itracker.ejb.client.resources.*;
40 import cowsultants.itracker.ejb.client.util.*;
41 import cowsultants.itracker.web.util.*;
42
43
44 public class ImportDataProcessAction extends ITrackerAction {
45
46     public ImportDataProcessAction () {
47     }
48
49     public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
50         ActionErrors errors = new ActionErrors();
51
52         if(! isLoggedIn(request, response)) {
53             return mapping.findForward("login");
54         }
55
56         if(! hasPermission(UserUtilities.PERMISSION_USER_ADMIN, request, response)) {
57             return mapping.findForward("unauthorized");
58         }
59
60         try {
61             InitialContext ic = new InitialContext();
62
63             HttpSession session = request.getSession(true);
64             UserModel importer = (UserModel) session.getAttribute(Constants.USER_KEY);
65             if(importer == null) {
66                 return mapping.findForward("unauthorized");
67             }
68
69             ImportDataModel model = (ImportDataModel) session.getAttribute(Constants.IMPORT_DATA_KEY);
70             GenericModel[] importData = model.getData();
71             Logger.logDebug("Importing configuration data.");
72             createConfig(model, importer, ic);
73             Logger.logDebug("Importing user data.");
74             createUsers(model, importer, ic);
75             Logger.logDebug("Importing project data.");
76             createProjects(model, importer, ic);
77             Logger.logDebug("Importing issue data.");
78             createIssues(model, importer, ic);
79             Logger.logDebug("Import complete.");
80
81         } catch(Exception JavaDoc e) {
82             Logger.logError("Exception while importing data.", e);
83             errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("itracker.web.error.system"));
84         }
85
86         if(! errors.isEmpty()) {
87             saveErrors(request, errors);
88             return mapping.findForward("error");
89         } else {
90             errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("itracker.web.error.importexport.importcomplete"));
91             saveErrors(request, errors);
92         }
93
94         return mapping.findForward("adminindex");
95     }
96
97     private boolean createConfig(ImportDataModel model, UserModel importer, InitialContext ic) {
98         try {
99             Object JavaDoc scRef = ic.lookup("java:comp/env/" + SystemConfiguration.JNDI_NAME);
100             SystemConfigurationHome scHome = (SystemConfigurationHome) PortableRemoteObject.narrow(scRef, SystemConfigurationHome.class);
101             SystemConfiguration sc = scHome.create();
102
103             GenericModel[] importData = model.getData();
104             for(int i = 0; i < importData.length; i++) {
105                 if(importData[i] instanceof ConfigurationModel && ! model.getExistingModel(i)) {
106                     ConfigurationModel configItem = (ConfigurationModel) importData[i];
107                     ConfigurationModel newConfigItem = sc.createConfigurationItem(configItem);
108                     configItem.setId(newConfigItem.getId());
109
110                     // Now add a new language key
111
String JavaDoc key = SystemConfigurationUtilities.getLanguageKey(configItem);
112                     sc.updateLanguageItem(new LanguageModel(ImportExportUtilities.EXPORT_LOCALE_STRING, key, configItem.getName()));
113                     ITrackerResources.clearKeyFromBundles(key, true);
114                 } else if(importData[i] instanceof CustomFieldModel && ! model.getExistingModel(i)) {
115                     CustomFieldModel customField = (CustomFieldModel) importData[i];
116                     CustomFieldModel newCustomField = sc.createCustomField(customField);
117                     customField.setId(newCustomField.getId());
118
119                     // Now add new language keys. One for the field and then add one for for
120
// each option that exists.
121
String JavaDoc key = CustomFieldUtilities.getCustomFieldLabelKey(customField.getId());
122                     sc.updateLanguageItem(new LanguageModel(ImportExportUtilities.EXPORT_LOCALE_STRING, key, customField.getName()));
123                     ITrackerResources.clearKeyFromBundles(key, true);
124                     if(customField.getFieldType() == CustomFieldUtilities.TYPE_LIST) {
125                         for(int j = 0; j < customField.getOptions().length; j++) {
126                             String JavaDoc optionKey = CustomFieldUtilities.getCustomFieldOptionLabelKey(customField.getId(), customField.getOptions()[j].getId());
127                             sc.updateLanguageItem(new LanguageModel(ImportExportUtilities.EXPORT_LOCALE_STRING, optionKey, customField.getOptions()[j].getName()));
128                             ITrackerResources.clearKeyFromBundles(optionKey, true);
129                         }
130                     }
131                 }
132             }
133             sc.resetConfigurationCache();
134         } catch(Exception JavaDoc e) {
135             return false;
136         }
137
138         return true;
139     }
140
141     private boolean createUsers(ImportDataModel model, UserModel importer, InitialContext ic) {
142         try {
143             Object JavaDoc uhRef = ic.lookup("java:comp/env/" + UserHandler.JNDI_NAME);
144             UserHandlerHome uhHome = (UserHandlerHome) PortableRemoteObject.narrow(uhRef, UserHandlerHome.class);
145             UserHandler uh = uhHome.create();
146
147             GenericModel[] importData = model.getData();
148             for(int i = 0; i < importData.length; i++) {
149                 if(importData[i] instanceof UserModel && ! model.getExistingModel(i)) {
150                     UserModel user = (UserModel) importData[i];
151                     user.setRegistrationType(UserUtilities.REGISTRATION_TYPE_IMPORT);
152                     if(model.getCreatePasswords()) {
153                         user.setPassword(UserUtilities.encryptPassword(user.getLogin()));
154                     }
155                     user.setLogin(user.getLogin());
156                     UserModel newUser = uh.createUser(user);
157                     user.setId(newUser.getId());
158                 }
159             }
160         } catch(Exception JavaDoc e) {
161             return false;
162         }
163
164         return true;
165     }
166
167     private boolean createProjects(ImportDataModel model, UserModel importer, InitialContext ic) {
168         try {
169             Object JavaDoc phRef = ic.lookup("java:comp/env/" + ProjectHandler.JNDI_NAME);
170             ProjectHandlerHome phHome = (ProjectHandlerHome) PortableRemoteObject.narrow(phRef, ProjectHandlerHome.class);
171             ProjectHandler ph = phHome.create();
172
173             GenericModel[] importData = model.getData();
174             for(int i = 0; i < importData.length; i++) {
175                 if(importData[i] instanceof ProjectModel && ! model.getExistingModel(i)) {
176                     ProjectModel project = (ProjectModel) importData[i];
177                     ProjectModel newProject = ph.createProject(project);
178                     project.setId(newProject.getId());
179
180                     HashSet owners = new HashSet();
181                     for(int j = 0; j < project.getOwners().length; j++) {
182                         owners.add(project.getOwners()[j].getId());
183                     }
184                     ph.setProjectOwners(project, owners);
185
186                     HashSet fields = new HashSet();
187                     for(int j = 0; j < project.getCustomFields().length; j++) {
188                         fields.add(project.getCustomFields()[j].getId());
189                     }
190                     ph.setProjectFields(project, fields);
191
192                     ComponentModel[] components = project.getComponents();
193                     for(int j = 0; j < components.length; j++) {
194                         ComponentModel newComponent = ph.addProjectComponent(project.getId(), components[j]);
195                         components[j].setId(newComponent.getId());
196                     }
197
198                     VersionModel[] versions = project.getVersions();
199                     for(int j = 0; j < versions.length; j++) {
200                         VersionModel newVersion = ph.addProjectVersion(project.getId(), versions[j]);
201                         versions[j].setId(newVersion.getId());
202                     }
203                 }
204             }
205         } catch(Exception JavaDoc e) {
206             return false;
207         }
208
209         return true;
210     }
211
212     private boolean createIssues(ImportDataModel model, UserModel importer, InitialContext ic) {
213         try {
214             Object JavaDoc ihRef = ic.lookup("java:comp/env/" + IssueHandler.JNDI_NAME);
215             IssueHandlerHome ihHome = (IssueHandlerHome) PortableRemoteObject.narrow(ihRef, IssueHandlerHome.class);
216             IssueHandler ih = ihHome.create();
217
218             GenericModel[] importData = model.getData();
219             for(int i = 0; i < importData.length; i++) {
220                 if(importData[i] instanceof IssueModel && ! model.getExistingModel(i)) {
221                     IssueModel issue = (IssueModel) importData[i];
222                     IssueModel newIssue = ih.createIssue(issue, issue.getProjectId(), issue.getCreatorId(), importer.getId());
223                     issue.setId(newIssue.getId());
224
225                     // Assign the issue
226
if(issue.getOwner() != null) {
227                         ih.assignIssue(issue.getId(), issue.getOwnerId(), importer.getId());
228                     }
229
230                     // Now set Issue Custom Fields
231
IssueFieldModel[] fields = issue.getFields();
232                     if(fields.length > 0) {
233                         for(int j = 0; j < fields.length; j++) {
234                             fields[j].setIssueId(issue.getId());
235                         }
236                         ih.setIssueFields(issue.getId(), issue.getFields());
237                     }
238
239                     // Now add all the issue history
240
IssueHistoryModel[] history = issue.getHistory();
241                     if(history.length > 0) {
242                         for(int j = 0; j < history.length; j++) {
243                             history[j].setIssueId(issue.getId());
244                             ih.addIssueHistory(history[j]);
245                         }
246                     }
247
248                     // Now add components and versions
249
HashSet components = new HashSet();
250                     ComponentModel[] componentsArray = issue.getComponents();
251                     if(componentsArray.length > 0) {
252                         for(int j = 0; j < componentsArray.length; j++) {
253                             components.add(componentsArray[j].getId());
254                         }
255                         ih.setIssueComponents(issue.getId(), components, importer.getId());
256                     }
257                     HashSet versions = new HashSet();
258                     VersionModel[] versionsArray = issue.getVersions();
259                     if(versionsArray.length > 0) {
260                         for(int j = 0; j < versionsArray.length; j++) {
261                             versions.add(versionsArray[j].getId());
262                         }
263                         ih.setIssueVersions(issue.getId(), versions, importer.getId());
264                     }
265
266                     // Now add any attachments
267
IssueAttachmentModel[] attachments = issue.getAttachments();
268                     if(attachments.length > 0) {
269                         for(int j = 0; j < history.length; j++) {
270                             attachments[j].setIssueId(issue.getId());
271                             ih.addIssueAttachment(attachments[j], null);
272                         }
273                     }
274                 }
275             }
276         } catch(Exception JavaDoc e) {
277             return false;
278         }
279
280         return true;
281     }
282
283     private void printArray(GenericModel[] models) {
284         for(int i = 0; i < models.length; i++) {
285             Logger.logDebug(i + ") " + models[i].toString());
286         }
287     }
288
289 }
290   
Popular Tags