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 Collab.Net <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 Collab.Net.
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 Collab.Net.
047: */
048:
049: import java.io.Serializable;
050: import java.util.List;
051: import java.util.ArrayList;
052: import java.util.Comparator;
053: import java.util.Collections;
054: import org.apache.torque.TorqueException;
055:
056: import org.apache.torque.util.Criteria;
057: import org.tigris.scarab.services.cache.ScarabCache;
058:
059: /**
060: * This class represents an AttributePeer.
061: *
062: * @author <a href="mailto:jon@collab.net">Jon S. Stevens</a>
063: * @version $Id: AttributePeer.java 9977 2005-12-09 00:40:59Z hair $
064: */
065: public class AttributePeer extends BaseAttributePeer {
066: public static final Integer ASSIGNED_TO__PK = new Integer(2);
067: public static final Integer STATUS__PK = new Integer(3);
068: public static final Integer RESOLUTION__PK = new Integer(4);
069: public static final Integer TOTAL_VOTES__PK = new Integer(13);
070: public static final String EMAIL_TO = "to";
071: public static final String CC_TO = "cc";
072: public static final String USER = "user";
073: public static final String NON_USER = "non-user";
074:
075: private static final String ATTRIBUTE_PEER = "AttributePeer";
076:
077: /**
078: * Gets a List of all of the Attribute objects in the database.
079: */
080: public static List getAttributes(String attributeType,
081: boolean includeDeleted, String sortColumn,
082: String sortPolarity) throws TorqueException {
083: List result = null;
084: Boolean deletedBool = (includeDeleted ? Boolean.TRUE
085: : Boolean.FALSE);
086: // 4th element is ignored due to bug in torque that is being
087: // Matched in ScarabCache
088: Serializable[] keys = { ATTRIBUTE_PEER, attributeType,
089: deletedBool, sortColumn, null, sortPolarity };
090: Object obj = ScarabCache.get(keys);
091: if (obj == null) {
092: Criteria crit = new Criteria();
093: crit.add(AttributePeer.ATTRIBUTE_ID, 0, Criteria.NOT_EQUAL);
094: if (!includeDeleted) {
095: crit.add(AttributePeer.DELETED, 0);
096: }
097: // add user type criteria - user or non-user
098: if (attributeType.equals("user")) {
099: crit.add(AttributePeer.ATTRIBUTE_TYPE_ID,
100: AttributeTypePeer.USER_TYPE_KEY);
101: } else {
102: crit.add(AttributePeer.ATTRIBUTE_TYPE_ID,
103: AttributeTypePeer.USER_TYPE_KEY,
104: Criteria.NOT_EQUAL);
105: }
106: // sort criteria
107: if (sortColumn.equals("desc")) {
108: addSortOrder(crit, AttributePeer.DESCRIPTION,
109: sortPolarity);
110: } else if (sortColumn.equals("date")) {
111: addSortOrder(crit, AttributePeer.CREATED_DATE,
112: sortPolarity);
113: } else if (sortColumn.equals("type")) {
114: crit.addJoin(AttributePeer.ATTRIBUTE_TYPE_ID,
115: AttributeTypePeer.ATTRIBUTE_TYPE_ID);
116: addSortOrder(crit,
117: AttributeTypePeer.ATTRIBUTE_TYPE_NAME,
118: sortPolarity);
119: } else if (!sortColumn.equals("user")) {
120: // sort by name
121: addSortOrder(crit, AttributePeer.ATTRIBUTE_NAME,
122: sortPolarity);
123: }
124: result = doSelect(crit);
125: } else {
126: result = (List) obj;
127: }
128: if (sortColumn.equals("user")) {
129: result = sortAttributesByCreatingUser(result, sortPolarity);
130: }
131:
132: ScarabCache.put(result, keys);
133: return result;
134: }
135:
136: /**
137: * Gets a List of all of the Attribute objects filtered
138: * on name or description
139: */
140: public static List getFilteredAttributes(String name,
141: String description, String searchField)
142: throws TorqueException {
143: List result = null;
144: List allAttributes = getAttributes();
145: if (allAttributes == null || allAttributes.size() == 0) {
146: result = Collections.EMPTY_LIST;
147: } else {
148: List attrIds = new ArrayList();
149: for (int i = 0; i < allAttributes.size(); i++) {
150: attrIds.add(((Attribute) allAttributes.get(i))
151: .getAttributeId());
152: }
153: Criteria crit = new Criteria();
154: crit.addIn(AttributePeer.ATTRIBUTE_ID, attrIds);
155: Criteria.Criterion c = null;
156: Criteria.Criterion c1 = null;
157: Criteria.Criterion c2 = null;
158:
159: if (name != null) {
160: c1 = crit.getNewCriterion(AttributePeer.ATTRIBUTE_NAME,
161: addWildcards(name), Criteria.LIKE);
162: }
163: if (description != null) {
164: c2 = crit.getNewCriterion(AttributePeer.DESCRIPTION,
165: addWildcards(description), Criteria.LIKE);
166: }
167: if (searchField.equals("Name")) {
168: c = c1;
169: } else if (searchField.equals("Description")) {
170: c = c2;
171: } else if (searchField.equals("Any")) {
172: c = c1.or(c2);
173: }
174: crit.and(c);
175: result = AttributePeer.doSelect(crit);
176: }
177: return result;
178: }
179:
180: private static Object addWildcards(String s) {
181: return new StringBuffer(s.length() + 2).append('%').append(s)
182: .append('%').toString();
183: }
184:
185: /**
186: * Gets a List of all of the Attribute objects in the database.
187: * Sorts on selected column.
188: */
189: public static List getAttributes() throws TorqueException {
190: return getAttributes(NON_USER, false,
191: AttributePeer.ATTRIBUTE_NAME, "asc");
192: }
193:
194: /**
195: * Gets a List of Attribute objects in the database.
196: */
197: public static List getAttributes(String attributeType)
198: throws TorqueException {
199: return getAttributes(attributeType, false,
200: AttributePeer.ATTRIBUTE_NAME, "asc");
201: }
202:
203: /**
204: * Gets a List of Attribute objects in the database.
205: */
206: public static List getAttributes(String attributeType,
207: boolean includeDeleted) throws TorqueException {
208: return getAttributes(attributeType, includeDeleted,
209: AttributePeer.ATTRIBUTE_NAME, "asc");
210: }
211:
212: /**
213: * Gets a List of data Attribute objects in the database.
214: * Sorts on selected column.
215: */
216: public static List getAttributes(String sortColumn,
217: String sortPolarity) throws TorqueException {
218: return getAttributes(NON_USER, false, sortColumn, sortPolarity);
219: }
220:
221: private static List sortAttributesByCreatingUser(List result,
222: String sortPolarity) throws TorqueException {
223: final int polarity = ("asc".equals(sortPolarity)) ? 1 : -1;
224: Comparator c = new Comparator() {
225: public int compare(Object o1, Object o2) {
226: int i = 0;
227: try {
228: i = polarity
229: * ((Attribute) o1)
230: .getCreatedUserName()
231: .compareTo(
232: ((Attribute) o2)
233: .getCreatedUserName());
234: } catch (Exception e) {
235: //
236: }
237: return i;
238: }
239: };
240: Collections.sort(result, c);
241: return result;
242: }
243:
244: private static Criteria addSortOrder(Criteria crit,
245: String sortColumn, String sortPolarity) {
246: if (sortPolarity.equals("desc")) {
247: crit.addDescendingOrderByColumn(sortColumn);
248: } else {
249: crit.addAscendingOrderByColumn(sortColumn);
250: }
251: return crit;
252: }
253:
254: }
|