KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > engines > lock > LockEngine


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 /*
15  * ----- BEGIN LICENSE BLOCK -----
16  * Version: JCSL 1.0
17  *
18  * The contents of this file are subject to the Jahia Community Source License
19  * 1.0 or later (the "License"); you may not use this file except in
20  * compliance with the License. You may obtain a copy of the License at
21  * http://www.jahia.org/license
22  *
23  * Software distributed under the License is distributed on an "AS IS" basis,
24  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
25  * for the rights, obligations and limitations governing use of the contents
26  * of the file. The Original and Upgraded Code is the Jahia CMS and Portal
27  * Server. The developer of the Original and Upgraded Code is JAHIA Ltd. JAHIA
28  * Ltd. owns the copyrights in the portions it created. All Rights Reserved.
29  *
30  * The Shared Modifications are Jahia Lock Engine.
31  *
32  * The Developer of the Shared Modifications is Jahia Solution S�rl.
33  * Portions created by the Initial Developer are Copyright (C) 2002 by the
34  * Initial Developer. All Rights Reserved.
35  *
36  * Contributor(s):
37  * Apr 28 2002 Jahia Solutions S�rl: MAP Initial release.
38  *
39  * ----- END LICENSE BLOCK -----
40  */

41
42 package org.jahia.engines.lock;
43
44 import java.util.Enumeration JavaDoc;
45 import java.util.HashMap JavaDoc;
46
47 import org.jahia.data.JahiaData;
48 import org.jahia.engines.EngineToolBox;
49 import org.jahia.engines.JahiaEngine;
50 import org.jahia.exceptions.JahiaException;
51 import org.jahia.params.ParamBean;
52 import org.jahia.registries.ServicesRegistry;
53 import org.jahia.services.lock.LockKey;
54 import org.jahia.services.lock.LockPrerequisites;
55 import org.jahia.services.lock.LockPrerequisitesResult;
56 import org.jahia.services.lock.LockService;
57
58 /**
59  * <p>Title: Jahia Lock Engine</p> <p>Description: This engine displays all necessary forms to
60  * permit the user (who has write and/or admin access) to change a Jahia page workflow state.
61  * </p> <p>Copyright: Copyright (c) 2003</p> <p>Company: Jahia Solutions SaRL</p>
62  *
63  * @author MAP
64  * @version 1.0
65  */

