001: /*
002: * Coefficient - facilitates project based collaboration
003: * Copyright (C) 2003, Dylan Etkin, CSIR icomtek
004: * PO Box 395
005: * Pretoria 0001, RSA
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or (at your option) any later version.
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: */
019:
020: package za.org.coefficient.modules.attribute;
021:
022: import net.sf.hibernate.HibernateException;
023: import net.sf.hibernate.Hibernate;
024:
025: import za.org.coefficient.authentication.Role;
026: import za.org.coefficient.core.AttributeElement;
027: import za.org.coefficient.core.ProjectAttribute;
028: import za.org.coefficient.core.ProjectAttributeData;
029: import za.org.coefficient.core.Project;
030: import za.org.coefficient.interfaces.CoefficientContext;
031: import za.org.coefficient.modules.BaseModule;
032: import net.sf.hibernate.util.HibernateUtil;
033: import za.org.coefficient.util.ejb.SecurityUtil;
034: import za.org.coefficient.util.ejb.VelocityScreenUtil;
035:
036: import java.util.HashMap;
037: import java.util.List;
038: import java.util.ListIterator;
039:
040: /**
041: * @pojo2ejb.class
042: * name="AttributeManagement"
043: * jndi-prefix="za/org/coefficient/admin/"
044: * interface-extends="za.org.coefficient.interfaces.Module"
045: * interface-local-extends="za.org.coefficient.interfaces.ModuleLocal"
046: *
047: * @web.resource-env-ref
048: * name="za/org/coefficient/admin/AttributeManagement"
049: * type="za.org.coefficient.modules.attribute.AttributeManagement"
050: * @web.resource-env-ref
051: * name="AttributeManagement"
052: * type="za.org.coefficient.modules.attribute.AttributeManagement"
053: */
054: public class AttributeManagement extends BaseModule {
055: //~ Static fields/initializers =============================================
056:
057: /*
058: * button names
059: */
060: public static final String ADD = "add";
061: public static final String UPDATE = "update";
062: public static final String DELETE = "delete";
063: public static final String EDIT = "edit";
064:
065: /*
066: * context keywords
067: */
068: public static final String ERROR = "error";
069: public static final String ATTRIBUTE = "attribute";
070: public static final String ATTRIBUTES = "attributes";
071: public static final String MODULE = "module";
072: public static final String OP = "op";
073:
074: /*
075: * form fields
076: */
077: public static final String BUTTON = "button";
078: public static final String NAME = "name";
079: public static final String DESCRIPTION = "description";
080: public static final String TITLE = "title";
081: public static final String MORE = "more";
082: public static final String ID = "id";
083: public static final String ON = "on";
084:
085: /*
086: * velocity pages
087: */
088: public static final String ERRORPAGE = "error.vm";
089: public static final String ADMINPAGE = "admin.vm";
090: public static final String LISTPAGE = "list.vm";
091: public static final String INDEXPAGE = "index.vm";
092: public static final String ADDMSG = "add.vm";
093: public static final String COMPLETEDMSG = "completed.vm";
094:
095: /*
096: * error messages
097: */
098: public static final String ENTERFIELDS = "You must enter a name and description";
099:
100: //~ Instance fields ========================================================
101:
102: //
103: // roles which can execute the command method
104: // everybody can execute list
105: //
106: String[] commandRoles = { SecurityUtil.SITE_ADMIN_ROLE_DESC, };
107: String[] doInitialWorkRoles = { SecurityUtil.SITE_ADMIN_ROLE_DESC, };
108:
109: //~ Methods ================================================================
110:
111: public String getMainMethod() {
112: // NOTE: this can be any method of this class that makes sense
113: return "doInitialWork";
114: }
115:
116: public String getModuleDescription() {
117: return "Attributes Management";
118: }
119:
120: public String getModuleDisplayName() {
121: return "Attribute Administration";
122: }
123:
124: public String canExecuteForRole(CoefficientContext ctx,
125: String methodName, Role usersHighestRole) {
126: int i;
127: String role = usersHighestRole.getDescription();
128:
129: if (methodName.equals("command")) {
130: for (i = 0; i < commandRoles.length; i++) {
131: if (role.equals(commandRoles[i])) {
132: return null;
133: }
134: }
135:
136: return "not authorised";
137: }
138:
139: if (methodName.equals("doInitialWork")) {
140: for (i = 0; i < doInitialWorkRoles.length; i++) {
141: if (role.equals(doInitialWorkRoles[i])) {
142: return null;
143: }
144: }
145:
146: return "not authorised";
147: }
148:
149: return null;
150: }
151:
152: /**
153: * executed by the project champion. gives full access to
154: * the task data
155: *
156: */
157: public synchronized CoefficientContext command(
158: CoefficientContext ctx) {
159: HashMap map = new HashMap();
160: String button = ctx.getParameter(BUTTON, "");
161: String title = ctx.getParameter(TITLE, "");
162: String name = ctx.getParameter(NAME, "");
163: String description = ctx.getParameter(DESCRIPTION, "");
164: String more = ctx.getParameter(MORE, "");
165: String id = ctx.getParameter(ID, "");
166: Long idLong;
167: StringBuffer sb = new StringBuffer("Problem here");
168: List allAttributes = getExistingAttributes();
169:
170: try {
171: idLong = Long.decode(id);
172: } catch (Throwable t) {
173: idLong = new Long(0);
174: }
175:
176: if (button.toLowerCase().equals(ADD)) {
177: if (title.equals("")) {
178: map.put(ERROR, "must supply a title");
179: map.put(MODULE, this );
180: sb = VelocityScreenUtil.getProcessedScreen(ADMINPAGE,
181: map);
182: } else {
183: ProjectAttribute pa = new ProjectAttribute();
184: pa.setName(title);
185:
186: int i;
187: for (i = 0; true; i++) {
188: name = ctx.getParameter(NAME + i);
189: description = ctx.getParameter(DESCRIPTION + i);
190: if (name == null) {
191: break;
192: }
193: AttributeElement ae = new AttributeElement();
194: ae.setName(name);
195: ae.setDescription(description);
196: pa.getElements().add(ae);
197: }
198:
199: if (more.toLowerCase().equals(ON)) {
200: map.put("newattribute", pa);
201: map.put(MODULE, this );
202: sb = VelocityScreenUtil.getProcessedScreen(
203: ADMINPAGE, map);
204: } else {
205: try {
206: HibernateUtil.saveOrUpdate(pa);
207: } catch (Throwable t) {
208: System.out.println("Hibernate throws " + t);
209: }
210: }
211: }
212: }
213:
214: //
215: // handle delete button
216: //
217: if (button.toLowerCase().equals(DELETE)) {
218: ProjectAttribute pa;
219: int i;
220: boolean ticked = false;
221: for (i = 0; i < allAttributes.size(); i++) {
222: pa = (ProjectAttribute) (allAttributes.get(i));
223: if (idLong.equals(pa.getId())) {
224: ticked = true;
225: try {
226: if (canDelete(pa)) {
227: HibernateUtil.delete(pa);
228: List data = getExistingData(idLong);
229: ListIterator iterator = data.listIterator();
230: while (iterator.hasNext()) {
231: ProjectAttributeData pad = (ProjectAttributeData) iterator
232: .next();
233: HibernateUtil.delete(pad);
234: }
235: }
236: } catch (Throwable t) {
237: System.out.println("Hibernate throws " + t);
238: }
239:
240: break;
241: }
242: }
243: if (!ticked) {
244: map.put(ERROR, "You must tick an attribute to delete");
245: }
246: }
247: allAttributes = getExistingAttributes();
248: map.put(ATTRIBUTES, allAttributes);
249: map.put(MODULE, this );
250: sb = VelocityScreenUtil.getProcessedScreen(ADMINPAGE, map);
251: ctx.setModuleContent(sb.toString(), getModuleDisplayName());
252: return ctx;
253: }
254:
255: public boolean canDelete(ProjectAttribute pa) {
256: try {
257: String query = new String(
258: "FROM "
259: + Project.class.getName()
260: + " as project, "
261: + ProjectAttribute.class.getName()
262: + " as pa where pa in elements(project.attributes) and "
263: + " pa.id = " + pa.getId());
264: List list = HibernateUtil.find(query);
265: if (list == null) {
266: return true;
267: }
268: if (list.size() != 0) {
269: return false;
270: } else {
271: return true;
272: }
273: } catch (Throwable t) {
274: System.out.println("canDelete throws " + t);
275: return false;
276: }
277: }
278:
279: public CoefficientContext doInitialWork(CoefficientContext ctx) {
280: List allAttributes = getExistingAttributes();
281: HashMap map = new HashMap();
282: map.put(ATTRIBUTES, allAttributes);
283: StringBuffer sb = new StringBuffer("Problem here");
284: map.put(MODULE, this );
285: sb = VelocityScreenUtil.getProcessedScreen(ADMINPAGE, map);
286:
287: // Set the html into the context
288: ctx.setModuleContent(sb.toString(), getModuleDisplayName());
289: return ctx;
290: }
291:
292: public List getExistingAttributes() {
293: List list = null;
294: try {
295: list = HibernateUtil.find("FROM "
296: + ProjectAttribute.class.getName()
297: + " as attribute " + "ORDER BY attribute.id");
298: } catch (Throwable t) {
299: System.out.println("Hibernate session throws " + t);
300: }
301:
302: return list;
303: }
304:
305: public List getExistingData(Long attributeId) {
306: List list = null;
307: try {
308: list = HibernateUtil.find("FROM "
309: + ProjectAttributeData.class.getName()
310: + " as data " + "where data.attributeId = ? "
311: + "ORDER BY data.id", attributeId, Hibernate.LONG);
312: } catch (Throwable t) {
313: System.out.println("Hibernate session throws " + t);
314: }
315:
316: return list;
317: }
318:
319: }
|