KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > za > org > coefficient > modules > attribute > AttributeManagement


1 /*
2  * Coefficient - facilitates project based collaboration
3  * Copyright (C) 2003, Dylan Etkin, CSIR icomtek
4  * PO Box 395
5  * Pretoria 0001, RSA
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19
20 package za.org.coefficient.modules.attribute;
21
22 import net.sf.hibernate.HibernateException;
23 import net.sf.hibernate.Hibernate;
24
25 import za.org.coefficient.authentication.Role;
26 import za.org.coefficient.core.AttributeElement;
27 import za.org.coefficient.core.ProjectAttribute;
28 import za.org.coefficient.core.ProjectAttributeData;
29 import za.org.coefficient.core.Project;
30 import za.org.coefficient.interfaces.CoefficientContext;
31 import za.org.coefficient.modules.BaseModule;
32 import net.sf.hibernate.util.HibernateUtil;
33 import za.org.coefficient.util.ejb.SecurityUtil;
34 import za.org.coefficient.util.ejb.VelocityScreenUtil;
35
36 import java.util.HashMap JavaDoc;
37 import java.util.List JavaDoc;
38 import java.util.ListIterator JavaDoc;
39
40 /**
41  * @pojo2ejb.class
42  * name="AttributeManagement"
43  * jndi-prefix="za/org/coefficient/admin/"
44  * interface-extends="za.org.coefficient.interfaces.Module"
45  * interface-local-extends="za.org.coefficient.interfaces.ModuleLocal"
46  *
47  * @web.resource-env-ref
48  * name="za/org/coefficient/admin/AttributeManagement"
49  * type="za.org.coefficient.modules.attribute.AttributeManagement"
50  * @web.resource-env-ref
51  * name="AttributeManagement"
52  * type="za.org.coefficient.modules.attribute.AttributeManagement"
53  */

