1 19 20 package za.org.coefficient.modules.category; 21 22 import net.sf.hibernate.Hibernate; 23 import net.sf.hibernate.util.HibernateUtil; 24 import net.sf.hibernate.Session; 25 26 import org.apache.commons.lang.StringUtils; 27 28 import za.org.coefficient.authentication.Role; 29 import za.org.coefficient.core.Category; 30 import za.org.coefficient.core.Constants; 31 import za.org.coefficient.core.Project; 32 import za.org.coefficient.interfaces.CoefficientContext; 33 import za.org.coefficient.modules.BaseModule; 34 import za.org.coefficient.modules.project.ProjectConstants; 35 import za.org.coefficient.util.common.HibernatePager; 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 ; 41 import java.util.HashMap ; 42 import java.util.List ; 43 44 61 public class CategoryBrowse extends BaseModule { 62 64 public final String ACTION = "action"; 65 public final String ALL = "All"; 66 public final String BROWSE = "browse"; 67 public final String BROWSEPAGE = "browse.vm"; 68 public final String CATDISP = "catdisp"; 69 public final String CATEGORY = "category"; 70 public final String CATEGORY_BROWSE_PAGER = "CategoryBrowsePager"; 71 public final String CATEGORY_ID = "BrowseCategoryId"; 72 public final String CHILDID = "childid"; 73 public final String CLOSE = "close"; 74 public final String DESCRIPTION = "description"; 75 public final String ERROR = "error"; 76 public final String ID = "id"; 77 public final String LIST = "list"; 78 public final String MODULE = "module"; 79 public final String MSG = "msg"; 80 public final String NAME = "name"; 81 public final String NEXT = "next"; 82 public final String OP = "op"; 83 public final String OPEN = "open"; 84 public final String PAGE = "page"; 85 public final String PAGER = "pager"; 86 public final String PARENT = "parent"; 87 public final String PATH = "path"; 88 public final String PREVIOUS = "previous"; 89 public final String PROJECTS = "projects"; 90 public final String ROOT = "root"; 91 public final String SESSION_ID = "CategoryBrowseBean"; 92 93 95 public String getMainMethod() { 96 return "interpretOp"; 98 } 99 100 public String getModuleDescription() { 101 return "browsing projects via category"; 102 } 103 104 public String getModuleDisplayName() { 105 return "Category Browse"; 106 } 107 108 public String canExecuteForRole(CoefficientContext ctx, String methodName, 109 Role usersHighestRole) { 110 return null; 111 } 112 113 private synchronized void closeCategory(CoefficientContext ctx) { 114 try { 115 Object obj = ctx.getSessionAttribute(SESSION_ID); 116 ctx.removeSessionAttribute(CATEGORY_BROWSE_PAGER); 117 List openList; 118 if (obj == null) { 119 openList = new ArrayList (); 120 openList.add(getRoot()); 121 } else { 122 openList = (ArrayList ) obj; 123 } 124 String id = ctx.getParameter(ID); 125 Category category = null; 126 if (id == null) { 127 category = getRoot(); 128 id = "" + category.getId(); 129 } else { 130 openList.remove(id); 131 } 132 ctx.setSessionAttribute(SESSION_ID, openList); 133 viewCategoryTree(ctx, ""); 134 } catch (Throwable t) { 135 System.out.println("categoryBrowse.viewCategoryTree throws" + t); 136 t.printStackTrace(); 137 } 138 } 139 140 public void interpretOp(CoefficientContext ctx) { 141 String action = ctx.getParameter(ACTION); 145 146 if (action == null) { 150 viewCategoryTree(ctx, ""); 151 } else { 152 if (action.equals(CLOSE)) { 153 closeCategory(ctx); 154 } 155 if (action.equals(OPEN)) { 156 openCategory(ctx, ""); 157 } 158 if (action.equals(LIST)) { 159 listProjects(ctx); 160 } 161 } 162 } 163 164 private void listProjects(CoefficientContext ctx) { 165 Category cat = getCategory(ctx.getParameter(CATEGORY + ID)); 166 HibernatePager hp = null; 167 try { 168 hp = (HibernatePager) ctx.getSessionAttribute(CATEGORY_BROWSE_PAGER); 169 Long catid = (Long ) ctx.getSessionAttribute(CATEGORY_ID); 170 if ((hp == null) || (catid == null) 171 || ((catid != null) && (catid.compareTo(cat.getId()) != 0))) { 172 Object [] data = new Object [1]; 173 data[0] = cat.getId(); 174 hp = new HibernatePager("select project from " 175 + Project.class.getName() + " as project, " 176 + " category in project.categories " 177 + "where category.id = ? " 178 + "order by project.statistics.currentData.rank desc ", data, 179 Constants.MAX_ELEMENTS_PER_PAGE); 180 ctx.setSessionAttribute(CATEGORY_BROWSE_PAGER, hp); 181 ctx.setSessionAttribute(CATEGORY_ID, cat.getId()); 182 } else { 183 if (ctx.getParameter(NEXT) != null) { 184 hp.next(); 185 } 186 if (ctx.getParameter(PREVIOUS) != null) { 187 hp.previous(); 188 } 189 if (ctx.getParameter(PAGE) != null) { 190 hp.goToPage(ctx.getParameterAsInteger(PAGE).intValue()); 191 } 192 } 193 } catch (Throwable t) { 194 System.out.println("listProjects throws " + t); 195 t.printStackTrace(); 196 } 197 viewCategoryTree(ctx, ""); 198 } 199 200 private void openCategory(CoefficientContext ctx, String msg) { 201 try { 202 CategoryDisplayData cat = null; 203 ctx.removeSessionAttribute(CATEGORY_BROWSE_PAGER); 204 Category root = getRoot(); 205 Object obj = ctx.getSessionAttribute(SESSION_ID); 206 List openList; 207 if (obj == null) { 208 openList = new ArrayList (); 209 openList.add("" + root.getId()); 210 } else { 211 openList = (ArrayList ) obj; 212 } 213 214 String id = ctx.getParameter(ID); 215 Category category = null; 216 if (id == null) { 217 category = getRoot(); 218 id = "" + category.getId(); 219 } else { 220 category = getRoot(); 221 openList.add(id); 222 } 223 ctx.setSessionAttribute(SESSION_ID, openList); 224 viewCategoryTree(ctx, ""); 225 } catch (Throwable t) { 226 System.out.println("categoryBrowse.viewCategoryTree throws" + t); 227 } 228 } 229 230 private CoefficientContext viewCategoryTree(CoefficientContext ctx, String msg) { 231 try { 232 CategoryDisplayData cat = null; 233 Category root = getRoot(); 234 Object obj = ctx.getSessionAttribute(SESSION_ID); 235 List openList; 236 if (obj == null) { 237 openList = new ArrayList (); 238 openList.add("" + root.getId()); 239 } else { 240 openList = (ArrayList ) obj; 241 } 242 243 HibernatePager hp = 244 (HibernatePager) ctx.getSessionAttribute(CATEGORY_BROWSE_PAGER); 245 246 String id = ctx.getParameter(ID); 247 Category category = getRoot(); 248 List subCategories = 249 getSubCategoriesForDisplay("" + root.getId(), openList); 250 cat = new CategoryDisplayData(); 251 cat.setProjectCount(anyProjectsCount(category)); 252 cat.setId(category.getId()); 253 if (category.getParentCategory() == null) { 254 cat.setParentId(root.getId()); 255 } else { 256 cat.setParentId(category.getParentCategory().getId()); 257 } 258 cat.setName(category.getName()); 259 cat.setDescription(category.getDescription()); 260 cat.setChildren(subCategories); 261 HashMap map = new HashMap (); 262 if (!msg.trim() 263 .equals("")) { 264 map.put(ERROR, msg); 265 } 266 map.put(CATDISP, cat); 267 ctx.setSessionAttribute(SESSION_ID, openList); 268 map.put(ROOT, root); 269 map.put(PAGER, hp); 270 map.put("user", ctx.getCurrentUser()); 271 if (ctx.getSessionAttribute(CATEGORY_ID) != null) { 272 map.put(CATEGORY + ID, ctx.getSessionAttribute(CATEGORY_ID)); 273 } 274 275 map.put("project_name", ProjectConstants.PROJECT_NAME); 276 map.put("project_name_cap", 277 StringUtils.capitalise(ProjectConstants.PROJECT_NAME)); 278 if(ctx.getCurrentUser() != null && 279 ctx.getCurrentUser().getSystemRole().getRoleValue() 280 <= SecurityUtil.SITE_MODERATOR_ROLE_VAL) { 281 map.put("userIsAdmin", new Boolean (true)); 282 } 283 StringBuffer sb = 284 VelocityScreenUtil.getProcessedScreen(BROWSEPAGE, map); 285 ctx.setModuleContent(sb.toString(), getModuleDisplayName()); 286 } catch (Throwable t) { 287 System.out.println("categoryBrowse.viewCategoryTree throws" + t); 288 } 289 return ctx; 290 } 291 292 private Category getCategory(String id) { 293 List list = null; 294 Category cat = null; 295 try { 296 list = 297 HibernateUtil.find("FROM " + Category.class.getName() 298 + " as category WHERE category.id = ? ", new Long (id), 299 Hibernate.LONG); 300 301 int i; 302 for (i = 0; i < list.size(); i++) { 303 cat = (Category) (list.get(i)); 304 return cat; 305 } 306 } catch (Throwable t) { 307 System.out.println("getCategory Hibernate throws " + t); 308 } 309 310 return cat; 311 } 312 313 316 private Category getRoot() { 317 List list = null; 318 Category root = null; 319 try { 320 list = 321 HibernateUtil.find("FROM " + Category.class.getName() 322 + " as category WHERE category.name = ? " 323 + " ORDER BY category.id ", ALL, Hibernate.STRING); 324 int i; 325 for (i = 0; i < list.size(); i++) { 326 Category cat = (Category) (list.get(i)); 327 if (i > 0) { 328 HibernateUtil.delete(cat); 329 } else { 330 root = cat; 331 } 332 } 333 if (list.size() == 0) { 334 Category cat = new Category(); 335 cat.setName(ALL); 336 cat.setDescription("All Categories"); 337 cat.setParentCategory(null); 338 try { 339 HibernateUtil.saveOrUpdate(cat); 340 } catch (Throwable t) { 341 System.out.println("getRoot1 Hibernate throws " + t); 342 } 343 344 root = cat; 345 } 346 } catch (Throwable t) { 347 System.out.println("getRoot2 Hibernate throws " + t); 348 } 349 350 return root; 351 } 352 353 private List getSubCategories(String id) { 354 List list = null; 355 Category cat = null; 356 try { 357 list = 358 HibernateUtil.find("FROM " + Category.class.getName() 359 + " as category " 360 + " WHERE category.parentCategory.id = ? ", new Long (id), 361 Hibernate.LONG); 362 } catch (Throwable t) { 363 System.out.println("getSubCategories Hibernate throws " + t); 364 } 365 366 return list; 367 } 368 369 private List getSubCategoriesForDisplay(String id, List openList) { 370 List categories = getSubCategories(id); 371 Category cat; 372 373 List display = new ArrayList (); 374 CategoryDisplayData displaydata; 375 List childrenAsDisplay; 376 377 if (openList.contains(id)) { 378 int i; 379 int j; 380 381 for (i = 0; i < categories.size(); i++) { 382 cat = (Category) categories.get(i); 383 displaydata = new CategoryDisplayData(); 384 displaydata.setId(cat.getId()); 385 displaydata.setName(cat.getName()); 386 displaydata.setDescription(cat.getDescription()); 387 childrenAsDisplay = 388 getSubCategoriesForDisplay("" + cat.getId(), openList); 389 displaydata.setChildren(childrenAsDisplay); 390 displaydata.setProjectCount(anyProjectsCount(cat)); 391 display.add(displaydata); 392 } 393 } 394 395 return display; 396 } 397 398 401 private int anyProjectsCount(Category cat) { 402 List list = new ArrayList (); 403 try { 404 list = 405 HibernateUtil.find("select count(project) from " 406 + Project.class.getName() + " as project, " 407 + " category in project.categories " 408 + "where category.id = ?", cat.getId(), Hibernate.LONG); 409 410 if (list.size() > 0) { 411 return (((Integer ) (list.get(0))).intValue()); 412 } else { 413 return 0; 414 } 415 } catch (Throwable t) { 416 System.out.println("anyProjects throws " + t); 417 } 418 419 return 0; 420 } 421 } 422 | Popular Tags |