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.io.Serializable;
050: import java.util.List;
051: import java.util.LinkedList;
052:
053: import org.apache.torque.TorqueException;
054: import org.apache.torque.om.Persistent;
055: import org.apache.torque.manager.CacheListener;
056: import org.apache.torque.util.Criteria;
057: import org.tigris.scarab.util.Log;
058:
059: /**
060: * This class manages Issue objects.
061: * The skeleton for this class was autogenerated by Torque * You should add additional methods to this class to meet the
062: * application requirements. This class will only be generated as
063: * long as it does not already exist in the output directory.
064: */
065: public class IssueManager extends BaseIssueManager implements
066: CacheListener {
067: private static final String ISSUE = "Issue";
068: protected static final String GET_ISSUE_BY_ID = "getIssueById";
069:
070: /**
071: * Creates a new <code>IssueManager</code> instance.
072: *
073: * @exception TorqueException if an error occurs
074: */
075: public IssueManager() throws TorqueException {
076: super ();
077: setRegion(getClassName().replace('.', '_'));
078: }
079:
080: /**
081: * If the id is not specified, return null otherwise return the
082: * issue object. if the id does not have a character prefix
083: * the default code is prepended
084: */
085: public static Issue getIssueById(String id, String defaultCode) {
086: Issue issue = null;
087: if (id != null) {
088: id = id.trim();
089: if (id.length() != 0 && defaultCode != null) {
090: char firstChar = id.charAt(0);
091: if ('0' <= firstChar && firstChar <= '9') {
092: id = defaultCode + id;
093: }
094: }
095: issue = IssueManager.getIssueById(id);
096: }
097: return issue;
098: }
099:
100: /**
101: * If the id is not specified, return null otherwise return the
102: * issue object.
103: */
104: public static Issue getIssueById(String id) {
105: if (id == null || id.length() == 0) {
106: return null;
107: }
108: Issue.FederatedId fid = new Issue.FederatedId(id);
109: return getIssueById(fid);
110: }
111:
112: public static Issue getIssueById(Issue.FederatedId fid) {
113: return IssueManager.getIssueByIdImpl(fid);
114: }
115:
116: public static Issue getIssueByIdImpl(Issue.FederatedId fid) {
117: Issue result = null;
118: Object obj = getMethodResult().get(ISSUE, GET_ISSUE_BY_ID, fid);
119: if (obj != null) {
120: try {
121: Issue cachedById = (Issue) obj;
122: Issue cachedByPk = getInstance(cachedById.getIssueId());
123: // we need to compare this to the cached by pk, issue, in case
124: // the issue was moved.
125: if (cachedById.getFederatedId().equals(
126: cachedByPk.getFederatedId())) {
127: result = cachedByPk;
128: } else {
129: getMethodResult().remove(ISSUE, GET_ISSUE_BY_ID,
130: fid);
131: }
132: } catch (TorqueException e) {
133: Log.get().error("", e);
134: }
135: }
136:
137: if (result == null) {
138: Criteria crit = new Criteria(5).add(IssuePeer.ID_PREFIX,
139: fid.getPrefix()).add(IssuePeer.ID_COUNT,
140: fid.getCount());
141: crit.setIgnoreCase(true);
142:
143: if (fid.getDomain() != null) {
144: crit.add(IssuePeer.ID_DOMAIN, fid.getDomain());
145: }
146:
147: try {
148: List issues = IssuePeer.doSelect(crit);
149: if (!issues.isEmpty()) {
150: result = (Issue) issues.get(0);
151: IssueManager.putInstance(result);
152: getMethodResult().put(result, ISSUE,
153: GET_ISSUE_BY_ID, fid);
154: }
155: } catch (Exception e) {
156: Log.get().error("", e);
157: // return null
158: }
159: }
160:
161: return result;
162: }
163:
164: protected Persistent putInstanceImpl(Persistent om)
165: throws TorqueException {
166: Persistent oldOm = super .putInstanceImpl(om);
167: // saving an issue object could affect some cached results, since it could be a move
168: Serializable obj = (Serializable) om;
169: getMethodResult().removeAll(obj,
170: Issue.GET_MODULE_ATTRVALUES_MAP);
171: getMethodResult().remove(obj, Issue.GET_USER_ATTRIBUTEVALUES);
172: return oldOm;
173: }
174:
175: /**
176: * Notify other managers with relevant CacheEvents.
177: */
178: protected void registerAsListener() {
179: AttributeValueManager.addCacheListener(this );
180: AttachmentManager.addCacheListener(this );
181: DependManager.addCacheListener(this );
182: ActivityManager.addCacheListener(this );
183: AttributeManager.addCacheListener(this );
184: RModuleAttributeManager.addCacheListener(this );
185: }
186:
187: // -------------------------------------------------------------------
188: // CacheListener implementation
189:
190: public void addedObject(Persistent om) {
191: if (om instanceof AttributeValue) {
192: AttributeValue castom = (AttributeValue) om;
193: Long key = castom.getIssueId();
194: try {
195: Serializable obj = getInstance(key);
196: if (obj != null) {
197: getMethodResult().remove(obj,
198: Issue.GET_MODULE_ATTRVALUES_MAP);
199: getMethodResult().remove(obj,
200: Issue.GET_USER_ATTRIBUTEVALUES);
201: }
202: } catch (TorqueException e) {
203: Log.get().warn("Invalid Issue id ", e);
204: }
205: } else if (om instanceof Attachment) {
206: Attachment castom = (Attachment) om;
207: try {
208: Serializable obj = getInstance(castom.getIssueId());
209: if (obj != null) {
210: getMethodResult().remove(obj, Issue.GET_URLS);
211: getMethodResult()
212: .removeAll(obj, Issue.GET_COMMENTS);
213: getMethodResult().removeAll(obj,
214: Issue.GET_EXISTING_ATTACHMENTS);
215: }
216: } catch (TorqueException e) {
217: Log.get().warn("Invalid Issue id ", e);
218: }
219: } else if (om instanceof Depend) {
220: Depend castom = (Depend) om;
221: Long key = castom.getObserverId();
222: try {
223: Serializable obj = getInstance(key);
224: if (obj != null) {
225: getMethodResult().removeAll(obj, Issue.GET_PARENTS);
226: }
227: key = castom.getObservedId();
228: obj = getInstance(key);
229: if (obj != null) {
230: getMethodResult()
231: .removeAll(obj, Issue.GET_CHILDREN);
232: }
233: } catch (TorqueException e) {
234: Log.get().warn("Invalid Issue id ", e);
235: }
236: } else if (om instanceof Activity) {
237: Activity castom = (Activity) om;
238: Long key = castom.getIssueId();
239: try {
240: Serializable obj = getInstance(key);
241: if (obj != null) {
242: getMethodResult()
243: .removeAll(obj, Issue.GET_ACTIVITY);
244: }
245: } catch (TorqueException e) {
246: Log.get().warn("Invalid Issue id ", e);
247: }
248: } else if (om instanceof Attribute) {
249: getMethodResult().clear();
250: } else if (om instanceof RModuleAttribute) {
251: getMethodResult().clear();
252: }
253: }
254:
255: public void refreshedObject(Persistent om) {
256: addedObject(om);
257: }
258:
259: /** fields which interest us with respect to cache events */
260: public List getInterestedFields() {
261: List interestedCacheFields = new LinkedList();
262: interestedCacheFields.add(AttributeValuePeer.ISSUE_ID);
263: interestedCacheFields.add(AttachmentPeer.ISSUE_ID);
264: interestedCacheFields.add(DependPeer.OBSERVER_ID);
265: interestedCacheFields.add(DependPeer.OBSERVED_ID);
266: interestedCacheFields.add(AttributePeer.ATTRIBUTE_ID);
267: interestedCacheFields.add(RModuleAttributePeer.MODULE_ID);
268: return interestedCacheFields;
269: }
270: }
|