KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > za > org > coefficient > modules > category > CategoryManagement


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.category;
21
22 import net.sf.hibernate.Hibernate;
23
24 import za.org.coefficient.authentication.Role;
25 import za.org.coefficient.core.Category;
26 import za.org.coefficient.core.Project;
27 import za.org.coefficient.interfaces.CoefficientContext;
28 import za.org.coefficient.modules.BaseModule;
29 import net.sf.hibernate.util.HibernateUtil;
30 import za.org.coefficient.util.ejb.SecurityUtil;
31 import za.org.coefficient.util.ejb.VelocityScreenUtil;
32
33 import java.util.ArrayList JavaDoc;
34 import java.util.HashMap JavaDoc;
35 import java.util.List JavaDoc;
36 import java.util.StringTokenizer JavaDoc;
37
38 /**
39  * @author Laurie Butgerite
40  *
41  * @pojo2ejb.class
42  * name="CategoryManagement"
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/CategoryManagement"
49  * type="za.org.coefficient.modules.category.CategoryManagement"
50  * @web.resource-env-ref
51  * name="CategoryManagement"
52  * type="za.org.coefficient.modules.category.CategoryManagement"
53  *
54  */

55 public class CategoryManagement extends BaseModule {
56     //~ Instance fields ========================================================
57

58     public final String JavaDoc ACTION = "action";
59     public final String JavaDoc ADD = "add";
60     public final String JavaDoc ADDPAGE = "add.vm";
61     public final String JavaDoc ALL = "All";
62     public final String JavaDoc CATDISP = "catdisp";
63     public final String JavaDoc CATEGORY = "category";
64     public final String JavaDoc CHILDID = "childid";
65     public final String JavaDoc CLOSE = "close";
66     public final String JavaDoc DELETE = "delete";
67     public final String JavaDoc DESCRIPTION = "description";
68     public final String JavaDoc EDIT = "edit";
69     public final String JavaDoc ERROR = "error";
70     public final String JavaDoc GET = "get";
71     public final String JavaDoc ID = "id";
72     public final String JavaDoc INDEXPAGE = "index.vm";
73     public final String JavaDoc LIST = "list";
74     public final String JavaDoc LISTPAGE = "list.vm";
75     public final String JavaDoc MODULE = "module";
76     public final String JavaDoc MSG = "msg";
77     public final String JavaDoc NAME = "name";
78     public final String JavaDoc OP = "op";
79     public final String JavaDoc OPEN = "open";
80     public final String JavaDoc PARENT = "parent";
81     public final String JavaDoc PATH = "path";
82     public final String JavaDoc ROOT = "root";
83     public final String JavaDoc SESSION_ID = "CategoryManagementBean";
84     public final String JavaDoc TEMPPAGE = "temp.vm";
85
86     //
87
// roles which can execute the interpretOp method
88
//
89
String JavaDoc[] interpretOpRoles = { SecurityUtil.SITE_ADMIN_ROLE_DESC };
90
91     //~ Methods ================================================================
92

93     public Category getCategoryByName(String JavaDoc name) {
94         Category root = getRoot();
95         List JavaDoc categories;
96         categories = getSubCategoriesForDisplay("" + root.getId(), null);
97         CategoryDisplayData cat = new CategoryDisplayData();
98         Category hit = new Category();
99         int i;
100         StringTokenizer JavaDoc toks = new StringTokenizer JavaDoc(name, "/");
101         String JavaDoc st;
102         while (toks.hasMoreElements()) {
103             st = (String JavaDoc) toks.nextElement();
104             for (i = 0; i < categories.size(); i++) {
105                 cat = (CategoryDisplayData) (categories.get(i));
106                 if (st.equals(cat.getName())) {
107                     hit = getCategory("" + cat.getId());
108                     categories = cat.getChildren();
109                     i = 0;
110                 }
111             }
112         }
113
114         return hit;
115     }
116
117     public CoefficientContext getCategoryByName(CoefficientContext ctx) {
118         String JavaDoc name = ctx.getParameter(NAME, "");
119         Category hit = getCategoryByName(name);
120
121         HashMap JavaDoc map = new HashMap JavaDoc();
122         map.put(CATEGORY, hit);
123         List JavaDoc list;
124         list = listAll();
125         map.put("list", list);
126         StringBuffer JavaDoc sb = VelocityScreenUtil.getProcessedScreen(TEMPPAGE, map);
127         ctx.setModuleContent(sb.toString(), getModuleDisplayName());
128         return ctx;
129     }
130
131     public String JavaDoc getMainMethod() {
132         // NOTE: this can be any method of this class that makes sense
133
return "interpretOp";
134     }
135
136     public String JavaDoc getModuleDescription() {
137         return "For mangement of categories";
138     }
139
140     public String JavaDoc getModuleDisplayName() {
141         return "Category Administration";
142     }
143
144     private CoefficientContext addCategory(CoefficientContext ctx) {
145         try {
146             String JavaDoc parentid = ctx.getParameter(ID, "");
147             String JavaDoc childid = ctx.getParameter(CHILDID, "");
148             Category parent = null;
149             Category category = null;
150             String JavaDoc name = ctx.getParameter(NAME, "");
151             String JavaDoc description = ctx.getParameter(DESCRIPTION, "");
152             HashMap JavaDoc map = new HashMap JavaDoc();
153             if (name.trim()
154                     .equals("")) {
155                 String JavaDoc msg;
156                 parent = getCategory(parentid);
157                 if (!childid.trim()
158                             .equals("")) {
159                     msg = new String JavaDoc("Editing Category");
160                     category = getCategory(childid);
161                     map.put(CATEGORY, category);
162                 } else {
163                     msg = new String JavaDoc("Adding category");
164                 }
165                 map.put(MSG, msg);
166                 map.put(MODULE, this);
167                 map.put(PARENT, parent);
168                 StringBuffer JavaDoc sb =
169                     VelocityScreenUtil.getProcessedScreen(ADDPAGE, map);
170                 ctx.setModuleContent(sb.toString(), getModuleDisplayName());
171             } else {
172                 if (name.equals(ALL)) {
173                     map.put(MODULE, this);
174                     map.put(ERROR, "Name must not be 'All'");
175                     StringBuffer JavaDoc sb =
176                         VelocityScreenUtil.getProcessedScreen(INDEXPAGE, map);
177                     ctx.setModuleContent(sb.toString(), getModuleDisplayName());
178                 } else {
179                     parent = getCategory(parentid);
180                     Category newCategory = new Category();
181                     newCategory.setName(name);
182                     newCategory.setDescription(description);
183                     newCategory.setParentCategory(parent);
184                     if (!childid.trim()
185                                 .equals("")) {
186                         Long JavaDoc id = null;
187                         try {
188                             id = new Long JavaDoc(childid);
189                         } catch (Throwable JavaDoc t) {
190                             System.out.println("throws " + t + " with "
191                                 + childid);
192                         }
193                         newCategory = getCategory(childid);
194                         newCategory.setName(name);
195                         newCategory.setDescription(description);
196                     }
197                     try {
198                         HibernateUtil.saveOrUpdate(newCategory);
199                     } catch (Throwable JavaDoc t) {
200                         System.out.println("Adding Hibernate throws " + t);
201                     }
202                     viewCategoryTree(ctx, "");
203                 }
204             }
205         } catch (Throwable JavaDoc t) {
206             System.out.println("categoryManagement.addCategorythrows" + t);
207         }
208         return ctx;
209     }
210
211     public String JavaDoc canExecuteForRole(CoefficientContext ctx, String JavaDoc methodName,
212         Role usersHighestRole) {
213         String JavaDoc role = usersHighestRole.getDescription();
214         int i;
215
216         if (methodName.equals("interpretOp")) {
217             for (i = 0; i < interpretOpRoles.length; i++) {
218                 if (role.equals(interpretOpRoles[i])) {
219                     return null;
220                 }
221             }
222             return "not authorised";
223         }
224         return null;
225     }
226
227     private synchronized void closeCategory(CoefficientContext ctx) {
228         try {
229             Object JavaDoc obj = ctx.getSessionAttribute(SESSION_ID);
230             List JavaDoc openList;
231             if (obj == null) {
232                 openList = new ArrayList JavaDoc();
233                 openList.add(getRoot());
234             } else {
235                 openList = (ArrayList JavaDoc) obj;
236             }
237             String JavaDoc id = ctx.getParameter(ID);
238             Category category = null;
239             if (id == null) {
240                 category = getRoot();
241                 id = "" + category.getId();
242             } else {
243                 //category = getCategory(id);
244
boolean b;
245                 b = openList.remove(id);
246             }
247             ctx.setSessionAttribute(SESSION_ID, openList);
248             viewCategoryTree(ctx, "");
249         } catch (Throwable JavaDoc t) {
250             System.out.println("categoryManagement.viewCategoryTree throws" + t);
251             t.printStackTrace();
252         }
253     }
254
255     private synchronized void deleteCategory(CoefficientContext ctx) {
256         String JavaDoc id = ctx.getParameter(ID);
257         Object JavaDoc obj = ctx.getSessionAttribute(SESSION_ID);
258         List JavaDoc openList;
259         if (obj == null) {
260             openList = new ArrayList JavaDoc();
261             openList.add(getRoot());
262         } else {
263             openList = (ArrayList JavaDoc) obj;
264         }
265         if (id == null) {
266             viewCategoryTree(ctx, "");
267         } else {
268             //List subCategories = getSubCategoriesForDisplay(id, openList);
269
List JavaDoc subCategories = getSubCategories(id);
270             if (subCategories.size() != 0) {
271                 viewCategoryTree(ctx, "must delete subcategories first");
272             } else {
273                 openList.remove(id);
274                 Category category = getCategory(id);
275                 if (anyProjects(category)) {
276                     viewCategoryTree(ctx,
277                         "Can't delete. There are projects under this category");
278                 } else {
279                     try {
280                         HibernateUtil.delete(category);
281                     } catch (Throwable JavaDoc t) {
282                         System.out.println("delete Hibernate throws " + t);
283                     }
284                     viewCategoryTree(ctx, "");
285                 }
286             }
287         }
288     }
289
290     public void interpretOp(CoefficientContext ctx) {
291         //
292
// find out what action needs to be taken
293
//
294
String JavaDoc action = ctx.getParameter(ACTION);
295
296         //
297
// no action selected
298
//
299
if (action == null) {
300             viewCategoryTree(ctx, "");
301         } else {
302             if (action.equals(ADD)) {
303                 addCategory(ctx);
304             }
305             if (action.equals(CLOSE)) {
306                 closeCategory(ctx);
307             }
308             if (action.equals(OPEN)) {
309                 openCategory(ctx, "");
310             }
311             if (action.equals(DELETE)) {
312                 deleteCategory(ctx);
313             }
314             if (action.equals(EDIT)) {
315                 addCategory(ctx);
316             }
317             if (action.equals(LIST)) {
318                 listAll(ctx);
319             }
320             if (action.equals(GET)) {
321                 getCategoryByName(ctx);
322             }
323         }
324     }
325
326     public List JavaDoc listAll() {
327         Category root = getRoot();
328
329         //
330
// an arrayList of strings
331
//
332
List JavaDoc list = new ArrayList JavaDoc();
333         List JavaDoc categories;
334
335         categories = getSubCategoriesForDisplay("" + root.getId(), null);
336         String JavaDoc prefix = new String JavaDoc();
337         list(categories, prefix, list);
338
339         return list;
340     }
341
342     public CoefficientContext listAll(CoefficientContext ctx) {
343         List JavaDoc list;
344         list = listAll();
345
346         HashMap JavaDoc map = new HashMap JavaDoc();
347         map.put("list", list);
348         StringBuffer JavaDoc sb = VelocityScreenUtil.getProcessedScreen(TEMPPAGE, map);
349         ctx.setModuleContent(sb.toString(), getModuleDisplayName());
350         return ctx;
351     }
352
353     public List JavaDoc listAllCategories() throws Exception JavaDoc {
354         return HibernateUtil.find("FROM " + Category.class.getName()
355             + " as cats where cats.parentCategory is not null");
356     }
357
358     private CoefficientContext openCategory(CoefficientContext ctx, String JavaDoc msg) {
359         try {
360             CategoryDisplayData cat = null;
361             Category root = getRoot();
362             Object JavaDoc obj = ctx.getSessionAttribute(SESSION_ID);
363             List JavaDoc openList;
364             if (obj == null) {
365                 openList = new ArrayList JavaDoc();
366                 openList.add("" + root.getId());
367             } else {
368                 openList = (ArrayList JavaDoc) obj;
369             }
370
371             String JavaDoc id = ctx.getParameter(ID);
372             Category category = null;
373             if (id == null) {
374                 category = getRoot();
375                 id = "" + category.getId();
376             } else {
377                 category = getRoot();
378                 openList.add(id);
379             }
380             List JavaDoc subCategories =
381                 getSubCategoriesForDisplay("" + root.getId(), openList);
382             cat = new CategoryDisplayData();
383             cat.setId(category.getId());
384             if (category.getParentCategory() == null) {
385                 cat.setParentId(root.getId());
386             } else {
387                 cat.setParentId(category.getParentCategory().getId());
388             }
389             cat.setName(category.getName());
390             cat.setDescription(category.getDescription());
391             cat.setChildren(subCategories);
392             HashMap JavaDoc map = new HashMap JavaDoc();
393             if (!msg.trim()
394                     .equals("")) {
395                 map.put(ERROR, msg);
396             }
397             map.put(CATDISP, cat);
398             ctx.setSessionAttribute(SESSION_ID, openList);
399             map.put(ROOT, root);
400             StringBuffer JavaDoc sb =
401                 VelocityScreenUtil.getProcessedScreen(LISTPAGE, map);
402             ctx.setModuleContent(sb.toString(), getModuleDisplayName());
403         } catch (Throwable JavaDoc t) {
404             System.out.println("categoryManagement.viewCategoryTree throws" + t);
405         }
406         return ctx;
407     }
408
409     private CoefficientContext viewCategoryTree(CoefficientContext ctx, String JavaDoc msg) {
410         try {
411             CategoryDisplayData cat = null;
412             Category root = getRoot();
413             Object JavaDoc obj = ctx.getSessionAttribute(SESSION_ID);
414             List JavaDoc openList;
415             if (obj == null) {
416                 openList = new ArrayList JavaDoc();
417                 openList.add("" + root.getId());
418             } else {
419                 openList = (ArrayList JavaDoc) obj;
420             }
421
422             String JavaDoc id = ctx.getParameter(ID);
423             Category category = getRoot();
424             List JavaDoc subCategories =
425                 getSubCategoriesForDisplay("" + root.getId(), openList);
426             cat = new CategoryDisplayData();
427             cat.setId(category.getId());
428             if (category.getParentCategory() == null) {
429                 cat.setParentId(root.getId());
430             } else {
431                 cat.setParentId(category.getParentCategory().getId());
432             }
433             cat.setName(category.getName());
434             cat.setDescription(category.getDescription());
435             cat.setChildren(subCategories);
436             HashMap JavaDoc map = new HashMap JavaDoc();
437             if (!msg.trim()
438                     .equals("")) {
439                 map.put(ERROR, msg);
440             }
441             map.put(CATDISP, cat);
442             ctx.setSessionAttribute(SESSION_ID, openList);
443             map.put(ROOT, root);
444             StringBuffer JavaDoc sb =
445                 VelocityScreenUtil.getProcessedScreen(LISTPAGE, map);
446             ctx.setModuleContent(sb.toString(), getModuleDisplayName());
447         } catch (Throwable JavaDoc t) {
448             System.out.println("categoryManagement.viewCategoryTree throws" + t);
449         }
450         return ctx;
451     }
452
453     private Category getCategory(String JavaDoc id) {
454         List JavaDoc list = null;
455         Category cat = null;
456         try {
457             list =
458                 HibernateUtil.find("FROM " + Category.class.getName()
459                     + " as category WHERE category.id = ? ", new Long JavaDoc(id),
460                     Hibernate.LONG);
461
462             int i;
463             for (i = 0; i < list.size(); i++) {
464                 cat = (Category) (list.get(i));
465                 return cat;
466             }
467         } catch (Throwable JavaDoc t) {
468             System.out.println("getCategory Hibernate throws " + t);
469         }
470
471         return cat;
472     }
473
474     /**
475      * see if the root category is there and if it isn't create it
476      */

477     private Category getRoot() {
478         List JavaDoc list = null;
479         Category root = null;
480         try {
481             list =
482                 HibernateUtil.find("FROM " + Category.class.getName()
483                     + " as category WHERE category.name = ? "
484                     + " ORDER BY category.id ", ALL, Hibernate.STRING);
485
486             int i;
487             for (i = 0; i < list.size(); i++) {
488                 Category cat = (Category) (list.get(i));
489                 if (i > 0) {
490                     HibernateUtil.delete(cat);
491                 } else {
492                     root = cat;
493                 }
494             }
495             if (list.size() == 0) {
496                 Category cat = new Category();
497                 cat.setName(ALL);
498                 cat.setDescription("All Categories");
499                 cat.setParentCategory(null);
500                 try {
501                     HibernateUtil.saveOrUpdate(cat);
502                 } catch (Throwable JavaDoc t) {
503                     System.out.println("getRoot1 Hibernate throws " + t);
504                 }
505
506                 root = cat;
507             }
508         } catch (Throwable JavaDoc t) {
509             System.out.println("getRoot2 Hibernate throws " + t);
510         }
511
512         return root;
513     }
514
515     private List JavaDoc getSubCategories(String JavaDoc id) {
516         List JavaDoc list = null;
517         Category cat = null;
518         try {
519             list =
520                 HibernateUtil.find("FROM " + Category.class.getName()
521                     + " as category "
522                     + " WHERE category.parentCategory.id = ? ", new Long JavaDoc(id),
523                     Hibernate.LONG);
524         } catch (Throwable JavaDoc t) {
525             System.out.println("getSubCategories Hibernate throws " + t);
526         }
527
528         return list;
529     }
530
531     private List JavaDoc getSubCategoriesForDisplay(String JavaDoc id, List JavaDoc openList) {
532         List JavaDoc categories = getSubCategories(id);
533         Category cat;
534
535         List JavaDoc display = new ArrayList JavaDoc();
536         CategoryDisplayData displaydata;
537         List JavaDoc childrenAsDisplay;
538
539         if ((openList == null) || openList.contains(id)) {
540             int i;
541             int j;
542
543             for (i = 0; i < categories.size(); i++) {
544                 cat = (Category) categories.get(i);
545                 displaydata = new CategoryDisplayData();
546                 displaydata.setId(cat.getId());
547                 displaydata.setName(cat.getName());
548                 displaydata.setDescription(cat.getDescription());
549                 childrenAsDisplay =
550                     getSubCategoriesForDisplay("" + cat.getId(), openList);
551                 displaydata.setChildren(childrenAsDisplay);
552                 display.add(displaydata);
553             }
554         }
555
556         return display;
557     }
558
559     /**
560      * return true if there are any projects using this category
561      */

562     private boolean anyProjects(Category cat) {
563         List JavaDoc list = new ArrayList JavaDoc();
564         try {
565             list =
566                 HibernateUtil.find("select project from " + Project.class.getName()
567                     + " as project, " + " category in project.categories "
568                     + "where category.id = ?",
569                 /*
570                 "select project from " + Project.class.getName() +
571                 " as project " +
572                 " where categories in elements(project.categories) " +
573                 " and category_id = ?",
574                 //" and category.id = ?",
575                 */

576                 cat.getId(), Hibernate.LONG);
577
578             int i;
579             Project p;
580             for (i = 0; i < list.size(); i++) {
581                 p = (Project) (list.get(i));
582             }
583         } catch (Throwable JavaDoc t) {
584             System.out.println("anyProjects throws " + t);
585         }
586
587         return list.size() > 0;
588     }
589
590     private void list(List JavaDoc categories, String JavaDoc prefix, List JavaDoc output) {
591         CategoryDisplayData cat;
592         int i;
593         String JavaDoc st;
594         if (categories != null) {
595             for (i = 0; i < categories.size(); i++) {
596                 cat = (CategoryDisplayData) (categories.get(i));
597                 output.add(prefix + "/" + cat.getName());
598                 list(cat.getChildren(), prefix + "/" + cat.getName(), output);
599             }
600         }
601     }
602
603     private void removeBranch(CategoryDisplayData cat, String JavaDoc id) {
604         if (id.equals("" + cat.getId())) {
605             cat.setChildren(new ArrayList JavaDoc());
606         } else {
607             int i;
608
609             List JavaDoc children = cat.getChildren();
610             CategoryDisplayData c;
611             for (i = 0; i < children.size(); i++) {
612                 c = (CategoryDisplayData) (children.get(i));
613                 removeBranch(c, id);
614             }
615         }
616     }
617 }
618
Popular Tags