KickJava   Java API By Example, From Geeks To Geeks.

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


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
24 import org.apache.commons.lang.StringUtils;
25
26 import za.org.coefficient.authentication.Role;
27 import za.org.coefficient.core.AttributeElement;
28 import za.org.coefficient.core.Project;
29 import za.org.coefficient.core.ProjectAttribute;
30 import za.org.coefficient.core.ProjectAttributeData;
31 import za.org.coefficient.interfaces.CoefficientContext;
32 import za.org.coefficient.modules.BaseModule;
33 import za.org.coefficient.modules.project.ProjectConstants;
34 import za.org.coefficient.util.common.HibernatePager;
35 import za.org.coefficient.util.common.InvokerFactory;
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  * @pojo2ejb.class
46  * name="AttributeSearch"
47  * jndi-prefix="za/org/coefficient/orphan/"
48  * interface-extends="za.org.coefficient.interfaces.Module"
49  * interface-local-extends="za.org.coefficient.interfaces.ModuleLocal"
50  *
51  * @web.resource-env-ref
52  * name="za/org/coefficient/orphan/AttributeSearch"
53  * type="za.org.coefficient.modules.attribute.AttributeSearch"
54  * @web.resource-env-ref
55  * name="AttributeSearch"
56  * type="za.org.coefficient.modules.attribute.AttributeSearch"
57  *
58  */

