KickJava   Java API By Example, From Geeks To Geeks.

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


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 import net.sf.hibernate.util.HibernateUtil;
24 import net.sf.hibernate.Session;
25
26 import org.apache.commons.lang.StringUtils;
27
28 import za.org.coefficient.authentication.Role;
29 import za.org.coefficient.core.Category;
30 import za.org.coefficient.core.Constants;
31 import za.org.coefficient.core.Project;
32 import za.org.coefficient.interfaces.CoefficientContext;
33 import za.org.coefficient.modules.BaseModule;
34 import za.org.coefficient.modules.project.ProjectConstants;
35 import za.org.coefficient.util.common.HibernatePager;
36 import za.org.coefficient.util.ejb.SecurityUtil;
37 import net.sf.hibernate.util.HibernateUtil;
38 import za.org.coefficient.util.ejb.VelocityScreenUtil;
39
40 import java.util.ArrayList JavaDoc;
41 import java.util.HashMap JavaDoc;
42 import java.util.List JavaDoc;
43
44 /**
45  *
46  * @author Laurie Butgerite
47  *
48  * @pojo2ejb.class
49  * name="CategoryBrowse"
50  * jndi-prefix="za/org/coefficient/orphan/"
51  * interface-extends="za.org.coefficient.interfaces.Module"
52  * interface-local-extends="za.org.coefficient.interfaces.ModuleLocal"
53  *
54  * @web.resource-env-ref
55  * name="za/org/coefficient/orphan/CategoryBrowse"
56  * type="za.org.coefficient.modules.category.CategoryBrowse"
57  * @web.resource-env-ref
58  * name="CategoryBrowse"
59  * type="za.org.coefficient.modules.category.CategoryBrowse"
60  */

