KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > webman > acl > eventhandler > ACGroupAddUserEventHandler


1 package de.webman.acl.eventhandler;
2
3 import java.util.*;
4
5 import com.teamkonzept.web.*;
6 import com.teamkonzept.lib.*;
7 import com.teamkonzept.webman.*;
8 import com.teamkonzept.webman.mainint.*;
9 import com.teamkonzept.webman.mainint.events.*;
10 import de.webman.acl.*;
11
12 /**
13 Updates a group definiton in the database
14  * @author $Author: alex $
15  * @version $Revision: 1.3 $
16  */

17 public class ACGroupAddUserEventHandler extends DefaultEventHandler implements ParameterTypes, DatabaseDefaults, FrameConstants
18 {
19 private ACGroupAddUserEventHandler() {}
20
21     private static ACGroupAddUserEventHandler instance = new ACGroupAddUserEventHandler();
22
23     public static ACGroupAddUserEventHandler getInstance()
24     {
25         return instance;
26     }
27
28     public void handleEvent(TKEvent evt) throws TKException
29     {
30         try {
31             WebManEvent.checkEvent(evt.getRemoteUser(), evt.getName(), ContextConstants.ACCESS_CONTROL);
32
33             String JavaDoc groupId = evt.getParameter( PARAMETER, "GROUP_ID");
34             String JavaDoc userId = evt.getParameter( PARAMETER, "USER_ID");
35
36             if (groupId != null && groupId.length() > 0)
37             {
38                 doGroup(evt);
39             }
40             else if (userId != null && userId.length() > 0)
41             {
42                 doUser(evt);
43             }
44         } catch (Throwable JavaDoc e) {
45             throw WebmanExceptionHandler.getException(e);
46         }
47     }
48
49     public boolean isHandler(TKEvent evt)
50     {
51         return evt.getName().equalsIgnoreCase( "AC_GROUP_ADD_CHILD" );
52     }
53
54     /**
55         fuegt zu einer Gruppe Benutzer oder Untergruppen hinzu
56      */

57     private void doGroup(TKEvent evt) throws Throwable JavaDoc
58     {
59             String JavaDoc groupId = evt.getParameter( PARAMETER, "GROUP_ID");
60
61             TKParams params = evt.getParams();
62             if (groupId != null) {
63                 Profile theGroup = null;
64                 theGroup = ProfileFactory.getInstance().getProfile(new Integer JavaDoc(groupId));
65                 if (theGroup != null) {
66                     addLogin(theGroup, params, "ADD_USER_ID");
67                     addLogin(theGroup, params, "ADD_GROUP_ID");
68
69                 }
70             }
71             TKHTMLTemplate t = evt.getPrepHTMLTemplate( "f_ac_group.tmpl" );
72             String JavaDoc editPage = evt.getParameter(PARAMETER, "EDIT_PAGE");
73             t.set("EDIT_PAGE", (editPage == null ? "1" : editPage));
74             HTMLUtils.fillFrameSet( t, LEFT_FRAME_WIDTH, "AC_GROUP_LIST", "AC_GROUP_EDIT" );
75             t.set("GROUP_ID", new Integer JavaDoc(groupId));
76             WebManEvent.fillEventsIntoTemplate(evt.getRemoteUser(), t, ACCESS_CONTROL);
77             evt.finishTemplate( t );
78     }
79
80     /**
81        convenience method for adding either users or groups to a profile
82     */

83     private void addLogin(Profile profile, TKParams params, String JavaDoc parameter) throws Exception JavaDoc
84     {
85         TKVector vec = new TKVector();
86         if ( params.hasMultiple( PARAMETER, parameter ))
87         {
88             vec = params.getVector( PARAMETER, parameter );
89         }
90         else if ( params.get( PARAMETER, parameter ) != null )
91         {
92             vec.addElement( params.get( PARAMETER, parameter ));
93         }
94         Enumeration vecEnum = vec.elements();
95         while (vecEnum.hasMoreElements())
96         {
97             String JavaDoc lId = (String JavaDoc)vecEnum.nextElement();
98             Login login = LoginFactory.getInstance().getLogin(new Integer JavaDoc(lId));
99             if (login != null) {
100                 profile.addChild(login);
101                 ProfileFactory.getInstance().modifyProfile(profile);
102             }
103         }
104     }
105
106     /**
107         fügt einen Benutzer in mehrere Gruppen ein
108     */

109     private void doUser(TKEvent evt) throws Throwable JavaDoc
110     {
111             String JavaDoc userId = evt.getParameter( PARAMETER, "USER_ID");
112
113             TKParams params = evt.getParams();
114             if (userId != null && userId.length() > 0) {
115                 Login theUser = null;
116                 theUser = LoginFactory.getInstance().getLogin(new Integer JavaDoc(userId));
117                 if (theUser != null) {
118                     addLoginToGroups(theUser, params, "ADD_GROUP_ID");
119                 }
120             }
121             TKHTMLTemplate t = evt.getPrepHTMLTemplate( "f_ac_user.tmpl" );
122             String JavaDoc editPage = evt.getParameter(PARAMETER, "EDIT_PAGE");
123             t.set("EDIT_PAGE", (editPage == null ? "1" : editPage));
124             HTMLUtils.fillFrameSet( t, LEFT_FRAME_WIDTH, "AC_USER_LIST", "AC_USER_EDIT" );
125             t.set("USER_ID", new Integer JavaDoc(userId));
126             WebManEvent.fillEventsIntoTemplate(evt.getRemoteUser(), t, ACCESS_CONTROL);
127             evt.finishTemplate( t );
128     }
129
130     /**
131         fügt den Benutzer in verschiedene Gruppen ein...
132     */

133     private void addLoginToGroups(Login login, TKParams params, String JavaDoc parameter) throws Exception JavaDoc
134     {
135         TKVector vec = new TKVector();
136         if ( params.hasMultiple( PARAMETER, parameter ))
137         {
138             vec = params.getVector( PARAMETER, parameter );
139         }
140         else if ( params.get( PARAMETER, parameter ) != null )
141         {
142             vec.addElement( params.get( PARAMETER, parameter ));
143         }
144         Enumeration vecEnum = vec.elements();
145         while (vecEnum.hasMoreElements())
146         {
147             String JavaDoc lId = (String JavaDoc)vecEnum.nextElement();
148             Profile profile = ProfileFactory.getInstance().getProfile(new Integer JavaDoc(lId));
149             if (profile != null) {
150                 profile.addChild(login);
151                 ProfileFactory.getInstance().modifyProfile(profile);
152             }
153         }
154     }
155 }
156
Popular Tags