1 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 ; 41 import java.util.HashMap ; 42 import java.util.List ; 43 44 59 public class AttributeSearch extends BaseModule { 60 62 65 public static final String ATTRIBUTE_BROWSE_PAGER = "Attribute_pager"; 66 public static final String ERROR = "error"; 67 public static final String PAGER = "pager"; 68 public static final String ATTRIBUTE = "attribute"; 69 public static final String ATTRIBUTES = "attributes"; 70 public static final String MODULE = "module"; 71 public static final String OP = "op"; 72 public static final String NEXT = "next"; 73 public static final String PREVIOUS = "previous"; 74 public static final String PAGE = "page"; 75 76 79 public static final String SEARCHPAGE = "search.vm"; 80 public static final String RESULTPAGE = "result.vm"; 81 82 84 public String getMainMethod() { 85 return "doInitialWork"; 87 } 88 89 public String getModuleDescription() { 90 return "Attribute Search"; 91 } 92 93 public String getModuleDisplayName() { 94 return "Attribute Search"; 95 } 96 97 public String canExecuteForRole(CoefficientContext ctx, String methodName, 98 Role usersHighestRole) { 99 return null; 100 } 101 102 public CoefficientContext doInitialWork(CoefficientContext ctx) { 103 HashMap map = new HashMap (); 104 StringBuffer sb = new StringBuffer ("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 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 map = new HashMap (); 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 (true)); 137 } 138 StringBuffer 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 map = new HashMap (); 146 StringBuffer sb = new StringBuffer ("Problem here"); 147 148 ArrayList values = new ArrayList (); 149 String st = parse(ctx, values); 150 Object [] obj = new Object [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 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 (true)); 167 } 168 sb = VelocityScreenUtil.getProcessedScreen(RESULTPAGE, map); 169 170 ctx.setModuleContent(sb.toString(), getModuleDisplayName()); 172 return ctx; 173 } 174 175 private List getAttributes() { 176 List attributeDisplayData = new ArrayList (); 177 178 try { 182 AttributeDisplayData displayData = new AttributeDisplayData(); 183 List attributes = (List )InvokerFactory.getInvoker() 184 .invokeMethodOnModule("AttributeDataManagement", 185 "getExistingAttributes", 186 new Object []{}); 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 data = (List )InvokerFactory.getInvoker() 196 .invokeMethodOnModule("AttributeDataManagement", 197 "getExistingDataKeywords", 198 new Object []{pa.getId()}); 199 displayData.setAttributeData(data); 200 attributeDisplayData.add(displayData); 201 } 202 203 } catch (Throwable t) { 205 System.err.println("<< error loading attributes"); 206 } 207 208 return attributeDisplayData; 209 } 210 211 private String parse(CoefficientContext ctx, List vals) { 212 StringBuffer sb = 213 new StringBuffer ("select project from " 214 + Project.class.getName() + " " 215 + "as project " 216 + " left outer join project.attributeData" 217 + " as attData where 1 = 1 "); 218 219 222 try { 223 List existing = (List )InvokerFactory.getInvoker() 224 .invokeMethodOnModule("AttributeDataManagement", 225 "getExistingAttributes", 226 new Object []{}); 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 attributeId = ctx.getParameter("attribute" + i); 234 if (attributeId != null) { 235 Long id; 236 id = new Long (attributeId); 237 String [] 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.id = ? "); 245 vals.add(new Long (values[j])); 246 foundCount++; 247 } 248 } 249 } 250 if(existing.size() > 0) { 251 sb.append(" ) "); 252 } 253 } catch (Throwable 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 |