66 public class LockEngine implements JahiaEngine {
67
68     public static final String JavaDoc ENGINE_NAME = "lock";
69     public static final String JavaDoc LOCK_ENGINE_JSP = "lock";
70     private static final String JavaDoc TEMPLATE_JSP = "lock";
71
72     /** Engine's unique instance. */
73     private static LockEngine instance = null;
74     private EngineToolBox toolBox;
75
76     /** Logging */
77     private static final org.apache.log4j.Logger logger =
78             org.apache.log4j.Logger.getLogger (LockEngine.class);
79
80
81     private LockEngine () {
82         logger.debug ("***** Starting " + LockEngine.class.getName () + " engine *****");
83         toolBox = EngineToolBox.getInstance ();
84     }
85
86     /**
87      * @return The instance of this engine
88      */

89     public static synchronized LockEngine getInstance () {
90
91         if (instance == null) {
92             logger.debug ("Instanciate Lock engine");
93             instance = new LockEngine ();
94         }
95         return instance;
96     }
97
98     /**
99      * @param jParams ;)
100      *
101      * @return Always true
102      */

103     public boolean authoriseRender (ParamBean jParams) {
104         // Always allowed to render workflow. In other hand the button is displayed
105
// only if the logged user has write or admin access.
106
return true;
107     }
108
109     /**
110      * @param jParams ;)
111      *
112      * @return Always false
113      */

114     public boolean needsJahiaData (ParamBean jParams) {
115         return false;
116     }
117
118     /**
119      * Compose a valid workflow engine URL.
120      *
121      * @param jParams ;)
122      * @param theObj not used
123      *
124      * @return The composed URL; on s'en serait doute...
125      *
126      * @throws JahiaException
127      */

128     public String JavaDoc renderLink (ParamBean jParams, Object JavaDoc theObj)
129             throws JahiaException {
130         LockKey lockKey = (LockKey) theObj;
131         return jParams.composeEngineUrl ("lock", "?action=display&lockKey=" + lockKey);
132     }
133
134     /**
135      * Process the engine action triggered on the displayed form. The actions can be decomposed
136      * as follow : screen ::= "display" | "apply" | "save" | "cancel"
137      *
138      * @param jParams ;)
139      * @param jData not used
140      *
141      * @throws JahiaException
142      */

143     public void handleActions (ParamBean jParams, JahiaData jData)
144             throws JahiaException {
145         HashMap JavaDoc engineMap = new HashMap JavaDoc ();
146         processAction (jParams, engineMap);
147     }
148
149     /**
150      * Retrieve the engine name.
151      *
152      * @return the engine name.
153      */

154     public final String JavaDoc getName () {
155         return ENGINE_NAME;
156     }
157
158     public void redirect (ParamBean jParams, HashMap JavaDoc engineMap, LockKey lockKey)
159             throws JahiaException {
160         String JavaDoc redirectedToLockEngine = jParams.getParameter ("redirectedToLockEngine");
161         if (redirectedToLockEngine == null) {
162             initLockEngine (jParams, engineMap, lockKey);
163             toolBox.displayScreen (jParams, engineMap);
164         } else {
165             processAction (jParams, engineMap);
166         }
167     }
168
169     private void processAction (ParamBean jParams, HashMap JavaDoc engineMap)
170             throws JahiaException {
171         String JavaDoc actionScreen = jParams.getParameter ("action");
172         // screen = display
173
if ("display".equals (actionScreen)) {
174             String JavaDoc lockKeyStr = jParams.getParameter ("lockKey");
175             if (lockKeyStr != null) {
176                 LockKey lockKey = LockKey.composeLockKey (lockKeyStr);
177                 LockPrerequisitesResult lpr = LockPrerequisites.getInstance ().
178                         getLockPrerequisitesResult (lockKey);
179                 String JavaDoc showDetails = jParams.getParameter ("showDetails");
180                 if (showDetails != null) {
181                     lpr.setShowDetails (Boolean.valueOf (showDetails).booleanValue ());
182                 }
183                 initLockEngine (jParams, engineMap, lockKey);
184                 LockService lockRegistry = ServicesRegistry.getInstance ().getLockService ();
185                 Long JavaDoc timeRemaining = lockRegistry.getTimeRemaining (lockKey);
186                 if (timeRemaining != null && timeRemaining.longValue () < 0) {
187                     engineMap.put ("jspSource", "close");
188                 }
189                 toolBox.displayScreen (jParams, engineMap);
190             }
191         }
192         // apply modifications
193
else if ("save".equals (actionScreen) || "apply".equals (actionScreen)) {
194             Enumeration JavaDoc paramNames = jParams.getRequest ().getParameterNames ();
195             while (paramNames.hasMoreElements ()) {
196                 String JavaDoc paramName = (String JavaDoc) paramNames.nextElement ();
197                 LockKey lockKey = LockKey.composeLockKey (paramName);
198                 if (lockKey != null) {
199                     LockService lockRegistry = ServicesRegistry.getInstance ().getLockService ();
200                     if (LockPrerequisites.getInstance ().isLockAlreadyAcquired (lockKey)) {
201                         lockRegistry.steal (lockKey, jParams.getUser (), jParams.getSessionID ());
202                     } else {
203                         lockRegistry.nuke (lockKey, jParams.getUser (), jParams.getSessionID ());
204                     }
205                 }
206             }
207             if ("apply".equals (actionScreen)) {
208                 engineMap.put ("jspSource", "apply");
209             } else {
210                 engineMap.put ("jspSource", "close");
211             }
212             engineMap.put (RENDER_TYPE_PARAM, new Integer JavaDoc (JahiaEngine.RENDERTYPE_FORWARD));
213             toolBox.displayScreen (jParams, engineMap);
214         }
215     }
216
217     private void initLockEngine (ParamBean jParams, HashMap JavaDoc engineMap, LockKey lockKey)
218             throws JahiaException {
219         engineMap.put ("lockKey", lockKey);
220         LockPrerequisitesResult lockPrerequisitesResult =
221                 LockPrerequisites.getInstance ().getLockPrerequisitesResult (lockKey);
222         engineMap.put ("lockPrerequisitesResult", lockPrerequisitesResult);
223         engineMap.put ("jspSource", TEMPLATE_JSP);
224         engineMap.put (ENGINE_NAME_PARAM, "Jahia Locks");
225         engineMap.put (RENDER_TYPE_PARAM, new Integer JavaDoc (JahiaEngine.RENDERTYPE_FORWARD));
226         jParams.getRequest ().setAttribute ("engineTitle", "Lock"); // Displayed in 'engine.jsp'
227
}
228
229 }
Popular Tags