54 public class AttributeManagement extends BaseModule {
55     //~ Static fields/initializers =============================================
56

57     /*
58      * button names
59      */

60     public static final String JavaDoc ADD = "add";
61     public static final String JavaDoc UPDATE = "update";
62     public static final String JavaDoc DELETE = "delete";
63     public static final String JavaDoc EDIT = "edit";
64
65     /*
66      * context keywords
67      */

68     public static final String JavaDoc ERROR = "error";
69     public static final String JavaDoc ATTRIBUTE = "attribute";
70     public static final String JavaDoc ATTRIBUTES = "attributes";
71     public static final String JavaDoc MODULE = "module";
72     public static final String JavaDoc OP = "op";
73
74     /*
75      * form fields
76      */

77     public static final String JavaDoc BUTTON = "button";
78     public static final String JavaDoc NAME = "name";
79     public static final String JavaDoc DESCRIPTION = "description";
80     public static final String JavaDoc TITLE = "title";
81     public static final String JavaDoc MORE = "more";
82     public static final String JavaDoc ID = "id";
83     public static final String JavaDoc ON = "on";
84
85     /*
86      * velocity pages
87      */

88     public static final String JavaDoc ERRORPAGE = "error.vm";
89     public static final String JavaDoc ADMINPAGE = "admin.vm";
90     public static final String JavaDoc LISTPAGE = "list.vm";
91     public static final String JavaDoc INDEXPAGE = "index.vm";
92     public static final String JavaDoc ADDMSG = "add.vm";
93     public static final String JavaDoc COMPLETEDMSG = "completed.vm";
94
95     /*
96      * error messages
97      */

98     public static final String JavaDoc ENTERFIELDS =
99         "You must enter a name and description";
100
101     //~ Instance fields ========================================================
102

103     //
104
// roles which can execute the command method
105
// everybody can execute list
106
//
107
String JavaDoc[] commandRoles = { SecurityUtil.SITE_ADMIN_ROLE_DESC, };
108     String JavaDoc[] doInitialWorkRoles = { SecurityUtil.SITE_ADMIN_ROLE_DESC, };
109
110     //~ Methods ================================================================
111

112     public String JavaDoc getMainMethod() {
113         // NOTE: this can be any method of this class that makes sense
114
return "doInitialWork";
115     }
116
117     public String JavaDoc getModuleDescription() {
118         return "Attributes Management";
119     }
120
121     public String JavaDoc getModuleDisplayName() {
122         return "Attribute Administration";
123     }
124
125     public String JavaDoc canExecuteForRole(CoefficientContext ctx, String JavaDoc methodName,
126                                     Role usersHighestRole) {
127         int i;
128         String JavaDoc role = usersHighestRole.getDescription();
129
130         if (methodName.equals("command")) {
131             for (i = 0; i < commandRoles.length; i++) {
132                 if (role.equals(commandRoles[i])) {
133                     return null;
134                 }
135             }
136
137             return "not authorised";
138         }
139
140         if (methodName.equals("doInitialWork")) {
141             for (i = 0; i < doInitialWorkRoles.length; i++) {
142                 if (role.equals(doInitialWorkRoles[i])) {
143                     return null;
144                 }
145             }
146
147             return "not authorised";
148         }
149
150         return null;
151     }
152
153     /**
154      * executed by the project champion. gives full access to
155      * the task data
156      *
157      */

158     public synchronized CoefficientContext command(CoefficientContext ctx) {
159         HashMap JavaDoc map = new HashMap JavaDoc();
160         String JavaDoc button = ctx.getParameter(BUTTON, "");
161         String JavaDoc title = ctx.getParameter(TITLE, "");
162         String JavaDoc name = ctx.getParameter(NAME, "");
163         String JavaDoc description = ctx.getParameter(DESCRIPTION, "");
164         String JavaDoc more = ctx.getParameter(MORE, "");
165         String JavaDoc id = ctx.getParameter(ID, "");
166         Long JavaDoc idLong;
167         StringBuffer JavaDoc sb = new StringBuffer JavaDoc("Problem here");
168         List JavaDoc allAttributes = getExistingAttributes();
169
170         try {
171             idLong = Long.decode(id);
172         } catch (Throwable JavaDoc t) {
173             idLong = new Long JavaDoc(0);
174         }
175
176         if (button.toLowerCase().equals(ADD)) {
177             if (title.equals("")) {
178                 map.put(ERROR, "must supply a title");
179                 map.put(MODULE, this);
180                 sb = VelocityScreenUtil.getProcessedScreen(ADMINPAGE, map);
181             } else {
182                 ProjectAttribute pa = new ProjectAttribute();
183                 pa.setName(title);
184
185                 int i;
186                 for (i = 0; true; i++) {
187                     name = ctx.getParameter(NAME + i);
188                     description = ctx.getParameter(DESCRIPTION + i);
189                     if (name == null) {
190                         break;
191                     }
192                     AttributeElement ae = new AttributeElement();
193                     ae.setName(name);
194                     ae.setDescription(description);
195                     pa.getElements()
196                         .add(ae);
197                 }
198
199                 if (more.toLowerCase()
200                     .equals(ON)) {
201                     map.put("newattribute", pa);
202                     map.put(MODULE, this);
203                     sb = VelocityScreenUtil.getProcessedScreen(ADMINPAGE, map);
204                 } else {
205                     try {
206                         HibernateUtil.saveOrUpdate(pa);
207                     } catch (Throwable JavaDoc t) {
208                         System.out.println("Hibernate throws " + t);
209                     }
210                 }
211             }
212         }
213
214         //
215
// handle delete button
216
//
217
if (button.toLowerCase().equals(DELETE)) {
218             ProjectAttribute pa;
219             int i;
220             boolean ticked = false;
221             for (i = 0; i < allAttributes.size(); i++) {
222                 pa = (ProjectAttribute) (allAttributes.get(i));
223                 if (idLong.equals(pa.getId())) {
224                     ticked = true;
225                     try {
226                         if ( canDelete(pa) ) {
227                             HibernateUtil.delete(pa);
228                             List JavaDoc data = getExistingData(idLong);
229                             ListIterator JavaDoc iterator = data.listIterator();
230                             while ( iterator.hasNext() ) {
231                                 ProjectAttributeData pad =
232                                     (ProjectAttributeData) iterator.next();
233                                 HibernateUtil.delete(pad);
234                             }
235                         }
236                     } catch (Throwable JavaDoc t) {
237                         System.out.println("Hibernate throws " + t);
238                     }
239
240                     break;
241                 }
242             }
243             if (!ticked) {
244                 map.put(ERROR, "You must tick an attribute to delete");
245             }
246         }
247         allAttributes = getExistingAttributes();
248         map.put(ATTRIBUTES, allAttributes);
249         map.put(MODULE, this);
250         sb = VelocityScreenUtil.getProcessedScreen(ADMINPAGE, map);
251         ctx.setModuleContent(sb.toString(), getModuleDisplayName());
252         return ctx;
253     }
254
255     public boolean canDelete(ProjectAttribute pa) {
256         try {
257             String JavaDoc query = new String JavaDoc("FROM " + Project.class.getName() +
258                                       " as project, "
259                                       + ProjectAttribute.class.getName() +
260                                       " as pa where pa in elements(project.attributes) and " +
261                                       " pa.id = " + pa.getId());
262             List JavaDoc list = HibernateUtil.find(query);
263             if ( list == null ) {
264                 return true;
265             }
266             if ( list.size() != 0 ) {
267                 return false;
268             }
269             else {
270                 return true;
271             }
272         }
273         catch ( Throwable JavaDoc t ) {
274             System.out.println("canDelete throws " + t);
275             return false;
276         }
277     }
278
279     public CoefficientContext doInitialWork(CoefficientContext ctx) {
280         List JavaDoc allAttributes = getExistingAttributes();
281         HashMap JavaDoc map = new HashMap JavaDoc();
282         map.put(ATTRIBUTES, allAttributes);
283         StringBuffer JavaDoc sb = new StringBuffer JavaDoc("Problem here");
284         map.put(MODULE, this);
285         sb = VelocityScreenUtil.getProcessedScreen(ADMINPAGE, map);
286
287         // Set the html into the context
288
ctx.setModuleContent(sb.toString(), getModuleDisplayName());
289         return ctx;
290     }
291
292     public List JavaDoc getExistingAttributes() {
293         List JavaDoc list = null;
294         try {
295             list =
296                 HibernateUtil.find("FROM " + ProjectAttribute.class.getName()
297                              + " as attribute " + "ORDER BY attribute.id");
298         } catch (Throwable JavaDoc t) {
299             System.out.println("Hibernate session throws " + t);
300         }
301
302         return list;
303     }
304
305     public List JavaDoc getExistingData(Long JavaDoc attributeId) {
306         List JavaDoc list = null;
307         try {
308             list =
309                 HibernateUtil.find("FROM " + ProjectAttributeData.class.getName()
310                              + " as data " + "where data.attributeId = ? "
311                              + "ORDER BY data.id", attributeId, Hibernate.LONG);
312         } catch (Throwable JavaDoc t) {
313             System.out.println("Hibernate session throws " + t);
314         }
315
316         return list;
317     }
318
319 }
320
Popular Tags