1 19 20 package za.org.coefficient.modules.attribute; 21 22 import net.sf.hibernate.Hibernate; 23 import net.sf.hibernate.HibernateException; 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.ArrayList ; 37 import java.util.HashMap ; 38 import java.util.List ; 39 import java.util.StringTokenizer ; 40 41 55 public class AttributeDataManagement extends BaseModule { 56 58 61 public static final String ADD = "add"; 62 public static final String UPDATE = "update"; 63 public static final String DELETE = "delete"; 64 public static final String SELECT = "select"; 65 public static final String EDIT = "edit"; 66 67 70 public static final String ERROR = "error"; 71 public static final String HEADING = "heading"; 72 public static final String ATTRIBUTE = "attribute"; 73 public static final String ATTRIBUTES = "attributes"; 74 public static final String LINES = "lines"; 75 public static final String LINE = "line"; 76 public static final String MODULE = "module"; 77 public static final String OP = "op"; 78 public static final String ROWS = "rows"; 79 public static final String COLS = "cols"; 80 public static final String SIZE = "size"; 81 public static final String EQUALS = "="; 82 83 86 public static final String BUTTON = "button"; 87 public static final String NAME = "name"; 88 public static final String DESCRIPTION = "description"; 89 public static final String TITLE = "title"; 90 public static final String MORE = "more"; 91 public static final String ID = "id"; 92 public static final String ON = "on"; 93 94 97 public static final String DATAPAGE = "data.vm"; 98 public static final String TEXTPAGE = "text.vm"; 99 public static final String TEXTAREAPAGE = "textarea.vm"; 100 public static final String HEADINGPAGE = "heading.vm"; 101 public static final String TDPAGE = "td.vm"; 102 public static final String DELETEPAGE = "delete.vm"; 103 public static final String CANNOTDELETEPAGE = "cannotdelete.vm"; 104 105 108 public static final String ENTERFIELDS = 109 "You must enter a name and description"; 110 111 113 String [] commandRoles = { SecurityUtil.SITE_ADMIN_ROLE_DESC, }; 118 String [] doInitialWorkRoles = { SecurityUtil.SITE_ADMIN_ROLE_DESC, }; 119 120 122 public String getMainMethod() { 123 return "doInitialWork"; 125 } 126 127 public String getModuleDescription() { 128 return "AttributeData Management"; 129 } 130 131 public String getModuleDisplayName() { 132 return "Attribute Data Administration"; 133 } 134 135 public String canExecuteForRole(CoefficientContext ctx, String methodName, 136 Role usersHighestRole) { 137 int i; 138 String role = usersHighestRole.getDescription(); 139 140 if (methodName.equals("command")) { 141 for (i = 0; i < commandRoles.length; i++) { 142 if (role.equals(commandRoles[i])) { 143 return null; 144 } 145 } 146 147 return "not authorised"; 148 } 149 150 if (methodName.equals("doInitialWork")) { 151 for (i = 0; i < doInitialWorkRoles.length; i++) { 152 if (role.equals(doInitialWorkRoles[i])) { 153 return null; 154 } 155 } 156 157 return "not authorised"; 158 } 159 160 return null; 161 } 162 163 public List getExistingAttributes() { 164 List list = null; 165 try { 166 list = 167 HibernateUtil.find("FROM " + ProjectAttribute.class.getName() 168 + " as attribute " + "ORDER BY attribute.id"); 169 } catch (Throwable t) { 170 System.out.println("Hibernate session throws " + t); 171 } 172 173 return list; 174 } 175 176 public ProjectAttribute getAttributeByName(String name) { 177 List list = null; 178 try { 179 list = 180 HibernateUtil.find("FROM " + ProjectAttribute.class.getName() 181 + " as attribute " + " where attribute.name = ?", name, 182 Hibernate.STRING); 183 if (list.size() > 0) { 184 return (ProjectAttribute) (list.get(0)); 185 } 186 } catch (Throwable t) { 187 System.out.println("Hibernate session throws " + t); 188 } 189 190 return new ProjectAttribute(); 191 } 192 193 public ProjectAttribute getAttributeByNumber(Long id) { 194 List list = null; 195 try { 196 list = 197 HibernateUtil.find("FROM " + ProjectAttribute.class.getName() 198 + " as attribute " + " where attribute.id = ?", id, 199 Hibernate.LONG); 200 201 if (list.size() > 0) { 202 return (ProjectAttribute) (list.get(0)); 203 } 204 } catch (Throwable t) { 205 System.out.println("Hibernate session throws " + t); 206 } 207 208 return new ProjectAttribute(); 209 } 210 211 public List getExistingData(Long attributeId) { 212 List list = null; 213 try { 214 list = 215 HibernateUtil.find("FROM " + ProjectAttributeData.class.getName() 216 + " as data " + "where data.attributeId = ? " 217 + "ORDER BY data.id", attributeId, Hibernate.LONG); 218 } catch (Throwable t) { 219 System.out.println("Hibernate session throws " + t); 220 } 221 222 return list; 223 } 224 225 public List getExistingDataKeywords(Long attributeId) { 226 List list = getExistingData(attributeId); 227 List newlist = new ArrayList (); 228 229 int i; 230 Long elementId = null; 231 ProjectAttributeData pad; 232 for (i = 0; i < list.size(); i++) { 233 pad = (ProjectAttributeData) (list.get(i)); 234 if (elementId == null) { 235 elementId = pad.getElementId(); 236 newlist.add(pad); 237 } else { 238 if (elementId.compareTo(pad.getElementId()) == 0) { 239 newlist.add(pad); 240 } 241 } 242 } 243 244 return newlist; 245 } 246 247 252 public synchronized CoefficientContext command(CoefficientContext ctx) { 253 HashMap map = new HashMap (); 254 String button = ctx.getParameter(BUTTON, ""); 255 String title = ctx.getParameter(TITLE, ""); 256 String name = ctx.getParameter(NAME, ""); 257 String description = ctx.getParameter(DESCRIPTION, ""); 258 String more = ctx.getParameter(MORE, ""); 259 String id = ctx.getParameter(ID, ""); 260 Long idLong; 261 StringBuffer sb = new StringBuffer ("Problem here"); 262 List allAttributes = getExistingAttributes(); 263 264 try { 265 idLong = Long.decode(id); 266 } catch (Throwable t) { 267 idLong = new Long (0); 268 } 269 270 if (button.toLowerCase().equals(ADD)) { 271 ProjectAttribute pa = null; 272 273 try { 274 pa = getAttributeByNumber(ctx.getParameterAsLong(ATTRIBUTE + ID)); 275 } catch (Throwable t) { 276 System.out.println("throws " + t); 277 } 278 279 List elements = pa.getElements(); 280 AttributeElement element; 281 282 int i; 283 284 for (i = 0; i < elements.size(); i++) { 285 element = (AttributeElement) (elements.get(i)); 286 ProjectAttributeData pad = new ProjectAttributeData(); 287 pad.setAttributeId(pa.getId()); 288 pad.setElementId(element.getId()); 289 pad.setDescription(ctx.getParameter(element.getName(), "")); 290 try { 291 HibernateUtil.saveOrUpdate(pad); 292 } catch (Throwable t) { 293 System.out.println("Hibernate throws " + t); 294 } 295 } 296 } 297 298 if (button.toLowerCase().equals(DELETE)) { 302 try { 303 List list = 304 HibernateUtil.find("FROM " + ProjectAttributeData.class.getName() 305 + " as data " + "where data.id = " + id); 306 if ( list != null && list.size() > 0 ) { 307 ProjectAttributeData pad = (ProjectAttributeData)(list.get(0)); 308 if ( canDelete(pad) ) { 309 HibernateUtil.delete(pad); 310 } 311 } 312 } 313 catch ( Throwable t ) { 314 System.out.println("trouble getting project attribute data " + t); 315 } 316 } 317 setupSelect(ctx, map); 318 allAttributes = getExistingAttributes(); 319 map.put(ATTRIBUTES, allAttributes); 320 sb = VelocityScreenUtil.getProcessedScreen(DATAPAGE, map); 321 ctx.setModuleContent(sb.toString(), getModuleDisplayName()); 322 return ctx; 323 } 324 325 public CoefficientContext doInitialWork(CoefficientContext ctx) { 326 List allAttributes = getExistingAttributes(); 327 HashMap map = new HashMap (); 328 map.put(ATTRIBUTES, allAttributes); 329 StringBuffer sb = new StringBuffer ("Problem here"); 330 map.put(MODULE, this); 331 sb = VelocityScreenUtil.getProcessedScreen(DATAPAGE, map); 332 333 ctx.setModuleContent(sb.toString(), getModuleDisplayName()); 335 return ctx; 336 } 337 338 private boolean canDelete(ProjectAttributeData pad) { 339 try { 340 String query = new String ("FROM " + Project.class.getName() + 341 " as project, " + ProjectAttributeData.class.getName() + 342 " as pad where pad in elements(project.attributeData) and " + 343 " pad.id = " + pad.getId()); 344 List list = HibernateUtil.find(query); 345 if ( list == null ) 346 return true; 347 if ( list.size() != 0 ) { 348 return false; 349 } 350 else 351 return true; 352 } 353 catch ( Throwable t ) { 354 System.out.println("canDelete throws " + t); 355 return false; 356 } 357 } 358 359 private void setupSelect(CoefficientContext ctx, HashMap map) { 360 String attributeName = ctx.getParameter(ATTRIBUTE + NAME, ""); 361 ProjectAttribute pa = getAttributeByName(attributeName); 362 ProjectAttributeData pad = new ProjectAttributeData(); 363 List data = new ArrayList (); 364 StringBuffer sb = new StringBuffer ("problem here"); 365 AttributeElement element; 366 map.put(SELECT, "yes"); 367 List elements = pa.getElements(); 368 List lines = new ArrayList (); 369 String line = new String (); 370 int i; 371 372 map.put(ATTRIBUTE, pa); 373 map.put(ATTRIBUTE + ID, pa.getId()); 374 String heading = new String (); 375 for (i = 0; i < elements.size(); i++) { 376 element = (AttributeElement) (elements.get(i)); 377 map.put(NAME, element.getName()); 378 sb = VelocityScreenUtil.getProcessedScreen(HEADINGPAGE, map); 379 heading = heading.concat(sb.toString()); 380 } 381 map.put(HEADING, heading); 382 383 data = getExistingData(pa.getId()); 384 385 HashMap m = new HashMap (); 386 line = new String (); 387 if (data != null) { 388 int cnt = elements.size(); 389 for (i = 0; i < data.size(); i++) { 390 pad = (ProjectAttributeData) (data.get(i)); 391 m.put(LINE, pad.getDescription()); 392 sb = VelocityScreenUtil.getProcessedScreen(TDPAGE, m); 393 line = line.concat(sb.toString()); 394 395 396 if (cnt == 1) { 397 HashMap mp = new HashMap (); 398 mp.put("attributeData", pad); 399 mp.put(ATTRIBUTE, pa); 400 if ( canDelete(pad) ) { 401 sb = VelocityScreenUtil.getProcessedScreen(DELETEPAGE, mp); 402 } 403 else 404 sb = VelocityScreenUtil.getProcessedScreen(CANNOTDELETEPAGE, mp); 405 406 line = line.concat(sb.toString()); 407 408 lines.add(line); 409 line = new String (); 410 } else { 411 if (((i + 1) % cnt) == 0) { 412 413 lines.add(line); 414 line = new String (); 415 } 416 } 417 } 418 } 419 420 String desc; 421 line = new String (); 422 m = new HashMap (); 423 for (i = 0; i < elements.size(); i++) { 424 element = (AttributeElement) (elements.get(i)); 425 m.put(NAME, element.getName()); 426 desc = element.getDescription(); 427 StringTokenizer toks = new StringTokenizer (desc, ","); 428 String cols = "20"; 429 String rows = "3"; 430 while (toks.hasMoreElements()) { 431 String st; 432 st = (String ) (toks.nextElement()); 433 if (st.startsWith(COLS + EQUALS)) { 434 m.put(COLS, st.substring(5)); 435 } 436 if (st.startsWith(ROWS + EQUALS)) { 437 m.put(ROWS, st.substring(5)); 438 } 439 if (st.startsWith(SIZE + EQUALS)) { 440 m.put(SIZE, st.substring(5)); 441 } 442 } 443 if (desc.startsWith("textarea")) { 444 sb = VelocityScreenUtil.getProcessedScreen(TEXTAREAPAGE, m); 445 } else { 446 sb = VelocityScreenUtil.getProcessedScreen(TEXTPAGE, m); 447 } 448 line = line.concat(sb.toString()); 449 map.put(LINES, lines); 450 } 451 map.put("input", line); 453 line = new String (); 454 } 455 } 456 | Popular Tags |