KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > tigris > scarab > util > xmlissues > ScarabIssues


1 package org.tigris.scarab.util.xmlissues;
2
3 /* ================================================================
4  * Copyright (c) 2000-2002 CollabNet. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are
8  * met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in the
15  * documentation and/or other materials provided with the distribution.
16  *
17  * 3. The end-user documentation included with the redistribution, if
18  * any, must include the following acknowlegement: "This product includes
19  * software developed by Collab.Net <http://www.Collab.Net/>."
20  * Alternately, this acknowlegement may appear in the software itself, if
21  * and wherever such third-party acknowlegements normally appear.
22  *
23  * 4. The hosted project names must not be used to endorse or promote
24  * products derived from this software without prior written
25  * permission. For written permission, please contact info@collab.net.
26  *
27  * 5. Products derived from this software may not use the "Tigris" or
28  * "Scarab" names nor may "Tigris" or "Scarab" appear in their names without
29  * prior written permission of Collab.Net.
30  *
31  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
32  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
33  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
34  * IN NO EVENT SHALL COLLAB.NET OR ITS CONTRIBUTORS BE LIABLE FOR ANY
35  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
37  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
38  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
39  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
40  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
41  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42  *
43  * ====================================================================
44  *
45  * This software consists of voluntary contributions made by many
46  * individuals on behalf of Collab.Net.
47  */

48
49 import java.io.File JavaDoc;
50 import java.text.ParseException JavaDoc;
51 import java.util.ArrayList JavaDoc;
52 import java.util.HashMap JavaDoc;
53 import java.util.HashSet JavaDoc;
54 import java.util.Iterator JavaDoc;
55 import java.util.List JavaDoc;
56 import java.util.Locale JavaDoc;
57 import java.util.Map JavaDoc;
58 import java.util.Set JavaDoc;
59
60 import org.apache.commons.collections.map.LinkedMap;
61 import org.apache.commons.logging.Log;
62 import org.apache.commons.logging.LogFactory;
63 import org.apache.fulcrum.localization.Localization;
64 import org.tigris.scarab.om.Activity;
65 import org.tigris.scarab.om.ActivityManager;
66 import org.tigris.scarab.om.ActivitySet;
67 import org.tigris.scarab.om.ActivitySetManager;
68 import org.tigris.scarab.om.ActivitySetType;
69 import org.tigris.scarab.om.ActivitySetTypeManager;
70 import org.tigris.scarab.om.ActivitySetTypePeer;
71 import org.tigris.scarab.om.Attachment;
72 import org.tigris.scarab.om.AttachmentManager;
73 import org.tigris.scarab.om.AttachmentType;
74 import org.tigris.scarab.om.Attribute;
75 import org.tigris.scarab.om.AttributeOption;
76 import org.tigris.scarab.om.AttributeValue;
77 import org.tigris.scarab.om.Depend;
78 import org.tigris.scarab.om.DependManager;
79 import org.tigris.scarab.om.Issue;
80 import org.tigris.scarab.om.IssueManager;
81 import org.tigris.scarab.om.IssueType;
82 import org.tigris.scarab.om.Module;
83 import org.tigris.scarab.om.ModuleManager;
84 import org.tigris.scarab.om.RModuleOption;
85 import org.tigris.scarab.om.RModuleOptionManager;
86 import org.tigris.scarab.om.ScarabUser;
87 import org.tigris.scarab.om.ScarabUserManager;
88 import org.tigris.scarab.util.ScarabConstants;
89
90 /**
91  * <p>This class manages the validation and importing of issues.</p>
92  *
93  * <p>This classes has a format dictated by the <a
94  * HREF="http://jakarta.apache.org/commons/betwixt/">Betwixt</a>
95  * parser. As the parser extracts elements out of the XML file, it
96  * looks for the public getters and setters in this class's signature
97  * and thereby makes determinations on what JavaBean methods to call
98  * in this class. Reading this source file in isolation: Doing so
99  * would make it look like you could do things like remove the data
100  * member 'issue' and its accessor methods because it looks as though
101  * they are unused, whereas they are in fact signals to the Betwixt
102  * parser: It reads them and interprets their presence and instruction
103  * to create instances of issues from the XML being parsed.</p>
104  *
105  * <p>Also, of note, the design of this class is that, it has two
106  * modes based off the setting of the inValidationMode class. When
107  * parsing w/ the {@link #inValidationMode} flag set, the db is not
108  * touched. The code just validates the XML's data content checking
109  * the users exist in the db, that the attributes and modules
110  * referenced already exit. A parse with the {@link
111  * #inValidationMode} set to false will do actual insert of the XML
112  * issues.</p>
113  *
114  * @author <a HREF="mailto:jon@latchkey.com">Jon S. Stevens</a>
115  * @author <a HREF="mailto:dlr@collab.net">Daniel Rall</a>
116  * @version $Id: ScarabIssues.java 9476 2005-03-16 22:56:11Z dabbous $
117  */

