KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > campware > cream > modules > actions > CreamAction


1 package org.campware.cream.modules.actions;
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.ParsePosition JavaDoc;
46 import org.apache.velocity.context.Context;
47
48 import org.apache.turbine.modules.actions.VelocitySecureAction;
49 import org.apache.turbine.util.RunData;
50 import org.apache.turbine.util.security.AccessControlList;
51 import org.apache.turbine.Turbine;
52
53 /**
54  * Cream Secure action.
55  *
56  * Always performs a Security Check that you've defined before
57  * executing the doBuildtemplate().
58  * @author <a HREF="mailto:pandzic@volny.cz">Nenad Pandzic</a>
59  */

60 public class CreamAction extends VelocitySecureAction
61 {
62     public static final int ENTITY=1001;
63     public static final int DOCUMENT=1002;
64     public static final int LOOKUP=1003;
65     public static final int SYSTEM=1004;
66     public static final int REPORT=1005;
67     public static final int UTIL=1006;
68
69     private int defModuleType;
70     private String JavaDoc defModuleName=new String JavaDoc();
71
72     protected void initScreen()
73     {
74     }
75     
76     /**
77      * Implement this to add information to the context.
78      *
79      * @param data Turbine information.
80      * @param context Context for web pages.
81      * @exception Exception, a generic exception.
82      */

83     public void doPerform( RunData data,Context context )
84         throws Exception JavaDoc
85     {
86         data.setMessage("Can't find the button!");
87     }
88
89     /**
90      * This currently only checks to make sure that user is allowed to
91      * view the storage area. If you create an action that requires more
92      * security then override this method.
93      *
94      * @param data Turbine information.
95      * @return True if the user is authorized to access the screen.
96      * @exception Exception, a generic exception.
97      */

98     protected boolean isAuthorized( RunData data ) throws Exception JavaDoc
99     {
100         initScreen();
101         boolean isAuthorized = false;
102
103         AccessControlList acl = data.getACL();
104         
105         if (data.getUser().hasLoggedIn())
106         {
107             if (acl.hasPermission( defModuleName + "_MODIFY") || acl.hasRole("turbine_root"))
108             {
109                 isAuthorized = true;
110             }
111             else
112             {
113                 data.setMessage("Sorry, you don't have permission for this operation!");
114                 data.setScreenTemplate("CreamError.vm");
115
116                 isAuthorized = false;
117             }
118         }
119         else
120         {
121             data.setScreenTemplate(Turbine.getConfiguration().getString("template.login"));
122
123             isAuthorized = false;
124         }
125
126         return isAuthorized;
127     }
128
129     protected Date JavaDoc parseDateTime(String JavaDoc d)
130         throws Exception JavaDoc
131     {
132 // return DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.MEDIUM).parse(d);
133
SimpleDateFormat JavaDoc formatter = new SimpleDateFormat JavaDoc ("dd.MM.yyyy hh:mm:ss");
134         ParsePosition JavaDoc pos = new ParsePosition JavaDoc(0);
135         return formatter.parse(d, pos);
136     }
137
138     protected Date JavaDoc parseDate(String JavaDoc d)
139         throws Exception JavaDoc
140     {
141 // return DateFormat.getDateInstance(DateFormat.SHORT).parse(d);
142
SimpleDateFormat JavaDoc formatter = new SimpleDateFormat JavaDoc ("dd.MM.yyyy");
143         ParsePosition JavaDoc pos = new ParsePosition JavaDoc(0);
144         try{
145             Date JavaDoc myDate= formatter.parse(d, pos);
146             return myDate;
147         }
148         catch (Exception JavaDoc e)
149         {
150             return null;
151         }
152             
153     }
154
155     protected String JavaDoc formatDate(Date JavaDoc d)
156     {
157         SimpleDateFormat JavaDoc formatter = new SimpleDateFormat JavaDoc ("dd.MM.yyyy");
158         return formatter.format(d);
159     }
160     
161
162     protected String JavaDoc getTempCode()
163     {
164         Date JavaDoc currDate= new Date JavaDoc();
165
166         return Integer.toString(currDate.hashCode());
167     }
168
169     protected String JavaDoc getRowCode(String JavaDoc s, int i)
170     {
171         String JavaDoc is= new String JavaDoc();
172         
173         is= Integer.toString(i);
174         while (is.length()<7)
175         {
176             is="0" + is;
177         }
178
179         is= s + is;
180         return is;
181     }
182
183     protected void setModuleName(String JavaDoc name)
184     {
185             defModuleName=name;
186     }
187
188     protected void setModuleType(int modtype)
189     {
190             defModuleType=modtype;
191     }
192
193     
194     public void doInsertrow(RunData data, Context context)
195     throws Exception JavaDoc
196     {
197         try{
198             doInsert(data, context);
199         }catch (Exception JavaDoc e){
200             handleCreamException(data, e);
201         }
202     }
203
204     public void doUpdaterow(RunData data, Context context)
205     throws Exception JavaDoc
206     {
207         try{
208             doUpdate(data, context);
209         }catch (Exception JavaDoc e){
210             handleCreamException(data, e);
211         }
212     }
213     
214     public void doDeleterow(RunData data, Context context)
215     throws Exception JavaDoc
216     {
217         try{
218             doDelete(data, context);
219         }catch (Exception JavaDoc e){
220             handleCreamException(data, e);
221         }
222     }
223     
224     public void doDeleteselectedrows(RunData data, Context context)
225     throws Exception JavaDoc
226     {
227         try{
228             doDeleteselected(data, context);
229         }catch (Exception JavaDoc e){
230             handleCreamException(data, e);
231         }
232     }
233
234     public void doInsert(RunData data, Context context)
235     throws Exception JavaDoc
236     {
237         
238     }
239
240     public void doUpdate(RunData data, Context context)
241     throws Exception JavaDoc
242     {
243         
244     }
245     
246     public void doDelete(RunData data, Context context)
247     throws Exception JavaDoc
248     {
249         
250     }
251     
252     public void doDeleteselected(RunData data, Context context)
253     throws Exception JavaDoc
254     {
255         
256     }
257
258     protected void handleCreamException(RunData data, String JavaDoc message)
259     {
260         data.setMessage(message);
261         this.setTemplate( data, "CreamError.vm");
262     }
263
264     private void handleCreamException(RunData data, Exception JavaDoc e)
265     {
266         this.setTemplate( data, "CreamError.vm");
267     }
268
269 }
270
Popular Tags