1 19 20 package za.org.coefficient.modules.attribute; 21 22 import net.sf.hibernate.HibernateException; 23 import net.sf.hibernate.Hibernate; 24 25 import za.org.coefficient.authentication.Role; 26 import za.org.coefficient.core.AttributeElement; 27 import za.org.coefficient.core.ProjectAttribute; 28 import za.org.coefficient.core.ProjectAttributeData; 29 import za.org.coefficient.core.Project; 30 import za.org.coefficient.interfaces.CoefficientContext; 31 import za.org.coefficient.modules.BaseModule; 32 import net.sf.hibernate.util.HibernateUtil; 33 import za.org.coefficient.util.ejb.SecurityUtil; 34 import za.org.coefficient.util.ejb.VelocityScreenUtil; 35 36 import java.util.HashMap ; 37 import java.util.List ; 38 import java.util.ListIterator ; 39 40 54 public class AttributeManagement extends BaseModule { 55 57 60 public static final String ADD = "add"; 61 public static final String UPDATE = "update"; 62 public static final String DELETE = "delete"; 63 public static final String EDIT = "edit"; 64 65 68 public static final String ERROR = "error"; 69 public static final String ATTRIBUTE = "attribute"; 70 public static final String ATTRIBUTES = "attributes"; 71 public static final String MODULE = "module"; 72 public static final String OP = "op"; 73 74 77 public static final String BUTTON = "button"; 78 public static final String NAME = "name"; 79 public static final String DESCRIPTION = "description"; 80 public static final String TITLE = "title"; 81 public static final String MORE = "more"; 82 public static final String ID = "id"; 83 public static final String ON = "on"; 84 85 88 public static final String ERRORPAGE = "error.vm"; 89 public static final String ADMINPAGE = "admin.vm"; 90 public static final String LISTPAGE = "list.vm"; 91 public static final String INDEXPAGE = "index.vm"; 92 public static final String ADDMSG = "add.vm"; 93 public static final String COMPLETEDMSG = "completed.vm"; 94 95 98 public static final String ENTERFIELDS = 99 "You must enter a name and description"; 100 101 103 String [] commandRoles = { SecurityUtil.SITE_ADMIN_ROLE_DESC, }; 108 String [] doInitialWorkRoles = { SecurityUtil.SITE_ADMIN_ROLE_DESC, }; 109 110 112 public String getMainMethod() { 113 return "doInitialWork"; 115 } 116 117 public String getModuleDescription() { 118 return "Attributes Management"; 119 } 120 121 public String getModuleDisplayName() { 122 return "Attribute Administration"; 123 } 124 125 public String canExecuteForRole(CoefficientContext ctx, String methodName, 126 Role usersHighestRole) { 127 int i; 128 String role = usersHighestRole.getDescription(); 129 130 if (methodName.equals("command")) { 131 for (i = 0; i < commandRoles.length; i++) { 132 if (role.equals(commandRoles[i])) { 133 return null; 134 } 135 } 136 137 return "not authorised"; 138 } 139 140 if (methodName.equals("doInitialWork")) { 141 for (i = 0; i < doInitialWorkRoles.length; i++) { 142 if (role.equals(doInitialWorkRoles[i])) { 143 return null; 144 } 145 } 146 147 return "not authorised"; 148 } 149 150 return null; 151 } 152 153 158 public synchronized CoefficientContext command(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, map); 181 } else { 182 ProjectAttribute pa = new ProjectAttribute(); 183 pa.setName(title); 184 185 int i; 186 for (i = 0; true; i++) { 187 name = ctx.getParameter(NAME + i); 188 description = ctx.getParameter(DESCRIPTION + i); 189 if (name == null) { 190 break; 191 } 192 AttributeElement ae = new AttributeElement(); 193 ae.setName(name); 194 ae.setDescription(description); 195 pa.getElements() 196 .add(ae); 197 } 198 199 if (more.toLowerCase() 200 .equals(ON)) { 201 map.put("newattribute", pa); 202 map.put(MODULE, this); 203 sb = VelocityScreenUtil.getProcessedScreen(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 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 = 232 (ProjectAttributeData) iterator.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 ("FROM " + Project.class.getName() + 258 " as project, " 259 + ProjectAttribute.class.getName() + 260 " as pa where pa in elements(project.attributes) and " + 261 " pa.id = " + pa.getId()); 262 List list = HibernateUtil.find(query); 263 if ( list == null ) { 264 return true; 265 } 266 if ( list.size() != 0 ) { 267 return false; 268 } 269 else { 270 return true; 271 } 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 ctx.setModuleContent(sb.toString(), getModuleDisplayName()); 289 return ctx; 290 } 291 292 public List getExistingAttributes() { 293 List list = null; 294 try { 295 list = 296 HibernateUtil.find("FROM " + 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 = 309 HibernateUtil.find("FROM " + 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 } 320 | Popular Tags |