1 16 17 package org.apache.jetspeed.modules.actions.portlets; 18 19 import org.apache.jetspeed.om.profile.Profile; 21 import org.apache.jetspeed.om.profile.QueryLocator; 22 import org.apache.jetspeed.portal.portlets.VelocityPortlet; 23 import org.apache.jetspeed.services.logging.JetspeedLogFactoryService; 24 import org.apache.jetspeed.services.logging.JetspeedLogger; 25 import org.apache.jetspeed.portal.portlets.browser.DatabaseBrowserIterator; 26 import org.apache.jetspeed.services.Profiler; 27 import org.apache.jetspeed.util.PortletConfigState; 28 import org.apache.jetspeed.util.PortletSessionState; 29 30 import org.apache.turbine.util.RunData; 32 33 import org.apache.velocity.context.Context; 35 36 import java.util.ArrayList ; 38 import java.util.Iterator ; 39 40 import org.apache.regexp.RE; 42 import org.apache.regexp.RECompiler; 43 44 51 public class PsmlBrowseAction extends VelocityPortletAction 52 { 53 54 protected static final String PSML_REFRESH_FLAG = "psmlRefreshFlag"; 55 protected static final String TRUE = "true"; 56 protected static final String FALSE = "false"; 57 protected static final String PROFILE_ITERATOR = "profileIterator"; 58 protected static final String PAGE_SIZE = "page-size"; 59 protected static final String CUSTOMIZE_TEMPLATE = "customize-template"; 60 private static final String PEID = "js_peid"; 61 62 63 public static final String FILTER_VALUE = "filter_value"; 64 65 66 public static final String FILTER_REGEXP = "filter_regexp"; 67 68 69 public static final String FILTER_TYPE = "filter_type"; 70 71 72 public static final String FILTER_TYPE_USER = "filter_type_user"; 73 74 75 public static final String FILTER_TYPE_ROLE = "filter_type_role"; 76 77 78 public static final String FILTER_TYPE_GROUP = "filter_type_group"; 79 80 83 private static final JetspeedLogger logger = JetspeedLogFactoryService.getLogger(PsmlBrowseAction.class.getName()); 84 85 90 protected void buildConfigureContext(VelocityPortlet portlet, 91 Context context, 92 RunData rundata) 93 { 94 try 95 { 96 super.buildConfigureContext(portlet, context, rundata); 97 } 98 catch (Exception ex) 99 { 100 logger.error("Exception", ex); 101 } 102 context.put(PAGE_SIZE, PortletConfigState.getParameter(portlet, rundata, PAGE_SIZE, "20")); 103 setTemplate(rundata, PortletConfigState.getParameter(portlet, rundata, CUSTOMIZE_TEMPLATE, null)); 104 } 105 109 protected void buildNormalContext(VelocityPortlet portlet, 110 Context context, 111 RunData rundata) 112 { 113 int start = rundata.getParameters().getInt("start", 0); 114 115 if (start < 0) 116 { 117 start = 0; 118 } 119 120 String pageSize = PortletConfigState.getParameter(portlet, rundata, PAGE_SIZE, "20"); 121 int size = Integer.parseInt(pageSize); 122 123 int next = start + size + 1; 124 int prev = start - size - 1; 125 126 128 131 boolean refreshFlag = (rundata.getUser().getTemp(PSML_REFRESH_FLAG, FALSE)).equals(TRUE); 132 rundata.getUser().setTemp(PSML_REFRESH_FLAG, FALSE); 133 134 DatabaseBrowserIterator windowIterator = 136 (DatabaseBrowserIterator) PortletSessionState.getAttribute(portlet, rundata, PROFILE_ITERATOR); 137 if ((windowIterator == null) || refreshFlag) 138 { 139 int index = 0; 140 QueryLocator ql = new QueryLocator(QueryLocator.QUERY_ALL); 141 ArrayList entries = new ArrayList (); 142 Iterator i = Profiler.query(ql); 143 144 String filterValue = rundata.getParameters().getString(FILTER_VALUE); 146 if (filterValue != null && !filterValue.trim().equalsIgnoreCase("")) 147 { 148 String filterType = rundata.getParameters().getString(FILTER_TYPE, FILTER_TYPE_USER); 149 boolean useRE = rundata.getParameters().getBoolean(FILTER_REGEXP); 150 RE r = null; 151 RECompiler rc = null; 152 if (useRE) 153 { 154 try 155 { 156 rc = new RECompiler(); 157 r = new RE(); 158 r.setProgram(rc.compile(filterValue)); 159 } 160 catch (org.apache.regexp.RESyntaxException rex) 161 { 162 logger.warn("PsmlBrowseAction: error processing regular expression [" + filterValue + "]: " + 163 rex.toString()); 164 } 165 } 166 try 167 { 168 while (i.hasNext()) 169 { 170 Profile profile = (Profile) i.next(); 171 String compareValue = null; 172 if (filterType.equals(FILTER_TYPE_USER)) 173 { 174 compareValue = profile.getUserName(); 175 } 176 else if (filterType.equals(FILTER_TYPE_ROLE)) 177 { 178 compareValue = profile.getRoleName(); } 179 else if (filterType.equals(FILTER_TYPE_GROUP)) 180 { 181 compareValue = profile.getGroupName(); 182 } 183 184 if (compareValue != null) 185 { 186 if (useRE && r.match(compareValue)) 187 { 188 entries.add(profile); 189 } 190 else if (compareValue.startsWith(filterValue)) 191 { 192 entries.add(profile); 193 } 194 } 195 } 196 } 197 catch (Exception e) 198 { 199 logger.error("Exception", e); 200 } 201 } 202 else 203 { 204 while (i.hasNext()) 205 { 206 Profile profile = (Profile) i.next(); 207 entries.add(profile); 209 index++; 210 } 211 } 212 213 ArrayList entryType = new ArrayList (); 214 entryType.add("Profile"); 215 windowIterator = new DatabaseBrowserIterator(entries, entryType, entryType, size); 216 PortletSessionState.setAttribute(portlet, rundata, PROFILE_ITERATOR, windowIterator); 217 } 218 else 219 { 220 windowIterator.setTop(start); 221 } 222 223 224 if (windowIterator != null) 225 { 226 context.put("psml", windowIterator); 227 if (start > 0) 228 { 229 context.put("prev", String.valueOf(prev + 1)); 230 } 231 if (next <= windowIterator.getResultSetSize()) 232 { 233 context.put("next", String.valueOf(next - 1)); 234 } 235 236 } 237 else 238 { 239 logger.error("No Psml entries Found"); 240 } 241 242 } 243 244 249 public void doUpdate(RunData rundata, Context context) 250 { 251 String pageSize = null; 252 253 VelocityPortlet portlet = (VelocityPortlet) context.get("portlet"); 254 if (portlet != null) 255 { 256 String peid = portlet.getID(); 257 if ((peid != null) 258 && peid.equals(rundata.getParameters().getString(PEID))) 259 { 260 pageSize = rundata.getParameters().getString(PAGE_SIZE); 261 } 262 if (pageSize != null) 263 { 264 PortletConfigState.setInstanceParameter(portlet, rundata, PAGE_SIZE, pageSize); 265 PortletSessionState.clearAttribute(portlet, rundata, PROFILE_ITERATOR); 266 } 267 } 268 269 buildNormalContext(portlet, context, rundata); 270 } 271 272 277 public void doRefresh(RunData rundata, Context context) 278 { 279 VelocityPortlet portlet = (VelocityPortlet) context.get("portlet"); 280 PortletSessionState.clearAttribute(portlet, rundata, PROFILE_ITERATOR); 281 rundata.getParameters().remove(FILTER_VALUE); 282 buildNormalContext(portlet, context, rundata); 283 } 284 285 290 public void doFilter(RunData rundata, Context context) 291 { 292 VelocityPortlet portlet = (VelocityPortlet) context.get("portlet"); 293 PortletSessionState.clearAttribute(portlet, rundata, PROFILE_ITERATOR); 294 buildNormalContext(portlet, context, rundata); 295 } 296 297 } 298 | Popular Tags |