KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > engines > updatecontainer > UpdateContainer_Engine


1 // $Id: UpdateContainer_Engine.java 15996 2006-12-13 20:50:24Z bpapez $
2
//
3
//
4
// ____.
5
// __/\ ______| |__/\. _______
6
// __ .____| | \ | +----+ \
7
// _______| /--| | | - \ _ | : - \_________
8
// \\______: :---| : : | : | \________>
9
// |__\---\_____________:______: :____|____:_____\
10
// /_____|
11
//
12
// . . . i n j a h i a w e t r u s t . . .
13
//
14

15 //
16
// EV 10.01.20001
17
// NK 06.02.2002 Added Multiple fields edit at once support
18
// NK 05.02.2002 Version 2.0 Multiple field edit + customisable field ( read/write permission check )
19
//
20

21 package org.jahia.engines.updatecontainer;
22
23 import java.util.ArrayList JavaDoc;
24 import java.util.Collections JavaDoc;
25 import java.util.Enumeration JavaDoc;
26 import java.util.HashMap JavaDoc;
27 import java.util.HashSet JavaDoc;
28 import java.util.Hashtable JavaDoc;
29 import java.util.Iterator JavaDoc;
30 import java.util.Locale JavaDoc;
31 import java.util.Properties JavaDoc;
32 import java.util.Set JavaDoc;
33 import java.util.Vector JavaDoc;
34
35 import javax.servlet.http.HttpSession JavaDoc;
36
37 import org.jahia.bin.Jahia;
38 import org.jahia.content.ContentContainerKey;
39 import org.jahia.content.ContentContainerListsXRefManager;
40 import org.jahia.data.JahiaData;
41 import org.jahia.data.containers.ContainerEditView;
42 import org.jahia.data.containers.ContainerEditViewFieldGroup;
43 import org.jahia.data.containers.JahiaContainer;
44 import org.jahia.data.containers.JahiaContainerList;
45 import org.jahia.data.containers.JahiaContentContainerFacade;
46 import org.jahia.data.events.JahiaEvent;
47 import org.jahia.data.fields.JahiaContentFieldFacade;
48 import org.jahia.data.fields.JahiaField;
49 import org.jahia.data.fields.LoadFlags;
50 import org.jahia.engines.EngineToolBox;
51 import org.jahia.engines.JahiaEngine;
52 import org.jahia.engines.addcontainer.AddContainer_Engine;
53 import org.jahia.engines.categories.ManageCategories;
54 import org.jahia.engines.lock.LockEngine;
55 import org.jahia.engines.rights.ManageRights;
56 import org.jahia.engines.validation.EngineValidationHelper;
57 import org.jahia.engines.validation.ValidationError;
58 import org.jahia.engines.validation.ValidationErrorSorter;
59 import org.jahia.exceptions.JahiaException;
60 import org.jahia.exceptions.JahiaForbiddenAccessException;
61 import org.jahia.exceptions.JahiaSessionExpirationException;
62 import org.jahia.exceptions.JahiaUpdateLockException;
63 import org.jahia.params.ParamBean;
64 import org.jahia.registries.ServicesRegistry;
65 import org.jahia.services.audit.LoggingEventListener;
66 import org.jahia.services.cache.CacheFactory;
67 import org.jahia.services.cache.HtmlCache;
68 import org.jahia.services.containers.ContentContainer;
69 import org.jahia.services.fields.ContentField;
70 import org.jahia.services.lock.LockKey;
71 import org.jahia.services.lock.LockService;
72 import org.jahia.services.usermanager.JahiaUser;
73 import org.jahia.services.version.EntryLoadRequest;
74 import org.jahia.utils.LanguageCodeConverters;
75
76 /**
77  * Display the popup that let the user update a container.
78  *
79  * @author EV
80  * @author NK
81  * @version 2.0
82  */

