KickJava   Java API By Example, From Geeks To Geeks.

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


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.Hibernate;
23 import net.sf.hibernate.HibernateException;
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.ArrayList JavaDoc;
37 import java.util.HashMap JavaDoc;
38 import java.util.List JavaDoc;
39 import java.util.StringTokenizer JavaDoc;
40
41 /**
42  * @pojo2ejb.class
43  * name="AttributeDataManagement"
44  * jndi-prefix="za/org/coefficient/admin/"
45  * interface-extends="za.org.coefficient.interfaces.Module"
46  * interface-local-extends="za.org.coefficient.interfaces.ModuleLocal"
47  *
48  * @web.resource-env-ref
49  * name="za/org/coefficient/admin/AttributeDataManagement"
50  * type="za.org.coefficient.modules.attribute.AttributeDataManagement"
51  * @web.resource-env-ref
52  * name="AttributeDataManagement"
53  * type="za.org.coefficient.modules.attribute.AttributeDataManagement"
54  */

55 public class AttributeDataManagement extends BaseModule {
56     //~ Static fields/initializers =============================================
57

58     /*
59      * button names
60      */

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

70     public static final String JavaDoc ERROR = "error";
71     public static final String JavaDoc HEADING = "heading";
72     public static final String JavaDoc ATTRIBUTE = "attribute";
73     public static final String JavaDoc ATTRIBUTES = "attributes";
74     public static final String JavaDoc LINES = "lines";
75     public static final String JavaDoc LINE = "line";
76     public static final String JavaDoc MODULE = "module";
77     public static final String JavaDoc OP = "op";
78     public static final String JavaDoc ROWS = "rows";
79     public static final String JavaDoc COLS = "cols";
80     public static final String JavaDoc SIZE = "size";
81     public static final String JavaDoc EQUALS = "=";
82
83     /*
84      * form fields
85      */

86     public static final String JavaDoc BUTTON = "button";
87     public static final String JavaDoc NAME = "name";
88     public static final String JavaDoc DESCRIPTION = "description";
89     public static final String JavaDoc TITLE = "title";
90     public static final String JavaDoc MORE = "more";
91     public static final String JavaDoc ID = "id";
92     public static final String JavaDoc ON = "on";
93
94     /*
95      * velocity pages
96      */

97     public static final String JavaDoc DATAPAGE = "data.vm";
98     public static final String JavaDoc TEXTPAGE = "text.vm";
99     public static final String JavaDoc TEXTAREAPAGE = "textarea.vm";
100     public static final String JavaDoc HEADINGPAGE = "heading.vm";
101     public static final String JavaDoc TDPAGE = "td.vm";
102     public static final String JavaDoc DELETEPAGE = "delete.vm";
103     public static final String JavaDoc CANNOTDELETEPAGE = "cannotdelete.vm";
104
105     /*
106      * error messages
107      */

108     public static final String JavaDoc ENTERFIELDS =
109         "You must enter a name and description";
110
111     //~ Instance fields ========================================================
112

113     //
114
// roles which can execute the command method
115
// everybody can execute list
116
//
117
String JavaDoc[] commandRoles = { SecurityUtil.SITE_ADMIN_ROLE_DESC, };
118     String JavaDoc[] doInitialWorkRoles = { SecurityUtil.SITE_ADMIN_ROLE_DESC, };
119
120     //~ Methods ================================================================
121

122     public String JavaDoc getMainMethod() {
123         // NOTE: this can be any method of this class that makes sense
124
return "doInitialWork";
125     }
126
127     public String JavaDoc getModuleDescription() {
128         return "AttributeData Management";
129     }
130
131     public String JavaDoc getModuleDisplayName() {
132         return "Attribute Data Administration";
133     }
134
135     public String JavaDoc canExecuteForRole(CoefficientContext ctx, String JavaDoc methodName,
136                                     Role usersHighestRole) {
137         int i;
138         String JavaDoc role = usersHighestRole.getDescription();
139
140         if (methodName.equals("command")) {
141             for (i = 0; i < commandRoles.length; i++) {
142                 if (role.equals(commandRoles[i])) {
143                     return null;
144                 }
145             }
146
147             return "not authorised";
148         }
149
150         if (methodName.equals("doInitialWork")) {
151             for (i = 0; i < doInitialWorkRoles.length; i++) {
152                 if (role.equals(doInitialWorkRoles[i])) {
153                     return null;
154                 }
155             }
156
157             return "not authorised";
158         }
159
160         return null;
161     }
162
163     public List JavaDoc getExistingAttributes() {
164         List JavaDoc list = null;
165         try {
166             list =
167                 HibernateUtil.find("FROM " + ProjectAttribute.class.getName()
168                              + " as attribute " + "ORDER BY attribute.id");
169         } catch (Throwable JavaDoc t) {
170             System.out.println("Hibernate session throws " + t);
171         }
172
173         return list;
174     }
175
176     public ProjectAttribute getAttributeByName(String JavaDoc name) {
177         List JavaDoc list = null;
178         try {
179             list =
180                 HibernateUtil.find("FROM " + ProjectAttribute.class.getName()
181                              + " as attribute " + " where attribute.name = ?", name,
182                              Hibernate.STRING);
183             if (list.size() > 0) {
184                 return (ProjectAttribute) (list.get(0));
185             }
186         } catch (Throwable JavaDoc t) {
187             System.out.println("Hibernate session throws " + t);
188         }
189
190         return new ProjectAttribute();
191     }
192
193     public ProjectAttribute getAttributeByNumber(Long JavaDoc id) {
194         List JavaDoc list = null;
195         try {
196             list =
197                 HibernateUtil.find("FROM " + ProjectAttribute.class.getName()
198                              + " as attribute " + " where attribute.id = ?", id,
199                              Hibernate.LONG);
200
201             if (list.size() > 0) {
202                 return (ProjectAttribute) (list.get(0));
203             }
204         } catch (Throwable JavaDoc t) {
205             System.out.println("Hibernate session throws " + t);
206         }
207
208         return new ProjectAttribute();
209     }
210
211     public List JavaDoc getExistingData(Long JavaDoc attributeId) {
212         List JavaDoc list = null;
213         try {
214             list =
215                 HibernateUtil.find("FROM " + ProjectAttributeData.class.getName()
216                              + " as data " + "where data.attributeId = ? "
217                              + "ORDER BY data.id", attributeId, Hibernate.LONG);
218         } catch (Throwable JavaDoc t) {
219             System.out.println("Hibernate session throws " + t);
220         }
221
222         return list;
223     }
224
225     public List JavaDoc getExistingDataKeywords(Long JavaDoc attributeId) {
226         List JavaDoc list = getExistingData(attributeId);
227         List JavaDoc newlist = new ArrayList JavaDoc();
228
229         int i;
230         Long JavaDoc elementId = null;
231         ProjectAttributeData pad;
232         for (i = 0; i < list.size(); i++) {
233             pad = (ProjectAttributeData) (list.get(i));
234             if (elementId == null) {
235                 elementId = pad.getElementId();
236                 newlist.add(pad);
237             } else {
238                 if (elementId.compareTo(pad.getElementId()) == 0) {
239                     newlist.add(pad);
240                 }
241             }
242         }
243
244         return newlist;
245     }
246
247     /**
248      * executed by the project champion. gives full access to
249      * the task data
250      *
251      */

252     public synchronized CoefficientContext command(CoefficientContext ctx) {
253         HashMap JavaDoc map = new HashMap JavaDoc();
254         String JavaDoc button = ctx.getParameter(BUTTON, "");
255         String JavaDoc title = ctx.getParameter(TITLE, "");
256         String JavaDoc name = ctx.getParameter(NAME, "");
257         String JavaDoc description = ctx.getParameter(DESCRIPTION, "");
258         String JavaDoc more = ctx.getParameter(MORE, "");
259         String JavaDoc id = ctx.getParameter(ID, "");
260         Long JavaDoc idLong;
261         StringBuffer JavaDoc sb = new StringBuffer JavaDoc("Problem here");
262         List JavaDoc allAttributes = getExistingAttributes();
263
264         try {
265             idLong = Long.decode(id);
266         } catch (Throwable JavaDoc t) {
267             idLong = new Long JavaDoc(0);
268         }
269
270         if (button.toLowerCase().equals(ADD)) {
271             ProjectAttribute pa = null;
272
273             try {
274                 pa = getAttributeByNumber(ctx.getParameterAsLong(ATTRIBUTE + ID));
275             } catch (Throwable JavaDoc t) {
276                 System.out.println("throws " + t);
277             }
278
279             List JavaDoc elements = pa.getElements();
280             AttributeElement element;
281
282             int i;
283
284             for (i = 0; i < elements.size(); i++) {
285                 element = (AttributeElement) (elements.get(i));
286                 ProjectAttributeData pad = new ProjectAttributeData();
287                 pad.setAttributeId(pa.getId());
288                 pad.setElementId(element.getId());
289                 pad.setDescription(ctx.getParameter(element.getName(), ""));
290                 try {
291                     HibernateUtil.saveOrUpdate(pad);
292                 } catch (Throwable JavaDoc t) {
293                     System.out.println("Hibernate throws " + t);
294                 }
295             }
296         }
297
298         //
299
// handle delete button
300
//
301
if (button.toLowerCase().equals(DELETE)) {
302             try {
303                 List JavaDoc list =
304                     HibernateUtil.find("FROM " + ProjectAttributeData.class.getName()
305                                  + " as data " + "where data.id = " + id);
306                 if ( list != null && list.size() > 0 ) {
307                     ProjectAttributeData pad = (ProjectAttributeData)(list.get(0));
308                     if ( canDelete(pad) ) {
309                         HibernateUtil.delete(pad);
310                     }
311                 }
312             }
313             catch ( Throwable JavaDoc t ) {
314                 System.out.println("trouble getting project attribute data " + t);
315             }
316         }
317         setupSelect(ctx, map);
318         allAttributes = getExistingAttributes();
319         map.put(ATTRIBUTES, allAttributes);
320         sb = VelocityScreenUtil.getProcessedScreen(DATAPAGE, map);
321         ctx.setModuleContent(sb.toString(), getModuleDisplayName());
322         return ctx;
323     }
324
325     public CoefficientContext doInitialWork(CoefficientContext ctx) {
326         List JavaDoc allAttributes = getExistingAttributes();
327         HashMap JavaDoc map = new HashMap JavaDoc();
328         map.put(ATTRIBUTES, allAttributes);
329         StringBuffer JavaDoc sb = new StringBuffer JavaDoc("Problem here");
330         map.put(MODULE, this);
331         sb = VelocityScreenUtil.getProcessedScreen(DATAPAGE, map);
332
333         // Set the html into the context
334
ctx.setModuleContent(sb.toString(), getModuleDisplayName());
335         return ctx;
336     }
337
338     private boolean canDelete(ProjectAttributeData pad) {
339         try {
340             String JavaDoc query = new String JavaDoc("FROM " + Project.class.getName() +
341                                       " as project, " + ProjectAttributeData.class.getName() +
342                                       " as pad where pad in elements(project.attributeData) and " +
343                                       " pad.id = " + pad.getId());
344             List JavaDoc list = HibernateUtil.find(query);
345             if ( list == null )
346                 return true;
347             if ( list.size() != 0 ) {
348                 return false;
349             }
350             else
351                 return true;
352         }
353         catch ( Throwable JavaDoc t ) {
354             System.out.println("canDelete throws " + t);
355             return false;
356         }
357     }
358
359     private void setupSelect(CoefficientContext ctx, HashMap JavaDoc map) {
360         String JavaDoc attributeName = ctx.getParameter(ATTRIBUTE + NAME, "");
361         ProjectAttribute pa = getAttributeByName(attributeName);
362         ProjectAttributeData pad = new ProjectAttributeData();
363         List JavaDoc data = new ArrayList JavaDoc();
364         StringBuffer JavaDoc sb = new StringBuffer JavaDoc("problem here");
365         AttributeElement element;
366         map.put(SELECT, "yes");
367         List JavaDoc elements = pa.getElements();
368         List JavaDoc lines = new ArrayList JavaDoc();
369         String JavaDoc line = new String JavaDoc();
370         int i;
371
372         map.put(ATTRIBUTE, pa);
373         map.put(ATTRIBUTE + ID, pa.getId());
374         String JavaDoc heading = new String JavaDoc();
375         for (i = 0; i < elements.size(); i++) {
376             element = (AttributeElement) (elements.get(i));
377             map.put(NAME, element.getName());
378             sb = VelocityScreenUtil.getProcessedScreen(HEADINGPAGE, map);
379             heading = heading.concat(sb.toString());
380         }
381         map.put(HEADING, heading);
382
383         data = getExistingData(pa.getId());
384
385         HashMap JavaDoc m = new HashMap JavaDoc();
386         line = new String JavaDoc();
387         if (data != null) {
388             int cnt = elements.size();
389             for (i = 0; i < data.size(); i++) {
390                 pad = (ProjectAttributeData) (data.get(i));
391                 m.put(LINE, pad.getDescription());
392                 sb = VelocityScreenUtil.getProcessedScreen(TDPAGE, m);
393                 line = line.concat(sb.toString());
394
395
396                 if (cnt == 1) {
397                     HashMap JavaDoc mp = new HashMap JavaDoc();
398                     mp.put("attributeData", pad);
399                     mp.put(ATTRIBUTE, pa);
400                     if ( canDelete(pad) ) {
401                         sb = VelocityScreenUtil.getProcessedScreen(DELETEPAGE, mp);
402                     }
403                     else
404                         sb = VelocityScreenUtil.getProcessedScreen(CANNOTDELETEPAGE, mp);
405             
406                     line = line.concat(sb.toString());
407
408                     lines.add(line);
409                     line = new String JavaDoc();
410                 } else {
411                     if (((i + 1) % cnt) == 0) {
412
413                         lines.add(line);
414                         line = new String JavaDoc();
415                     }
416                 }
417             }
418         }
419
420         String JavaDoc desc;
421         line = new String JavaDoc();
422         m = new HashMap JavaDoc();
423         for (i = 0; i < elements.size(); i++) {
424             element = (AttributeElement) (elements.get(i));
425             m.put(NAME, element.getName());
426             desc = element.getDescription();
427             StringTokenizer JavaDoc toks = new StringTokenizer JavaDoc(desc, ",");
428             String JavaDoc cols = "20";
429             String JavaDoc rows = "3";
430             while (toks.hasMoreElements()) {
431                 String JavaDoc st;
432                 st = (String JavaDoc) (toks.nextElement());
433                 if (st.startsWith(COLS + EQUALS)) {
434                     m.put(COLS, st.substring(5));
435                 }
436                 if (st.startsWith(ROWS + EQUALS)) {
437                     m.put(ROWS, st.substring(5));
438                 }
439                 if (st.startsWith(SIZE + EQUALS)) {
440                     m.put(SIZE, st.substring(5));
441                 }
442             }
443             if (desc.startsWith("textarea")) {
444                 sb = VelocityScreenUtil.getProcessedScreen(TEXTAREAPAGE, m);
445             } else {
446                 sb = VelocityScreenUtil.getProcessedScreen(TEXTPAGE, m);
447             }
448             line = line.concat(sb.toString());
449             map.put(LINES, lines);
450         }
451         //lines.add(line);
452
map.put("input", line);
453         line = new String JavaDoc();
454     }
455 }
456
Popular Tags