KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > admin > users > ManageGroups


1 // $Id: ManageGroups.java 10570 2005-09-13 09:35:40Z pvollenweider $
2
//
3
// ____.
4
// __/\ ______| |__/\. _______
5
// __ .____| | \ | +----+ \
6
// _______| /--| | | - \ _ | : - \_________
7
// \\______: :---| : : | : | \________>
8
// |__\---\_____________:______: :____|____:_____\
9
// /_____|
10
//
11
// . . . i n j a h i a w e t r u s t . . .
12
//
13
//
14

15 package org.jahia.admin.users;
16
17 import java.io.IOException JavaDoc;
18 import java.lang.reflect.InvocationTargetException JavaDoc;
19 import java.lang.reflect.Method JavaDoc;
20 import java.security.Principal JavaDoc;
21 import java.util.ArrayList JavaDoc;
22 import java.util.Enumeration JavaDoc;
23 import java.util.HashSet JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.Set JavaDoc;
26 import java.util.Vector JavaDoc;
27 import javax.servlet.ServletException JavaDoc;
28 import javax.servlet.http.HttpServletRequest JavaDoc;
29 import javax.servlet.http.HttpServletResponse JavaDoc;
30 import javax.servlet.http.HttpSession JavaDoc;
31
32 import org.jahia.admin.homepages.HomePageTool;
33 import org.jahia.bin.JahiaAdministration;
34 import org.jahia.data.JahiaData;
35 import org.jahia.data.viewhelper.principal.PrincipalViewHelper;
36 import org.jahia.exceptions.JahiaException;
37 import org.jahia.params.ParamBean;
38 import org.jahia.registries.ServicesRegistry;
39 import org.jahia.resourcebundle.JahiaResourceBundle;
40 import org.jahia.services.fields.ContentField;
41 import org.jahia.services.homepages.JahiaHomepage;
42 import org.jahia.services.homepages.JahiaHomepageLink;
43 import org.jahia.services.homepages.JahiaHomepagesService;
44 import org.jahia.services.pages.ContentPage;
45 import org.jahia.services.pages.JahiaPageBaseService;
46 import org.jahia.services.sites.JahiaSite;
47 import org.jahia.services.sites.JahiaSitesService;
48 import org.jahia.services.usermanager.JahiaDBUser;
49 import org.jahia.services.usermanager.JahiaGroup;
50 import org.jahia.services.usermanager.JahiaGroupManagerService;
51 import org.jahia.services.usermanager.JahiaSiteGroupManagerService;
52 import org.jahia.services.usermanager.JahiaSiteUserManagerService;
53 import org.jahia.services.usermanager.JahiaUser;
54 import org.jahia.services.usermanager.JahiaUserManagerService;
55 import org.jahia.services.version.EntryLoadRequest;
56 import org.jahia.utils.JahiaTools;
57 import org.jahia.utils.LanguageCodeConverters;
58
59 /**
60  * desc: This class is used by the administration to manage groups
61  * (add a user to a group, and for adding, editing and deleting groups) in
62  * Jahia portal.
63  *
64  * Copyright: Copyright (c) 2002
65  * Company: Jahia Ltd
66  *
67  * @author AK
68  * @author MJ
69  * @author MAP
70  * @version 2.0
71  */