59 public class AttributeSearch extends BaseModule {
60     //~ Static fields/initializers =============================================
61

62     /*
63      * context keywords
64      */

65     public static final String JavaDoc ATTRIBUTE_BROWSE_PAGER = "Attribute_pager";
66     public static final String JavaDoc ERROR = "error";
67     public static final String JavaDoc PAGER = "pager";
68     public static final String JavaDoc ATTRIBUTE = "attribute";
69     public static final String JavaDoc ATTRIBUTES = "attributes";
70     public static final String JavaDoc MODULE = "module";
71     public static final String JavaDoc OP = "op";
72     public static final String JavaDoc NEXT = "next";
73     public static final String JavaDoc PREVIOUS = "previous";
74     public static final String JavaDoc PAGE = "page";
75
76     /*
77      * velocity pages
78      */

79     public static final String JavaDoc SEARCHPAGE = "search.vm";
80     public static final String JavaDoc RESULTPAGE = "result.vm";
81
82     //~ Methods ================================================================
83

84     public String JavaDoc getMainMethod() {
85         // NOTE: this can be any method of this class that makes sense
86
return "doInitialWork";
87     }
88
89     public String JavaDoc getModuleDescription() {
90         return "Attribute Search";
91     }
92
93     public String JavaDoc getModuleDisplayName() {
94         return "Attribute Search";
95     }
96
97     public String JavaDoc canExecuteForRole(CoefficientContext ctx, String JavaDoc methodName,
98         Role usersHighestRole) {
99         return null;
100     }
101
102     public CoefficientContext doInitialWork(CoefficientContext ctx) {
103         HashMap JavaDoc map = new HashMap JavaDoc();
104         StringBuffer JavaDoc sb = new StringBuffer JavaDoc("Problem here");
105         map.put(MODULE, this);
106         map.put(ATTRIBUTES, getAttributes());
107         map.put("project_name", ProjectConstants.PROJECT_NAME);
108         map.put("project_name_cap",
109                 StringUtils.capitalise(ProjectConstants.PROJECT_NAME));
110         sb = VelocityScreenUtil.getProcessedScreen(SEARCHPAGE, map);
111
112         // Set the html into the context
113
ctx.setModuleContent(sb.toString(), getModuleDisplayName());
114         return ctx;
115     }
116
117     public CoefficientContext pager(CoefficientContext ctx) {
118         HibernatePager hp = null;
119         hp = (HibernatePager) ctx.getSessionAttribute(ATTRIBUTE_BROWSE_PAGER);
120         if (ctx.getParameter(NEXT) != null) {
121             hp.next();
122         }
123         if (ctx.getParameter(PREVIOUS) != null) {
124             hp.previous();
125         }
126         if (ctx.getParameter(PAGE) != null) {
127             hp.goToPage(ctx.getParameterAsInteger(PAGE).intValue());
128         }
129         ctx.setSessionAttribute(ATTRIBUTE_BROWSE_PAGER, hp);
130         HashMap JavaDoc map = new HashMap JavaDoc();
131         map.put(PAGER, hp);
132         map.put("user", ctx.getCurrentUser());
133         if(ctx.getCurrentUser() != null &&
134            ctx.getCurrentUser().getSystemRole().getRoleValue()
135            <= SecurityUtil.SITE_MODERATOR_ROLE_VAL) {
136             map.put("userIsAdmin", new Boolean JavaDoc(true));
137         }
138         StringBuffer JavaDoc sb =
139             VelocityScreenUtil.getProcessedScreen(RESULTPAGE, map);
140         ctx.setModuleContent(sb.toString(), getModuleDisplayName());
141         return ctx;
142     }
143
144     public CoefficientContext search(CoefficientContext ctx) {
145         HashMap JavaDoc map = new HashMap JavaDoc();
146         StringBuffer JavaDoc sb = new StringBuffer JavaDoc("Problem here");
147
148         ArrayList JavaDoc values = new ArrayList JavaDoc();
149         String JavaDoc st = parse(ctx, values);
150         Object JavaDoc[] obj = new Object JavaDoc[values.size()];
151         int i;
152         for (i = 0; i < values.size(); i++) {
153             obj[i] = values.get(i);
154         }
155         try {
156             HibernatePager hp = new HibernatePager(st, obj);
157             map.put(PAGER, hp);
158             ctx.setSessionAttribute(ATTRIBUTE_BROWSE_PAGER, hp);
159         } catch (Throwable JavaDoc t) {
160             System.out.println("trouble with pager " + t);
161         }
162         map.put(MODULE, this);
163         if(ctx.getCurrentUser() != null &&
164            ctx.getCurrentUser().getSystemRole().getRoleValue()
165            <= SecurityUtil.SITE_MODERATOR_ROLE_VAL) {
166             map.put("userIsAdmin", new Boolean JavaDoc(true));
167         }
168         sb = VelocityScreenUtil.getProcessedScreen(RESULTPAGE, map);
169
170         // Set the html into the context
171
ctx.setModuleContent(sb.toString(), getModuleDisplayName());
172         return ctx;
173     }
174
175     private List JavaDoc getAttributes() {
176         List JavaDoc attributeDisplayData = new ArrayList JavaDoc();
177
178         //
179
// load the attributes into a displayable format
180
//
181
try {
182             AttributeDisplayData displayData = new AttributeDisplayData();
183             List JavaDoc attributes = (List JavaDoc)InvokerFactory.getInvoker()
184                 .invokeMethodOnModule("AttributeDataManagement",
185                                       "getExistingAttributes",
186                                       new Object JavaDoc[]{});
187             int i;
188             ProjectAttribute pa;
189             for (i = 0; i < attributes.size(); i++) {
190                 displayData = new AttributeDisplayData();
191                 pa = (ProjectAttribute) (attributes.get(i));
192                 displayData.setAttributeName(pa.getName());
193                 displayData.setAttributeId(pa.getId());
194
195                 List JavaDoc data = (List JavaDoc)InvokerFactory.getInvoker()
196                     .invokeMethodOnModule("AttributeDataManagement",
197                                           "getExistingDataKeywords",
198                                           new Object JavaDoc[]{pa.getId()});
199                 displayData.setAttributeData(data);
200                 attributeDisplayData.add(displayData);
201             }
202
203             //map.put("attributes", attributeDisplayData);
204
} catch (Throwable JavaDoc t) {
205             System.err.println("<< error loading attributes");
206         }
207
208         return attributeDisplayData;
209     }
210
211     private String JavaDoc parse(CoefficientContext ctx, List JavaDoc vals) {
212         StringBuffer JavaDoc sb =
213             new StringBuffer JavaDoc("select project from "
214                              + Project.class.getName() + " "
215                              + "as project "
216                              + " left outer join project.attributeData"
217                              + " as attData where 1 = 1 ");
218
219                              //+ ProjectAttributeData.class.getName()
220
//+ " as attData where 1 = 1 ");
221

222         try {
223             List JavaDoc existing = (List JavaDoc)InvokerFactory.getInvoker()
224                 .invokeMethodOnModule("AttributeDataManagement",
225                                       "getExistingAttributes",
226                                       new Object JavaDoc[]{});
227
228             if(existing.size() > 0) {
229                 sb.append(" and ( ");
230             }
231             int foundCount = 0;
232             for (int i = 0; i < existing.size(); i++) {
233                 String JavaDoc attributeId = ctx.getParameter("attribute" + i);
234                 if (attributeId != null) {
235                     Long JavaDoc id;
236                     id = new Long JavaDoc(attributeId);
237                     String JavaDoc[] values = ctx.getParameterValues("data" + i);
238
239                     for (int j = 0; j < values.length; j++) {
240                         if(foundCount != 0) {
241                             sb.append(" and ");
242                         }
243                         //sb.append(" attData in project.attributeData and attData.id = ? ");
244
sb.append(" attData.id = ? ");
245                         vals.add(new Long JavaDoc(values[j]));
246                         foundCount++;
247                     }
248                 }
249             }
250             if(existing.size() > 0) {
251                 sb.append(" ) ");
252             }
253         } catch (Throwable JavaDoc t) {
254             System.out.println("throws " + t);
255         }
256
257         sb.append(" order by project.statistics.currentData.rank desc");
258
259         return sb.toString();
260     }
261 }
262
Popular Tags