83 public class UpdateContainer_Engine implements JahiaEngine {
84
85     /** logging */
86     private static final org.apache.log4j.Logger logger =
87             org.apache.log4j.Logger.getLogger (UpdateContainer_Engine.class);
88
89     /** unique instance of this class */
90     private static UpdateContainer_Engine instance = null;
91
92     private static final String JavaDoc TEMPLATE_JSP = "update_container";
93
94     public static final String JavaDoc ENGINE_NAME = "updatecontainer";
95     private EngineToolBox toolBox;
96
97     /**
98      * Default constructor, creates a new <code>UpdateContainer_Engine</code> instance.
99      */

100     private UpdateContainer_Engine () {
101         logger.debug ("***** Starting " + UpdateContainer_Engine.class.getName () + " engine *****");
102         toolBox = EngineToolBox.getInstance ();
103     }
104
105     /**
106      * Returns the unique instance of this class
107      *
108      * @return the unique instance of this class
109      */

110     public static UpdateContainer_Engine getInstance () {
111         if (instance == null) {
112             instance = new UpdateContainer_Engine ();
113         }
114         return instance;
115     }
116
117     /**
118      * authoriseRender
119      */

120     public boolean authoriseRender (ParamBean jParams) {
121         return toolBox.authoriseRender (jParams);
122     } // end authoriseRender
123

124     /**
125      * renderLink
126      */

127     public String JavaDoc renderLink (ParamBean jParams, Object JavaDoc theObj)
128             throws JahiaException {
129         ContentContainer contentContainer = (ContentContainer) theObj;
130         String JavaDoc params = "?mode=display&cid=" + contentContainer.getID ();
131         String JavaDoc useOriginPage = jParams.getParameter("use_container_origin_page");
132         if (useOriginPage != null && (new Boolean JavaDoc(useOriginPage)).booleanValue()){
133             Properties JavaDoc extraParams = new Properties JavaDoc();
134             extraParams.put(ParamBean.PAGE_ID_PARAMETER, Integer.toString(contentContainer.getPageID()));
135             return jParams.composeEngineUrl (ENGINE_NAME, extraParams, params);
136         }else{
137             return jParams.composeEngineUrl (ENGINE_NAME, params);
138         }
139     } // end renderLink
140

141     /**
142      * needsJahiaData
143      */

144     public boolean needsJahiaData (ParamBean jParams) {
145         return false;
146     } // end needsJahiaData
147

148     /**
149      * handles the engine actions
150      *
151      * @param jParams a ParamBean object
152      * @param jData a JahiaData object (not mandatory)
153      */

154     public void handleActions (ParamBean jParams, JahiaData jData)
155             throws JahiaException,
156             JahiaUpdateLockException,
157             JahiaForbiddenAccessException {
158         // initalizes the hashmap
159
HashMap JavaDoc engineMap = initEngineMap (jParams);
160
161         // init the flag
162
engineMap.put("org.jahia.engines.fieldEdit.flagStayOnSameField",Boolean.FALSE);
163
164         // get the screen
165
String JavaDoc theScreen = (String JavaDoc) engineMap.get ("screen");
166
167         // checks if the user has the right to display the engine
168
JahiaContainer theContainer = (JahiaContainer) engineMap.get (
169                 "theContainer");
170         JahiaUser theUser = jParams.getUser ();
171
172         // if the user has admin rights give him automaticaly also the write
173
// rights in order to avoid two recursions in the rights checking.
174
if (theContainer.checkAdminAccess (theUser)) {
175             engineMap.put ("adminAccess", Boolean.TRUE);
176             engineMap.put ("enableRightView", Boolean.TRUE);
177             engineMap.put ("writeAccess", Boolean.TRUE);
178
179         } else if (theContainer.checkWriteAccess (theUser)) {
180             engineMap.put ("writeAccess", Boolean.TRUE);
181         }
182
183         if (engineMap.get ("writeAccess") != null) {
184             // #ifdef LOCK
185
LockService lockRegistry = ServicesRegistry.getInstance ().
186                     getLockService ();
187             if (jParams.settings ().areLocksActivated ()) {
188                 LockKey lockKey = LockKey.composeLockKey (LockKey.
189                         UPDATE_CONTAINER_TYPE,
190                         theContainer.getID (), theContainer.getPageID ());
191                 if (lockRegistry.acquire (lockKey, jParams.getUser (),
192                         jParams.getSessionID (),
193                         jParams.getSession ().
194                         getMaxInactiveInterval ())) {
195                     // #endif
196
flushPageCacheThatDisplayContainer (jParams, theContainer);
197
198                     // fire event
199
JahiaEvent theEvent = new JahiaEvent (this, jParams,
200                             theContainer);
201                     ServicesRegistry.getInstance ().getJahiaEventService ()
202                             .fireUpdateContainerEngineAfterInit (theEvent);
203                     // end fire event
204

205                     if (!theScreen.equals ("cancel")) {
206                         processLastScreen (jParams, engineMap);
207                     }
208                     processCurrentScreen (jParams, engineMap);
209                     // #ifdef LOCK
210
} else {
211                     // Prerequisites are NOT completed ! Damned ! Redirect the JSP
212
// output to lock informations.
213
LockEngine.getInstance ().redirect (jParams, engineMap,lockKey);
214                     return; // we must abort before the second displayScreen
215
}
216             }
217             // #endif
218
} else {
219             throw new JahiaForbiddenAccessException ();
220         }
221
222         // displays the screen
223
toolBox.displayScreen (jParams, engineMap);
224
225     }
226
227
228     /**
229      * Retrieve the engine name.
230      *
231      * @return the engine name.
232      */

233     public final String JavaDoc getName () {
234         return ENGINE_NAME;
235     }
236
237     /**
238      * processes the last screen sent by the user
239      *
240      * @param jParams a ParamBean object
241      */

242     public void processLastScreen (ParamBean jParams, HashMap JavaDoc engineMap)
243             throws JahiaException, JahiaUpdateLockException,
244             JahiaForbiddenAccessException {
245         // logger.debug ("started");
246

247         // gets the last screen
248
// lastscreen = edit, rights, logs
249
String JavaDoc lastScreen = jParams.getRequest ().getParameter ("lastscreen");
250         if (lastScreen == null) {
251             //lastScreen = "edit";
252
lastScreen = EMPTY_STRING;
253         }
254         // indicates to sub engines that we are processing last screen
255
int mode = JahiaEngine.UPDATE_MODE;
256
257         String JavaDoc lastFieldIDStr = jParams.getRequest ().getParameter ("lastfid");
258
259         JahiaContentContainerFacade jahiaContentContainerFacade
260                 = (JahiaContentContainerFacade) engineMap.get (
261                         "UpdateContainer_Engine.JahiaContentContainerFacade");
262
263         if (lastScreen.equals ("edit")) {
264
265             int lastFieldID = -1;
266             try {
267                 lastFieldID = Integer.parseInt (lastFieldIDStr);
268             } catch (NumberFormatException JavaDoc nfe) {
269                 return;
270             }
271
272             Boolean JavaDoc flagStayOnSameField = (Boolean JavaDoc)
273                 engineMap.get("org.jahia.engines.fieldEdit.flagStayOnSameField");
274
275             AddContainer_Engine.processLastFields (lastFieldID, jParams,
276                     engineMap, mode, lastScreen,
277                     this.toolBox,
278                     (flagStayOnSameField != null && flagStayOnSameField.booleanValue()) );
279
280         } else if (lastScreen.equals ("versioning")) {
281             /*
282                          if (engineMap.get("adminAccess") != null) {
283                 VersioningEngine.getInstance().handleAction( jParams, mode, engineMap, JahiaObjectTool.CONTAINER_TYPE, theContainer );
284                          } else {
285                 throw new JahiaForbiddenAccessException();
286                          }
287              */

288         } else if (lastScreen.equals ("rightsMgmt")) {
289             if (engineMap.get ("adminAccess") != null) {
290                 ManageRights.getInstance ().handleActions (jParams, mode,
291                         engineMap, jahiaContentContainerFacade.getAclID ());
292             } else {
293                 throw new JahiaForbiddenAccessException ();
294             }
295         } else if (lastScreen.equals ("categories")) {
296             JahiaContainer theContainer = (JahiaContainer) engineMap.get (
297                     "theContainer");
298             ManageCategories.getInstance ().handleActions (jParams, mode,
299                     engineMap,
300                     new ContentContainerKey (jahiaContentContainerFacade.getContainerID ()),
301                     theContainer.getDefinition (), false);
302         }
303     } // end processLastScreen
304

305     /**
306      * prepares the screen requested by the user
307      *
308      * @param jParams a ParamBean object
309      */

310     public EngineValidationHelper processCurrentScreen (ParamBean jParams, HashMap JavaDoc engineMap)
311             throws JahiaException,
312             JahiaUpdateLockException,
313             JahiaForbiddenAccessException {
314         // logger.debug ("started");
315

316         JahiaContentContainerFacade jahiaContentContainerFacade
317                 = (JahiaContentContainerFacade) engineMap.get (
318                         "UpdateContainer_Engine.JahiaContentContainerFacade");
319
320         EntryLoadRequest entryLoadRequest
321                 = (EntryLoadRequest) engineMap.get ("entryLoadRequest");
322
323         HashSet JavaDoc updatedLanguageEntries = (HashSet JavaDoc) engineMap.get (
324                 "updatedLanguageEntries");
325         HashSet JavaDoc updatedFields = (HashSet JavaDoc) engineMap.get ("updated.fields");
326
327         JahiaField theField = null;
328         JahiaContainer theContainer = jahiaContentContainerFacade.getContainer (
329                 entryLoadRequest, true);
330         engineMap.put ("theContainer", theContainer);
331
332         // gets the current screen
333
// screen = edit, rights, logs
334
String JavaDoc theScreen = (String JavaDoc) engineMap.get ("screen");
335
336         // indicates to sub engines that we are processing last screen
337
int mode = JahiaEngine.LOAD_MODE;
338
339         Boolean JavaDoc flagStayOnSameField = (Boolean JavaDoc)
340             engineMap.get("org.jahia.engines.fieldEdit.flagStayOnSameField");
341
342         String JavaDoc fieldIDStr;
343         if (flagStayOnSameField != null && flagStayOnSameField.booleanValue()) {
344             fieldIDStr = jParams.getRequest ().getParameter ("lastfid");
345         } else {
346             fieldIDStr = jParams.getRequest ().getParameter ("fid");
347         }
348         int fieldID = -1;
349
350         try {
351             fieldID = Integer.parseInt (fieldIDStr);
352         } catch (NumberFormatException JavaDoc nfe) {
353             try {
354                 fieldID = (((Integer JavaDoc) engineMap.get ("field_id"))).intValue ();
355             } catch (Throwable JavaDoc t) {
356                 fieldID = -1;
357             }
358         }
359
360         ContainerEditView editView = (ContainerEditView) engineMap.get (
361                 "ctnEditView");
362         ContainerEditViewFieldGroup fieldGroup = null;
363
364         if (fieldID == -1) {
365             // get first visible field
366
String JavaDoc fieldName = (String JavaDoc) editView.getFields ().get (0);
367             theField = theContainer.getField (fieldName);
368             if (theField != null) {
369                 fieldID = theField.getID ();
370             }
371         } else {
372             theField = theContainer.getField (fieldID);
373         }
374         if (theField != null) {
375             fieldGroup = editView.getFieldGroupByFieldName (theField.
376                     getDefinition ().getName ());
377         }
378         if (fieldGroup == null) {
379             Vector JavaDoc views = new Vector JavaDoc (editView.getViews ().values ());
380             if (views.size () > 0) {
381                 fieldGroup = (ContainerEditViewFieldGroup) views.get (0);
382                 theField = theContainer.getField ((String JavaDoc) fieldGroup.
383                         getFieldNames ().get (0));
384                 fieldID = theField.getID ();
385             } else {
386                 // no one field visible
387
theField = null;
388                 fieldID = 0;
389             }
390         }
391
392         engineMap.put ("field_id", new Integer JavaDoc (fieldID));
393
394         // #ifdef LOCK
395
LockKey lockKey = LockKey.composeLockKey (LockKey.UPDATE_CONTAINER_TYPE,
396                 theContainer.getID (),
397                 theContainer.getPageID ());
398         LockService lockRegistry = ServicesRegistry.getInstance ().
399                 getLockService ();
400         // #endif
401

402         if (theScreen.equals ("edit")) {
403             AddContainer_Engine.processCurrentFields (fieldID, jParams,
404                     engineMap, mode, theScreen, this.toolBox);
405
406         } else if (theScreen.equals ("versioning")) {
407             /*
408                          if (engineMap.get("adminAccess") != null) {
409                 VersioningEngine.getInstance().handleAction( jParams, mode, engineMap, JahiaObjectTool.CONTAINER_TYPE, theContainer );
410                          } else {
411                 throw new JahiaForbiddenAccessException();
412                          }*/

413         } else if (theScreen.equals ("logs")) {
414             toolBox.loadLogData (jParams, LoggingEventListener.CONTAINER_TYPE,
415                     engineMap);
416         } else if (theScreen.equals ("rightsMgmt")) {
417             if (engineMap.get ("adminAccess") != null) {
418                 ManageRights.getInstance ().handleActions (jParams, mode,
419                         engineMap, theContainer.getAclID ());
420             } else {
421                 throw new JahiaForbiddenAccessException ();
422             }
423         } else if (theScreen.equals ("categories")) {
424             ManageCategories.getInstance ().handleActions (jParams, mode,
425                     engineMap,
426                     new ContentContainerKey (jahiaContentContainerFacade.getContainerID ()),
427                     theContainer.getDefinition (), false);
428         } else if (theScreen.equals ("save") || theScreen.equals ("apply")) {
429
430             // #ifdef LOCK
431
// Did somebody steal the lock ? Panpan cucul !
432
if (jParams.settings ().areLocksActivated () &&
433                     lockRegistry.isStealedInContext (lockKey, jParams.getUser (),
434                             jParams.getSessionID ())) {
435                 engineMap.put ("screen",
436                         jParams.getRequest ().getParameter ("lastscreen"));
437                 engineMap.put ("jspSource", "apply");
438                 return null;
439             }
440             // #endif
441

442             logger.debug(
443                 "processCurrentScreen > we are in validate Mode");
444             // before saving container, call validation on subengines.
445
// switch to validate mode
446
mode = JahiaEngine.VALIDATE_MODE;
447
448             final EngineValidationHelper evh =
449                 AddContainer_Engine.validate(
450                     UpdateContainer_Engine.ENGINE_NAME,
451                     jahiaContentContainerFacade,
452                     jParams,
453                     engineMap);
454             
455             engineMap.put(JahiaEngine.ENGINE_VALIDATION_HELPER, evh);
456             final JahiaEvent validationEvent = new JahiaEvent (this, jParams, theContainer);
457             ServicesRegistry.getInstance ().getJahiaEventService ()
458                     .fireContainerValidation(validationEvent);
459
460             if (evh != null && evh.hasErrors()) {
461
462                 final ContainerEditViewFieldGroup currentFieldGroup =
463                     AddContainer_Engine.getFieldGroup(
464                         fieldID,
465                         theContainer,
466                         (ContainerEditView)engineMap.get(
467                             "ctnEditView"));
468
469                 Collections.sort(
470                     evh.getErrors(),
471                     new ValidationErrorSorter(currentFieldGroup));
472
473                 final ValidationError ve =
474                     (ValidationError)evh.getErrors().get(0);
475                 final JahiaField field = (JahiaField)ve.getSource();
476
477                 AddContainer_Engine.processCurrentFields(
478                     field.getID(),
479                     jParams,
480                     engineMap,
481                     JahiaEngine.LOAD_MODE,
482                     "edit",
483                     this.toolBox);
484
485                 // prepare view
486
engineMap.put(
487                     JahiaEngine.ENGINE_VALIDATION_HELPER,
488                     evh);
489                 engineMap.put("screen", "edit");
490                 engineMap.put("jspSource", TEMPLATE_JSP);
491                 return evh;
492             }
493             engineMap.remove(JahiaEngine.ENGINE_VALIDATION_HELPER);
494             
495             mode = JahiaEngine.SAVE_MODE;
496
497             // fire event
498
JahiaEvent theEvent = new JahiaEvent (this, jParams, theContainer);
499             ServicesRegistry.getInstance ().getJahiaEventService ()
500                     .fireUpdateContainerEngineBeforeSave (theEvent);
501             // end fire event
502

503             // save the container info
504

505             EntryLoadRequest loadVersion = EntryLoadRequest.CURRENT;
506             if (ServicesRegistry.getInstance ().getJahiaVersionService ().
507                     isStagingEnabled (theContainer.getJahiaID ()))
508                 loadVersion = EntryLoadRequest.STAGED;
509
510             JahiaContainerList theList = ServicesRegistry.getInstance ().
511                     getJahiaContainersService ().
512                     loadContainerListInfo (theContainer.
513                     getListID (), loadVersion);
514             // 0 for parentAclID in saveContainerInfo, because container already exists
515
// -> container already has an aclID
516
// -> no need to create a new one
517
ServicesRegistry.getInstance ().getJahiaContainersService ().
518                     saveContainerInfo (theContainer, theList.getParentEntryID (), 0,
519                             jParams);
520
521             Enumeration JavaDoc enumeration = jahiaContentContainerFacade.getFields ();
522             while (enumeration.hasMoreElements ()) {
523                 JahiaContentFieldFacade contentFieldFacade = (
524                         JahiaContentFieldFacade) enumeration.nextElement ();
525                 Enumeration JavaDoc fields = contentFieldFacade.getFields ();
526                 EntryLoadRequest processingEntryLoadRequest = null;
527                 while (fields.hasMoreElements ()) {
528                     JahiaField field = (JahiaField) fields.nextElement ();
529                     if (field.hasChanged () &&
530                             (field.getLanguageCode ().equals (ContentField.
531                             SHARED_LANGUAGE)
532                             ||
533                             updatedLanguageEntries.contains (field.getLanguageCode ()))
534                             || updatedFields.contains (new Integer JavaDoc (field.getID ()))) {
535                         // save the active entry only if the staging doesn't exists.
536
boolean processField = true;
537                         processingEntryLoadRequest =
538                                 new EntryLoadRequest (field.getWorkflowState (),
539                                         field.getVersionID (),
540                                         new ArrayList JavaDoc ());
541                         processingEntryLoadRequest.getLocales ()
542                                 .add (LanguageCodeConverters.languageCodeToLocale (
543                                         field.getLanguageCode ()));
544                         if (field.getWorkflowState () ==
545                                 EntryLoadRequest.ACTIVE_WORKFLOW_STATE) {
546                             ArrayList JavaDoc entryLocales = new ArrayList JavaDoc ();
547                             entryLocales.add (LanguageCodeConverters
548                                     .languageCodeToLocale (field.
549                                     getLanguageCode ()));
550                             EntryLoadRequest stagingLoadRequest =
551                                     new EntryLoadRequest (EntryLoadRequest.
552                                     STAGING_WORKFLOW_STATE,
553                                             0, entryLocales);
554
555                             processField = (contentFieldFacade.getField (
556                                     stagingLoadRequest, false) == null);
557                             if (processField) {
558                                 processingEntryLoadRequest = stagingLoadRequest;
559                             }
560                         }
561                         if (processField) {
562                             engineMap.put ("theField", field);
563                             jParams.setSubstituteEntryLoadRequest (
564                                     processingEntryLoadRequest);
565                             toolBox.processFieldTypes (jParams, mode, engineMap);
566                             jParams.resetSubstituteEntryLoadRequest ();
567                         }
568                     }
569                 }
570             }
571
572             // save rights
573
if (engineMap.get ("adminAccess") != null) {
574                 engineMap.put ("logObjectType",
575                         Integer.toString (LoggingEventListener.
576                         CONTAINER_TYPE));
577                 engineMap.put ("logObject", theContainer);
578                 //ViewRights.getInstance().handleActions( jParams, mode, engineMap, theContainer.getAclID() );
579
ManageRights.getInstance ().handleActions (jParams, mode,
580                         engineMap, theContainer.getAclID ());
581             }
582
583             // save categories
584
ManageCategories.getInstance ().handleActions (jParams, mode,
585                     engineMap,
586                     new ContentContainerKey (jahiaContentContainerFacade.
587                     getContainerID ()),
588                     theContainer.getDefinition (), false);
589
590             flushPageCacheThatDisplayContainer (jParams, theContainer);
591
592             // fire event
593
theEvent = new JahiaEvent (this, jParams, theContainer);
594             ServicesRegistry.getInstance ().getJahiaEventService ().
595                     fireUpdateContainer (theEvent);
596             // flag for subEngine: means that is a call from updateContainer, reset the flag
597
jParams.getSession ().setAttribute ("UpdateContainer", "false");
598             if (theScreen.equals ("apply")) {
599                 engineMap.put ("prevScreenIsApply", new Boolean JavaDoc (true));
600                 String JavaDoc lastScreen = jParams.getRequest ().getParameter (
601                         "lastscreen");
602                 engineMap.put ("screen", lastScreen);
603             }
604             // #ifdef LOCK
605
else {
606                 if (jParams.settings ().areLocksActivated ()) {
607                     lockRegistry.release (lockKey, jParams.getUser (),
608                             jParams.getSessionID ());
609                 }
610             }
611             // #endif
612
logger.debug ("Changes applied and saved !");
613         } else if (theScreen.equals ("cancel")) {
614             // #ifdef LOCK
615
if (jParams.settings ().areLocksActivated ()) {
616                 lockRegistry.release (lockKey, jParams.getUser (),
617                         jParams.getSessionID ());
618                 flushPageCacheThatDisplayContainer (jParams, theContainer);
619             }
620             // #endif
621
// flag for subEngine: means that is a call from updateContainer, reset the flag
622
jParams.getSession ().setAttribute ("UpdateContainer", "false");
623
624         }
625         return null;
626     } // end processCurrentScreen
627

628     private void flushPageCacheThatDisplayContainer (ParamBean jParams,
629                                                      JahiaContainer theContainer)
630             throws JahiaException {
631
632         EntryLoadRequest loadVersion = EntryLoadRequest.CURRENT;
633         if (ServicesRegistry.getInstance ().getJahiaVersionService ().
634                 isStagingEnabled (theContainer.getJahiaID ()))
635             loadVersion = EntryLoadRequest.STAGED;
636
637         JahiaContainerList theList = ServicesRegistry.getInstance ().
638                 getJahiaContainersService ().
639                 loadContainerListInfo (theContainer.
640                 getListID (), loadVersion);
641         // since we have made modifications concerning this page, let's flush
642
// the content cache for all the users and browsers as well as all
643
// pages that display this containerList...
644
Set JavaDoc containerPageRefs = ContentContainerListsXRefManager.
645                 getInstance ().
646                 getAbsoluteContainerListPageIDs (theList.
647                 getID ());
648
649         // Get the cache instance and invalidate the related page entries
650
HtmlCache htmlCache = CacheFactory.getHtmlCache ();
651         if (htmlCache == null)
652             logger.warn ("Could not get the HTML cache instance!!");
653
654         if (containerPageRefs != null) {
655             Iterator JavaDoc pageRefIDs = containerPageRefs.iterator ();
656             while (pageRefIDs.hasNext ()) {
657                 Integer JavaDoc curPageID = (Integer JavaDoc) pageRefIDs.next ();
658                 if (htmlCache != null)
659                     htmlCache.invalidatePageEntries (curPageID.toString (),jParams.getEntryLoadRequest().getWorkflowState());
660             }
661         } else {
662             logger.debug ("Why is cross ref list empty ?");
663         }
664         // this is still necessary in the case we are on the page itself
665
// and the containerList is not referenced in an absolute way.
666
if (htmlCache != null)
667             htmlCache.invalidatePageEntries (Integer.toString (jParams.getPageID ()),
668                                              jParams.getEntryLoadRequest().getWorkflowState());
669     }
670
671     /**
672      * inits the engine map
673      *
674      * @param jParams a ParamBean object (with request and response)
675      *
676      * @return a HashMap object containing all the basic values needed by an engine
677      */

678     private HashMap JavaDoc initEngineMap (ParamBean jParams)
679             throws JahiaException,
680             JahiaSessionExpirationException {
681         HashMap JavaDoc engineMap = new HashMap JavaDoc ();
682
683         logger.debug ("Start initEngineMap");
684
685         // flag for subEngine: means that is a call from updateContainer
686
jParams.getSession ().setAttribute ("UpdateContainer", "true");
687
688         JahiaContainer theContainer = null;
689         String JavaDoc theScreen = jParams.getRequest ().getParameter ("screen");
690         String JavaDoc ctnidStr = jParams.getRequest ().getParameter ("cid");
691         int ctnid = 0;
692
693         try {
694             ctnid = Integer.parseInt (ctnidStr);
695         } catch (NumberFormatException JavaDoc nfe) {
696             String JavaDoc errorMsg = "Error in parameters : cid (" + ctnidStr +
697                     ") cannot be converted in int";
698             throw new JahiaException ("Error in parameters", errorMsg,
699                     JahiaException.DATA_ERROR,
700                     JahiaException.CRITICAL_SEVERITY);
701         }
702
703         // gets session values
704
//HttpSession theSession = jParams.getRequest().getSession( true );
705
HttpSession JavaDoc theSession = jParams.getSession ();
706         engineMap = (HashMap JavaDoc) theSession.getAttribute ("jahia_session_engineMap");
707         Integer JavaDoc selectedFieldID = null;
708         boolean prevScreenIsApply = false;
709
710         if (engineMap != null && theScreen != null) {
711             Boolean JavaDoc prevScreenIsApplyBool = (Boolean JavaDoc) engineMap.get (
712                     "prevScreenIsApply");
713             prevScreenIsApply = (prevScreenIsApplyBool != null &&
714                     prevScreenIsApplyBool.booleanValue ());
715             selectedFieldID = (Integer JavaDoc) engineMap.get ("field_id");
716         }
717
718         // Resolve language code
719
String JavaDoc languageCode = jParams.getRequest ().getParameter ("engine_lang");
720         if (prevScreenIsApply) {
721             languageCode = (String JavaDoc) engineMap.get ("languageCode");
722         }
723         if (languageCode == null) {
724             Locale JavaDoc navLocale = (Locale JavaDoc) theSession.getAttribute (ParamBean.
725                     SESSION_LOCALE);
726             languageCode = navLocale.toString ();
727         }
728         if (languageCode == null) {
729             languageCode = jParams.settings ().getDefaultLanguageCode ();
730         }
731         String JavaDoc prevLanguageCode = jParams.getRequest ().getParameter (
732                 "prev_engine_lang");
733         if (prevLanguageCode == null) {
734             prevLanguageCode = languageCode;
735         }
736
737         ArrayList JavaDoc locales = new ArrayList JavaDoc ();
738         locales.add (LanguageCodeConverters.languageCodeToLocale (languageCode));
739         EntryLoadRequest entryLoadRequest =
740                 new EntryLoadRequest (EntryLoadRequest.STAGING_WORKFLOW_STATE,
741                         0,
742                         locales);
743
744         ArrayList JavaDoc prevLocales = new ArrayList JavaDoc ();
745         prevLocales.add (LanguageCodeConverters.languageCodeToLocale (
746                 prevLanguageCode));
747         EntryLoadRequest prevEntryLoadRequest =
748                 new EntryLoadRequest (EntryLoadRequest.STAGING_WORKFLOW_STATE,
749                         0,
750                         prevLocales);
751
752         JahiaContentContainerFacade jahiaContentContainerFacade = null;
753
754         if (theScreen != null) {
755
756             logger.debug ("The Screen is not null, load it from session");
757
758             // if no, load the container value from the session
759
engineMap = (HashMap JavaDoc) theSession.getAttribute (
760                     "jahia_session_engineMap");
761
762             ///////////////////////////////////////////////////////////////////////////////////////
763
// FIXME -Fulco-
764
//
765
// This is a quick hack, engineMap should not be null if the session didn't
766
// expired. Maybe there are other cases where the engineMap can be null, I didn't
767
// checked them at all.
768
///////////////////////////////////////////////////////////////////////////////////////
769
if (engineMap == null) {
770                 throw new JahiaSessionExpirationException ();
771             }
772
773             jahiaContentContainerFacade = (JahiaContentContainerFacade)
774                     engineMap.get (
775                             "UpdateContainer_Engine.JahiaContentContainerFacade");
776
777             if (prevScreenIsApply && theScreen.equals ("edit")) {
778                 // reinit jahiaContentContainerFacade
779
ArrayList JavaDoc localeList = jParams.getSite ().
780                         getLanguageSettingsAsLocales (false);
781
782                 jahiaContentContainerFacade
783                         = new JahiaContentContainerFacade (ctnid,
784                                 LoadFlags.ALL,
785                                 jParams,
786                                 localeList,
787                                 true);
788
789                 engineMap.put (
790                         "UpdateContainer_Engine.JahiaContentContainerFacade",
791                         jahiaContentContainerFacade);
792
793                 // reset session
794
engineMap.remove ("updatedLanguageEntries");
795                 engineMap.remove ("updatedLanguageEntries");
796                 engineMap.remove ("updated.fields");
797                 engineMap.remove ("prevScreenIsApply");
798
799                 org.jahia.engines.shared.Page_Field.resetPageBeanSession (
800                         jParams);
801
802             }
803
804         } else {
805
806             logger.debug ("the Screen is null load from storage");
807
808             // init engine map
809
engineMap = new HashMap JavaDoc ();
810
811             // apply check container structure
812
//
813
/*
814                          theContainer = ServicesRegistry.getInstance()
815                          .getJahiaContainersService()
816                          .loadContainer(ctnid,LoadFlags.NOTHING, jParams);
817              */

818
819             // reset session
820
org.jahia.engines.shared.Page_Field.resetPageBeanSession (jParams);
821
822             // init the JahiaContentFieldFacade
823
ArrayList JavaDoc localeList = jParams.getSite ().
824                     getLanguageSettingsAsLocales (false);
825
826             jahiaContentContainerFacade
827                     = new JahiaContentContainerFacade (ctnid,
828                             LoadFlags.ALL,
829                             jParams,
830                             localeList,
831                             true);
832
833             engineMap.put ("UpdateContainer_Engine.JahiaContentContainerFacade",
834                     jahiaContentContainerFacade);
835
836             if (theScreen == null || theScreen.equals (EMPTY_STRING)) {
837                 theScreen = "edit";
838             }
839
840             // the the selected fieldID
841
if (selectedFieldID != null) {
842                 engineMap.put ("field_id", selectedFieldID);
843             }
844
845             // init container list field acls
846
// we get a staging version of a container to ensure it contains all fields.
847
Enumeration JavaDoc enumeration = jahiaContentContainerFacade.getContainers ();
848             while (enumeration.hasMoreElements ()) {
849                 theContainer = (JahiaContainer) enumeration.nextElement ();
850                 if (theContainer.getWorkflowState () >
851                         EntryLoadRequest.ACTIVE_WORKFLOW_STATE) {
852                     break;
853                 }
854             }
855             HashMap JavaDoc ctnListFieldAcls = AddContainer_Engine.
856                     getCtnListFieldAclMap (theContainer,
857                             jParams);
858             engineMap.put ("ctnListFieldAcls", ctnListFieldAcls);
859             Set JavaDoc visibleFields = AddContainer_Engine.getCtnListVisibleFields (
860                     theContainer, jParams.getUser (), ctnListFieldAcls);
861
862             // create the edit view
863
ContainerEditView editView = AddContainer_Engine.
864                     customizeContainerEditView (
865                             theContainer, jParams, visibleFields);
866             if (editView == null) {
867                 // create a default one
868
editView = AddContainer_Engine.buildContainerEditView (
869                         theContainer, jParams, visibleFields);
870             }
871             engineMap.put ("ctnEditView", editView);
872         }
873
874         engineMap.put ("entryLoadRequest", entryLoadRequest);
875
876         engineMap.put ("prevEntryLoadRequest", prevEntryLoadRequest);
877
878         engineMap.put ("languageCode", languageCode);
879         engineMap.put ("prevLanguageCode", prevLanguageCode);
880
881         // remember the requested language codes :
882
HashSet JavaDoc updatedLanguageEntries = (HashSet JavaDoc) engineMap.get (
883                 "updatedLanguageEntries");
884         if (updatedLanguageEntries == null) {
885             updatedLanguageEntries = new HashSet JavaDoc ();
886         }
887         if (!updatedLanguageEntries.contains (languageCode)) {
888             updatedLanguageEntries.add (languageCode);
889         }
890         engineMap.put ("updatedLanguageEntries", updatedLanguageEntries);
891
892         // remember the updated fields ( Apply Change to all lang options )
893
HashSet JavaDoc updatedFields = (HashSet JavaDoc) engineMap.get ("updated.fields");
894         if (updatedFields == null) {
895             updatedFields = new HashSet JavaDoc ();
896         }
897         engineMap.put ("updated.fields", updatedFields);
898
899         theContainer = jahiaContentContainerFacade.getContainer (
900                 entryLoadRequest, true);
901         engineMap.put ("theContainer", theContainer);
902
903         engineMap.put (RENDER_TYPE_PARAM, new Integer JavaDoc (JahiaEngine.RENDERTYPE_FORWARD));
904         engineMap.put (ENGINE_NAME_PARAM, ENGINE_NAME);
905         engineMap.put (ENGINE_URL_PARAM,
906                 jParams.composeEngineUrl (ENGINE_NAME,
907                         "?cid=" + theContainer.getID ()));
908         theSession.setAttribute ("jahia_session_engineMap", engineMap);
909
910         // sets screen
911
engineMap.put ("screen", theScreen);
912
913         if (theScreen.equals ("save")) {
914             engineMap.put ("jspSource", "close");
915
916         } else if (theScreen.equals ("apply")) {
917             engineMap.put ("jspSource", "apply");
918
919         } else if (theScreen.equals ("cancel")) {
920             engineMap.put ("jspSource", "close");
921
922         } else {
923             engineMap.put ("jspSource", TEMPLATE_JSP);
924         }
925
926         engineMap.put ("enableCategories", new Boolean JavaDoc (true));
927
928         // sets engineMap for JSPs
929
engineMap.put ("fieldForms", new Hashtable JavaDoc ());
930         jParams.getRequest ().setAttribute ("engineTitle", "Update Container");
931         jParams.getRequest ().setAttribute ("org.jahia.engines.EngineHashMap",
932                 engineMap);
933
934         return engineMap;
935     }
936
937 }
938
Popular Tags