118 public class ScarabIssues implements java.io.Serializable JavaDoc
119 {
120     private static final Log LOG = LogFactory.getLog(ScarabIssues.class);
121
122     private XmlModule module = null;
123
124     /**
125      * Betwixt parser adds here instance of issues found in parsed xml.
126      */

127     private List JavaDoc issues = null;
128
129     private String JavaDoc importType = null;
130
131     private int importTypeCode = -1;
132     
133     private List JavaDoc allDependencies = new ArrayList JavaDoc();
134
135     /**
136      * Maps issue IDs from the XML file to IDs assigned by the DB.
137      */

138     private Map JavaDoc issueXMLMap = new HashMap JavaDoc();
139
140     /**
141      * Maps activity set IDs from the XML file to IDs assigned by the
142      * DB.
143      */

144     private Map JavaDoc activitySetIdMap = new HashMap JavaDoc();
145
146     /**
147      * Maps attachment IDs from the XML file to IDs assigned by the
148      * DB.
149      */

150     private Map JavaDoc attachmentIdMap = new HashMap JavaDoc();
151
152     /**
153      * Maps dependency IDs from the XML file to IDs assigned by the
154      * DB.
155      */

156     private List JavaDoc dependActivitySetId = new ArrayList JavaDoc();
157
158     private static final int CREATE_SAME_DB = 1;
159     private static final int CREATE_DIFFERENT_DB = 2;
160     private static final int UPDATE_SAME_DB = 3;
161
162     private static Attribute nullAttribute = null;
163
164     /**
165      * We default to be in validation mode. Insert only occurs
166      * post-validation.
167      */

168     private boolean inValidationMode = true;
169
170     /**
171      * A record of any errors encountered during the import. Set by
172      * ImportIssues after an instance is created during XML parsing.
173      */

174     ImportErrors importErrors;
175
176     /**
177      * The users referenced by the XML file.
178      */

179     private Set JavaDoc importUsers = new HashSet JavaDoc();
180
181     /**
182      * The current file attachment handling code has a security bug
183      * that can allow a user to see any file on the host that is
184      * readable by scarab. It is not easy to exploit this hole, and
185      * there are cases where we want to use the functionality and can
186      * be sure the hole is not being exploited. So adding a flag to
187      * disallow file attachments when importing through the UI.
188      *
189      * This flag is set by ImportIssues after our instance is created
190      * during XML parsing.
191      */

192     private boolean allowFileAttachments = false;
193     
194     public ScarabIssues()
195     {
196         issues = new ArrayList JavaDoc();
197         if (nullAttribute == null)
198         {
199             try
200             {
201                 nullAttribute = Attribute.getInstance(0);
202             }
203             catch (Exception JavaDoc e)
204             {
205                 LOG.warn("Could not assign nullAttribute", e);
206             }
207         }
208     }
209
210     /**
211      * Instances of this class will have their {@link
212      * #allowFileAttachments} flag set to this value upon
213      * instantiation. Current file attachment handling code has a
214      * security bug that can allow a user to see any file on the host
215      * that is readable by scarab. It is not easy to exploit this
216      * hole, and there are cases where we want to use the
217      * functionality and can be sure the hole is not being exploited.
218      * So adding a flag to allow file attachments under certain
219      * circumstances.
220      */

221     public void allowFileAttachments(boolean flag)
222     {
223         this.allowFileAttachments = flag;
224     }
225
226     public void inValidationMode(boolean flag)
227     {
228         inValidationMode = flag;
229     }
230
231     public void setImportType(String JavaDoc value)
232     {
233         this.importType = value;
234         if (importType.equals("create-same-db"))
235         {
236             importTypeCode = CREATE_SAME_DB;
237         }
238         else if (importType.equals("create-different-db"))
239         {
240             importTypeCode = CREATE_DIFFERENT_DB;
241         }
242         else if (importType.equals("update-same-db"))
243         {
244             importTypeCode = UPDATE_SAME_DB;
245         }
246     }
247
248     public String JavaDoc getImportType()
249     {
250         return this.importType;
251     }
252
253     public int getImportTypeCode()
254     {
255         return this.importTypeCode;
256     }
257
258     /**
259      * @return Map of original id -> new scarab id.
260      */

261     public Map JavaDoc getIDs()
262     {
263         return this.issueXMLMap;
264     }
265
266     public XmlModule getModule()
267     {
268         return this.module;
269     }
270
271     public void setModule(XmlModule module)
272     {
273         LOG.debug("Module.setModule(): " + module.getName());
274         this.module = module;
275     }
276
277     void doValidateUsers()
278         throws Exception JavaDoc
279     {
280         if (importUsers != null && !importUsers.isEmpty())
281         {
282             for (Iterator JavaDoc itr = importUsers.iterator(); itr.hasNext();)
283             {
284                 String JavaDoc userStr = (String JavaDoc)itr.next();
285                 try
286                 {
287                     ScarabUser user = ScarabUserManager.getInstance(userStr,
288                          getModule().getDomain());
289                     if (user == null)
290                     {
291                         throw new Exception JavaDoc(); //EXCEPTION
292
}
293                 }
294                 catch (Exception JavaDoc e)
295                 {
296                     String JavaDoc error = Localization.format(
297                         ScarabConstants.DEFAULT_BUNDLE_NAME,
298                         getLocale(),
299                         "CouldNotLocateUsername", userStr);
300                     importErrors.add(error);
301                 }
302             }
303         }
304     }
305
306     void doValidateDependencies()
307         throws Exception JavaDoc
308     {
309         if (allDependencies != null && !allDependencies.isEmpty())
310         {
311             for (Iterator JavaDoc itr = allDependencies.iterator(); itr.hasNext();)
312             {
313                 XmlActivity activity = (XmlActivity)itr.next();
314                 Dependency dependency = activity.getDependency();
315                 String JavaDoc child = (String JavaDoc)issueXMLMap.get(dependency.getChild());
316                 String JavaDoc parent = (String JavaDoc)issueXMLMap.get(dependency.getParent());
317                 if (parent == null || child == null)
318                 {
319                     LOG.debug("Could not find issues for parent '" + parent +
320                               "' and child '" + child + '\'');
321                     continue;
322                 }
323                 try
324                 {
325                     Issue parentIssueOM = IssueManager.getIssueById(parent);
326                     if (parentIssueOM == null)
327                     {
328                         throw new Exception JavaDoc(); //EXCEPTION
329
}
330                 }
331                 catch (Exception JavaDoc e)
332                 {
333                     String JavaDoc error = Localization.format(
334                         ScarabConstants.DEFAULT_BUNDLE_NAME,
335                         getLocale(),
336                         "CouldNotLocateParentDepend", parent);
337                     importErrors.add(error);
338                 }
339                 try
340                 {
341                     Issue childIssueOM = IssueManager.getIssueById(child);
342                     if (childIssueOM == null)
343                     {
344                         throw new Exception JavaDoc(); //EXCEPTION
345
}
346                 }
347                 catch (Exception JavaDoc e)
348                 {
349                     String JavaDoc error = Localization.format(
350                         ScarabConstants.DEFAULT_BUNDLE_NAME,
351                         getLocale(),
352                         "CouldNotLocateChildDepend", child);
353                     importErrors.add(error);
354                 }
355             }
356         }
357         allDependencies.clear();
358     }
359
360     void doHandleDependencies()
361         throws Exception JavaDoc
362     {
363         LOG.debug("Number of dependencies found: " + allDependencies.size());
364         for (Iterator JavaDoc itr = allDependencies.iterator(); itr.hasNext();)
365         {
366             Object JavaDoc[] data = (Object JavaDoc[])itr.next();
367             ActivitySet activitySetOM = (ActivitySet) data[0];
368             XmlActivity activity = (XmlActivity) data[1];
369             
370
371             Dependency dependency = activity.getDependency();
372             String JavaDoc child = (String JavaDoc)issueXMLMap.get(dependency.getChild());
373             String JavaDoc parent = (String JavaDoc)issueXMLMap.get(dependency.getParent());
374             if (parent == null || child == null)
375             {
376                 LOG.debug("Could not find issues: parent: " + parent + " child: " + child);
377                 continue;
378             }
379
380             if (getImportTypeCode() == UPDATE_SAME_DB)
381             {
382                 LOG.error("update-same-db import type not yet implemented");
383             }
384             else
385             {
386                 try
387                 {
388                     String JavaDoc type = dependency.getType();
389                     Depend newDependOM = DependManager.getInstance();
390                     Issue parentIssueOM = IssueManager.getIssueById(parent);
391                     Issue childIssueOM = IssueManager.getIssueById(child);
392                     newDependOM.setDefaultModule(parentIssueOM.getModule());
393                     newDependOM.setObservedId(parentIssueOM.getIssueId());
394                     newDependOM.setObserverId(childIssueOM.getIssueId());
395                     newDependOM.setDependType(type);
396                     LOG.debug("Dep: " + dependency.getId() + " Type: " + type + " Parent: " + parent + " Child: " + child);
397                     LOG.debug("XML Activity id: " + activity.getId());
398                     if (activity.isAddDependency())
399                     {
400                         parentIssueOM
401                           .doAddDependency(activitySetOM, newDependOM, childIssueOM, null);
402                         LOG.debug("Added Dep Type: " + type + " Parent: " + parent + " Child: " + child);
403                         LOG.debug("----------------------------------------------------");
404                     }
405                     else if (activity.isDeleteDependency())
406                     {
407                         parentIssueOM
408                           .doDeleteDependency(activitySetOM, newDependOM, null);
409                         LOG.debug("Deleted Dep Type: " + type + " Parent: " + parent + " Child: " + child);
410                         LOG.debug("----------------------------------------------------");
411                     }
412                     else if (activity.isUpdateDependency())
413                     {
414                         Depend oldDependOM = parentIssueOM.getDependency(childIssueOM);
415                         if (oldDependOM == null)
416                         {
417                             throw new Exception JavaDoc ("Whoops! Could not find the original dependency!"); //EXCEPTION
418
}
419                         // we definitely know we are doing an update here.
420
newDependOM.setDeleted(false);
421                         parentIssueOM
422                           .doChangeDependencyType(activitySetOM, oldDependOM, newDependOM, null);
423                         LOG.debug("Updated Dep Type: " + type + " Parent: " + parent + " Child: " + child);
424                         LOG.debug("Old Type: " + oldDependOM.getDependType().getName() + " New type: " + newDependOM.getDependType().getName());
425                         LOG.debug("----------------------------------------------------");
426                     }
427                 }
428                 catch (Exception JavaDoc e)
429                 {
430                     e.printStackTrace();
431                     throw e; //EXCEPTION
432
}
433             }
434         }
435     }
436
437     public List JavaDoc getIssues()
438     {
439         return issues;
440     }
441
442     public void addIssue(XmlIssue issue)
443         throws Exception JavaDoc
444     {
445         LOG.debug("Module.addIssue(): " + issue.getId());
446         try
447         {
448             if (inValidationMode)
449             {
450                 importErrors.setParseContext(module.getCode() + issue.getId());
451                 doIssueValidateEvent(getModule(), issue);
452             }
453             else
454             {
455                 doIssueEvent(getModule(), issue);
456             }
457         }
458         catch (Exception JavaDoc e)
459         {
460             e.printStackTrace();
461             throw e; //EXCEPTION
462
}
463         finally
464         {
465             importErrors.setParseContext(null);
466         }
467     }
468
469     /**
470      * Validates the data from a XML representation of an issue.
471      * Examines as much of the data as possible, even if errors are
472      * encountered in parent data.
473      *
474      * @param module The module containing <code>issue</code>.
475      * @param issue The issue to validate.
476      */

477     private void doIssueValidateEvent(XmlModule module, XmlIssue issue)
478         throws Exception JavaDoc
479     {
480         // Check for the existance of the module.
481
Module moduleOM = null;
482         try
483         {
484             moduleOM = ModuleManager.getInstance(module.getDomain(),
485                                                  module.getName(),
486                                                  module.getCode());
487             if (moduleOM == null)
488             {
489                 throw new Exception JavaDoc(); //EXCEPTION
490
}
491
492             // TODO: Handle user import. Until then, ignore the
493
// module's owner.
494
//importUsers.add(module.getOwner());
495
}
496         catch (Exception JavaDoc e)
497         {
498             Object JavaDoc[] args = { module.getName(), module.getCode(),
499                               module.getDomain() };
500             String JavaDoc error = Localization.format(
501                 ScarabConstants.DEFAULT_BUNDLE_NAME,
502                 getLocale(),
503                 "CouldNotFindModule", args);
504             importErrors.add(error);
505         }
506
507         // Check for the existance of the issue type.
508
IssueType issueTypeOM = null;
509         try
510         {
511             issueTypeOM = IssueType.getInstance(issue.getArtifactType());
512             if (issueTypeOM == null)
513             {
514                 throw new Exception JavaDoc(); //EXCEPTION
515
}
516         }
517         catch (Exception JavaDoc e)
518         {
519             String JavaDoc error = Localization.format(
520                 ScarabConstants.DEFAULT_BUNDLE_NAME,
521                 getLocale(),
522                 "CouldNotFindIssueType", issue.getArtifactType());
523             importErrors.add(error);
524         }
525         if (!moduleOM.getRModuleIssueType(issueTypeOM).getActive())
526         {
527             String JavaDoc error = Localization.format(
528                 ScarabConstants.DEFAULT_BUNDLE_NAME,
529                 getLocale(),
530                 "IssueTypeInactive", issue.getArtifactType());
531             importErrors.add(error);
532         }
533         List JavaDoc moduleAttributeList = null;
534         if (moduleOM != null)
535         {
536             moduleAttributeList = moduleOM.getAttributes(issueTypeOM);
537         }
538
539         List JavaDoc activitySets = issue.getActivitySets();
540         for (Iterator JavaDoc itr = activitySets.iterator(); itr.hasNext();)
541         {
542             XmlActivitySet activitySet = (XmlActivitySet) itr.next();
543             if (activitySet.getCreatedBy() != null)
544             {
545                 importUsers.add(activitySet.getCreatedBy());
546             }
547             if (activitySet.getAttachment() != null)
548             {
549                 String JavaDoc attachCreatedBy = activitySet.getAttachment().getCreatedBy();
550                 if (attachCreatedBy != null)
551                 {
552                     importUsers.add(attachCreatedBy);
553                 }
554             }
555             
556             // Validate the activity set's type.
557
try
558             {
559                 ActivitySetType ttOM =
560                     ActivitySetTypeManager.getInstance(activitySet.getType());
561                 if (ttOM == null)
562                 {
563                     throw new Exception JavaDoc(); //EXCEPTION
564
}
565             }
566             catch (Exception JavaDoc e)
567             {
568                 String JavaDoc error = Localization.format(
569                     ScarabConstants.DEFAULT_BUNDLE_NAME,
570                     getLocale(),
571                     "CouldNotFindActivitySetType", activitySet.getType());
572                 importErrors.add(error);
573             }
574
575             // Validate the activity set's date.
576
validateDate(activitySet.getCreatedDate(), true);
577
578             List JavaDoc activities = activitySet.getActivities();
579             for (Iterator JavaDoc itrb = activities.iterator(); itrb.hasNext();)
580             {
581                 validateActivity(moduleOM, issueTypeOM, moduleAttributeList,
582                                  activitySet, (XmlActivity) itrb.next());
583             }
584         }
585     }
586
587     /**
588      * Validates an individual activity. A helper method for {@link
589      * #doIssueValidateEvent(XmlModule, XmlIssue)}.
590      *
591      * @param moduleOM
592      * @param issueTypeOM
593      * @param moduleAttributeList The attributes for
594      * <code>moduleOM</code> and <code>issueTypeOM</code>.
595      * @param activitySet The transaction which <code>activity</code>
596      * was a part of.
597      * @param activity The activity to validate.
598      * @see #doIssueValidateEvent(XmlModule, XmlIssue)
599      */

600     private void validateActivity(Module moduleOM, IssueType issueTypeOM,
601                                   List JavaDoc moduleAttributeList,
602                                   XmlActivitySet activitySet,
603                                   XmlActivity activity)
604     {
605         validateDate(activity.getEndDate(), false);
606         if (activity.getOldUser() != null)
607         {
608             importUsers.add(activity.getOldUser());
609         }
610         if (activity.getNewUser() != null)
611         {
612             importUsers.add(activity.getNewUser());
613         }
614         XmlAttachment activityAttachment = activity.getAttachment();
615         if (activityAttachment != null)
616         {
617             if (allowFileAttachments &&
618                 activityAttachment.getReconcilePath() &&
619                 !new File JavaDoc(activityAttachment.getFilename()).exists())
620             {
621                 String JavaDoc error = Localization.format
622                     (ScarabConstants.DEFAULT_BUNDLE_NAME, getLocale(),
623                      "CouldNotFindFileAttachment",
624                      activityAttachment.getFilename());
625                 importErrors.add(error);
626             }
627
628             validateDate(activityAttachment.getCreatedDate(), true);
629             validateDate(activityAttachment.getModifiedDate(), false);
630
631             String JavaDoc attachCreatedBy = activityAttachment.getCreatedBy();
632             if (attachCreatedBy != null)
633             {
634                 importUsers.add(attachCreatedBy);
635             }
636         }
637
638         // Get the Attribute associated with the Activity
639
Attribute attributeOM = null;
640         String JavaDoc activityAttribute = activity.getAttribute();
641         try
642         {
643             attributeOM = Attribute.getInstance(activityAttribute);
644             if (attributeOM == null)
645             {
646                 throw new Exception JavaDoc(); //EXCEPTION
647
}
648         }
649         catch (Exception JavaDoc e)
650         {
651             String JavaDoc error = Localization.format
652                 (ScarabConstants.DEFAULT_BUNDLE_NAME, getLocale(),
653                  "CouldNotFindGlobalAttribute", activityAttribute);
654             importErrors.add(error);
655         }
656
657         if (attributeOM != null)
658         {
659             if (attributeOM.equals(nullAttribute))
660             {
661                 // Add any dependency activities to a list for later
662
// processing.
663
if (isDependencyActivity(activity))
664                 {
665                     if (!isDuplicateDependency(activitySet))
666                     {
667                         allDependencies.add(activity);
668                         LOG.debug("-------------Stored Dependency # " +
669                                   allDependencies.size() + "-------------");
670                     }
671
672                     // Dependency activities don't require further
673
// validation.
674
return;
675                 }
676             }
677             else
678             {
679                 // The null attribute will never be in this list.
680
if (moduleAttributeList != null &&
681                     !moduleAttributeList.contains(attributeOM))
682                 {
683                     String JavaDoc error = Localization.format
684                         (ScarabConstants.DEFAULT_BUNDLE_NAME, getLocale(),
685                          "CouldNotFindRModuleAttribute", activityAttribute);
686                     importErrors.add(error);
687                 }
688                 else if (activity.getNewOption() != null)
689                 {
690                     // check for global options
691
AttributeOption attributeOptionOM = null;
692                     try
693                     {
694                         attributeOptionOM = AttributeOption
695                             .getInstance(attributeOM, activity.getNewOption(),
696                                          moduleOM, issueTypeOM);
697                         if (attributeOptionOM == null)
698                         {
699                             throw new Exception JavaDoc(); //EXCEPTION
700
}
701                     }
702                     catch (Exception JavaDoc e)
703                     {
704                         Object JavaDoc[] args = { activity.getNewOption(),
705                                           attributeOM.getName() };
706                         String JavaDoc error = Localization.format
707                             (ScarabConstants.DEFAULT_BUNDLE_NAME, getLocale(),
708                              "CouldNotFindAttributeOption", args);
709                         importErrors.add(error);
710                     }
711                     // check for module options
712
try
713                     {
714                         RModuleOption rmo = RModuleOptionManager
715                             .getInstance(moduleOM, issueTypeOM,
716                                          attributeOptionOM);
717                         if (rmo == null)
718                         {
719                             throw new Exception JavaDoc(); //EXCEPTION
720
}
721                     }
722                     catch (Exception JavaDoc e)
723                     {
724                         Object JavaDoc[] args = { activity.getNewOption(),
725                                           attributeOM.getName() };
726                         String JavaDoc error = Localization.format
727                             (ScarabConstants.DEFAULT_BUNDLE_NAME,
728                              getLocale(),
729                              "CouldNotFindModuleAttributeOption",
730                              args);
731                         importErrors.add(error);
732                     }
733                 }
734                 else if (activity.getOldOption() != null)
735                 {
736                     AttributeOption attributeOptionOM = null;
737                     try
738                     {
739                         attributeOptionOM = AttributeOption
740                             .getInstance(attributeOM, activity.getOldOption());
741                         if (attributeOptionOM == null)
742                         {
743                             throw new Exception JavaDoc(); //EXCEPTION
744
}
745                     }
746                     catch (Exception JavaDoc e)
747                     {
748                         String JavaDoc error = Localization.format
749                             (ScarabConstants.DEFAULT_BUNDLE_NAME, getLocale(),
750                              "CouldNotFindAttributeOption",
751                              activity.getOldOption());
752                         importErrors.add(error);
753                     }
754                     // check for module options
755
try
756                     {
757                         RModuleOption rmo = RModuleOptionManager
758                             .getInstance(moduleOM, issueTypeOM,
759                                          attributeOptionOM);
760                         if (rmo == null)
761                         {
762                             throw new Exception JavaDoc(); //EXCEPTION
763
}
764                     }
765                     catch (Exception JavaDoc e)
766                     {
767                         Object JavaDoc[] args = { activity.getOldOption(),
768                                           attributeOM.getName() };
769                         String JavaDoc error = Localization.format
770                             (ScarabConstants.DEFAULT_BUNDLE_NAME, getLocale(),
771                              "CouldNotFindModuleAttributeOption", args);
772                         importErrors.add(error);
773                     }
774                 }
775             }
776         }
777     }
778
779     /**
780      * Records any validation errors encountered.
781      *
782      * @param xmlDate The XML bean for the date.
783      * @param required Whether a valid date is required (parse errors
784      * are reported regardless of this setting).
785      */

786     private void validateDate(BaseDate xmlDate, boolean required)
787     {
788         try
789         {
790             // Report parse error failures even for optional dates.
791
if ((xmlDate != null && xmlDate.getDate() == null) && required)
792             {
793                 // Trigger error handling.
794
throw new ParseException JavaDoc(null, -1); //EXCEPTION
795
}
796         }
797         catch (ParseException JavaDoc e)
798         {
799             String JavaDoc errorMsg =
800                 (e.getErrorOffset() != -1 ? ": " + e.getMessage() : "");
801             String JavaDoc[] args = { xmlDate.getTimestamp(), xmlDate.getFormat(),
802                               errorMsg };
803             String JavaDoc error = Localization.format
804                 (ScarabConstants.DEFAULT_BUNDLE_NAME, getLocale(),
805                  "InvalidDate", args);
806             importErrors.add(error);
807         }
808     }
809
810     private Issue createNewIssue(XmlModule module, XmlIssue issue)
811         throws Exception JavaDoc
812     {
813         // get the instance of the module
814
Module moduleOM = ModuleManager.getInstance(module.getDomain(),
815                                                     module.getName(),
816                                                     module.getCode());
817         // get the instance of the issue type
818
IssueType issueTypeOM = IssueType.getInstance(issue.getArtifactType());
819         issueTypeOM.setName(issue.getArtifactType());
820         // get me a new issue since we couldn't find one before
821
Issue issueOM = Issue.getNewInstance(moduleOM, issueTypeOM);
822         // create the issue in the database
823
issueOM.save();
824
825         // Add the mapping between the issue id and the id that was created.
826
// This mapping is used dependency checking and printing out in
827
// results list of original id and new id. The original issue id can be
828
// null. In this case, have the original id show as 'null (index)'
829
// where index is count into the issueXMLMap. We add the index to keep
830
// the key unique. This substitute original id also shouldn't interfere
831
// w/ issueXMLMap's use dependency checking.
832
String JavaDoc issueID = "Null (" + Integer.toString(issueXMLMap.size()) + ")";
833         if(issue.getId() != null)
834         {
835             issueID = module.getCode() + issue.getId();
836         }
837         issueXMLMap.put(issueID, issueOM.getUniqueId());
838
839         LOG.debug("Created new Issue: " + issueOM.getUniqueId());
840         return issueOM;
841     }
842
843     private void doIssueEvent(XmlModule module, XmlIssue issue)
844         throws Exception JavaDoc
845     {
846 /////////////////////////////////////////////////////////////////////////////////
847
// Get me an issue
848
Issue issueOM = null;
849         if (getImportTypeCode() == CREATE_SAME_DB || getImportTypeCode() == CREATE_DIFFERENT_DB)
850         {
851             issueOM = createNewIssue(module, issue);
852         }
853         else
854         {
855             issueOM = IssueManager.getIssueById(module.getCode() + issue.getId());
856             if (issueOM == null)
857             {
858                 issueOM = createNewIssue(module, issue);
859             }
860             else
861             {
862                 LOG.debug("Found Issue in db: " + issueOM.getUniqueId());
863             }
864         }
865
866 /////////////////////////////////////////////////////////////////////////////////
867

868         // Loop over the XML activitySets
869
List JavaDoc activitySets = issue.getActivitySets();
870         LOG.debug("-----------------------------------");
871         LOG.debug("Number of ActivitySets in Issue: " + activitySets.size());
872         for (Iterator JavaDoc itr = activitySets.iterator(); itr.hasNext();)
873         {
874             XmlActivitySet activitySet = (XmlActivitySet) itr.next();
875             LOG.debug("Processing ActivitySet: " + activitySet.getId());
876
877 /////////////////////////////////////////////////////////////////////////////////
878
// Deal with the attachment for the activitySet
879
XmlAttachment activitySetAttachment = activitySet.getAttachment();
880             Attachment activitySetAttachmentOM = null;
881             if (activitySetAttachment != null)
882             {
883                 if (getImportTypeCode() == UPDATE_SAME_DB)
884                 {
885                     try
886                     {
887                         activitySetAttachmentOM = AttachmentManager
888                             .getInstance(activitySetAttachment.getId());
889                         LOG.debug("Found existing ActivitySet Attachment");
890                     }
891                     catch (Exception JavaDoc e)
892                     {
893                         activitySetAttachmentOM = createAttachment(issueOM, activitySetAttachment);
894                     }
895                 }
896                 else
897                 {
898                     activitySetAttachmentOM = createAttachment(issueOM, activitySetAttachment);
899                     LOG.debug("Created ActivitySet Attachment object");
900                 }
901             }
902             else
903             {
904                 LOG.debug("OK- No Attachment in this ActivitySet");
905             }
906
907 /////////////////////////////////////////////////////////////////////////////////
908
// Attempt to get the activitySet OM
909
boolean alreadyCreated = false;
910             ActivitySet activitySetOM = null;
911             if (getImportTypeCode() == UPDATE_SAME_DB)
912             {
913                 try
914                 {
915                     activitySetOM = ActivitySetManager.getInstance(activitySet.getId());
916                     LOG.debug("Found ActivitySet: " + activitySet.getId() +
917                               " in db: " + activitySetOM.getActivitySetId());
918                 }
919                 catch (Exception JavaDoc e)
920                 {
921                     activitySetOM = ActivitySetManager.getInstance();
922                 }
923             }
924             else
925             {
926                 // first try to get the ActivitySet from the internal map
927
if (activitySetIdMap.containsKey(activitySet.getId()))
928                 {
929                     activitySetOM = ActivitySetManager.getInstance(
930                         (String JavaDoc)activitySetIdMap.get(activitySet.getId()));
931                     alreadyCreated = true;
932                     LOG.debug("Found ActivitySet: " + activitySet.getId() +
933                               " in map: " + activitySetOM.getActivitySetId());
934                 }
935                 else // it hasn't been encountered previously
936
{
937                     activitySetOM = ActivitySetManager.getInstance();
938                     LOG.debug("Created new ActivitySet");
939                 }
940             }
941
942             ScarabUser activitySetCreatedByOM = ScarabUserManager.getInstance(activitySet.getCreatedBy(),
943                  module.getDomain());
944             if (!alreadyCreated)
945             {
946                 // Populate the ActivitySet
947
// Get the ActivitySet type/createdby values (we know these are valid)
948
ActivitySetType ttOM = ActivitySetTypeManager.getInstance(activitySet.getType());
949                 activitySetOM.setActivitySetType(ttOM);
950                 activitySetOM.setCreatedBy(activitySetCreatedByOM.getUserId());
951                 activitySetOM.setCreatedDate(activitySet.getCreatedDate().getDate());
952                 if (activitySetAttachmentOM != null)
953                 {
954                     activitySetAttachmentOM.save();
955                     activitySetOM.setAttachment(activitySetAttachmentOM);
956                 }
957                 activitySetOM.save();
958                 activitySetIdMap.put(activitySet.getId(),
959                                      activitySetOM.getPrimaryKey().toString());
960             }
961
962             // Determine if this ActivitySet should be marked as the
963
// creation event
964
ActivitySet creationSet = issueOM.getActivitySet();
965             if (ActivitySetTypePeer.CREATE_ISSUE__PK
966                 .equals(activitySetOM.getTypeId())
967                ||
968                (ActivitySetTypePeer.MOVE_ISSUE__PK
969                 .equals(activitySetOM.getTypeId()) &&
970                         (creationSet == null || activitySetOM.getCreatedDate()
971                          .before(creationSet.getCreatedDate()))) )
972             {
973                 issueOM.setActivitySet(activitySetOM);
974             }
975
976 /////////////////////////////////////////////////////////////////////////////////
977
// Deal with changing user attributes. this code needs to be in this *strange*
978
// location because we look at the entire activityset in order to determine
979
// that this is a change user activity set. of course in the future, it would
980
// be really nice to create an activityset/activiy type that more accurately
981
// reflects what type of change this is. so that it is easier to case for. for
982
// now, we just look at some fingerprints to determine this information. -JSS
983

984             if (activitySet.isChangeUserAttribute())
985             {
986                 List JavaDoc activities = activitySet.getActivities();
987                 XmlActivity activityA = (XmlActivity)activities.get(0);
988                 XmlActivity activityB = (XmlActivity)activities.get(1);
989                 
990                 ScarabUser assigneeOM = ScarabUserManager
991                     .getInstance(activityA.getOldUser(), module.getDomain());
992                 ScarabUser assignerOM = ScarabUserManager
993                     .getInstance(activityB.getNewUser(), module.getDomain());
994
995                 Attribute oldAttributeOM = Attribute.getInstance(activityA.getAttribute());
996
997                 AttributeValue oldAttValOM = issueOM.getUserAttributeValue(assigneeOM, oldAttributeOM);
998                 if (oldAttValOM == null)
999                 {
1000                    LOG.error("User '" + assigneeOM.getName() + "' was not previously '" + oldAttributeOM.getName() + "' to the issue!");
1001                }
1002
1003                // Get the Attribute associated with the new Activity
1004
Attribute newAttributeOM = Attribute.getInstance(activityB.getAttribute());
1005
1006                issueOM.changeUserAttributeValue(activitySetOM,
1007                            assigneeOM,
1008                            assignerOM,
1009                            oldAttValOM,
1010                            newAttributeOM, null);
1011                LOG.debug("-------------Updated User AttributeValue------------");
1012                continue;
1013            }
1014
1015/////////////////////////////////////////////////////////////////////////////////
1016

1017            // Deal with the activities in the activitySet
1018
List JavaDoc activities = activitySet.getActivities();
1019            LOG.debug("Number of Activities in ActivitySet: " + activities.size());
1020
1021            LinkedMap avMap = issueOM.getModuleAttributeValuesMap();
1022            LOG.debug("Total Module Attribute Values: " + avMap.size());
1023            for (Iterator JavaDoc itrb = activities.iterator(); itrb.hasNext();)
1024            {
1025                XmlActivity activity = (XmlActivity) itrb.next();
1026                LOG.debug("Looking at activity id: " + activity.getId());
1027
1028                // Get the Attribute associated with the Activity
1029
Attribute attributeOM = Attribute.getInstance(activity.getAttribute());
1030
1031                // deal with the activity attachment (if there is one)
1032
XmlAttachment activityAttachment = activity.getAttachment();
1033                Attachment activityAttachmentOM = null;
1034                if (activityAttachment != null)
1035                {
1036                    // look for an existing attachment in the activity
1037
// the case is when we have a URL and we create it
1038
// and then delete it, the attachment id is still the
1039
// same so there is no reason to re-create the attachment
1040
// again.
1041
String JavaDoc previousXmlId = activityAttachment.getId();
1042                    String JavaDoc previousId = (String JavaDoc)attachmentIdMap
1043                        .get(previousXmlId);
1044                    if (previousId == null)
1045                    {
1046                        activityAttachmentOM = createAttachment(
1047                            issueOM, activityAttachment);
1048                        activityAttachmentOM.save();
1049                        attachmentIdMap.put(previousXmlId,
1050                            activityAttachmentOM.getPrimaryKey().toString());
1051                        
1052                        // Special case. After the Attachment object has been
1053
// saved, if the ReconcilePath == true, then assume
1054
// that the fileName is an absolute path to a file and
1055
// copy it to the right directory
1056
// structure under Scarab's path.
1057
if (allowFileAttachments && activityAttachment.getReconcilePath())
1058                        {
1059                            activityAttachmentOM
1060                                .copyFileFromTo(activityAttachment.getFilename(),
1061                                                activityAttachmentOM.getFullPath());
1062                        }
1063                        LOG.debug("Created Activity Attachment object");
1064                    }
1065                    else
1066                    {
1067                        activityAttachmentOM = AttachmentManager
1068                            .getInstance(previousId);
1069                        LOG.debug("Found existing Activity Attachment");
1070                    }
1071                }
1072                else
1073                {
1074                    LOG.debug("OK- No Attachment in this Activity");
1075                }
1076
1077                // deal with null attributes (need to do this before we create the
1078
// activity right below because this will create its own activity).
1079
if (attributeOM.equals(nullAttribute))
1080                {
1081                    // add any dependency activities to a list for later processing
1082
if (isDependencyActivity(activity))
1083                    {
1084                        if (!isDuplicateDependency(activitySet))
1085                        {
1086                            Object JavaDoc[] obj = {activitySetOM, activity, activityAttachmentOM};
1087                            allDependencies.add(obj);
1088                            dependActivitySetId.add(activitySet.getId());
1089                            LOG.debug("-------------Stored Dependency # " + allDependencies.size() + "-------------");
1090                            continue;
1091                        }
1092                    }
1093                    else
1094                    {
1095                        // create the activity record.
1096
ActivityManager.createTextActivity(issueOM, nullAttribute, activitySetOM,
1097                                    activity.getDescription(), activityAttachmentOM,
1098                                    activity.getOldValue(), activity.getNewValue());
1099        
1100                        LOG.debug("-------------Saved Null Attribute-------------");
1101                        continue;
1102                    }
1103                }
1104
1105                // create the activityOM
1106
createActivity(activity, module,
1107                                            issueOM, attributeOM, activitySetOM);
1108
1109                // check to see if this is a new activity or an update activity
1110
AttributeValue avalOM = null;
1111                for (Iterator JavaDoc moduleAttributeValueItr = avMap.mapIterator();
1112                     moduleAttributeValueItr.hasNext() && avalOM == null;)
1113                {
1114                    AttributeValue testAvalOM = (AttributeValue)
1115                        avMap.get(moduleAttributeValueItr.next());
1116                    Attribute avalAttributeOM = testAvalOM.getAttribute();
1117
1118                    LOG.debug("Checking Attribute match: " + avalAttributeOM.getName() +
1119                              " against: " + attributeOM.getName());
1120                    if (avalAttributeOM.equals(attributeOM))
1121                    {
1122                        avalOM = testAvalOM;
1123                    }
1124                }
1125
1126                if (avalOM != null)
1127                {
1128                    Attribute avalAttributeOM = avalOM.getAttribute();
1129                    LOG.debug("Attributes match!");
1130                    AttributeValue avalOM2 = null;
1131                    if (!activity.isNewActivity())
1132                    {
1133                        LOG.debug("Activity is not new.");
1134                        avalOM2 = AttributeValue.getNewInstance(
1135                            avalAttributeOM.getAttributeId(),
1136                            avalOM.getIssue());
1137                        avalOM2.setProperties(avalOM);
1138                    }
1139
1140                    if (avalAttributeOM.isOptionAttribute())
1141                    {
1142                        LOG.debug("We have an Option Attribute: " +
1143                                  avalAttributeOM.getName());
1144                        AttributeOption newAttributeOptionOM = AttributeOption
1145                            .getInstance(attributeOM, activity.getNewOption(),
1146                                         issueOM.getModule(),
1147                                         issueOM.getIssueType());
1148                        if (activity.isNewActivity())
1149                        {
1150                            if (newAttributeOptionOM != null)
1151                            {
1152                                avalOM.setOptionId(newAttributeOptionOM.getOptionId());
1153                                avalOM.startActivitySet(activitySetOM);
1154                                avalOM.setAttribute(attributeOM);
1155                                avalOM.setActivityDescription(
1156                                    activity.getDescription());
1157                                avalOM.save();
1158                                LOG.debug("-------------Saved Attribute Value-------------");
1159                            }
1160                            else
1161                            {
1162                                LOG.warn("NewAttributeOptionOM is null for " +
1163                                         activity.getNewOption());
1164                            }
1165                        }
1166                        else
1167                        {
1168                            avalOM2.setOptionId(newAttributeOptionOM.getOptionId());
1169                            HashMap JavaDoc map = new HashMap JavaDoc();
1170                            map.put(avalOM.getAttributeId(), avalOM2);
1171                            issueOM.setAttributeValues(activitySetOM, map, null, activitySetCreatedByOM);
1172                            LOG.debug("-------------Saved Option Attribute Change-------------");
1173                        }
1174                    }
1175                    else if (avalAttributeOM.isUserAttribute())
1176                    {
1177                        LOG.debug("We have a User Attribute: "
1178                                  + avalAttributeOM.getName());
1179                        if (activity.isNewActivity())
1180                        {
1181                            // Don't need to pass in the attachment because
1182
// it is already in the activitySetOM.
1183
// If we can't get an assignee new-user, then
1184
// use the activity set creator as assignee.
1185
ScarabUser assigneeOM = ScarabUserManager
1186                                .getInstance(activity.getNewUser(),
1187                                             module.getDomain());
1188                            assigneeOM = (assigneeOM != null)
1189                                ? assigneeOM: activitySetCreatedByOM;
1190                            issueOM.assignUser(activitySetOM,
1191                                activity.getDescription(),
1192                                assigneeOM, null, avalAttributeOM, null);
1193                            LOG.debug("-------------Saved User Assign-------------");
1194                        }
1195                        else if (activity.isRemoveUserActivity())
1196                        {
1197                            // remove a user activity
1198
ScarabUser oldUserOM = ScarabUserManager
1199                                .getInstance(activity.getOldUser(), module.getDomain());
1200                            // need to reset the aval because the current one
1201
// is marked as new for some reason which causes an
1202
// insert and that isn't the right behavior here
1203
// (we want an update)
1204
avalOM = null;
1205                            for (Iterator JavaDoc i = issueOM.getAttributeValues(
1206                                 avalAttributeOM).iterator();
1207                                 i.hasNext() && avalOM == null;)
1208                            {
1209                                AttributeValue av = (AttributeValue)i.next();
1210                                if (oldUserOM.getUserId().equals(av.getUserId()))
1211                                {
1212                                    avalOM = av;
1213                                }
1214                            }
1215
1216                            if (avalOM == null)
1217                            {
1218                                if (LOG.isDebugEnabled())
1219                                {
1220                                    LOG.debug("Could not find previous AttributeValue assigning " +
1221                                        (oldUserOM == null ? "NULL" :
1222                                        oldUserOM.getUserName()) +
1223                                        " to attribute " +
1224                                              avalAttributeOM.getName());
1225                                }
1226                            }
1227                            else
1228                            {
1229                                // don't need to pass in the attachment because
1230
// it is already in the activitySetOM
1231
issueOM.deleteUser(activitySetOM, oldUserOM,
1232                                    activitySetCreatedByOM, avalOM, null);
1233                                LOG.debug("-------------Saved User Remove-------------");
1234                            }
1235                        }
1236                    }
1237                    else if (avalAttributeOM.isTextAttribute())
1238                    {
1239                        LOG.debug("We have a Text Attribute: " + avalAttributeOM.getName());
1240
1241                        avalOM.startActivitySet(activitySetOM);
1242                        avalOM.setAttribute(attributeOM);
1243                        avalOM.setActivityDescription(activity.getDescription());
1244
1245                        if (activity.isNewActivity())
1246                        {
1247                            avalOM.setValue(activity.getNewValue());
1248                        }
1249                        else if (!activity.getNewValue()
1250                                .equals(avalOM.getValue()))
1251                        {
1252                            avalOM2.setValue(activity.getNewValue());
1253                            avalOM.setProperties(avalOM2);
1254                        }
1255
1256                        avalOM.save();
1257                        LOG.debug("-------------Saved Attribute Value-------------");
1258                    }
1259                }
1260                issueOM.save();
1261                LOG.debug("-------------Saved Issue-------------");
1262            }
1263        }
1264    }
1265
1266    /**
1267     * Checks to see if there is a Dependency value for the Activity
1268     */

1269    private boolean isDependencyActivity(XmlActivity activity)
1270    {
1271        return (activity.getDependency() != null);
1272    }
1273
1274    private boolean isDuplicateDependency(XmlActivitySet activitySet)
1275    {
1276        return (dependActivitySetId.contains(activitySet.getId()));
1277    }
1278
1279    private Activity createActivity(XmlActivity activity, XmlModule module,
1280                                         Issue issueOM,
1281                                         Attribute attributeOM,
1282                                         ActivitySet activitySetOM)
1283        throws Exception JavaDoc
1284    {
1285        Activity activityOM = null;
1286        if (getImportTypeCode() == UPDATE_SAME_DB)
1287        {
1288            try
1289            {
1290                activityOM = ActivityManager.getInstance(activity.getId());
1291            }
1292            catch (Exception JavaDoc e)
1293            {
1294                activityOM = ActivityManager.getInstance();
1295            }
1296        }
1297        else
1298        {
1299            activityOM = ActivityManager.getInstance();
1300        }
1301
1302        activityOM.setIssue(issueOM);
1303        activityOM.setAttribute(attributeOM);
1304        activityOM.setActivitySet(activitySetOM);
1305        if (activity.getEndDate() != null)
1306        {
1307            activityOM.setEndDate(activity.getEndDate().getDate());
1308        }
1309
1310        // Set the attachment for the activity
1311
Attachment newAttachmentOM = null;
1312        if (activity.getAttachment() != null)
1313        {
1314            newAttachmentOM = createAttachment(issueOM, activity.getAttachment());
1315            newAttachmentOM.save();
1316            activityOM.setAttachment(newAttachmentOM);
1317        }
1318
1319        LOG.debug("Created New Activity");
1320        return activityOM;
1321    }
1322
1323    private Attachment createAttachment(Issue issueOM, XmlAttachment attachment)
1324        throws Exception JavaDoc
1325    {
1326        Attachment attachmentOM = AttachmentManager.getInstance();
1327        attachmentOM.setIssue(issueOM);
1328        AttachmentType type = AttachmentType.getInstance(attachment.getType());
1329        if (allowFileAttachments || !Attachment.FILE__PK.equals(type.getAttachmentTypeId()))
1330        {
1331            attachmentOM.setName(attachment.getName());
1332            attachmentOM.setAttachmentType(type);
1333            attachmentOM.setMimeType(attachment.getMimetype());
1334            attachmentOM.setFileName(attachment.getFilename());
1335            attachmentOM.setData(attachment.getData());
1336        }
1337        else
1338        {
1339            // add a comment that the file will not be imported. An alternative would be
1340
// to skip the activity altogether, but we will then need to check that there
1341
// are other activities or we need to completely ignore the ActivitySet
1342
attachmentOM.setName("comment");
1343            attachmentOM.setTypeId(Attachment.COMMENT__PK);
1344            attachmentOM.setMimeType("text/plain");
1345            String JavaDoc text = "File, " + attachment.getFilename() +
1346                ", was not imported. The old description follows:\n\n" + attachment.getName();
1347            String JavaDoc data = attachment.getData(); // this should be null, but just in case
1348
if (data != null)
1349            {
1350                text += "\n\n" + data;
1351            }
1352            attachmentOM.setData(text);
1353        }
1354        
1355        attachmentOM.setCreatedDate(attachment.getCreatedDate().getDate());
1356        ModifiedDate modifiedDate = attachment.getModifiedDate();
1357        if (modifiedDate != null)
1358        {
1359            attachmentOM.setModifiedDate(modifiedDate.getDate());
1360        }
1361        ScarabUser creUser = ScarabUserManager
1362            .getInstance(attachment.getCreatedBy(), issueOM.getModule().getScarabInstanceId());
1363        if (creUser != null)
1364        {
1365            attachmentOM.setCreatedBy(creUser.getUserId());
1366        }
1367
1368        ScarabUser modUserOM = null;
1369        String JavaDoc modifiedBy = attachment.getModifiedBy();
1370        if (modifiedBy != null)
1371        {
1372            modUserOM = ScarabUserManager
1373                .getInstance(attachment.getModifiedBy(),
1374                    issueOM.getModule().getScarabInstanceId());
1375            if (modUserOM != null)
1376            {
1377                attachmentOM.setModifiedBy(modUserOM.getUserId());
1378            }
1379        }
1380
1381        attachmentOM.setDeleted(attachment.getDeleted());
1382        return attachmentOM;
1383    }
1384
1385    private Locale JavaDoc getLocale()
1386    {
1387        return ScarabConstants.DEFAULT_LOCALE;
1388    }
1389}
1390
Popular Tags