KickJava   Java API By Example, From Geeks To Geeks.

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


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.Date JavaDoc;
44 import java.text.SimpleDateFormat JavaDoc;
45 import java.text.DecimalFormat JavaDoc;
46 import java.text.DecimalFormatSymbols JavaDoc;
47
48 import org.apache.turbine.util.RunData;
49 import org.apache.turbine.util.security.AccessControlList;
50
51 import org.apache.torque.util.Criteria;
52
53 import org.apache.velocity.context.Context;
54 import org.apache.turbine.Turbine;
55
56 /**
57  * Grabs a record from a database and makes
58  * the data available in the template.
59  *
60  * @author <a HREF="mailto:pandzic@volny.cz">Nenad Pandzic</a>
61  */

62 public class CreamForm extends SecureScreen
63 {
64
65     private int defModuleType;
66     private String JavaDoc defModuleName=new String JavaDoc();
67     private String JavaDoc defIdName=new String JavaDoc();
68     private String JavaDoc defFormIdName=new String JavaDoc();
69     protected RunData myData;
70
71     protected void initScreen()
72     {
73     }
74
75     /**
76      * Grab a record from the database based on the entry_id
77      * found in the form. Make the data available in the
78      * template.
79      */

80     public void doBuildTemplate(RunData data, Context context)
81     {
82         try
83         {
84             myData= data;
85             int entry_id = data.getParameters().getInt(defFormIdName);
86             
87             if (entry_id>0)
88             {
89                 Criteria criteria = new Criteria();
90                 criteria.add(defIdName, entry_id);
91                 getEntry(criteria, context);
92
93                 context.put("mode", "update");
94             }
95             else if (entry_id<0)
96             {
97                 getNewRelated(entry_id * (-1), context);
98                 context.put("mode", "insert");
99             }
100             else
101             {
102                 getNew(context);
103                 context.put("mode", "insert");
104             }
105
106             getLookups(context);
107             
108             context.put("df", new SimpleDateFormat JavaDoc ("dd.MM.yyyy"));
109             context.put("dtf", new SimpleDateFormat JavaDoc ("dd.MM.yyyy hh:mm:ss"));
110             
111             DecimalFormatSymbols JavaDoc symb= new DecimalFormatSymbols JavaDoc();
112             symb.setDecimalSeparator('.');
113             
114             context.put("af", new DecimalFormat JavaDoc ("0.00", symb));
115             context.put("rf", new DecimalFormat JavaDoc ("0.000000", symb));
116             context.put("today", new Date JavaDoc());
117         }
118         catch (Exception JavaDoc e)
119         {
120             // log something ?
121
}
122     }
123
124     protected boolean isAuthorized( RunData data ) throws Exception JavaDoc
125     {
126
127         initScreen();
128         boolean isAuthorized = false;
129         AccessControlList acl = data.getACL();
130         
131         if (data.getUser().hasLoggedIn())
132         {
133             if (acl.hasPermission( defModuleName + "_VIEW") || acl.hasRole("turbine_root"))
134             {
135                 isAuthorized = true;
136             }
137             else
138             {
139                 data.setMessage("Sorry, you don't have permission for this operation!");
140                 data.setScreenTemplate("CreamError.vm");
141
142                 isAuthorized = false;
143             }
144         }
145         else
146         {
147             data.setScreenTemplate(Turbine.getConfiguration().getString("template.login"));
148
149             isAuthorized = false;
150         }
151         
152         return isAuthorized;
153     }
154
155     protected boolean getEntry(Criteria criteria, Context context)
156     {
157             return false;
158     }
159
160     protected boolean getNew(Context context)
161     {
162             return false;
163     }
164
165     protected boolean getNewRelated(int relid, Context context)
166     {
167             return false;
168     }
169
170     protected boolean getLookups(Context context)
171     {
172             return false;
173     }
174
175     protected void setIdName(String JavaDoc name)
176     {
177             defIdName=name;
178     }
179
180     protected void setFormIdName(String JavaDoc name)
181     {
182             defFormIdName=name;
183     }
184
185     protected void setModuleName(String JavaDoc name)
186     {
187             defModuleName=name;
188     }
189
190     protected void setModuleType(int modtype)
191     {
192             defModuleType=modtype;
193     }
194
195     protected String JavaDoc formatDateTime(Date JavaDoc d)
196     {
197 // return DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM).format(d);
198
SimpleDateFormat JavaDoc formatter = new SimpleDateFormat JavaDoc ("dd.MM.yyyy hh:mm:ss");
199         return formatter.format(d);
200     }
201
202     protected String JavaDoc formatDate(Date JavaDoc d)
203     {
204 // return DateFormat.getDateInstance(DateFormat.MEDIUM).format(d);
205
SimpleDateFormat JavaDoc formatter = new SimpleDateFormat JavaDoc ("dd.MM.yyyy");
206         return formatter.format(d);
207     }
208     
209     
210 }
211
Popular Tags