001: package org.tigris.scarab.screens.admin;
002:
003: /* ================================================================
004: * Copyright (c) 2000-2002 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.text.ParseException;
050: import java.text.SimpleDateFormat;
051: import java.util.Date;
052: import java.util.Iterator;
053: import java.util.List;
054:
055: import org.apache.fulcrum.parser.ParameterParser;
056: import org.apache.torque.util.Criteria;
057: import org.apache.turbine.RunData;
058: import org.apache.turbine.TemplateContext;
059: import org.tigris.scarab.om.ActivitySetPeer;
060: import org.tigris.scarab.screens.Default;
061: import org.tigris.scarab.services.cache.ScarabCache;
062:
063: /**
064: * Screen creates an iterator of ActivitySets for the ActivityList.vm template
065: * with no parameters it returns all ActivitySets sorted in reverse
066: * creation date order. Parameters can be used to limit results
067: * sort=pk will sort in reverse pk order
068: * frompk and topk can be used to specify integers to limit results
069: * fromdate and todate can be entered in "yyyy-MM-dd HH:mm:ss" format
070: *
071: * @author <a href="mailto:jmcnally@collab.net">John McNally</a>
072: * @version $Id: ActivityList.java 9255 2004-11-14 21:07:04Z dep4b $
073: */
074: public class ActivityList extends Default {
075: /**
076: * builds up the context for display of variables on the page.
077: */
078: protected void doBuildTemplate(RunData data, TemplateContext context)
079: throws Exception {
080: super .doBuildTemplate(data, context);
081: ParameterParser pp = data.getParameters();
082: String fromPk = pp.getString("frompk");
083: String toPk = pp.getString("topk");
084: String fromDateString = pp.getString("fromdate");
085: String toDateString = pp.getString("todate");
086: boolean isByPk = "pk".equals(pp.get("sort"))
087: || (fromPk != null) || (toPk != null);
088: // max=# will give the number of records, -1 will return all records
089: //int max = pp.getInt("max", 100);
090:
091: Criteria crit = new Criteria();
092: Criteria.Criterion c = null;
093: if (isByPk) {
094: crit
095: .addAscendingOrderByColumn(ActivitySetPeer.TRANSACTION_ID);
096: if (fromPk != null) {
097: c = crit.getNewCriterion(
098: ActivitySetPeer.TRANSACTION_ID, fromPk,
099: Criteria.GREATER_EQUAL);
100: }
101: if (toPk != null) {
102: if (c == null) {
103: c = crit.getNewCriterion(
104: ActivitySetPeer.TRANSACTION_ID, toPk,
105: Criteria.LESS_EQUAL);
106: } else {
107: c.and(crit.getNewCriterion(
108: ActivitySetPeer.TRANSACTION_ID, toPk,
109: Criteria.LESS_EQUAL));
110:
111: }
112: }
113: } else {
114: crit
115: .addAscendingOrderByColumn(ActivitySetPeer.CREATED_DATE);
116:
117: if (fromDateString != null) {
118: c = crit.getNewCriterion(ActivitySetPeer.CREATED_DATE,
119: parseDate(fromDateString),
120: Criteria.GREATER_EQUAL);
121: }
122:
123: if (toDateString != null) {
124: if (c == null) {
125: c = crit.getNewCriterion(
126: ActivitySetPeer.CREATED_DATE,
127: parseDate(toDateString),
128: Criteria.LESS_EQUAL);
129: } else {
130: c.and(crit.getNewCriterion(
131: ActivitySetPeer.CREATED_DATE,
132: parseDate(toDateString),
133: Criteria.LESS_EQUAL));
134: }
135: }
136: }
137: if (c != null) {
138: crit.add(c);
139: }
140:
141: final List sets = ActivitySetPeer.doSelect(crit);
142: // the following iterator starts at the end of the list and
143: // iterates toward the beginning, removing items from the list
144: // as it goes. This helps with memory usage. The original list
145: // size can still be too large, but if it is not, processing by
146: // the template should not add additional memory burden.
147: Iterator setIterator = new Iterator() {
148: public boolean hasNext() {
149: return !sets.isEmpty();
150: }
151:
152: public Object next() {
153: ScarabCache.clear();
154: return sets.remove(sets.size() - 1);
155: }
156:
157: public void remove() {
158: //not implemented
159: }
160: };
161: context.put("activitySets", setIterator);
162: }
163:
164: private Date parseDate(String s) throws ParseException {
165: SimpleDateFormat[] sdf = {
166: new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"),
167: new SimpleDateFormat("yyyy-MM-dd HH:mm"),
168: new SimpleDateFormat("yyyy-MM-dd"),
169: new SimpleDateFormat("HH:mm:ss"),
170: new SimpleDateFormat("HH:mm") };
171:
172: Date date = null;
173: for (int i = 0; i < sdf.length - 1; i++) {
174: try {
175: date = sdf[i].parse(s);
176: } catch (ParseException e) {
177: // try the next one
178: }
179: }
180:
181: if (date == null) {
182: //TODO [HD] Shouldn't this be better a NPE ?
183: // That better hits the truth...
184: throw new ParseException(s
185: + " could not be parsed as a date", 0); //EXCEPTION
186: }
187: return date;
188: }
189: }
|