KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.List JavaDoc;
50 import java.util.LinkedList JavaDoc;
51 import java.util.ArrayList JavaDoc;
52 import java.util.Iterator JavaDoc;
53 import java.io.Serializable JavaDoc;
54
55 import org.apache.torque.TorqueException;
56 import org.apache.torque.om.Persistent;
57 import org.apache.torque.util.Criteria;
58 import org.apache.torque.manager.CacheListener;
59 import org.tigris.scarab.util.Log;
60
61 /**
62  * This class manages Module objects.
63  *
64  * @author <a HREF="mailto:jmcnally@collab.net">John McNally</a>
65  * @version $Id: ModuleManager.java 9104 2004-05-10 21:04:51Z dabbous $
66  */

67 public class ModuleManager
68     extends BaseModuleManager
69     implements CacheListener
70 {
71     /**
72      * Creates a new <code>ModuleManager</code> instance.
73      *
74      * @exception TorqueException if an error occurs
75      */

76     public ModuleManager()
77         throws TorqueException
78     {
79         super();
80         setRegion(getClassName().replace('.', '_'));
81     }
82
83     protected Module getInstanceImpl()
84     {
85         return new ScarabModule();
86     }
87
88     /**
89      * Get an instance of a Module by realName and code. If the result
90      * != 1, then throw a TorqueException.
91      *
92      * FIXME: Use caching? John?
93      */

94     public static Module getInstance(String JavaDoc moduleDomain,
95                                      String JavaDoc moduleRealName,
96                                      String JavaDoc moduleCode)
97         throws TorqueException
98     {
99         return getManager().getInstanceImpl(moduleDomain, moduleRealName,
100                                             moduleCode);
101     }
102
103     /**
104      * Get an instance of a Module by realName and code. If the result
105      * != 1, then throw a TorqueException.
106      *
107      * FIXME: Use caching? John?
108      */

109     protected Module getInstanceImpl(String JavaDoc moduleDomain,
110                                      String JavaDoc moduleRealName,
111                                      String JavaDoc moduleCode)
112         throws TorqueException
113     {
114         Criteria crit = new Criteria();
115         crit.add(ScarabModulePeer.MODULE_NAME, moduleDomain);
116         crit.add(ScarabModulePeer.MODULE_NAME, moduleRealName);
117         crit.add(ScarabModulePeer.MODULE_CODE, moduleCode);
118         List JavaDoc result = ScarabModulePeer.doSelect(crit);
119         if (result.size() != 1)
120         {
121             throw new TorqueException ("Selected: " + result.size() +
122                 " rows. Expected 1."); //EXCEPTION
123
}
124         return (Module) result.get(0);
125     }
126
127     /**
128      * Create a list of Modules from the given list of issues. Each
129      * Module in the list of issues will only occur once in the list of
130      * Modules.
131      *
132      * @param issues a <code>List</code> value
133      * @return a <code>List</code> value
134      * @exception TorqueException if an error occurs
135      */

136     public static List JavaDoc getInstancesFromIssueList(List JavaDoc issues)
137         throws TorqueException
138     {
139         if (issues == null)
140         {
141             throw new IllegalArgumentException JavaDoc("Null issue list is not allowed."); //EXCEPTION
142
}
143
144         List JavaDoc modules = new ArrayList JavaDoc();
145         Iterator JavaDoc i = issues.iterator();
146         if (i.hasNext())
147         {
148             Issue issue = (Issue)i.next();
149             if (issue != null)
150             {
151                 Module module = issue.getModule();
152                 if (module != null && !modules.contains(module))
153                 {
154                     modules.add(module);
155                 }
156             }
157             else
158             {
159                 throw new TorqueException("Null issue in list is not allowed."); //EXCEPTION
160
}
161         }
162         return modules;
163     }
164
165
166     /**
167      * Notify other managers with relevant CacheEvents.
168      */

169     protected void registerAsListener()
170     {
171         RModuleIssueTypeManager.addCacheListener(this);
172         RModuleAttributeManager.addCacheListener(this);
173         AttributeGroupManager.addCacheListener(this);
174         RModuleOptionManager.addCacheListener(this);
175         AttributeManager.addCacheListener(this);
176         AttributeOptionManager.addCacheListener(this);
177         IssueTypeManager.addCacheListener(this);
178     }
179
180     // -------------------------------------------------------------------
181
// CacheListener implementation
182

183     public void addedObject(Persistent om)
184     {
185         if (om instanceof RModuleAttribute)
186         {
187             RModuleAttribute castom = (RModuleAttribute)om;
188             Integer JavaDoc key = castom.getModuleId();
189             try
190             {
191                 Serializable JavaDoc obj = getInstance(key);
192                 if (obj != null)
193                 {
194                     getMethodResult().removeAll(obj,
195                         AbstractScarabModule.GET_R_MODULE_ATTRIBUTES);
196                 }
197             }
198             catch(TorqueException e)
199             {
200                 Log.get().warn("Invalid Module id ", e);
201             }
202         }
203         else if (om instanceof RModuleOption)
204         {
205             RModuleOption castom = (RModuleOption)om;
206             Integer JavaDoc key = castom.getModuleId();
207             try
208             {
209                 Serializable JavaDoc obj = getInstance(key);
210                 if (obj != null)
211                 {
212                     getMethodResult().removeAll(obj,
213                         AbstractScarabModule.GET_LEAF_R_MODULE_OPTIONS);
214                 }
215             }
216             catch(TorqueException e)
217             {
218                 Log.get().warn("Invalid Module id ", e);
219             }
220         }
221         else if (om instanceof RModuleIssueType)
222         {
223             RModuleIssueType castom = (RModuleIssueType)om;
224             Integer JavaDoc key = castom.getModuleId();
225             try
226             {
227                 Serializable JavaDoc obj = getInstance(key);
228                 if (obj != null)
229                 {
230                     getMethodResult().remove(obj,
231                         AbstractScarabModule.GET_NAV_ISSUE_TYPES);
232                 }
233             }
234             catch(TorqueException e)
235             {
236                 Log.get().warn("Invalid Module id ", e);
237             }
238         }
239         else if (om instanceof IssueType)
240         {
241             getMethodResult().clear();
242         }
243         else if (om instanceof Attribute)
244         {
245             getMethodResult().clear();
246         }
247         else if (om instanceof AttributeOption)
248         {
249             getMethodResult().clear();
250         }
251     }
252
253     public void refreshedObject(Persistent om)
254     {
255         addedObject(om);
256     }
257
258     /** fields which interest us with respect to cache events */
259     public List JavaDoc getInterestedFields()
260     {
261         List JavaDoc interestedCacheFields = new LinkedList JavaDoc();
262         interestedCacheFields.add(RModuleOptionPeer.MODULE_ID);
263         interestedCacheFields.add(RModuleAttributePeer.MODULE_ID);
264         interestedCacheFields.add(RModuleIssueTypePeer.MODULE_ID);
265         interestedCacheFields.add(AttributeGroupPeer.MODULE_ID);
266         interestedCacheFields.add(AttributePeer.ATTRIBUTE_ID);
267         interestedCacheFields.add(AttributeOptionPeer.OPTION_ID);
268         interestedCacheFields.add(IssueTypePeer.ISSUE_TYPE_ID);
269         return interestedCacheFields;
270     }
271 }
272
Popular Tags