KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > tigris > scarab > om > IssueManager


1 package org.tigris.scarab.om;
2
3 /* ================================================================
4  * Copyright (c) 2000-2003 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 CollabNet <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 CollabNet.
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 CollabNet.
47  */

48
49 import java.io.Serializable JavaDoc;
50 import java.util.List JavaDoc;
51 import java.util.LinkedList JavaDoc;
52
53 import org.apache.torque.TorqueException;
54 import org.apache.torque.om.Persistent;
55 import org.apache.torque.manager.CacheListener;
56 import org.apache.torque.util.Criteria;
57 import org.tigris.scarab.util.Log;
58
59 /**
60  * This class manages Issue objects.
61  * The skeleton for this class was autogenerated by Torque * You should add additional methods to this class to meet the
62  * application requirements. This class will only be generated as
63  * long as it does not already exist in the output directory.
64  */

65 public class IssueManager
66     extends BaseIssueManager
67     implements CacheListener
68 {
69     private static final String JavaDoc ISSUE =
70         "Issue";
71     protected static final String JavaDoc GET_ISSUE_BY_ID =
72         "getIssueById";
73
74     /**
75      * Creates a new <code>IssueManager</code> instance.
76      *
77      * @exception TorqueException if an error occurs
78      */

79     public IssueManager()
80         throws TorqueException
81     {
82         super();
83         setRegion(getClassName().replace('.', '_'));
84     }
85
86     /**
87      * If the id is not specified, return null otherwise return the
88      * issue object. if the id does not have a character prefix
89      * the default code is prepended
90      */

91     public static Issue getIssueById(String JavaDoc id, String JavaDoc defaultCode)
92     {
93         Issue issue = null;
94         if (id != null)
95         {
96             id = id.trim();
97             if (id.length() != 0 && defaultCode != null)
98             {
99                 char firstChar = id.charAt(0);
100                 if ('0' <= firstChar && firstChar <= '9')
101                 {
102                     id = defaultCode + id;
103                 }
104             }
105             issue = IssueManager.getIssueById(id);
106         }
107         return issue;
108     }
109
110     /**
111      * If the id is not specified, return null otherwise return the
112      * issue object.
113      */

114     public static Issue getIssueById(String JavaDoc id)
115     {
116         if (id == null || id.length() == 0)
117         {
118             return null;
119         }
120         Issue.FederatedId fid = new Issue.FederatedId(id);
121         return getIssueById(fid);
122     }
123
124     public static Issue getIssueById(Issue.FederatedId fid)
125     {
126         return IssueManager.getIssueByIdImpl(fid);
127     }
128
129     public static Issue getIssueByIdImpl(Issue.FederatedId fid)
130     {
131         Issue result = null;
132         Object JavaDoc obj = getMethodResult().get(ISSUE, GET_ISSUE_BY_ID, fid);
133         if (obj != null)
134         {
135             try
136             {
137                 Issue cachedById = (Issue)obj;
138                 Issue cachedByPk = getInstance(cachedById.getIssueId());
139                 // we need to compare this to the cached by pk, issue, in case
140
// the issue was moved.
141
if (cachedById.getFederatedId().equals(cachedByPk.getFederatedId()))
142                 {
143                     result = cachedByPk;
144                 }
145                 else
146                 {
147                     getMethodResult().remove(ISSUE, GET_ISSUE_BY_ID, fid);
148                 }
149             }
150             catch (TorqueException e)
151             {
152                 Log.get().error("", e);
153             }
154         }
155
156         if (result == null)
157         {
158             Criteria crit = new Criteria(5)
159                 .add(IssuePeer.ID_PREFIX, fid.getPrefix())
160                 .add(IssuePeer.ID_COUNT, fid.getCount());
161             crit.setIgnoreCase(true);
162             
163             if ( fid.getDomain() != null)
164             {
165                 crit.add(IssuePeer.ID_DOMAIN, fid.getDomain());
166             }
167             
168             try
169             {
170                 List JavaDoc issues = IssuePeer.doSelect(crit);
171                 if (!issues.isEmpty())
172                 {
173                     result = (Issue)issues.get(0);
174                     IssueManager.putInstance(result);
175                     getMethodResult().put(result, ISSUE, GET_ISSUE_BY_ID, fid);
176                 }
177             }
178             catch (Exception JavaDoc e)
179             {
180                 Log.get().error("", e);
181                 // return null
182
}
183         }
184
185         return result;
186     }
187
188     protected Persistent putInstanceImpl(Persistent om)
189         throws TorqueException
190     {
191         Persistent oldOm = super.putInstanceImpl(om);
192         // saving an issue object could affect some cached results, since it could be a move
193
Serializable JavaDoc obj = (Serializable JavaDoc)om;
194         getMethodResult().removeAll(obj, Issue.GET_MODULE_ATTRVALUES_MAP);
195         getMethodResult().remove(obj, Issue.GET_USER_ATTRIBUTEVALUES);
196         return oldOm;
197     }
198
199
200     /**
201      * Notify other managers with relevant CacheEvents.
202      */

203     protected void registerAsListener()
204     {
205         AttributeValueManager.addCacheListener(this);
206         AttachmentManager.addCacheListener(this);
207         DependManager.addCacheListener(this);
208         ActivityManager.addCacheListener(this);
209         AttributeManager.addCacheListener(this);
210         RModuleAttributeManager.addCacheListener(this);
211     }
212
213
214     // -------------------------------------------------------------------
215
// CacheListener implementation
216

217     public void addedObject(Persistent om)
218     {
219         if (om instanceof AttributeValue)
220         {
221             AttributeValue castom = (AttributeValue)om;
222             Long JavaDoc key = castom.getIssueId();
223             try
224             {
225                 Serializable JavaDoc obj = getInstance(key);
226                 if (obj != null)
227                 {
228                     getMethodResult().remove(obj, Issue.GET_MODULE_ATTRVALUES_MAP);
229                     getMethodResult().remove(obj, Issue.GET_USER_ATTRIBUTEVALUES);
230                 }
231             }
232             catch(TorqueException e)
233             {
234                 Log.get().warn("Invalid Issue id ", e);
235             }
236         }
237         else if (om instanceof Attachment)
238         {
239             Attachment castom = (Attachment)om;
240             try
241             {
242                 Serializable JavaDoc obj = getInstance(castom.getIssueId());
243                 if (obj != null)
244                 {
245                     getMethodResult().remove(obj, Issue.GET_URLS);
246                     getMethodResult().removeAll(obj, Issue.GET_COMMENTS);
247                     getMethodResult().removeAll(obj,
248                         Issue.GET_EXISTING_ATTACHMENTS);
249                 }
250             }
251             catch(TorqueException e)
252             {
253                 Log.get().warn("Invalid Issue id ", e);
254             }
255         }
256         else if (om instanceof Depend)
257         {
258             Depend castom = (Depend)om;
259             Long JavaDoc key = castom.getObserverId();
260             try
261             {
262                 Serializable JavaDoc obj = getInstance(key);
263                 if (obj != null)
264                 {
265                     getMethodResult().removeAll(obj, Issue.GET_PARENTS);
266                 }
267                 key = castom.getObservedId();
268                 obj = getInstance(key);
269                 if (obj != null)
270                 {
271                     getMethodResult().removeAll(obj, Issue.GET_CHILDREN);
272                 }
273             }
274             catch(TorqueException e)
275             {
276                 Log.get().warn("Invalid Issue id ", e);
277             }
278         }
279         else if (om instanceof Activity)
280         {
281             Activity castom = (Activity)om;
282             Long JavaDoc key = castom.getIssueId();
283             try
284             {
285                 Serializable JavaDoc obj = getInstance(key);
286                 if (obj != null)
287                 {
288                     getMethodResult().removeAll(obj, Issue.GET_ACTIVITY);
289                 }
290             }
291             catch(TorqueException e)
292             {
293                 Log.get().warn("Invalid Issue id ", e);
294             }
295         }
296         else if (om instanceof Attribute)
297         {
298             getMethodResult().clear();
299         }
300         else if (om instanceof RModuleAttribute)
301         {
302             getMethodResult().clear();
303         }
304     }
305
306     public void refreshedObject(Persistent om)
307     {
308         addedObject(om);
309     }
310
311     /** fields which interest us with respect to cache events */
312     public List JavaDoc getInterestedFields()
313     {
314         List JavaDoc interestedCacheFields = new LinkedList JavaDoc();
315         interestedCacheFields.add(AttributeValuePeer.ISSUE_ID);
316         interestedCacheFields.add(AttachmentPeer.ISSUE_ID);
317         interestedCacheFields.add(DependPeer.OBSERVER_ID);
318         interestedCacheFields.add(DependPeer.OBSERVED_ID);
319         interestedCacheFields.add(AttributePeer.ATTRIBUTE_ID);
320         interestedCacheFields.add(RModuleAttributePeer.MODULE_ID);
321         return interestedCacheFields;
322     }
323 }
324
325
326
327
328
329
Popular Tags