72
73 public class ManageGroups
74 {
75     private static final String JavaDoc JSP_PATH = JahiaAdministration.JSP_PATH;
76
77     private static JahiaUserManagerService uMgr;
78     private static JahiaGroupManagerService gMgr;
79     private static JahiaSiteUserManagerService uSiteMgr;
80     private JahiaSite jahiaSite;
81     private String JavaDoc groupMessage = "";
82     private static HashSet JavaDoc groupMembers; // Contain the group members of the selected group list
83

84     ParamBean jParams;
85
86     private HomePageTool homePageTool = null;
87     private GroupMembersTool groupMembersTool = null;
88
89     /**
90      * Default constructor.
91      *
92      * @param request Servlet request.
93      * @param response Servlet response.
94      * @param session Servlet session for the current user.
95      * @throws Throwable
96      */

97     public ManageGroups(HttpServletRequest JavaDoc request,
98                         HttpServletResponse JavaDoc response,
99                         HttpSession JavaDoc session)
100     throws Throwable JavaDoc
101     {
102         // get services...
103
ServicesRegistry sReg = ServicesRegistry.getInstance();
104         if (sReg != null) {
105             uMgr = sReg.getJahiaUserManagerService();
106             gMgr = sReg.getJahiaGroupManagerService();
107             uSiteMgr = sReg.getJahiaSiteUserManagerService();
108         }
109         // get the current website. get the jahiaserver if it's null...
110
jahiaSite = (JahiaSite) session.getAttribute( ParamBean.SESSION_SITE );
111         JahiaData jData = (JahiaData) request.getAttribute("org.jahia.data.JahiaData");
112         this.jParams = jData.params();
113
114         if (jahiaSite == null) {
115             JahiaSitesService sitesService = sReg.getJahiaSitesService();
116             jahiaSite = sitesService.getSite(0);
117             session.setAttribute( ParamBean.SESSION_SITE, jahiaSite );
118         }
119         userRequestDispatcher( request, response, session );
120     } // end constructor
121

122
123
124     /**
125      * This method is used like a dispatcher for user requests.
126      *
127      * @param request
128      * @param response
129      * @param session
130      * @throws Throwable
131      */

132     private void userRequestDispatcher( HttpServletRequest JavaDoc request,
133                                         HttpServletResponse JavaDoc response,
134                                         HttpSession JavaDoc session )
135     throws Throwable JavaDoc
136     {
137         String JavaDoc operation = request.getParameter("sub");
138
139         if (operation.equals("display")) {
140             displayGroupList(request, response, session );
141         } else if (operation.equals("search")) {
142             displayGroupList(request, response, session);
143         } else if (operation.equals("create")) {
144             displayGroupCreate( request, response, session);
145         } else if (operation.equals("edit")) {
146             displayGroupEdit(request, response, session);
147         } else if (operation.equals("membership")) {
148             displayGroupMembership( request, response, session );
149         } else if (operation.equals("copy")) {
150             displayGroupCopy(request, response, session);
151         } else if (operation.equals("remove")) {
152             displayGroupRemove(request, response, session);
153         } else if (operation.equals("processCreate")) {
154             if (processGroupCreate(request, response, session)) {
155                 displayGroupList(request, response, session);
156             } else {
157                 displayGroupCreate(request, response, session);
158             }
159         } else if (operation.equals("processEdit")) {
160             if (processGroupEdit(request, response, session)) {
161                 displayGroupList(request, response, session);
162             } else {
163                 displayGroupEdit(request, response, session);
164             }
165         } else if (operation.equals("processRemove")) {
166             processGroupRemove(request, response, session);
167         } else if (operation.equals("processCopy")) {
168             if (processGroupCopy(request, response, session)) {
169                 displayGroupList(request, response, session);
170             } else {
171                 displayGroupCopy(request, response, session);
172             }
173         } else if (operation.equals("homepage")) {
174             processHomePage(request, response, session);
175         } else if (operation.equals("groupmembers")) {
176             processGroupMembers(request, response, session);
177         }
178     }
179
180     /**
181     * Forward the servlet request and servlet response objects, using the request
182     * dispatcher (from the ServletContext). Note: please be careful, use only
183     * context relative path.
184     *
185     * @param request
186     * @param response
187     * @param session
188     * @param target context-relative path.
189     * @throws IOException
190     * @throws ServletException
191     */

192     private void doRedirect( HttpServletRequest JavaDoc request,
193                              HttpServletResponse JavaDoc response,
194                              HttpSession JavaDoc session,
195                              String JavaDoc target )
196     throws IOException JavaDoc, ServletException JavaDoc
197     {
198         try
199         {
200             request.setAttribute("currentSiteBean",jahiaSite);
201
202             // check null warning msg
203
if( request.getAttribute("warningMsg") == null ) {
204                 request.setAttribute("warningMsg", "");
205             }
206
207             // check null jsp bottom message, and fill in if necessary...
208
if( request.getAttribute("msg") == null ) {
209                 request.setAttribute("msg",
210                     JahiaResourceBundle.getAdminResource("org.jahia.admin.copyright",
211                         jParams, jParams.getLocale()));
212             }
213
214             if( request.getAttribute("focus") == null ) {
215                 request.setAttribute("focus", "-none-");
216             }
217
218             // check null configuration step title, and fill in if necessary...
219
if( request.getAttribute("title") == null ) {
220                 request.setAttribute("title", "Manage Groups");
221             }
222
223             // get browser entity...
224
String JavaDoc userAgent = request.getHeader("user-agent");
225
226             // redirect!
227
JahiaAdministration.doRedirect( request, response, session, target );
228
229         } catch (IOException JavaDoc ie) {
230             logger.error("Error ", ie);
231         } catch (ServletException JavaDoc se) {
232             logger.error("Error ", se);
233             if (se.getRootCause() != null) {
234                 logger.error("Error root cause", se.getRootCause());
235             }
236         }
237     }
238
239      /**
240       * Display the group list.
241       *
242       * @param request
243       * @param response
244       * @param session
245       * @throws IOException
246       * @throws ServletException
247       */

248     private void displayGroupList( HttpServletRequest JavaDoc request,
249                                    HttpServletResponse JavaDoc response,
250                                    HttpSession JavaDoc session )
251     throws IOException JavaDoc, ServletException JavaDoc
252     {
253         // get list of groups...
254
request.setAttribute("providerList", gMgr.getProviderList());
255         request.setAttribute("resultList", PrincipalViewHelper.getGroupSearchResult(request, jahiaSite.getID()));
256         request.setAttribute("currentSite", jahiaSite.getServerName());
257         request.setAttribute("jspSource", JSP_PATH + "group_management/group_management.jsp");
258         request.setAttribute("directMenu", JSP_PATH + "direct_menu.jsp");
259         request.setAttribute("groupSearch", JSP_PATH + "group_management/group_search.jsp");
260         session.setAttribute("jahiaDisplayMessage",
261             JahiaResourceBundle.getAdminResource("org.jahia.admin.copyright",
262                 jParams, jParams.getLocale()));
263         session.setAttribute("groupMessage", groupMessage);
264         // Remove any home page definition from the session.
265
session.setAttribute("homePageID", null);
266         session.setAttribute("homePageLabel", null);
267         doRedirect(request, response, session, JSP_PATH + "admin.jsp");
268         groupMessage = "";
269     }
270
271      /**
272       * Display a form destinated to create a group.
273       *
274       * @param request
275       * @param response
276       * @param session
277       * @throws IOException
278       * @throws ServletException
279       * @throws JahiaException
280       */

281     private void displayGroupCreate( HttpServletRequest JavaDoc request,
282                                      HttpServletResponse JavaDoc response,
283                                      HttpSession JavaDoc session )
284     throws IOException JavaDoc, ServletException JavaDoc, JahiaException
285     {
286         logger.debug("Started");
287         request.setAttribute("groupName", JahiaTools.nnString(request.getParameter("groupName")));
288
289         // Is here any home page definition ?
290
JahiaHomepagesService hpServ = ServicesRegistry.getInstance().getJahiaHomepagesService();
291         JahiaHomepage homePage = hpServ.getHomepage(jahiaSite.getGroupDefaultHomepageDef());
292         if (homePage != null) {
293             JahiaHomepageLink homePageLink = (JahiaHomepageLink)homePage;
294             session.setAttribute("homePageID", new Integer JavaDoc(homePageLink.getPageID()));
295             request.setAttribute("defaultHomePage", JahiaPageBaseService.getInstance().
296                 lookupPageWhitoutTemplates(homePageLink.getPageID()).getTitle() +
297                 " (" + homePage.getName() + ")");
298         }
299         request.setAttribute("jspSource", JSP_PATH + "group_management/group_create.jsp");
300         request.setAttribute("directMenu", JSP_PATH + "direct_menu.jsp");
301         session.setAttribute("groupMessage", groupMessage);
302         session.setAttribute("jahiaDisplayMessage",
303             JahiaResourceBundle.getAdminResource("org.jahia.admin.copyright",
304                 jParams, jParams.getLocale()));
305         doRedirect(request, response, session, JSP_PATH + "admin.jsp" );
306         groupMessage = "";
307     }
308
309     /**
310      * Create the new group in the jahia DB.
311      *
312      * @param request
313      * @param response
314      * @param session
315      * @return true if group successfully created.
316      * @throws IOException
317      * @throws ServletException
318      * @throws JahiaException
319      */

320     private boolean processGroupCreate(HttpServletRequest JavaDoc request,
321                                        HttpServletResponse JavaDoc response,
322                                        HttpSession JavaDoc session)
323     throws IOException JavaDoc, ServletException JavaDoc, JahiaException
324     {
325         String JavaDoc groupName = (String JavaDoc)request.getParameter("groupName").trim();
326         if (groupName.length() == 0) {
327           groupMessage = JahiaResourceBundle.getAdminResource("org.jahia.admin.groupMessage.specifyGroupName.label",
328                                              jParams, jParams.getLocale());
329             return false;
330         }
331         // Does the introduced groupName contain some errors ?
332
if (groupName.length() == 0) {
333           groupMessage = JahiaResourceBundle.getAdminResource("org.jahia.admin.groupMessage.specifyGroupName.label",
334                                              jParams, jParams.getLocale());
335             return false;
336         }
337         // The following test is really disputable because we should can enter
338
// as well accentueted char and any internationalized char.
339
else if (!JahiaTools.isAlphaValid(groupName)) {
340           groupMessage = JahiaResourceBundle.getAdminResource("org.jahia.admin.groupMessage.onlyLettersDigitsUnderscores.label",
341                                              jParams, jParams.getLocale());
342             return false;
343         } else if (gMgr.groupExists (jahiaSite.getID(), groupName)) {
344           groupMessage = JahiaResourceBundle.getAdminResource("org.jahia.admin.groupMessage.group.label",
345               jParams, jParams.getLocale());
346           groupMessage += "[ " + groupName + "] ";
347           groupMessage += JahiaResourceBundle.getAdminResource("org.jahia.admin.groupMessage.alreadyExist.label",
348               jParams, jParams.getLocale());
349             return false;
350         }
351         // try to create the new group...
352
JahiaGroup grp = createGroup(session, groupName);
353         if (grp == null) {
354           groupMessage = JahiaResourceBundle.getAdminResource("org.jahia.admin.groupMessage.unableCreateGroup.label",
355               jParams, jParams.getLocale());
356           groupMessage += " " + groupName;
357             return false;
358         } else {
359           groupMessage = JahiaResourceBundle.getAdminResource("org.jahia.admin.groupMessage.group.label",
360               jParams, jParams.getLocale());
361           groupMessage += " [" + groupName + "] ";
362           groupMessage += JahiaResourceBundle.getAdminResource("org.jahia.admin.groupMessage.created.label",
363              jParams, jParams.getLocale());
364         }
365         // Lookup for home page settings and set it.
366
if (request.getParameter("setHomePage") != null) {
367             Integer JavaDoc homePageIDInt = (Integer JavaDoc)session.getAttribute("homePageID");
368             if (homePageIDInt != null) {
369                 int homePageID = homePageIDInt.intValue();
370                 logger.debug("Process Home Page ID : " + homePageID);
371                 grp.setHomepageID(homePageID);
372             }
373         }
374         return true;
375     }
376
377     /**
378      * Display group members and home page
379      *
380      * @param request
381      * @param response
382      * @param session
383      * @throws IOException
384      * @throws ServletException
385      * @throws JahiaException
386      */

387     private void displayGroupEdit(HttpServletRequest JavaDoc request,
388                                   HttpServletResponse JavaDoc response,
389                                   HttpSession JavaDoc session)
390     throws IOException JavaDoc, ServletException JavaDoc, JahiaException
391     {
392         logger.debug("Started");
393         String JavaDoc groupToEdit = request.getParameter("selectedGroup");
394         if (groupToEdit == null) { // Get the last group if none was selected.
395
groupToEdit = (String JavaDoc)session.getAttribute("selectedGroup");
396         }
397         if (groupToEdit == null || "null".equals(groupToEdit)) {
398           groupMessage = JahiaResourceBundle.getAdminResource("org.jahia.admin.groupMessage.selectGroup.label",
399               jParams, jParams.getLocale());
400             displayGroupList(request, response, session);
401             return;
402         }
403         // Consider actual selected group as the last one and store it in session.
404
session.setAttribute("selectedGroup", groupToEdit);
405         JahiaGroup theGroup = (JahiaGroup)gMgr.lookupGroup(jahiaSite.getID(), groupToEdit);
406         //predrag
407
session.setAttribute("providerName",theGroup.getProviderName());
408         //end predrag
409

410         if ("update".equals(request.getParameter("actionType"))) {
411             // let's recuperate the members of the group from the selection box
412
// if we are re-displaying the same form.
413
String JavaDoc[] newMembersList = (String JavaDoc[]) request.getParameterValues("selectMember");
414             // convert to HashSet
415
if (newMembersList != null) {
416                 Set JavaDoc updatedGroupSet = new HashSet JavaDoc();
417                 for (int i = 0; i < newMembersList.length; i++) {
418                     // remove identifier type ("u " or "g " and provider) for future use.
419
JahiaUser usr = (JahiaUser) uMgr.lookupUser(newMembersList[
420                                                                 i].substring(1));
421                     updatedGroupSet.add(usr);
422                 }
423                 // display the edit form with initial values
424
request.setAttribute("groupMembers", updatedGroupSet); //usersViewHelper.getUserListForDisplay(groupMembers));
425
} else {
426                 request.setAttribute("groupMembers", new HashSet JavaDoc()); //usersViewHelper.getUserListForDisplay(groupMembers));
427
}
428         } else {
429             groupMembers = (HashSet JavaDoc)getGroupMembers(groupToEdit, jahiaSite.getID());
430             // display the edit form with initial values
431
request.setAttribute("groupMembers", groupMembers); //usersViewHelper.getUserListForDisplay(groupMembers));
432
}
433
434         // Get the home page
435
if (session.getAttribute("homePageLabel") == null) {
436             int homePageID = theGroup.getHomepageID();
437             if (homePageID != -1) {
438                 // FIXME : This exception should arrive when the page is
439
// erased. In this case it is the rule of the page remover
440
// to ensure that no references to pages exist before
441
// removing.
442
/** @todo remove this exception catching when a correct page
443                    remover were implemented. */

444                 try {
445                     ArrayList JavaDoc locales = jahiaSite.getLanguageSettingsAsLocales(false);
446                     EntryLoadRequest loadRequest = jParams.getEntryLoadRequest();
447                     if ( !locales.contains(jParams.getLocale()) ){
448                         loadRequest = EntryLoadRequest.STAGED;
449                         locales.add(0,LanguageCodeConverters.languageCodeToLocale(ContentField.SHARED_LANGUAGE));
450                         loadRequest.setLocales(locales);
451                     }
452                     ContentPage contentPage = JahiaPageBaseService.
453                         getInstance().lookupContentPage(homePageID,loadRequest,false);
454                     session.setAttribute("homePageLabel", contentPage.getTitle(loadRequest));
455                     session.setAttribute("homePageID", new Integer JavaDoc(homePageID));
456                 } catch (JahiaException je) {
457                     logger.error("Error on page ", je);
458                     groupMessage = JahiaResourceBundle.getAdminResource("org.jahia.admin.groupMessage.pageProblem.label",
459                         jParams, jParams.getLocale());
460                     session.setAttribute("homePageLabel", "Disapeared !");
461                 }
462             } else {
463                 String JavaDoc homePageLabelValue =
464                     JahiaResourceBundle.getAdminResource("org.jahia.admin.none.label",
465                         jParams, jParams.getLocale());
466                 session.setAttribute("homePageLabel", homePageLabelValue);
467             }
468         }
469         request.setAttribute("jspSource", JSP_PATH + "group_management/group_edit.jsp");
470         request.setAttribute("directMenu", JSP_PATH + "direct_menu.jsp");
471         session.setAttribute("jahiaDisplayMessage",
472             JahiaResourceBundle.getAdminResource("org.jahia.admin.copyright",
473                 jParams, jParams.getLocale()));
474         session.setAttribute("groupMessage", groupMessage);
475         doRedirect(request, response, session, JSP_PATH + "admin.jsp");
476         groupMessage = "";
477     }
478
479     /**
480      * Process modifications to the group previously edited.
481      *
482      * @param request
483      * @param response
484      * @param session
485      * @throws IOException
486      * @throws ServletException
487      * @throws JahiaException
488      */

489     private boolean processGroupEdit(HttpServletRequest JavaDoc request,
490                                   HttpServletResponse JavaDoc response,
491                                   HttpSession JavaDoc session)
492     throws IOException JavaDoc, ServletException JavaDoc, JahiaException
493     {
494         logger.debug("Started");
495         String JavaDoc groupName = (String JavaDoc)request.getParameter("groupName");
496         JahiaGroup grp = gMgr.lookupGroup(jahiaSite.getID(), groupName);
497
498         if ("update".equals(request.getParameter("actionType"))) {
499             return false;
500         }
501
502         // Lookup for home page settings and set it.
503
Integer JavaDoc homePageIDInt = (Integer JavaDoc)session.getAttribute("homePageID");
504         if (homePageIDInt != null) {
505             int homePageID = homePageIDInt.intValue();
506             if (homePageID != -1) {
507                 logger.debug("Process Home Page ID : " + homePageID);
508                 grp.setHomepageID(homePageID);
509             } else {
510                 grp.removeProperty("group_homepage");
511             }
512             session.removeAttribute("homePageID");
513             session.removeAttribute("homePageLabel");
514         }
515
516         // let's recuperate the members of the group from the selection box
517
String JavaDoc[] newMembersList = (String JavaDoc[])request.getParameterValues("selectMember");
518         // convert to HashSet
519
HashSet JavaDoc candidateMembers = new HashSet JavaDoc();
520         if (newMembersList != null) {
521             for (int i = 0; i < newMembersList.length; i++) {
522                 // remove identifier type ("u " or "g " and provider) for future use.
523
JahiaUser usr = (JahiaUser)uMgr.lookupUser(newMembersList[i].substring(1));
524                 candidateMembers.add(usr);
525             }
526         }
527
528         // Update group members
529
if (candidateMembers.size() > 0) {
530             try { // FIXME : Is here a way to optmize these pointer to method ?
531
// Is there any new members to the original groupMembers
532
addRemoveGroupMembers(groupMembers, candidateMembers,
533                 JahiaGroup.class.getMethod("addMember", new Class JavaDoc[] {Principal JavaDoc.class}), grp);
534             // Is there any removed members from the original groupMembers
535
addRemoveGroupMembers(candidateMembers, groupMembers,
536                 JahiaGroup.class.getMethod("removeMember", new Class JavaDoc[] {Principal JavaDoc.class}), grp);
537             } catch (NoSuchMethodException JavaDoc nsme) {
538                 logger.debug("Error ", nsme);
539             }
540         } else {
541             // No member in the select box, all members have to be removed
542
Iterator JavaDoc it = groupMembers.iterator();
543             while (it.hasNext()) {
544                 grp.removeMember((JahiaUser)it.next());
545             }
546         }
547         groupMessage = JahiaResourceBundle.getAdminResource("org.jahia.admin.groupMessage.group.label",
548             jParams, jParams.getLocale());
549         groupMessage += " [" + groupName + "] ";
550         groupMessage += JahiaResourceBundle.getAdminResource("org.jahia.admin.groupMessage.updated.label",
551             jParams, jParams.getLocale());
552
553         return true;
554     }
555
556     /**
557      * Display group members defining from other sites
558      *
559      * @param request
560      * @param response
561      * @param session
562      * @throws IOException
563      * @throws ServletException
564      */

565     private void displayGroupMembership( HttpServletRequest JavaDoc request,
566                                          HttpServletResponse JavaDoc response,
567                                          HttpSession JavaDoc session)
568     throws IOException JavaDoc, ServletException JavaDoc
569     {
570         String JavaDoc selectedGroup = request.getParameter("selectedGroup");
571         if (selectedGroup == null) {
572           groupMessage = JahiaResourceBundle.getAdminResource("org.jahia.admin.groupMessage.selectGroup.label",
573               jParams, jParams.getLocale());
574             displayGroupList(request, response, session);
575         }
576         else {
577             request.setAttribute("groupName", selectedGroup);
578             Vector JavaDoc groupMembership = getGroupMembership(selectedGroup, jahiaSite.getID());
579             request.setAttribute("groupMembership", groupMembership);
580
581             request.setAttribute("jspSource", JSP_PATH + "group_management/group_view.jsp");
582             request.setAttribute("directMenu", JSP_PATH + "direct_menu.jsp");
583             session.setAttribute("jahiaDisplayMessage",
584                 JahiaResourceBundle.getAdminResource("org.jahia.admin.copyright",
585                     jParams, jParams.getLocale()));
586             doRedirect(request, response, session, JSP_PATH + "admin.jsp" );
587             groupMessage = "";
588         }
589     }
590
591     /**
592      * Display the form permitting to copy the selected group.
593      *
594      * @param request
595      * @param response
596      * @param session
597      * @throws IOException
598      * @throws ServletException
599      */

600     private void displayGroupCopy( HttpServletRequest JavaDoc request,
601                                    HttpServletResponse JavaDoc response,
602                                    HttpSession JavaDoc session )
603     throws IOException JavaDoc, ServletException JavaDoc
604     {
605         logger.debug("Started");
606         request.setAttribute("newGroup", JahiaTools.nnString(request.getParameter("newGroup")));
607         String JavaDoc selectedGroup = request.getParameter("selectedGroup");
608         if (selectedGroup == null) { // Get the last group if none was selected.
609
selectedGroup = (String JavaDoc)session.getAttribute("selectedGroup");
610         }
611         if (selectedGroup == null || "null".equals(selectedGroup)) {
612             groupMessage = "Please select a group in the select box";
613             displayGroupList(request, response, session);
614             return;
615         }
616         // Consider actual selected group as the last one and store it in session.
617
session.setAttribute("selectedGroup", selectedGroup);
618         session.setAttribute("groupMessage", groupMessage);
619
620         request.setAttribute("jspSource", JSP_PATH + "group_management/group_copy.jsp");
621         request.setAttribute("directMenu", JSP_PATH + "direct_menu.jsp");
622         session.setAttribute("jahiaDisplayMessage",
623             JahiaResourceBundle.getAdminResource("org.jahia.admin.copyright",
624                 jParams, jParams.getLocale()));
625         doRedirect(request, response, session, JSP_PATH + "admin.jsp" );
626         groupMessage = "";
627     }
628
629     /**
630      * Make a copy of a group.
631      *
632      * @param request
633      * @param response
634      * @param session
635      * @return true if group successfully copied.
636      * @throws IOException
637      * @throws ServletException
638      */

639     private boolean processGroupCopy(HttpServletRequest JavaDoc request,
640                                      HttpServletResponse JavaDoc response,
641                                      HttpSession JavaDoc session)
642     throws IOException JavaDoc, ServletException JavaDoc
643     {
644         String JavaDoc groupName = (String JavaDoc)request.getParameter("newGroup");
645         String JavaDoc sourceGroupName = (String JavaDoc)session.getAttribute("selectedGroup");
646         // Does the introduced groupName contain some errors ?
647
if (groupName.length() == 0) {
648           groupMessage = JahiaResourceBundle.getAdminResource("org.jahia.admin.groupMessage.specifyGroupName.label",
649               jParams, jParams.getLocale());
650             return false;
651         }
652         // The following test is really disputable because we should can enter
653
// as well accentueted char and any internationalized char.
654
else if (!JahiaTools.isAlphaValid(groupName)) {
655           groupMessage = JahiaResourceBundle.getAdminResource("org.jahia.admin.groupMessage.onlyLettersDigitsUnderscores.label",
656              jParams, jParams.getLocale());
657             return false;
658         }
659         else if (gMgr.groupExists (jahiaSite.getID(), groupName)) {
660           groupMessage = JahiaResourceBundle.getAdminResource("org.jahia.admin.groupMessage.group.label",
661               jParams, jParams.getLocale());
662           groupMessage += " [" + groupName + "] ";
663           groupMessage += JahiaResourceBundle.getAdminResource("org.jahia.admin.groupMessage.alreadyExist.label",
664               jParams, jParams.getLocale());
665             return false;
666         }
667
668         // Try to create the new group
669
if (createGroup(session, groupName) == null) {
670           groupMessage = JahiaResourceBundle.getAdminResource("org.jahia.admin.groupMessage.unableCreateGroup.label",
671               jParams, jParams.getLocale());
672             return false;
673         }
674         else {
675             JahiaGroup theNewGroup = gMgr.lookupGroup(jahiaSite.getID(), groupName);
676             groupMessage = JahiaResourceBundle.getAdminResource("org.jahia.admin.groupMessage.group.label",
677                 jParams, jParams.getLocale());
678             groupMessage += " [" + groupName + "] ";
679             groupMessage += JahiaResourceBundle.getAdminResource("org.jahia.admin.groupMessage.created.label",
680                 jParams, jParams.getLocale());
681             Set JavaDoc sourceGroupMembers = getGroupMembers(sourceGroupName, jahiaSite.getID());
682             Iterator JavaDoc it = sourceGroupMembers.iterator();
683             while (it.hasNext()) {
684                 theNewGroup.addMember((JahiaUser)it.next());
685             }
686             // Home page copy
687
JahiaGroup sourceGroup = gMgr.lookupGroup(jahiaSite.getID(), sourceGroupName);
688             int homePageID = sourceGroup.getHomepageID();
689             if (homePageID != -1) {
690                 theNewGroup.setHomepageID(homePageID);
691             }
692         }
693         return true;
694     }
695
696     /**
697      * Display a confirmation form to remove a group
698      *
699      * @param request
700      * @param response
701      * @param session
702      * @throws IOException
703      * @throws ServletException
704      */

705     private void displayGroupRemove( HttpServletRequest JavaDoc request,
706                                      HttpServletResponse JavaDoc response,
707                                      HttpSession JavaDoc session )
708     throws IOException JavaDoc, ServletException JavaDoc
709     {
710         String JavaDoc selectedGroup = request.getParameter("selectedGroup");
711         if (selectedGroup == null) {
712           groupMessage = JahiaResourceBundle.getAdminResource("org.jahia.admin.groupMessage.selectGroup.label",
713               jParams, jParams.getLocale());
714             displayGroupList(request, response, session);
715         }
716         else {
717             request.setAttribute("groupName", selectedGroup);
718
719             request.setAttribute("jspSource", JSP_PATH + "group_management/group_remove.jsp");
720             request.setAttribute("directMenu", JSP_PATH + "direct_menu.jsp");
721             session.setAttribute("jahiaDisplayMessage",
722                 JahiaResourceBundle.getAdminResource("org.jahia.admin.copyright",
723                     jParams, jParams.getLocale()));
724             session.setAttribute("groupMessage", groupMessage);
725             doRedirect(request, response, session, JSP_PATH + "admin.jsp");
726         }
727     }
728
729     /**
730      * Remove a group from the jahia DB
731      *
732      * @param request
733      * @param response
734      * @param session
735      * @throws IOException
736      * @throws ServletException
737      */

738     private void processGroupRemove( HttpServletRequest JavaDoc request,
739                                      HttpServletResponse JavaDoc response,
740                                      HttpSession JavaDoc session )
741     throws IOException JavaDoc, ServletException JavaDoc
742     {
743         groupMessage = "";
744
745         String JavaDoc groupName = (String JavaDoc) request.getParameter("groupName");
746         // first let's do a quick sanity check on the group name.
747
if ((groupName == null) || ("".equals(groupName))) {
748             groupMessage = JahiaResourceBundle.getAdminResource("org.jahia.admin.groupMessage.cannotRemoved.label",
749                 jParams, jParams.getLocale());
750             groupMessage += " [" + groupName + "] ";
751             groupMessage += JahiaResourceBundle.getAdminResource("org.jahia.admin.groupMessage.group.label",
752                 jParams, jParams.getLocale());
753         }
754
755         JahiaGroup theGroup = (JahiaGroup)gMgr.lookupGroup(jahiaSite.getID(), groupName);
756
757         // for the moment we forbid the deletion of the following groups,
758
// because code in Jahia assumes they are always there :
759
// - administrators
760
// - users
761
// - guest
762
if (JahiaGroupManagerService.ADMINISTRATORS_GROUPNAME.equals(groupName) ||
763             JahiaGroupManagerService.USERS_GROUPNAME.equals(groupName) ||
764             JahiaGroupManagerService.GUEST_GROUPNAME.equals(groupName)) {
765             groupMessage = JahiaResourceBundle.getAdminResource("org.jahia.admin.groupMessage.cannotRemoved.label",
766                 jParams, jParams.getLocale());
767             groupMessage += " [" + groupName + "] ";
768             groupMessage += JahiaResourceBundle.getAdminResource("org.jahia.admin.groupMessage.group.label",
769                 jParams, jParams.getLocale());
770         }
771
772         if (groupMessage.equals("")) {
773
774             Set JavaDoc groupMembers = getGroupMembers(groupName, jahiaSite.getID());
775             Iterator JavaDoc it = groupMembers.iterator();
776             while (it.hasNext()) {
777                 theGroup.removeMember((JahiaUser)it.next());
778             }
779
780             // delete membership...
781
try {
782                 JahiaSiteGroupManagerService siteGroupManager = ServicesRegistry.getInstance().getJahiaSiteGroupManagerService();
783                 siteGroupManager.removeGroup(theGroup);
784             } catch (Exception JavaDoc e) {
785             }
786             // delete group...
787
if (!gMgr.deleteGroup(theGroup)) {
788               groupMessage = JahiaResourceBundle.getAdminResource("org.jahia.admin.groupMessage.cannotRemoved.label",
789                   jParams, jParams.getLocale());
790               groupMessage += " [" + groupName + "] ";
791               groupMessage += JahiaResourceBundle.getAdminResource("org.jahia.admin.groupMessage.group.label",
792                   jParams, jParams.getLocale());
793             } else {
794               groupMessage = JahiaResourceBundle.getAdminResource("org.jahia.admin.groupMessage.group.label",
795                   jParams, jParams.getLocale());
796               groupMessage += " [" + groupName + "] ";
797               groupMessage += JahiaResourceBundle.getAdminResource("org.jahia.admin.groupMessage.removed.label",
798                   jParams, jParams.getLocale());
799             }
800         }
801         displayGroupList( request, response, session );
802         groupMessage = "";
803     }
804
805      /**
806       * Create a group
807       * FIXME : This method has to be rechecked.
808       *
809       * @param session ??? FIXME : Has to be removed ?
810       * @param name The group name
811       * @return A reference to the newly created group.
812       */

813     private JahiaGroup createGroup(HttpSession JavaDoc session, String JavaDoc name)
814     {
815         // create the group...
816
JahiaGroup theGroup = null;
817         theGroup = gMgr.createGroup(jahiaSite.getID(), name , null);
818         if (theGroup != null) {
819             // create group membership...
820
try {
821                 JahiaSiteGroupManagerService siteGroupManager =
822                     ServicesRegistry.getInstance().getJahiaSiteGroupManagerService();
823                 siteGroupManager.addGroup(jahiaSite.getID(), theGroup);
824             } catch (Exception JavaDoc e) { }
825         }
826         return theGroup;
827     }
828
829     // FIXME : Has this method really something to do here ?
830
/**
831       * Return the member vector of a given site and a given group.
832       *
833       * @param groupName
834       * @param jahiaSite
835       * @return the member Set of a given site and a given group.
836       */

837     private Set JavaDoc getGroupMembers(String JavaDoc groupName, int jahiaSite) {
838
839         JahiaGroup theGroup = (JahiaGroup)gMgr.lookupGroup(jahiaSite, groupName);
840         Set JavaDoc groupMembers = new HashSet JavaDoc();
841         if (theGroup != null) {
842             Enumeration JavaDoc groupMembersEnum = theGroup.members();
843             while (groupMembersEnum.hasMoreElements()) {
844                 Object JavaDoc member = (Object JavaDoc)groupMembersEnum.nextElement();
845                 // keep only members from this jahiaSite...
846
if (member instanceof JahiaUser) {
847                     // ugly hack because we can't test the site ID on an external
848
// user provider for the moment...
849
if (member instanceof JahiaDBUser) {
850                         int siteID = ((JahiaUser)member).getSiteID();
851                         if (siteID == jahiaSite || siteID == 0) {
852                             groupMembers.add((JahiaUser)member);
853                         }
854                     } else {
855                         groupMembers.add((JahiaUser)member);
856                     }
857                 } else {
858                     if (((JahiaGroup)member).getSiteID() == jahiaSite) {
859                         groupMembers.add((JahiaGroup)member);
860                     }
861                 }
862             }
863         }
864         return groupMembers;
865     }
866
867      /**
868       * Recover membership and its site of a group.
869       * FIXME : Has to be recheked.
870       * (Indeed. When we edit the group, this method doesn't provide the mebership anymore -PredragV-)
871       *
872       * @param groupName the group name
873       * @param jahiaSite a jahia site ID
874       * @return the group member ship
875       */

876     private Vector JavaDoc getGroupMembership(String JavaDoc groupName, int jahiaSite)
877     {
878         JahiaGroup theGroup = (JahiaGroup)gMgr.lookupGroup(jahiaSite, groupName);
879         Vector JavaDoc groupMembership = new Vector JavaDoc();
880         if (theGroup != null) {
881             Enumeration JavaDoc groupMembersEnum = theGroup.members();
882             while (groupMembersEnum.hasMoreElements()) {
883                 Object JavaDoc obj = (Object JavaDoc)groupMembersEnum.nextElement();
884                 // keep only out member of this jahiaSite...
885
if ((obj instanceof JahiaUser) && (((JahiaUser)obj).getSiteID() != jahiaSite)) {
886                     JahiaUser grpMembers = (JahiaUser)obj;
887                     groupMembership.add(grpMembers.getUsername()); // Member name
888
// Look for site name from membership
889
int memberSiteID = grpMembers.getSiteID();
890                     if (memberSiteID == 0) {
891                         groupMembership.add("jahia server");
892                     } else {
893                         try {
894                             JahiaSitesService sitesManager =
895                                 ServicesRegistry.getInstance().getJahiaSitesService();
896                             JahiaSite memberSite = sitesManager.getSite(memberSiteID);
897                             if (memberSite != null) {
898                                 groupMembership.add(memberSite.getServerName());
899                             } else {
900                                 groupMembership.add("unknow");
901                             }
902                         } catch (Exception JavaDoc e) {
903                         }
904                     }
905                 }
906             }
907         }
908         return groupMembership;
909     }
910
911     /**
912      * Add or remove members to a group.
913      * FIXME : This method has to be rechecked !
914      *
915      * @param left a set of members to check
916      * @param right a set of members to check
917      * @param addRem the addMembers or removeMembers method to apply.
918      * @param grp the object defining the previous method
919      */

920     private void addRemoveGroupMembers(HashSet JavaDoc left, HashSet JavaDoc right, Method JavaDoc addRem, JahiaGroup grp) {
921         Iterator JavaDoc itRight = right.iterator();
922         while (itRight.hasNext()) {
923             JahiaUser elementRight = (JahiaUser)itRight.next();
924             Iterator JavaDoc itLeft = left.iterator();
925             while (itLeft.hasNext()) {
926                 JahiaUser elementLeft = (JahiaUser)itLeft.next();
927                 if (elementLeft.getUsername().equals(elementRight.getUsername())) {
928                     elementRight = null;
929                     break;
930                 }
931             }
932             if (elementRight != null) {
933                 try {
934                     Object JavaDoc[] args = { elementRight };
935                     addRem.invoke(grp, args);
936                 } catch (IllegalAccessException JavaDoc iae) {
937                     logger.error("Error ", iae);
938                 } catch (InvocationTargetException JavaDoc ite) {
939                     logger.error("Error ", ite);
940                 }
941             }
942         }
943     }
944
945 // /**
946
// * Get the groups from a site a prepare them for display.
947
// *
948
// * @param searchResult
949
// * @return a formated array list of string.
950
// */
951
// private ArrayList getGroupListForDisplay(Vector searchResult) {
952
//
953
// ArrayList resultList = new ArrayList();
954
// Enumeration groupListEnum = searchResult.elements();
955
// while (groupListEnum.hasMoreElements()) {
956
// String groupKey = (String)groupListEnum.nextElement();
957
// JahiaGroup group = ServicesRegistry.getInstance().getJahiaGroupManagerService().lookupGroup(groupKey);
958
// String provider = JahiaString.adjustStringSize(group.getProviderName(), 6) + " ";
959
// // Construct a displayable groupname
960
// String grpname = JahiaString.adjustStringSize(group.getGroupname(), 15);
961
// // Find some group members for properties
962
// Enumeration grpMembers = group.members();
963
// String members = "(";
964
// while (grpMembers.hasMoreElements()) {
965
// Object obj = (Object)grpMembers.nextElement();
966
// if (obj instanceof JahiaUser) {
967
// JahiaUser tmpUser = (JahiaUser)obj;
968
// members += tmpUser.getUsername();
969
// } else {
970
// JahiaGroup tmpGroup = (JahiaGroup)obj;
971
// members += tmpGroup.getGroupname();
972
// }
973
// if (members.length() > 30) break;
974
// if (grpMembers.hasMoreElements()) members += ", ";
975
// }
976
// members += ")";
977
// members = JahiaString.adjustStringSize(members, 30);
978
// String result = provider + " " + grpname + " " + members;
979
// resultList.add(group.getGroupname());
980
// resultList.add(JahiaTools.replacePattern(result, " ", "&nbsp;"));
981
// }
982
// return resultList;
983
// }
984

985     /**
986      * Apply the HomePageTool object displaying a home page tool popup window.
987      *
988      * @param request
989      * @param response
990      * @param session
991      * @throws Throwable
992      */

993     private void processHomePage(HttpServletRequest JavaDoc request,
994                                  HttpServletResponse JavaDoc response,
995                                  HttpSession JavaDoc session)
996     throws Throwable JavaDoc
997     {
998         if (homePageTool == null) {
999             homePageTool = new HomePageTool(request, response, session);
1000        } else {
1001            homePageTool.requestDispatcher(request, response, session);
1002        }
1003    }
1004
1005    /**
1006     * Apply the GroupMembersTool object displaying the users search module.
1007     *
1008     * @param request
1009     * @param response
1010     * @param session
1011     * @throws Throwable
1012     */

1013    private void processGroupMembers(HttpServletRequest JavaDoc request,
1014                                     HttpServletResponse JavaDoc response,
1015                                     HttpSession JavaDoc session)
1016    throws Throwable JavaDoc
1017    {
1018        if (groupMembersTool == null) {
1019            groupMembersTool = new GroupMembersTool(request, response, session);
1020        } else {
1021            groupMembersTool.requestDispatcher(request, response, session);
1022        }
1023    }
1024
1025    private static org.apache.log4j.Logger logger =
1026            org.apache.log4j.Logger.getLogger(ManageGroups.class);
1027
1028}
1029
Popular Tags