001: package org.tigris.scarab.om;
002:
003: /* ================================================================
004: * Copyright (c) 2000-2005 CollabNet. All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions are
008: * met:
009: *
010: * 1. Redistributions of source code must retain the above copyright
011: * notice, this list of conditions and the following disclaimer.
012: *
013: * 2. Redistributions in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in the
015: * documentation and/or other materials provided with the distribution.
016: *
017: * 3. The end-user documentation included with the redistribution, if
018: * any, must include the following acknowlegement: "This product includes
019: * software developed by CollabNet <http://www.Collab.Net/>."
020: * Alternately, this acknowlegement may appear in the software itself, if
021: * and wherever such third-party acknowlegements normally appear.
022: *
023: * 4. The hosted project names must not be used to endorse or promote
024: * products derived from this software without prior written
025: * permission. For written permission, please contact info@collab.net.
026: *
027: * 5. Products derived from this software may not use the "Tigris" or
028: * "Scarab" names nor may "Tigris" or "Scarab" appear in their names without
029: * prior written permission of CollabNet.
030: *
031: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
032: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
033: * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
034: * IN NO EVENT SHALL COLLAB.NET OR ITS CONTRIBUTORS BE LIABLE FOR ANY
035: * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
036: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
037: * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
038: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
039: * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
040: * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
041: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
042: *
043: * ====================================================================
044: *
045: * This software consists of voluntary contributions made by many
046: * individuals on behalf of CollabNet.
047: */
048:
049: import java.util.List;
050: import java.util.LinkedList;
051: import java.util.ArrayList;
052: import java.util.Iterator;
053: import java.io.Serializable;
054:
055: import org.apache.torque.TorqueException;
056: import org.apache.torque.om.Persistent;
057: import org.apache.torque.util.Criteria;
058: import org.apache.torque.manager.CacheListener;
059: import org.tigris.scarab.util.Log;
060:
061: /**
062: * This class manages Module objects.
063: *
064: * @author <a href="mailto:jmcnally@collab.net">John McNally</a>
065: * @version $Id: ModuleManager.java 9977 2005-12-09 00:40:59Z hair $
066: */
067: public class ModuleManager extends BaseModuleManager implements
068: CacheListener {
069: /**
070: * Creates a new <code>ModuleManager</code> instance.
071: *
072: * @exception TorqueException if an error occurs
073: */
074: public ModuleManager() throws TorqueException {
075: super ();
076: setRegion(getClassName().replace('.', '_'));
077: }
078:
079: protected Module getInstanceImpl() {
080: return new ScarabModule();
081: }
082:
083: /**
084: * Get an instance of a Module by realName and code. If the result
085: * != 1, then throw a TorqueException.
086: *
087: * FIXME: Use caching? John?
088: */
089: public static Module getInstance(String moduleDomain,
090: String moduleRealName, String moduleCode)
091: throws TorqueException {
092: return getManager().getInstanceImpl(moduleDomain,
093: moduleRealName, moduleCode);
094: }
095:
096: /**
097: * Get an instance of a Module by realName and code. If the result
098: * != 1, then throw a TorqueException.
099: *
100: * FIXME: Use caching? John?
101: */
102: protected Module getInstanceImpl(final String moduleDomain,
103: final String moduleRealName, final String moduleCode)
104: throws TorqueException {
105: final Criteria crit = new Criteria();
106: if (moduleDomain != null) {
107: crit.add(ScarabModulePeer.DOMAIN, moduleDomain);
108: }
109: if (moduleRealName != null) {
110: crit.add(ScarabModulePeer.MODULE_NAME, moduleRealName);
111: }
112: if (moduleCode != null) {
113: crit.add(ScarabModulePeer.MODULE_CODE, moduleCode);
114: }
115: final List result = ScarabModulePeer.doSelect(crit);
116: if (result.size() != 1) {
117: throw new TorqueException("Selected: " + result.size()
118: + " rows. Expected 1."); //EXCEPTION
119: }
120: return (Module) result.get(0);
121: }
122:
123: /**
124: * Create a list of Modules from the given list of issues. Each
125: * Module in the list of issues will only occur once in the list of
126: * Modules.
127: *
128: * @param issues a <code>List</code> value
129: * @return a <code>List</code> value
130: * @exception TorqueException if an error occurs
131: */
132: public static List getInstancesFromIssueList(List issues)
133: throws TorqueException {
134: if (issues == null) {
135: throw new IllegalArgumentException(
136: "Null issue list is not allowed."); //EXCEPTION
137: }
138:
139: List modules = new ArrayList();
140: Iterator i = issues.iterator();
141: if (i.hasNext()) {
142: Issue issue = (Issue) i.next();
143: if (issue != null) {
144: Module module = issue.getModule();
145: if (module != null && !modules.contains(module)) {
146: modules.add(module);
147: }
148: } else {
149: throw new TorqueException(
150: "Null issue in list is not allowed."); //EXCEPTION
151: }
152: }
153: return modules;
154: }
155:
156: /**
157: * Notify other managers with relevant CacheEvents.
158: */
159: protected void registerAsListener() {
160: RModuleIssueTypeManager.addCacheListener(this );
161: RModuleAttributeManager.addCacheListener(this );
162: AttributeGroupManager.addCacheListener(this );
163: RModuleOptionManager.addCacheListener(this );
164: AttributeManager.addCacheListener(this );
165: AttributeOptionManager.addCacheListener(this );
166: IssueTypeManager.addCacheListener(this );
167: }
168:
169: // -------------------------------------------------------------------
170: // CacheListener implementation
171:
172: public void addedObject(Persistent om) {
173: if (om instanceof RModuleAttribute) {
174: RModuleAttribute castom = (RModuleAttribute) om;
175: Integer key = castom.getModuleId();
176: try {
177: Serializable obj = getInstance(key);
178: if (obj != null) {
179: getMethodResult()
180: .removeAll(
181: obj,
182: AbstractScarabModule.GET_R_MODULE_ATTRIBUTES);
183: }
184: } catch (TorqueException e) {
185: Log.get().warn("Invalid Module id ", e);
186: }
187: } else if (om instanceof RModuleOption) {
188: RModuleOption castom = (RModuleOption) om;
189: Integer key = castom.getModuleId();
190: try {
191: Serializable obj = getInstance(key);
192: if (obj != null) {
193: getMethodResult()
194: .removeAll(
195: obj,
196: AbstractScarabModule.GET_LEAF_R_MODULE_OPTIONS);
197: }
198: } catch (TorqueException e) {
199: Log.get().warn("Invalid Module id ", e);
200: }
201: } else if (om instanceof RModuleIssueType) {
202: RModuleIssueType castom = (RModuleIssueType) om;
203: Integer key = castom.getModuleId();
204: try {
205: Serializable obj = getInstance(key);
206: if (obj != null) {
207: getMethodResult().remove(obj,
208: AbstractScarabModule.GET_NAV_ISSUE_TYPES);
209: }
210: } catch (TorqueException e) {
211: Log.get().warn("Invalid Module id ", e);
212: }
213: } else if (om instanceof IssueType) {
214: getMethodResult().clear();
215: } else if (om instanceof Attribute) {
216: getMethodResult().clear();
217: } else if (om instanceof AttributeOption) {
218: getMethodResult().clear();
219: }
220: }
221:
222: public void refreshedObject(Persistent om) {
223: addedObject(om);
224: }
225:
226: /** fields which interest us with respect to cache events */
227: public List getInterestedFields() {
228: List interestedCacheFields = new LinkedList();
229: interestedCacheFields.add(RModuleOptionPeer.MODULE_ID);
230: interestedCacheFields.add(RModuleAttributePeer.MODULE_ID);
231: interestedCacheFields.add(RModuleIssueTypePeer.MODULE_ID);
232: interestedCacheFields.add(AttributeGroupPeer.MODULE_ID);
233: interestedCacheFields.add(AttributePeer.ATTRIBUTE_ID);
234: interestedCacheFields.add(AttributeOptionPeer.OPTION_ID);
235: interestedCacheFields.add(IssueTypePeer.ISSUE_TYPE_ID);
236: return interestedCacheFields;
237: }
238: }
|