KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > tigris > scarab > screens > admin > ActivityList


1 package org.tigris.scarab.screens.admin;
2
3 /* ================================================================
4  * Copyright (c) 2000-2002 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 Collab.Net <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 Collab.Net.
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 Collab.Net.
47  */

48
49 import java.text.ParseException JavaDoc;
50 import java.text.SimpleDateFormat JavaDoc;
51 import java.util.Date JavaDoc;
52 import java.util.Iterator JavaDoc;
53 import java.util.List JavaDoc;
54
55 import org.apache.fulcrum.parser.ParameterParser;
56 import org.apache.torque.util.Criteria;
57 import org.apache.turbine.RunData;
58 import org.apache.turbine.TemplateContext;
59 import org.tigris.scarab.om.ActivitySetPeer;
60 import org.tigris.scarab.screens.Default;
61 import org.tigris.scarab.services.cache.ScarabCache;
62
63 /**
64  * Screen creates an iterator of ActivitySets for the ActivityList.vm template
65  * with no parameters it returns all ActivitySets sorted in reverse
66  * creation date order. Parameters can be used to limit results
67  * sort=pk will sort in reverse pk order
68  * frompk and topk can be used to specify integers to limit results
69  * fromdate and todate can be entered in "yyyy-MM-dd HH:mm:ss" format
70  *
71  * @author <a HREF="mailto:jmcnally@collab.net">John McNally</a>
72  * @version $Id: ActivityList.java 9255 2004-11-14 21:07:04Z dep4b $
73  */

74 public class ActivityList extends Default
75 {
76     /**
77      * builds up the context for display of variables on the page.
78      */

79     protected void doBuildTemplate(RunData data, TemplateContext context)
80         throws Exception JavaDoc
81     {
82         super.doBuildTemplate(data, context);
83         ParameterParser pp = data.getParameters();
84         String JavaDoc fromPk = pp.getString("frompk");
85         String JavaDoc toPk = pp.getString("topk");
86         String JavaDoc fromDateString = pp.getString("fromdate");
87         String JavaDoc toDateString = pp.getString("todate");
88         boolean isByPk = "pk".equals(pp.get("sort")) || (fromPk != null)
89             || (toPk != null);
90         // max=# will give the number of records, -1 will return all records
91
//int max = pp.getInt("max", 100);
92

93         Criteria crit = new Criteria();
94         Criteria.Criterion c = null;
95         if (isByPk)
96         {
97             crit.addAscendingOrderByColumn(ActivitySetPeer.TRANSACTION_ID);
98             if (fromPk != null)
99             {
100                 c = crit.getNewCriterion(ActivitySetPeer.TRANSACTION_ID,
101                                          fromPk, Criteria.GREATER_EQUAL);
102             }
103             if (toPk != null)
104             {
105                 if (c == null)
106                 {
107                     c = crit.getNewCriterion(ActivitySetPeer.TRANSACTION_ID,
108                                              toPk, Criteria.LESS_EQUAL);
109                 }
110                 else
111                 {
112                     c.and(crit.getNewCriterion(ActivitySetPeer.TRANSACTION_ID,
113                                                toPk, Criteria.LESS_EQUAL));
114                     
115                 }
116             }
117         }
118         else
119         {
120             crit.addAscendingOrderByColumn(ActivitySetPeer.CREATED_DATE);
121
122             if (fromDateString != null)
123             {
124                 c = crit.getNewCriterion(ActivitySetPeer.CREATED_DATE,
125                                          parseDate(fromDateString),
126                                          Criteria.GREATER_EQUAL);
127             }
128
129             if (toDateString != null)
130             {
131                 if (c == null)
132                 {
133                     c = crit.getNewCriterion(ActivitySetPeer.CREATED_DATE,
134                                              parseDate(toDateString),
135                                              Criteria.LESS_EQUAL);
136                 }
137                 else
138                 {
139                     c.and(crit.getNewCriterion(ActivitySetPeer.CREATED_DATE,
140                                                parseDate(toDateString),
141                                                Criteria.LESS_EQUAL));
142                 }
143             }
144         }
145         if (c != null)
146         {
147             crit.add(c);
148         }
149         
150         final List JavaDoc sets = ActivitySetPeer.doSelect(crit);
151         // the following iterator starts at the end of the list and
152
// iterates toward the beginning, removing items from the list
153
// as it goes. This helps with memory usage. The original list
154
// size can still be too large, but if it is not, processing by
155
// the template should not add additional memory burden.
156
Iterator JavaDoc setIterator = new Iterator JavaDoc()
157             {
158                 public boolean hasNext()
159                 {
160                     return !sets.isEmpty();
161                 }
162
163                 public Object JavaDoc next()
164                 {
165                     ScarabCache.clear();
166                     return sets.remove(sets.size()-1);
167                 }
168
169                 public void remove()
170                 {
171                     //not implemented
172
}
173             };
174         context.put("activitySets", setIterator);
175     }
176
177     private Date JavaDoc parseDate(String JavaDoc s)
178         throws ParseException JavaDoc
179     {
180         SimpleDateFormat JavaDoc[] sdf = {
181             new SimpleDateFormat JavaDoc("yyyy-MM-dd HH:mm:ss"),
182             new SimpleDateFormat JavaDoc("yyyy-MM-dd HH:mm"),
183             new SimpleDateFormat JavaDoc("yyyy-MM-dd"),
184             new SimpleDateFormat JavaDoc("HH:mm:ss"),
185             new SimpleDateFormat JavaDoc("HH:mm")
186                 };
187
188         Date JavaDoc date = null;
189         for (int i=0; i<sdf.length-1; i++)
190         {
191             try
192             {
193                 date = sdf[i].parse(s);
194             }
195             catch (ParseException JavaDoc e)
196             {
197                 // try the next one
198
}
199         }
200
201         if (date == null)
202         {
203             //TODO [HD] Shouldn't this be better a NPE ?
204
// That better hits the truth...
205
throw new ParseException JavaDoc(s + " could not be parsed as a date", 0); //EXCEPTION
206
}
207         return date;
208     }
209 }
210
Popular Tags