KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > campware > cream > modules > screens > CreamList


1 package org.campware.cream.modules.screens;
2
3 /* ====================================================================
4  * Copyright (C) 2003-2005 Media Development Loan Fund
5  *
6  * * contact: contact@campware.org - http://www.campware.org
7  * Campware encourages further development. Please let us know.
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License
11  * as published by the Free Software Foundation; either version 2
12  * of the License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22  *
23  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
24  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
27  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
30  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
31  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
32  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
33  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  * ====================================================================
36  *
37  * This software consists of voluntary contributions made by many
38  * individuals on behalf of the Apache Software Foundation. For more
39  * information on the Apache Software Foundation, please see
40  * <http://www.apache.org/>.
41  */

42
43 import java.util.List JavaDoc;
44 import java.util.Locale JavaDoc;
45 import java.text.SimpleDateFormat JavaDoc;
46 import java.text.DecimalFormat JavaDoc;
47 import java.text.DecimalFormatSymbols JavaDoc;
48
49 import org.apache.turbine.util.RunData;
50 import org.apache.turbine.util.security.AccessControlList;
51
52 import org.apache.torque.util.Criteria;
53 import org.apache.velocity.context.Context;
54 import org.apache.turbine.Turbine;
55
56 /**
57  * Grab all the records in a table using a Peer, and
58  * place the Vector of data objects in the context
59  * where they can be displayed by a #foreach loop.
60  *
61  * @author <a HREF="mailto:pandzic@volny.cz">Nenad Pandzic</a>
62  */

63 public class CreamList extends SecureScreen
64 {
65
66     private int defModuleType;
67     private String JavaDoc defModuleName=new String JavaDoc();
68     private String JavaDoc defIdName=new String JavaDoc();
69     private String JavaDoc defOrderColumn=new String JavaDoc();
70
71     protected void initScreen()
72     {
73     }
74     
75     /**
76      * Place all the data object in the context
77      * for use in the template.
78      */

79     public void doBuildTemplate(RunData data, Context context)
80     {
81
82         int filterNo = 0;
83         int sortNo = 0;
84         String JavaDoc orderStr = new String JavaDoc();
85         String JavaDoc findStr = new String JavaDoc();
86
87         Locale JavaDoc myLocale = new Locale JavaDoc("ENGLISH", "UK");
88         data.setLocale(myLocale);
89         
90         filterNo = data.getParameters().getInt("filter", 0);
91         sortNo = data.getParameters().getInt("sortcol", 0);
92         orderStr = data.getParameters().getString("sortord", "ASC");
93         findStr = data.getParameters().getString("find", "");
94
95         Criteria criteria = new Criteria();
96         criteria.add(defIdName, 1000, Criteria.GREATER_THAN);
97
98         if (filterNo>0)
99         {
100             setFilter(filterNo, criteria, data);
101         }
102         else
103         {
104             if (findStr.length()>0)
105             {
106                 findStr= findStr + "%";
107                 setFind(findStr, criteria);
108             }
109         }
110
111         if (sortNo>0)
112         {
113             String JavaDoc sortCol= new String JavaDoc(getSortColumn(sortNo));
114             if (sortCol.length()>0){
115                 if (orderStr.equals("ASC")){
116                     criteria.addAscendingOrderByColumn(sortCol);
117                 }else{
118                     criteria.addDescendingOrderByColumn(sortCol);
119                 }
120             }
121         }
122
123         criteria.setIgnoreCase(true);
124
125         context.put("entries", getEntries(criteria));
126         context.put("df", new SimpleDateFormat JavaDoc ("dd.MM.yyyy"));
127
128         DecimalFormatSymbols JavaDoc symb= new DecimalFormatSymbols JavaDoc();
129         symb.setDecimalSeparator('.');
130         
131         context.put("af", new DecimalFormat JavaDoc ("0.00", symb));
132         context.put("rf", new DecimalFormat JavaDoc ("0.000000", symb));
133         
134     }
135
136     protected boolean isAuthorized( RunData data ) throws Exception JavaDoc
137     {
138
139         initScreen();
140         boolean isAuthorized = false;
141         AccessControlList acl = data.getACL();
142         
143         if (data.getUser().hasLoggedIn())
144         {
145             if (acl.hasPermission( defModuleName + "_VIEW") || acl.hasRole("turbine_root"))
146             {
147                 isAuthorized = true;
148             }
149             else
150             {
151                 data.setMessage("Sorry, you don't have permission for this operation!");
152                 data.setScreenTemplate("CreamError.vm");
153
154                 isAuthorized = false;
155             }
156         }
157         else
158         {
159             data.setScreenTemplate(Turbine.getConfiguration().getString("template.login"));
160
161             isAuthorized = false;
162         }
163         
164         return isAuthorized;
165     }
166
167     protected void setFilter(int filterNo, Criteria listCriteria, RunData data)
168     {
169     }
170
171     protected void setSort(int sortNo, Criteria listCriteria)
172     {
173     }
174
175     protected String JavaDoc getSortColumn(int sortNo)
176     {
177         return "";
178     }
179
180     protected void setFind(String JavaDoc findStr, Criteria listCriteria)
181     {
182     }
183
184     protected void setDefOrderColumn(String JavaDoc name)
185     {
186             defOrderColumn=name;
187     }
188
189     protected void setIdName(String JavaDoc name)
190     {
191             defIdName=name;
192     }
193
194     protected void setModuleName(String JavaDoc name)
195     {
196             defModuleName=name;
197     }
198
199     protected void setModuleType(int modtype)
200     {
201             defModuleType=modtype;
202     }
203
204     /**
205      * This will return all the records in
206      * the database but they have been mapped to
207      * objects so they can be directly used
208      * in the Velocity template.
209      */

210     protected List JavaDoc getEntries(Criteria criteria)
211     {
212             return null;
213     }
214
215
216 }
217
Popular Tags