61 public class CategoryBrowse extends BaseModule {
62     //~ Instance fields ========================================================
63

64     public final String JavaDoc ACTION = "action";
65     public final String JavaDoc ALL = "All";
66     public final String JavaDoc BROWSE = "browse";
67     public final String JavaDoc BROWSEPAGE = "browse.vm";
68     public final String JavaDoc CATDISP = "catdisp";
69     public final String JavaDoc CATEGORY = "category";
70     public final String JavaDoc CATEGORY_BROWSE_PAGER = "CategoryBrowsePager";
71     public final String JavaDoc CATEGORY_ID = "BrowseCategoryId";
72     public final String JavaDoc CHILDID = "childid";
73     public final String JavaDoc CLOSE = "close";
74     public final String JavaDoc DESCRIPTION = "description";
75     public final String JavaDoc ERROR = "error";
76     public final String JavaDoc ID = "id";
77     public final String JavaDoc LIST = "list";
78     public final String JavaDoc MODULE = "module";
79     public final String JavaDoc MSG = "msg";
80     public final String JavaDoc NAME = "name";
81     public final String JavaDoc NEXT = "next";
82     public final String JavaDoc OP = "op";
83     public final String JavaDoc OPEN = "open";
84     public final String JavaDoc PAGE = "page";
85     public final String JavaDoc PAGER = "pager";
86     public final String JavaDoc PARENT = "parent";
87     public final String JavaDoc PATH = "path";
88     public final String JavaDoc PREVIOUS = "previous";
89     public final String JavaDoc PROJECTS = "projects";
90     public final String JavaDoc ROOT = "root";
91     public final String JavaDoc SESSION_ID = "CategoryBrowseBean";
92
93     //~ Methods ================================================================
94

95     public String JavaDoc getMainMethod() {
96         // NOTE: this can be any method of this class that makes sense
97
return "interpretOp";
98     }
99
100     public String JavaDoc getModuleDescription() {
101         return "browsing projects via category";
102     }
103
104     public String JavaDoc getModuleDisplayName() {
105         return "Category Browse";
106     }
107
108     public String JavaDoc canExecuteForRole(CoefficientContext ctx, String JavaDoc methodName,
109         Role usersHighestRole) {
110         return null;
111     }
112
113     private synchronized void closeCategory(CoefficientContext ctx) {
114         try {
115             Object JavaDoc obj = ctx.getSessionAttribute(SESSION_ID);
116             ctx.removeSessionAttribute(CATEGORY_BROWSE_PAGER);
117             List JavaDoc openList;
118             if (obj == null) {
119                 openList = new ArrayList JavaDoc();
120                 openList.add(getRoot());
121             } else {
122                 openList = (ArrayList JavaDoc) obj;
123             }
124             String JavaDoc id = ctx.getParameter(ID);
125             Category category = null;
126             if (id == null) {
127                 category = getRoot();
128                 id = "" + category.getId();
129             } else {
130                 openList.remove(id);
131             }
132             ctx.setSessionAttribute(SESSION_ID, openList);
133             viewCategoryTree(ctx, "");
134         } catch (Throwable JavaDoc t) {
135             System.out.println("categoryBrowse.viewCategoryTree throws" + t);
136             t.printStackTrace();
137         }
138     }
139
140     public void interpretOp(CoefficientContext ctx) {
141         //
142
// find out what action needs to be taken
143
//
144
String JavaDoc action = ctx.getParameter(ACTION);
145
146         //
147
// no action selected
148
//
149
if (action == null) {
150             viewCategoryTree(ctx, "");
151         } else {
152             if (action.equals(CLOSE)) {
153                 closeCategory(ctx);
154             }
155             if (action.equals(OPEN)) {
156                 openCategory(ctx, "");
157             }
158             if (action.equals(LIST)) {
159                 listProjects(ctx);
160             }
161         }
162     }
163
164     private void listProjects(CoefficientContext ctx) {
165         Category cat = getCategory(ctx.getParameter(CATEGORY + ID));
166         HibernatePager hp = null;
167         try {
168             hp = (HibernatePager) ctx.getSessionAttribute(CATEGORY_BROWSE_PAGER);
169             Long JavaDoc catid = (Long JavaDoc) ctx.getSessionAttribute(CATEGORY_ID);
170             if ((hp == null) || (catid == null)
171                 || ((catid != null) && (catid.compareTo(cat.getId()) != 0))) {
172                 Object JavaDoc[] data = new Object JavaDoc[1];
173                 data[0] = cat.getId();
174                 hp = new HibernatePager("select project from "
175                         + Project.class.getName() + " as project, "
176                         + " category in project.categories "
177                         + "where category.id = ? "
178                         + "order by project.statistics.currentData.rank desc ", data,
179                         Constants.MAX_ELEMENTS_PER_PAGE);
180                 ctx.setSessionAttribute(CATEGORY_BROWSE_PAGER, hp);
181                 ctx.setSessionAttribute(CATEGORY_ID, cat.getId());
182             } else {
183                 if (ctx.getParameter(NEXT) != null) {
184                     hp.next();
185                 }
186                 if (ctx.getParameter(PREVIOUS) != null) {
187                     hp.previous();
188                 }
189                 if (ctx.getParameter(PAGE) != null) {
190                     hp.goToPage(ctx.getParameterAsInteger(PAGE).intValue());
191                 }
192             }
193         } catch (Throwable JavaDoc t) {
194             System.out.println("listProjects throws " + t);
195             t.printStackTrace();
196         }
197         viewCategoryTree(ctx, "");
198     }
199
200     private void openCategory(CoefficientContext ctx, String JavaDoc msg) {
201         try {
202             CategoryDisplayData cat = null;
203             ctx.removeSessionAttribute(CATEGORY_BROWSE_PAGER);
204             Category root = getRoot();
205             Object JavaDoc obj = ctx.getSessionAttribute(SESSION_ID);
206             List JavaDoc openList;
207             if (obj == null) {
208                 openList = new ArrayList JavaDoc();
209                 openList.add("" + root.getId());
210             } else {
211                 openList = (ArrayList JavaDoc) obj;
212             }
213
214             String JavaDoc id = ctx.getParameter(ID);
215             Category category = null;
216             if (id == null) {
217                 category = getRoot();
218                 id = "" + category.getId();
219             } else {
220                 category = getRoot();
221                 openList.add(id);
222             }
223             ctx.setSessionAttribute(SESSION_ID, openList);
224             viewCategoryTree(ctx, "");
225         } catch (Throwable JavaDoc t) {
226             System.out.println("categoryBrowse.viewCategoryTree throws" + t);
227         }
228     }
229
230     private CoefficientContext viewCategoryTree(CoefficientContext ctx, String JavaDoc msg) {
231         try {
232             CategoryDisplayData cat = null;
233             Category root = getRoot();
234             Object JavaDoc obj = ctx.getSessionAttribute(SESSION_ID);
235             List JavaDoc openList;
236             if (obj == null) {
237                 openList = new ArrayList JavaDoc();
238                 openList.add("" + root.getId());
239             } else {
240                 openList = (ArrayList JavaDoc) obj;
241             }
242
243             HibernatePager hp =
244                 (HibernatePager) ctx.getSessionAttribute(CATEGORY_BROWSE_PAGER);
245
246             String JavaDoc id = ctx.getParameter(ID);
247             Category category = getRoot();
248             List JavaDoc subCategories =
249                 getSubCategoriesForDisplay("" + root.getId(), openList);
250             cat = new CategoryDisplayData();
251             cat.setProjectCount(anyProjectsCount(category));
252             cat.setId(category.getId());
253             if (category.getParentCategory() == null) {
254                 cat.setParentId(root.getId());
255             } else {
256                 cat.setParentId(category.getParentCategory().getId());
257             }
258             cat.setName(category.getName());
259             cat.setDescription(category.getDescription());
260             cat.setChildren(subCategories);
261             HashMap JavaDoc map = new HashMap JavaDoc();
262             if (!msg.trim()
263                     .equals("")) {
264                 map.put(ERROR, msg);
265             }
266             map.put(CATDISP, cat);
267             ctx.setSessionAttribute(SESSION_ID, openList);
268             map.put(ROOT, root);
269             map.put(PAGER, hp);
270             map.put("user", ctx.getCurrentUser());
271             if (ctx.getSessionAttribute(CATEGORY_ID) != null) {
272                 map.put(CATEGORY + ID, ctx.getSessionAttribute(CATEGORY_ID));
273             }
274
275             map.put("project_name", ProjectConstants.PROJECT_NAME);
276             map.put("project_name_cap",
277                     StringUtils.capitalise(ProjectConstants.PROJECT_NAME));
278             if(ctx.getCurrentUser() != null &&
279                ctx.getCurrentUser().getSystemRole().getRoleValue()
280                <= SecurityUtil.SITE_MODERATOR_ROLE_VAL) {
281                 map.put("userIsAdmin", new Boolean JavaDoc(true));
282             }
283             StringBuffer JavaDoc sb =
284                 VelocityScreenUtil.getProcessedScreen(BROWSEPAGE, map);
285             ctx.setModuleContent(sb.toString(), getModuleDisplayName());
286         } catch (Throwable JavaDoc t) {
287             System.out.println("categoryBrowse.viewCategoryTree throws" + t);
288         }
289         return ctx;
290     }
291
292     private Category getCategory(String JavaDoc id) {
293         List JavaDoc list = null;
294         Category cat = null;
295         try {
296             list =
297                 HibernateUtil.find("FROM " + Category.class.getName()
298                     + " as category WHERE category.id = ? ", new Long JavaDoc(id),
299                     Hibernate.LONG);
300
301             int i;
302             for (i = 0; i < list.size(); i++) {
303                 cat = (Category) (list.get(i));
304                 return cat;
305             }
306         } catch (Throwable JavaDoc t) {
307             System.out.println("getCategory Hibernate throws " + t);
308         }
309
310         return cat;
311     }
312
313     /**
314      * see if the root category is there and if it isn't create it
315      */

316     private Category getRoot() {
317         List JavaDoc list = null;
318         Category root = null;
319         try {
320             list =
321                 HibernateUtil.find("FROM " + Category.class.getName()
322                     + " as category WHERE category.name = ? "
323                     + " ORDER BY category.id ", ALL, Hibernate.STRING);
324             int i;
325             for (i = 0; i < list.size(); i++) {
326                 Category cat = (Category) (list.get(i));
327                 if (i > 0) {
328                     HibernateUtil.delete(cat);
329                 } else {
330                     root = cat;
331                 }
332             }
333             if (list.size() == 0) {
334                 Category cat = new Category();
335                 cat.setName(ALL);
336                 cat.setDescription("All Categories");
337                 cat.setParentCategory(null);
338                 try {
339                     HibernateUtil.saveOrUpdate(cat);
340                 } catch (Throwable JavaDoc t) {
341                     System.out.println("getRoot1 Hibernate throws " + t);
342                 }
343
344                 root = cat;
345             }
346         } catch (Throwable JavaDoc t) {
347             System.out.println("getRoot2 Hibernate throws " + t);
348         }
349
350         return root;
351     }
352
353     private List JavaDoc getSubCategories(String JavaDoc id) {
354         List JavaDoc list = null;
355         Category cat = null;
356         try {
357             list =
358                 HibernateUtil.find("FROM " + Category.class.getName()
359                     + " as category "
360                     + " WHERE category.parentCategory.id = ? ", new Long JavaDoc(id),
361                     Hibernate.LONG);
362         } catch (Throwable JavaDoc t) {
363             System.out.println("getSubCategories Hibernate throws " + t);
364         }
365
366         return list;
367     }
368
369     private List JavaDoc getSubCategoriesForDisplay(String JavaDoc id, List JavaDoc openList) {
370         List JavaDoc categories = getSubCategories(id);
371         Category cat;
372
373         List JavaDoc display = new ArrayList JavaDoc();
374         CategoryDisplayData displaydata;
375         List JavaDoc childrenAsDisplay;
376
377         if (openList.contains(id)) {
378             int i;
379             int j;
380
381             for (i = 0; i < categories.size(); i++) {
382                 cat = (Category) categories.get(i);
383                 displaydata = new CategoryDisplayData();
384                 displaydata.setId(cat.getId());
385                 displaydata.setName(cat.getName());
386                 displaydata.setDescription(cat.getDescription());
387                 childrenAsDisplay =
388                     getSubCategoriesForDisplay("" + cat.getId(), openList);
389                 displaydata.setChildren(childrenAsDisplay);
390                 displaydata.setProjectCount(anyProjectsCount(cat));
391                 display.add(displaydata);
392             }
393         }
394
395         return display;
396     }
397
398     /**
399      * return the count of projects with this category
400      */

401     private int anyProjectsCount(Category cat) {
402         List JavaDoc list = new ArrayList JavaDoc();
403         try {
404             list =
405                 HibernateUtil.find("select count(project) from "
406                     + Project.class.getName() + " as project, "
407                     + " category in project.categories "
408                     + "where category.id = ?", cat.getId(), Hibernate.LONG);
409
410             if (list.size() > 0) {
411                 return (((Integer JavaDoc) (list.get(0))).intValue());
412             } else {
413                 return 0;
414             }
415         } catch (Throwable JavaDoc t) {
416             System.out.println("anyProjects throws " + t);
417         }
418
419         return 0;
420     }
421 }
422
Popular Tags