1 package org.jahia.engines.users; 2 3 import java.util.ArrayList ; 4 import java.util.Enumeration ; 5 import java.util.HashMap ; 6 import java.util.Iterator ; 7 import java.util.Set ; 8 import java.util.Vector ; 9 import java.util.HashSet ; 10 import java.util.List ; 11 12 import javax.servlet.http.HttpSession ; 13 14 import org.jahia.data.JahiaData; 15 import org.jahia.data.viewhelper.principal.PrincipalViewHelper; 16 import org.jahia.engines.EngineToolBox; 17 import org.jahia.engines.JahiaEngine; 18 import org.jahia.exceptions.JahiaException; 19 import org.jahia.exceptions.JahiaSessionExpirationException; 20 import org.jahia.params.ParamBean; 21 import org.jahia.registries.ServicesRegistry; 22 import org.jahia.services.usermanager.JahiaGroup; 23 import org.jahia.services.usermanager.JahiaGroupManagerService; 24 import org.jahia.services.usermanager.JahiaUser; 25 import org.jahia.services.usermanager.JahiaUserManagerService; 26 import org.jahia.utils.JahiaString; 27 import org.jahia.utils.JahiaTools; 28 29 30 39 40 public class SelectUG_Engine implements JahiaEngine { 41 private static final String TEMPLATE_JSP = "selectUG"; 42 private static final String CLOSE_JSP = "selectusers_close"; 43 private static SelectUG_Engine theObject = null; 44 public static final String ENGINE_NAME = "selectUG"; 45 private EngineToolBox toolBox; 46 47 48 private static final org.apache.log4j.Logger logger = 49 org.apache.log4j.Logger.getLogger (SelectUG_Engine.class); 50 51 54 private SelectUG_Engine () { 55 logger.debug ("***** Starting " + SelectUG_Engine.class.getName () + " engine *****"); 56 toolBox = EngineToolBox.getInstance (); 57 } 58 59 60 65 public static synchronized SelectUG_Engine getInstance () { 66 if (theObject == null) { 67 theObject = new SelectUG_Engine (); 68 } 69 return theObject; 70 } 72 73 80 public boolean authoriseRender (ParamBean jParams) { 81 return toolBox.authoriseRender (jParams); 82 } 84 85 95 public String renderLink (ParamBean jParams, Object theObj) 96 throws JahiaException { 97 String rightParams = (String ) theObj; 98 String params = EMPTY_STRING; 99 params += "?mode=display&screen=searchGroups"; 100 params += rightParams; 101 return jParams.composeEngineUrl (ENGINE_NAME, params); 102 } 104 105 108 public boolean needsJahiaData (ParamBean jParams) { 109 return false; 110 } 112 113 119 public void handleActions (ParamBean jParams, JahiaData jData) 120 throws JahiaException, 121 JahiaSessionExpirationException { 122 HashMap engineMap = initEngineMap (jParams); 124 125 processLastScreen (jParams, engineMap); 126 processCurrentScreen (jParams, engineMap); 127 128 toolBox.displayScreen (jParams, engineMap); 130 131 } 133 138 public final String getName () { 139 return ENGINE_NAME; 140 } 141 142 143 148 public void processLastScreen (ParamBean jParams, HashMap engineMap) 149 throws JahiaException { 150 String theScreen = (String ) engineMap.get ("screen"); 152 if (theScreen == null) { 153 throw new JahiaException ("SelectUG_Engine.processLastScreen", 154 "Error in parameters", 155 JahiaException.PARAMETER_ERROR, 156 JahiaException.CRITICAL_SEVERITY); 157 } 158 159 String searchString = jParams.getRequest().getParameter("searchString"); 161 jParams.getRequest().setAttribute("searchString", searchString); 162 String searchIn = jParams.getRequest().getParameter("searchIn"); 163 String [] searchInProps = jParams.getRequest().getParameterValues("properties"); 164 String storedOn = jParams.getRequest().getParameter("storedOn"); 165 String [] providers = jParams.getRequest().getParameterValues("providers"); 166 167 String [] selectedUsers = jParams.getRequest().getParameterValues("selectedUG"); 170 Set userKeySet = new HashSet (); 171 if (selectedUsers != null) { 172 for (int i=0; i < selectedUsers.length; i++) { 173 userKeySet.add(selectedUsers[i]); 174 } 175 } 176 engineMap.put("alreadySelectedUsers", userKeySet); 177 178 Integer userNameWidth = (Integer ) jParams.getRequest ().getSession ().getAttribute ( 179 "userNameWidth"); 180 if ("searchUsers".equals (theScreen)) { 181 182 String selectedGroupMember = (String ) jParams.getParameter("memberList"); 186 if (selectedGroupMember != null) { 187 userKeySet.clear(); 188 userKeySet.add(selectedGroupMember); 189 String userName = extractPrincipalName(selectedGroupMember, userNameWidth); 190 JahiaUser selectedUser = ServicesRegistry.getInstance().getJahiaUserManagerService().lookupUser(userName); 191 searchString = selectedUser.getUsername(); 192 jParams.getRequest().setAttribute("searchString", searchString); 193 } 194 195 String groupMembershipValue = (String ) jParams.getParameter("viewGroupMembership"); 197 if ((groupMembershipValue != null) || (selectedGroupMember != null)) { 198 List groupList = new ArrayList (); 200 Iterator userKeySetIter = userKeySet.iterator(); 201 while (userKeySetIter.hasNext()) { 202 String curUserKey = (String ) userKeySetIter.next(); 203 String userName = extractPrincipalName(curUserKey, userNameWidth); 204 JahiaUser selectedUser = ServicesRegistry.getInstance().getJahiaUserManagerService().lookupUser(userName); 205 Set groupMembership = PrincipalViewHelper.getUserGroupMembership(selectedUser, jParams.getSiteID()); 206 Iterator groupMembershipIter = groupMembership.iterator(); 207 while (groupMembershipIter.hasNext()) { 208 JahiaGroup group = (JahiaGroup) groupMembershipIter.next(); 209 buildGroupListEntry(group, userNameWidth, groupList); 210 } 211 } 212 engineMap.put("groupList", groupList); 213 } 214 215 Set searchResults = PrincipalViewHelper.getSearchResult (searchString, searchIn, searchInProps, storedOn, providers, 217 jParams.getSiteID ()); 218 if (searchResults != null) { 219 ArrayList resultList = new ArrayList (); 220 Iterator resultListEnum = searchResults.iterator (); 221 while (resultListEnum.hasNext ()) { 222 JahiaUser user = (JahiaUser) resultListEnum.next (); 223 buildUserListEntry(user, userNameWidth, resultList); 224 } 225 engineMap.put ("resultList", resultList); 226 engineMap.put ("selectUGEngine", "selectUsers"); 227 } 228 engineMap.put ("screen", "edit"); 229 } else if (theScreen.equals ("searchGroups")) { 230 231 String selectedMembershipGroup = (String ) jParams.getParameter("groupMembership"); 235 if (selectedMembershipGroup != null) { 236 userKeySet.clear(); 237 userKeySet.add(selectedMembershipGroup); 238 String groupName = extractPrincipalName(selectedMembershipGroup, userNameWidth); 239 JahiaGroup selectedGroup = ServicesRegistry.getInstance().getJahiaGroupManagerService().lookupGroup(groupName); 240 searchString = selectedGroup.getGroupname(); 241 jParams.getRequest().setAttribute("searchString", searchString); 242 } 243 244 String groupValueStr = (String ) jParams.getParameter("viewMembers"); 246 if ((groupValueStr != null) || (selectedMembershipGroup != null)) { 247 List memberList = new ArrayList (); 249 Iterator userKeySetIter = userKeySet.iterator(); 250 while (userKeySetIter.hasNext()) { 251 String curUserKey = (String ) userKeySetIter.next(); 252 String groupName = extractPrincipalName(curUserKey, userNameWidth); 253 JahiaGroup selectedGroup = ServicesRegistry.getInstance().getJahiaGroupManagerService().lookupGroup(groupName); 254 Enumeration membersEnum = selectedGroup.members(); 255 while (membersEnum.hasMoreElements()) { 256 Object curMember = membersEnum.nextElement(); 257 if (curMember instanceof JahiaUser) { 259 JahiaUser user = (JahiaUser) curMember; 260 buildUserListEntry(user, userNameWidth, memberList); 261 } 262 } 263 } 264 engineMap.put("memberList", memberList); 265 } 266 267 Set searchResults = PrincipalViewHelper.getGroupSearchResult (searchString, searchIn, searchInProps, storedOn, providers, 269 jParams.getSiteID ()); 270 if (searchResults != null) { 271 ArrayList resultList = new ArrayList (); 272 Iterator resultListEnum = searchResults.iterator (); 273 while (resultListEnum.hasNext ()) { 274 JahiaGroup group = (JahiaGroup) resultListEnum.next (); 275 buildGroupListEntry(group, userNameWidth, resultList); 276 } 277 engineMap.put ("resultList", resultList); 278 engineMap.put ("selectUGEngine", "selectGroups"); 279 } 280 engineMap.put ("screen", "edit"); 281 282 } else if (theScreen.equals ("save")) { 283 284 286 } 287 } 289 private String extractPrincipalName(String curPrincipalKey, Integer userNameWidth) { 290 return curPrincipalKey.substring("10000000r-- ".length() + userNameWidth.intValue() + 1); 291 } 292 293 private void buildUserListEntry(JahiaUser user, Integer userNameWidth, List memberList) { 294 String provider = JahiaString.adjustStringSize (user.getProviderName (), 6) + 295 " "; 296 String usrname = JahiaString.adjustStringSize (user.getUsername (), 297 userNameWidth.intValue ()); 298 String properties = EMPTY_STRING; 300 String firstname = user.getProperty ("firstname"); 301 String lastname = user.getProperty ("lastname"); 302 if (firstname != null) { 303 properties += firstname; 304 if (firstname.length () < 20) properties += " "; 305 } 306 if (lastname != null) properties += lastname; 307 if (EMPTY_STRING.equals (properties)) { 308 String email = user.getProperty ("email"); 309 if (email != null) properties += email; 310 } 311 properties = JahiaString.adjustStringSize (properties, 20); 312 String resultValue = "10000000r-- " + usrname + "u" + user.getName (); 313 String result = " " + provider + usrname + " " + properties; 314 memberList.add (JahiaTools.replacePattern (resultValue, " ", " ")); 315 memberList.add (JahiaTools.replacePattern (result, " ", " ")); 316 } 317 318 private void buildGroupListEntry(JahiaGroup group, Integer userNameWidth, List resultList) { 319 String provider = JahiaString.adjustStringSize (group.getProviderName (), 6) + 320 " "; 321 String grpname = JahiaString.adjustStringSize (group.getGroupname (), 323 userNameWidth.intValue ()); 324 Enumeration grpMembers = group.members (); 326 String members = "("; 327 while (grpMembers.hasMoreElements ()) { 328 Object obj = grpMembers.nextElement (); 329 if (obj instanceof JahiaUser) { 330 JahiaUser tmpUser = (JahiaUser) obj; 331 members += tmpUser.getUsername (); 332 } else { 333 JahiaGroup tmpGroup = (JahiaGroup) obj; 334 members += tmpGroup.getGroupname (); 335 } 336 if (members.length () > 20) break; 337 if (grpMembers.hasMoreElements ()) members += ", "; 338 } 339 members += ")"; 340 members = JahiaString.adjustStringSize (members, 20); 341 String resultValue = "10000000r-- " + grpname + "g" + group.getName (); 342 String result = " " + provider + grpname + " " + members; 343 resultList.add (JahiaTools.replacePattern (resultValue, " ", " ")); 344 resultList.add (JahiaTools.replacePattern (result, " ", " ")); 345 } 346 347 348 353 public void processCurrentScreen (ParamBean jParams, HashMap engineMap) 354 throws JahiaException { 355 357 JahiaUserManagerService uMgr = ServicesRegistry.getInstance () 358 .getJahiaUserManagerService (); 359 JahiaGroupManagerService gMgr = ServicesRegistry.getInstance () 360 .getJahiaGroupManagerService (); 361 Vector providerList = uMgr.getProviderList (); 362 engineMap.put ("providerList", providerList); 363 Vector group_providerList = gMgr.getProviderList (); 364 engineMap.put ("groupProviderList", group_providerList); 365 366 jParams.getRequest ().setAttribute ("jahia_session_engineMap", engineMap); 367 368 } 370 371 378 private HashMap initEngineMap (ParamBean jParams) 379 throws JahiaException, 380 JahiaSessionExpirationException { 381 String theScreen = jParams.getRequest ().getParameter ("screen"); 382 383 HttpSession theSession = jParams.getSession (); 386 387 HashMap engineMap = (HashMap ) theSession.getAttribute ("jahia_session_engineMap"); 388 389 if (engineMap == null) { 390 theScreen = "edit"; 391 engineMap = new HashMap (); 393 } 394 engineMap.put (RENDER_TYPE_PARAM, new Integer (JahiaEngine.RENDERTYPE_FORWARD)); 395 engineMap.put (ENGINE_NAME_PARAM, ENGINE_NAME); 396 engineMap.put (ENGINE_URL_PARAM, jParams.composeEngineUrl (ENGINE_NAME)); 397 engineMap.put ("selectUGEngine", "selectGroups"); 398 theSession.setAttribute ("jahia_session_engineMap", engineMap); 399 400 if (theScreen == null) { 401 theScreen = "edit"; 402 } 403 404 engineMap.put ("screen", theScreen); 406 if (theScreen.equals ("cancel")) { 407 engineMap.put ("jspSource", CLOSE_JSP); 408 } else if (theScreen.equals ("save")) { 409 engineMap.put ("jspSource", CLOSE_JSP); 410 } else { 411 engineMap.put ("jspSource", TEMPLATE_JSP); 412 } 413 414 jParams.getRequest ().setAttribute ("engineTitle", "Select Users"); 416 jParams.getRequest ().setAttribute ("org.jahia.engines.EngineHashMap", engineMap); 417 418 return engineMap; 419 } 421 422 } 423 | Popular Tags |