KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > engines > shared > Application_Field


1 //
2
// ____.
3
// __/\ ______| |__/\. _______
4
// __ .____| | \ | +----+ \
5
// _______| /--| | | - \ _ | : - \_________
6
// \\______: :---| : : | : | \________>
7
// |__\---\_____________:______: :____|____:_____\
8
// /_____|
9
//
10
// . . . i n j a h i a w e t r u s t . . .
11
//
12

13 //
14
// Application_Field
15
// EV 14.01.20001
16
// NK 19.04.2001 Lot of changes with roles to work with new user/group and in multi site
17

18 package org.jahia.engines.shared;
19
20 import java.security.Principal JavaDoc;
21 import java.util.ArrayList JavaDoc;
22 import java.util.Enumeration JavaDoc;
23 import java.util.HashMap JavaDoc;
24 import java.util.HashSet JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.Map JavaDoc;
28 import java.util.Properties JavaDoc;
29 import java.util.Set JavaDoc;
30 import java.util.Vector JavaDoc;
31
32 import javax.servlet.http.HttpSession JavaDoc;
33
34 import org.jahia.data.applications.ApplicationBean;
35 import org.jahia.data.applications.ApplicationContext;
36 import org.jahia.data.containers.JahiaContainer;
37 import org.jahia.data.fields.JahiaField;
38 import org.jahia.engines.JahiaEngine;
39 import org.jahia.engines.JahiaEngineTools;
40 import org.jahia.engines.users.SelectUG_Engine;
41 import org.jahia.exceptions.JahiaException;
42 import org.jahia.params.ParamBean;
43 import org.jahia.registries.ServicesRegistry;
44 import org.jahia.services.acl.JahiaBaseACL;
45 import org.jahia.services.applications.JahiaApplicationsDispatchingService;
46 import org.jahia.services.usermanager.JahiaGroup;
47 import org.jahia.services.usermanager.JahiaGroupManagerService;
48 import org.jahia.services.usermanager.JahiaUser;
49
50 public class Application_Field {
51
52     private static org.apache.log4j.Logger logger =
53             org.apache.log4j.Logger.getLogger(Application_Field.class);
54
55     private static Application_Field theObject = null;
56     private static final String JavaDoc JSP_FILE = "/jsp/jahia/engines/shared/application_field.jsp";
57     private static final String JavaDoc READONLY_JSP = "/jsp/jahia/engines/shared/readonly_application_field.jsp";
58     public static final String JavaDoc APPLICATION_ROLES = "applicationRoles";
59
60     /**
61         * AK 19.12.2000
62         *
63      * @return a single instance of the object.
64         */

65     public static synchronized Application_Field getInstance() {
66         if (theObject == null) {
67             theObject = new Application_Field();
68         }
69         return theObject;
70     }
71
72     /***
73         * handles the field actions
74         *
75         * @param jParams a ParamBean object
76      * @param modeInt the mode, according to JahiaEngine
77      * @param engineMap the engine parameters stored in a HashMap
78         * @return true if everything went okay, false if not
79      * @throws JahiaException
80         * @see org.jahia.engines.JahiaEngine
81         *
82         */

83     public boolean handleField( ParamBean jParams, Integer JavaDoc modeInt, HashMap JavaDoc engineMap )
84         throws JahiaException {
85
86         int mode = modeInt.intValue();
87         JahiaField theField = (JahiaField) engineMap.get( "theField" );
88         if ( theField == null ){
89             logger.debug("the field is null");
90         }
91         switch (mode) {
92             case (JahiaEngine.LOAD_MODE):
93                 return composeEngineMap(jParams, engineMap, theField);
94             case (JahiaEngine.UPDATE_MODE):
95                 return getFormData(jParams, engineMap, theField);
96             case (JahiaEngine.SAVE_MODE):
97                 return saveData(jParams, engineMap, theField);
98         }
99         return false;
100     }
101
102     /***
103         * gets POST data from the form and saves it in session
104         *
105         * @param jParams a ParamBean object
106         * @param engineMap the engine hashmap
107         * @param theField the field we are working on
108         * @return true if everything went okay, false if not
109         *
110         */

111     private boolean getFormData( ParamBean jParams, HashMap JavaDoc engineMap, JahiaField theField )
112     {
113         logger.debug("started");
114         String JavaDoc fieldValue = jParams.getRequest().getParameter(
115           "_" + new Integer JavaDoc(theField.getID()).toString() );
116         if (fieldValue != null) {
117             theField.setObject( fieldValue );
118         }
119         Integer JavaDoc oldAppID = (Integer JavaDoc)engineMap.get("appID");
120         if ((oldAppID == null) || (oldAppID.intValue() == -1)) {
121             return true;
122         }
123         ApplicationContext appContext = null;
124         try {
125             appContext = ServicesRegistry.getInstance()
126                                             .getJahiaApplicationContextService()
127                                             .getApplicationContext(oldAppID.
128                     intValue());
129         } catch ( Throwable JavaDoc t ){
130             logger.debug("Cannot access app context",t);
131             // this should'nt abort processing other fields
132
return true;
133         }
134         Vector JavaDoc roles = appContext.getRoles();
135         // Handle roles changes
136
ArrayList JavaDoc roleMembersList = new ArrayList JavaDoc();
137         if (roles != null) {
138             for (int roleNb = 0, size = roles.size(); roleNb < size; roleNb++) {
139                 Set JavaDoc roleMembers = getFormMembers(jParams, roleNb);
140                 roleMembersList.add(roleMembers);
141             }
142         }
143         Map JavaDoc appRoleMembers = new HashMap JavaDoc();
144         appRoleMembers.put(oldAppID, roleMembersList);
145         engineMap.put(APPLICATION_ROLES, appRoleMembers);
146         return true;
147     }
148
149     /***
150      * saves data in datasource
151      *
152      * @param jParams a ParamBean object
153      * @param engineMap the engine hashmap
154      * @param theField the field we are working on
155      * @throws JahiaException
156      * @return true if everything went okay, false if not
157      */

158     private boolean saveData (ParamBean jParams, HashMap JavaDoc engineMap,
159                               JahiaField theField)
160         throws JahiaException {
161
162         logger.debug("started");
163
164         boolean success = true;
165         if (engineMap.get("createApplication_" +
166                           theField.getDefinition().getName()) != null) {
167             theField.setValue( (String JavaDoc) theField.getObject());
168             // 0 for parentAclID in saveField, because field already exists
169
// -> field already has an aclID
170
// -> no need to create a new one
171

172             // get the application's roles list
173
Integer JavaDoc appID = null;
174             try {
175                 appID = new Integer JavaDoc(theField.getValue());
176             } catch (NumberFormatException JavaDoc nfe) {
177                 // invalid app ID, probably because we have no application
178
// installed on this site.
179
return true;
180             }
181             // Try to recover application role members
182
Map JavaDoc appRoleMembers = (Map JavaDoc)engineMap.get(APPLICATION_ROLES);
183             List JavaDoc roleMembersList = (List JavaDoc)appRoleMembers.get(appID);
184             if (roleMembersList == null) {
185                 return false;
186             }
187             success = theField.save(jParams);
188             ApplicationContext appContext = null;
189             try {
190                 appContext = ServicesRegistry.getInstance()
191                         .getJahiaApplicationContextService()
192                         .getApplicationContext(appID.intValue());
193             } catch ( Throwable JavaDoc t ){
194                 // context should exist anymore (deleted), abort save instead of
195
// fail all process
196
return true;
197             }
198             Vector JavaDoc roles = appContext.getRoles();
199             // Handle roles changes
200
if (roles != null) {
201                 JahiaGroupManagerService gMgr = ServicesRegistry.getInstance()
202                     .getJahiaGroupManagerService();
203                 String JavaDoc role = null;
204                 for (int roleNb = 0, size = roles.size(); roleNb < size; roleNb++) {
205                     role = (String JavaDoc) roles.get(roleNb);
206                     String JavaDoc gName = appID + "_" + theField.getID() + "_" + role;
207                     JahiaGroup grp = gMgr.lookupGroup(0, gName);
208                     if (grp == null) {
209                         // create group
210
grp = gMgr.createGroup(0, gName, new Properties JavaDoc());
211                     } else {
212                         Set JavaDoc membersSet = (Set JavaDoc)roleMembersList.get(roleNb);
213                         Enumeration JavaDoc members = grp.members();
214                         Principal JavaDoc p = null;
215                         while (members.hasMoreElements()) {
216                             p = (Principal JavaDoc) members.nextElement();
217                             if (p instanceof JahiaUser) {
218                                 if (!membersSet.contains(p)) {
219                                     logger.debug("removed member=" + p.getName());
220                                     grp.removeMember(p);
221                                 } else {
222                                     membersSet.remove(p);
223                                 }
224                             } else if (p instanceof JahiaGroup) {
225                                 if (!membersSet.contains(p)) {
226                                     logger.debug("removed member=" + p.getName());
227                                     grp.removeMember(p);
228                                 } else {
229                                     membersSet.remove(p);
230                                 }
231                             }
232                         }
233                         Iterator JavaDoc it = membersSet.iterator();
234                         while (it.hasNext()) {
235                             p = (Principal JavaDoc) it.next();
236                             if (p instanceof JahiaUser) {
237                                 grp.addMember(p);
238                             } else if (p instanceof JahiaGroup) {
239                                 grp.addMember(p);
240                             }
241                         }
242                     }
243                 }
244             }
245             Boolean JavaDoc flushApp = Boolean.FALSE;
246             if (engineMap.get("flushApp") != null) {
247                 flushApp = (Boolean JavaDoc) engineMap.get("flushApp");
248                 if (flushApp.booleanValue()) {
249                     HttpSession JavaDoc session = jParams.getRequest().getSession();
250                     JahiaApplicationsDispatchingService dispatcher =
251                             ServicesRegistry.getInstance().
252                             getJahiaApplicationsDispatchingService();
253                     if (dispatcher != null) {
254                         dispatcher.flushAllSessionsCaches(session);
255                     }
256                 }
257             }
258         }
259         engineMap.remove(APPLICATION_ROLES);
260         return success;
261     }
262
263     /***
264         * composes engine hash map
265         *
266         * @param jParams a ParamBean object
267         * @param engineMap the engine hashmap
268         * @param theField the field we are working on
269      * @throws JahiaException
270         * @return true if everything went okay, false if not
271         *
272         */

273     private boolean composeEngineMap( ParamBean jParams, HashMap JavaDoc engineMap, JahiaField theField )
274         throws JahiaException {
275
276         logger.debug("started");
277         // warns that application is being instanciated
278
// cannot use the field id to create a unique enginemap name,
279
// because can field id can change (in addcontainer mode, for example)
280
// so we use the field definition's name to create a unique enginemap name...
281
engineMap.put( "createApplication_" + theField.getDefinition().getName(), Boolean.TRUE );
282
283         // all apps
284
Vector JavaDoc appList = ServicesRegistry.getInstance().getJahiaApplicationsManagerService().getApplications();
285
286         // list of authorized apps
287
Vector JavaDoc authAppList = new Vector JavaDoc();
288         for (int i = 0, size = appList.size(); i < size; i++) {
289             ApplicationBean app = (ApplicationBean)appList.get(i);
290             if (app.getVisibleStatus() == 1 &&
291                 (app.isShared() || app.getJahiaID() == jParams.getSiteID() ||
292                  ServicesRegistry.getInstance().getAppsShareService().getShare(jParams.getSite(), app) != null)) {
293 /* removed by PAP */
294 // if (app.getJahiaID() != jParams.getSiteID()) {
295
// // it's a shared component
296
// JahiaSite ownerSite = ServicesRegistry.getInstance().
297
// getJahiaSitesService().getSite(app.getJahiaID());
298
// if ( ownerSite != null ){
299
// // we clone because we will change the name on the copy
300
// // and don't want to corrupt the cached entry.
301
// app = (ApplicationBean) app.clone();
302
// app.setName(app.getName()+"-["+ownerSite.getServerName()+"]");
303
// }
304
// }
305
authAppList.add(app);
306             }
307         }
308
309         if (authAppList == null) { authAppList = new Vector JavaDoc(); }
310         engineMap.put( "appList", authAppList.elements() );
311
312         Integer JavaDoc appID;
313         try {
314             appID = new Integer JavaDoc( (String JavaDoc) theField.getObject());
315             // now let's test to see if the appID is still valid, because it
316
// might have become invalid after an application has been
317
// undeployed.
318
} catch (NumberFormatException JavaDoc nfe) {
319             appID = new Integer JavaDoc( -1);
320         }
321         ApplicationBean appBean = ServicesRegistry.getInstance().getJahiaApplicationsManagerService().getApplication(appID.intValue());
322         if (appBean == null) {
323             // application has no valid definition.
324
appID = new Integer JavaDoc(-1);
325         }
326         if (appID.intValue() == -1) {
327             // Set a correct default value.
328
if (authAppList.size() > 0) {
329                 appID = new Integer JavaDoc( ( (ApplicationBean) authAppList.get(0)).
330                                     getID());
331             }
332         }
333         engineMap.put( "appID", appID );
334         Vector JavaDoc roles = new Vector JavaDoc();
335         if (appID.intValue() >= 0) {
336             try {
337                 ApplicationContext appContext = ServicesRegistry.getInstance().
338                                                 getJahiaApplicationContextService().
339                                                 getApplicationContext(appID.
340                         intValue());
341                 roles = appContext.getRoles();
342             } catch ( Throwable JavaDoc t ){
343                 logger.debug("Error retrieving application context appID[" + appID + "]");
344             }
345         }
346         engineMap.put( "roles", roles );
347
348         // Get role members for each web apps
349
Map JavaDoc appRoleMembers = (Map JavaDoc)engineMap.get(APPLICATION_ROLES);
350         if (appRoleMembers == null) {
351             appRoleMembers = new HashMap JavaDoc();
352         }
353         List JavaDoc roleMembersList = (List JavaDoc)appRoleMembers.get(appID);
354         if (roleMembersList == null) {
355             roleMembersList = new ArrayList JavaDoc();
356             // Fill role members in the array list.
357
JahiaGroupManagerService gMgr = ServicesRegistry.getInstance()
358           .getJahiaGroupManagerService();
359         for (int i = 0, size = roles.size(); i < size; i++) {
360             String JavaDoc role = (String JavaDoc)roles.get(i);
361             JahiaGroup grp = gMgr.lookupGroup(0,
362                 appID + "_" + theField.getID() + "_" + role);
363             if (grp != null)
364                 roleMembersList.add(getAppMembers(grp));
365             else
366                 roleMembersList.add(new HashSet JavaDoc());
367             }
368             appRoleMembers.put(appID, roleMembersList);
369         }
370         engineMap.put(APPLICATION_ROLES, appRoleMembers);
371         engineMap.put("selectUsrGrp",
372                       SelectUG_Engine.getInstance().renderLink(jParams, ""));
373
374         /* Hollis
375          * Why request application output when simply editing the field properties ?
376          * Doing so , we sometimes expect with an out of memory range exception ( random error )
377          * So I comment it .
378          */

379         //theField.setValue( FormDataManager.getInstance().formDecode(theField.getValue()) );
380
//theField.setValue( FormDataManager.getInstance().decode(theField.getValue()) );
381

382         boolean editable = false;
383         JahiaContainer theContainer = (JahiaContainer)engineMap.get("theContainer");
384         if (theContainer == null) {
385             // in case of a field , not a field in a container
386
editable = true;
387         } else {
388             HashMap JavaDoc ctnListFieldAcls = (HashMap JavaDoc) engineMap
389                     .get("ctnListFieldAcls");
390
391             if (theContainer.getListID() != 0 && ctnListFieldAcls != null
392                     && ctnListFieldAcls.size() > 0) {
393                 JahiaBaseACL acl = JahiaEngineTools.getCtnListFieldACL(
394                         ctnListFieldAcls, theField.getID());
395                 if (acl != null) {
396                     editable = acl.getPermission(jParams.getUser(),
397                             JahiaBaseACL.WRITE_RIGHTS, true);
398                 }
399             } else {
400                 editable = true;
401             }
402         }
403         String JavaDoc output = "";
404         if ( editable ){
405             output = ServicesRegistry.getInstance().getJahiaFetcherService().fetchServlet( jParams, JSP_FILE );
406         } else {
407             output = ServicesRegistry.getInstance().getJahiaFetcherService().fetchServlet( jParams, READONLY_JSP );
408         }
409         engineMap.put( "fieldForm", output );
410         return true;
411     }
412
413     private HashSet JavaDoc getFormMembers(ParamBean jParams, int roleNb) {
414         String JavaDoc[] authMembersStr = jParams.getRequest().getParameterValues("authMembers" + roleNb);
415         HashSet JavaDoc membersSet = new HashSet JavaDoc();
416         if (authMembersStr != null) {
417             for (int i = 0; i < authMembersStr.length; i++) {
418                 if (authMembersStr[i].charAt(0) == 'u') {
419                     JahiaUser user = ServicesRegistry.getInstance().
420                                      getJahiaUserManagerService().
421                                      lookupUser(authMembersStr[i].substring(1));
422                     membersSet.add(user);
423                 } else {
424                     JahiaGroup group = ServicesRegistry.getInstance().
425                                        getJahiaGroupManagerService().
426                                        lookupGroup(authMembersStr[i].substring(1));
427                     membersSet.add(group);
428                 }
429             }
430         }
431         return membersSet;
432     }
433
434     private HashSet JavaDoc getAppMembers(JahiaGroup grp) {
435         HashSet JavaDoc membersSet = new HashSet JavaDoc();
436         Enumeration JavaDoc enumeration = grp.members();
437         while (enumeration.hasMoreElements()) {
438             membersSet.add(enumeration.nextElement());
439         }
440         return membersSet;
441     }
442 }
443
Popular Tags