KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > za > org > coefficient > modules > project > BaseProjectBrowse


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.project;
21
22 import org.apache.commons.lang.StringUtils;
23
24 import za.org.coefficient.core.Constants;
25 import za.org.coefficient.core.Project;
26 import za.org.coefficient.interfaces.CoefficientContext;
27 import za.org.coefficient.modules.BaseModule;
28 import za.org.coefficient.util.common.HibernatePager;
29 import za.org.coefficient.util.ejb.VelocityScreenUtil;
30 import za.org.coefficient.util.ejb.SecurityUtil;
31
32 import java.util.HashMap JavaDoc;
33
34 /**
35  * This is the base class used for searching projects.
36  *
37  * @author detkin
38  */

39 public abstract class BaseProjectBrowse extends BaseModule {
40     //~ Static fields/initializers =============================================
41

42     private static final String JavaDoc PROJECT_PAGER = "__project_pager_";
43     private static final String JavaDoc UNAPPROVED_PROJECT_PAGER =
44         "__unapproved__project_pager_";
45
46     //~ Methods ================================================================
47

48     public CoefficientContext baseViewProjects(CoefficientContext ctx, boolean active)
49         throws Exception JavaDoc {
50         HashMap JavaDoc map = new HashMap JavaDoc();
51         HibernatePager hp = null;
52         if ((ctx.getParameter("projectName") != null)
53             && !ctx.getParameter("projectName")
54                    .trim()
55                    .equals("")) {
56             if (active) {
57                 hp = (HibernatePager) ctx.getSessionAttribute(PROJECT_PAGER);
58             } else {
59                 hp = (HibernatePager) ctx.getSessionAttribute(UNAPPROVED_PROJECT_PAGER);
60             }
61             if (hp == null) {
62                 if (active) {
63                     hp = new HibernatePager(Project.class, "statistics.currentData.rank",
64                             Constants.MAX_ELEMENTS_PER_PAGE, false);
65                 } else {
66                     hp = new HibernatePager(Project.class, "name",
67                             Constants.MAX_ELEMENTS_PER_PAGE);
68                 }
69             }
70             HashMap JavaDoc andSearchParams = new HashMap JavaDoc();
71             andSearchParams.put("active", new Boolean JavaDoc(active));
72             HashMap JavaDoc orSearchParams = new HashMap JavaDoc();
73             orSearchParams.put("name", ctx.getParameter("projectName"));
74             orSearchParams.put("description", ctx.getParameter("projectName"));
75             hp.setFromWhereAndInitialize(andSearchParams, orSearchParams, null);
76         } else if (ctx.getParameter("next") != null) {
77             if (active) {
78                 hp = (HibernatePager) ctx.getSessionAttribute(PROJECT_PAGER);
79             } else {
80                 hp = (HibernatePager) ctx.getSessionAttribute(UNAPPROVED_PROJECT_PAGER);
81             }
82             if (hp == null) {
83                 ctx.setError("Invalid operation");
84             } else {
85                 hp.next();
86             }
87         } else if (ctx.getParameter("previous") != null) {
88             if (active) {
89                 hp = (HibernatePager) ctx.getSessionAttribute(PROJECT_PAGER);
90             } else {
91                 hp = (HibernatePager) ctx.getSessionAttribute(UNAPPROVED_PROJECT_PAGER);
92             }
93
94             if (hp == null) {
95                 ctx.setError("Invalid operation");
96             } else {
97                 hp.previous();
98             }
99         } else if (ctx.getParameter("page") != null) {
100             if (active) {
101                 hp = (HibernatePager) ctx.getSessionAttribute(PROJECT_PAGER);
102             } else {
103                 hp = (HibernatePager) ctx.getSessionAttribute(UNAPPROVED_PROJECT_PAGER);
104             }
105             if (hp == null) {
106                 ctx.setError("pager does not exist");
107             } else {
108                 hp.goToPage(ctx.getParameterAsInteger("page").intValue());
109             }
110         } else {
111             HashMap JavaDoc searchParams = new HashMap JavaDoc();
112             searchParams.put("active", new Boolean JavaDoc(active));
113             if (active) {
114                 hp = new HibernatePager(Project.class, "statistics.currentData.rank",
115                         Constants.MAX_ELEMENTS_PER_PAGE, searchParams, false);
116             } else {
117                 hp = new HibernatePager(Project.class, "name",
118                         Constants.MAX_ELEMENTS_PER_PAGE, searchParams);
119             }
120         }
121
122         if (!ctx.isError()) {
123             map.put("pager", hp);
124
125             //map.put("projects", projects);
126
map.put("module", this);
127             if (active) {
128                 map.put("active", new Boolean JavaDoc(active));
129                 ctx.setSessionAttribute(PROJECT_PAGER, hp);
130             } else {
131                 ctx.setSessionAttribute(UNAPPROVED_PROJECT_PAGER, hp);
132             }
133
134             if (ctx.getCurrentUser() != null) {
135                 map.put("loggedIn", new Boolean JavaDoc(true));
136             }
137             map.put("project_name", ProjectConstants.PROJECT_NAME);
138             map.put("project_name_cap", StringUtils.capitalise(ProjectConstants.PROJECT_NAME));
139             map.put("user", ctx.getCurrentUser());
140             if(ctx.getCurrentUser() != null &&
141                ctx.getCurrentUser().getSystemRole().getRoleValue()
142                <= SecurityUtil.SITE_MODERATOR_ROLE_VAL) {
143                 map.put("userIsAdmin", new Boolean JavaDoc(true));
144             }
145             StringBuffer JavaDoc sb =
146                 VelocityScreenUtil.getProcessedScreen("browse-view.vm", map);
147
148             // Set the html into the context
149
ctx.setModuleContent(sb.toString(), getModuleDisplayName());
150         }
151         return ctx;
152     }
153 }
154
Popular Tags