KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > engines > selectpage > SelectPage_Engine


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 package org.jahia.engines.selectpage;
15
16 import java.util.ArrayList JavaDoc;
17 import java.util.HashMap JavaDoc;
18 import java.util.HashSet JavaDoc;
19 import java.util.List JavaDoc;
20 import java.util.Map JavaDoc;
21
22 import javax.servlet.http.HttpSession JavaDoc;
23
24 import org.jahia.data.JahiaData;
25 import org.jahia.data.fields.JahiaField;
26 import org.jahia.data.viewhelper.sitemap.PagesFilter;
27 import org.jahia.data.viewhelper.sitemap.SiteMapViewHelper;
28 import org.jahia.engines.EngineToolBox;
29 import org.jahia.engines.JahiaEngine;
30 import org.jahia.exceptions.JahiaException;
31 import org.jahia.params.ParamBean;
32 import org.jahia.registries.ServicesRegistry;
33 import org.jahia.services.pages.ContentPage;
34 import org.jahia.services.sitemap.JahiaSiteMapService;
35
36 /**
37  * <p>Title: Select page engine</p> <p>Description: Display a Jahia site map that permit to the
38  * user to select a DIRECT page. WARNING ! The SESSION_OBJECT object should exist and parameter
39  * OPERATION set before all call to this engine !!! </p> <p>Copyright: MAP (Jahia Solutions S�rl
40  * 2002)</p> <p>Company: Jahia Solutions S�rl</p>
41  *
42  * @author MAP
43  * @version 1.0
44  */

45 public class SelectPage_Engine implements JahiaEngine {
46
47     public static final String JavaDoc ENGINE_NAME = "selectpage";
48     public static final String JavaDoc SESSION_PARAMS = "org.jahia.engines.SelectPage_Engine.sessionParams";
49
50     // This parameter should contain the operation that should be execute on this
51
// engine. It should be initialize before calling this engine.
52
public static final String JavaDoc OPERATION = "selectPageOperation";
53     // Select page session parameters
54
// Define the source page ID selected in the engine. Set from the HTML form.
55
public static final String JavaDoc SOURCE_PAGE_ID = "sourcePageID";
56     // Define the page level to copy in the Jahia page tree. Available only for
57
// COPY_OPERATION operation. Set from the HTML form.
58
public static final String JavaDoc COPIED_PAGE_LEVEL = "copiedPageLevel";
59     // This parameter is the container parent page ID for new container creation.
60
// Correspond to the 'cpid' parameter. Useful for page edition.
61
public static final String JavaDoc PARENT_PAGE_ID = "parentPageID";
62     // The current page ID for update if exists.
63
// N.B. This parameter is used when the user will change a DIRECT Jahia page
64
// to a link type (URL or page link). The DIRECT type cannot be changed to a
65
// link type on it's self page.
66
// Set to -1 for page creation.
67
public static final String JavaDoc PAGE_ID = "pageID";
68     
69     // Engine redirection
70
private static final String JavaDoc SELECT_PAGE_JSP = "select_page";
71     private static final String JavaDoc CLOSE_JSP = "selectPage_close";
72
73     // These operations are the same as the Page_Field engine.
74
public static final String JavaDoc MOVE_OPERATION = "movePage";
75     public static final String JavaDoc COPY_OPERATION = "copyPage";
76     public static final String JavaDoc LINK_OPERATION = "linkJahiaPage";
77     // Display any page for selection.
78
public static final String JavaDoc SELECT_ANY_PAGE_OPERATION = "selectAnyPage";
79
80     private HashSet JavaDoc possibleOperations = new HashSet JavaDoc ();
81
82     private EngineToolBox toolBox;
83     private static SelectPage_Engine instance = null;
84
85     private final static String JavaDoc[] tabOptions = {"treeView", "flatView", "searchView"};
86     private final static String JavaDoc[] tabOptionsBasic = {"treeView", "flatView"};
87     private String JavaDoc lastDisplay;
88
89     private static final org.apache.log4j.Logger logger =
90             org.apache.log4j.Logger.getLogger (SelectPage_Engine.class);
91
92
93     /**
94      * @return This object instance
95      */

96     public static synchronized SelectPage_Engine getInstance () {
97         if (instance == null) {
98             instance = new SelectPage_Engine ();
99         }
100         return instance;
101     }
102
103     /**
104      * Autorize render.
105      *
106      * @param jParams ;)
107      *
108      * @return Always true.
109      */

110     public boolean authoriseRender (ParamBean jParams) {
111         return true;
112     }
113
114     /**
115      * Compose this engine URL.
116      *
117      * @param jParams The ParamBean.
118      * @param anObject An object dont on se tape.
119      *
120      * @return The URL to call this engine.
121      *
122      * @throws JahiaException
123      */

124     public String JavaDoc renderLink (ParamBean jParams, Object JavaDoc anObject)
125             throws JahiaException {
126         HashMap JavaDoc params = (HashMap JavaDoc) anObject;
127         String JavaDoc operation = (String JavaDoc) params.get (OPERATION);
128         Integer JavaDoc parentPageID = (Integer JavaDoc) params.get (PARENT_PAGE_ID);
129         Integer JavaDoc pageID = (Integer JavaDoc) params.get (PAGE_ID);
130         
131         lastDisplay = tabOptions[SiteMapViewHelper.TREE_VIEW];
132         
133         return jParams.composeEngineUrl (ENGINE_NAME, "?screen=display" +
134                 constructDisplayParams (lastDisplay, jParams.getLocale ().toString(),
135                         operation,
136                         parentPageID.intValue (),
137                         pageID.intValue ()));
138     }
139
140     public static String JavaDoc constructDisplayParams (String JavaDoc display, String JavaDoc languageCode,
141                                                  String JavaDoc operation, int parentPageID, int pageID) {
142         StringBuffer JavaDoc displayParams = new StringBuffer JavaDoc(154).append("&display=");
143         displayParams.append (display);
144         displayParams.append ("&language_code=");
145         displayParams.append (languageCode);
146         displayParams.append ("&");
147         displayParams.append (OPERATION);
148         displayParams.append ("=");
149         displayParams.append (operation);
150         displayParams.append ("&");
151         displayParams.append (PARENT_PAGE_ID);
152         displayParams.append ("=");
153         displayParams.append (parentPageID);
154         displayParams.append ("&");
155         displayParams.append (PAGE_ID);
156         displayParams.append ("=");
157         displayParams.append (pageID);
158         return displayParams.toString ();
159     }
160
161     /**
162      * Set if this engine needs Jahia data.
163      *
164      * @param jParams The ParamBean.
165      *
166      * @return Always false
167      */

168     public boolean needsJahiaData (ParamBean jParams) {
169         return false;
170     }
171
172     /**
173      * Handle the action given by the URL parameter. There are two possible action to this
174      * engine. - display : Prepare datas to display the sitemap. - store : Collect datas and put
175      * them in the session for the caller.
176      *
177      * @param jParams The ParamBean.
178      * @param jData The JahiaData object dont on se tape.
179      *
180      * @throws JahiaException
181      */

182     public void handleActions(ParamBean jParams, JahiaData jData)
183             throws JahiaException {
184
185         final PagesFilter pagesFilter = JahiaSiteMapService.getCurrentPagesFilter(
186                 (JahiaField)((Map JavaDoc)jParams.getSession().getAttribute("jahia_session_engineMap")).
187                 get("theField"), jParams);
188         
189         HashMap JavaDoc engineMap = new HashMap JavaDoc();
190         engineMap.put("tabOptions", (pagesFilter != null && pagesFilter.isSearchTabRequired() ? tabOptions : tabOptionsBasic));
191         String JavaDoc actionScreen = jParams.getParameter("screen");
192         // screen = display
193
if ("display".equals(actionScreen)) {
194             HashMap JavaDoc selectedPageParams = (HashMap JavaDoc) jParams.getSession()
195                 .getAttribute(SESSION_PARAMS);
196             if (selectedPageParams == null) {
197                 selectedPageParams = new HashMap JavaDoc();
198                 selectedPageParams.put(SOURCE_PAGE_ID, new Integer JavaDoc(-1));
199                 selectedPageParams.put(COPIED_PAGE_LEVEL, new Integer JavaDoc(-1));
200                 jParams.getSession().setAttribute(SESSION_PARAMS,
201                     selectedPageParams);
202             } else {
203                 String JavaDoc selectPageOperation = (String JavaDoc) selectedPageParams
204                     .get(OPERATION);
205                 if (!possibleOperations.contains(selectPageOperation)) {
206                     engineMap.put("engineError", selectPageOperation);
207                 }
208             }
209             // If tab selected, which one ?
210
String JavaDoc display = jParams.getParameter("display");
211             if (tabOptions[SiteMapViewHelper.SEARCH_VIEW].equals(display)) {
212                 lastDisplay = tabOptions[SiteMapViewHelper.SEARCH_VIEW];
213                 if (jData.containers() == null)
214                     jData = new JahiaData(jParams);
215
216                 jParams.getRequest().setAttribute("org.jahia.data.JahiaData",
217                     jData);
218                 engineMap.put("activeTabOption", new Integer JavaDoc(
219                     SiteMapViewHelper.SEARCH_VIEW));
220             } else {
221                 int pageInfosFlag = ContentPage.ACTIVE_PAGE_INFOS
222                 | ContentPage.STAGING_PAGE_INFOS;
223
224                 JahiaSiteMapService siteMapService = ServicesRegistry
225                 .getInstance().getJahiaSiteMapService();
226                 boolean directPagesOnly = true;
227                 
228                 List JavaDoc startPages = null;
229                 if (pagesFilter != null){
230                     directPagesOnly = pagesFilter.isDirectPagesOnly();
231                     startPages = pagesFilter.getStartPages(jParams.getSite().getHomeContentPage(),
232                             jParams.getUser(), pageInfosFlag, null, jParams);
233                 } else {
234                     // Get a site map helper for the site home page.
235
startPages = new ArrayList JavaDoc();
236                     startPages.add(jParams.getSite().getHomeContentPage());
237                 }
238               
239                 SiteMapViewHelper siteMapViewHelper = siteMapService
240                     .getTreeSiteMapViewHelper(jParams.getUser(), startPages,
241                         jParams.getSessionID(), pageInfosFlag, null,
242                         SiteMapViewHelper.DEFAULT_LEVEL, directPagesOnly, pagesFilter, jParams);
243                 engineMap.put("treeJahiaSiteMap", siteMapViewHelper);
244                 siteMapViewHelper = siteMapService.getFlatSiteMapViewHelper(
245                     jParams.getUser(), startPages, jParams.getSessionID(),
246                     pageInfosFlag, null, SiteMapViewHelper.DISPLAY_ALL_LEVEL,
247                     pagesFilter, jParams);
248                 engineMap.put("flatJahiaSiteMap", siteMapViewHelper);
249                 // Has the user made a site map handling (collapse,
250
// expand...)...
251
String JavaDoc siteMapParam = jParams.getParameter("sitemap");
252
253                 if (tabOptions[SiteMapViewHelper.TREE_VIEW].equals(display)) {
254                     lastDisplay = tabOptions[SiteMapViewHelper.TREE_VIEW];
255                     engineMap.put("activeTabOption", new Integer JavaDoc(
256                         SiteMapViewHelper.TREE_VIEW));
257                     if (siteMapParam != null) {
258                         siteMapService.invokeTreeSiteMapViewHelperMethod(
259                             jParams.getUser(), startPages, jParams
260                                 .getSessionID(), pageInfosFlag, null,
261                             siteMapParam, directPagesOnly, pagesFilter, jParams);
262                     }
263                 } else if (tabOptions[SiteMapViewHelper.FLAT_VIEW]
264                     .equals(display)) {
265                     lastDisplay = tabOptions[SiteMapViewHelper.FLAT_VIEW];
266                     engineMap.put("activeTabOption", new Integer JavaDoc(
267                         SiteMapViewHelper.FLAT_VIEW));
268                     if (siteMapParam != null) {
269                         siteMapService.invokeFlatSiteMapViewHelperMethod(
270                             jParams.getUser(), startPages, jParams
271                                 .getSessionID(), pageInfosFlag, null,
272                             siteMapParam, pagesFilter, jParams);
273                     }
274                 }
275             }
276             // Save the form selection to the session.
277
storeSelectedPageParameters(jParams);
278             engineMap.put("jspSource", SELECT_PAGE_JSP);
279             jParams.getRequest().setAttribute("engineTitle", "Select Page"); // Displayed
280
// in
281
// 'engine.jsp'
282
// screen = store
283
} else if ("store".equals(actionScreen)) {
284             // Save the form selection to the session.
285
storeSelectedPageParameters(jParams);
286             engineMap.put("jspSource", CLOSE_JSP);
287         }
288         engineMap.put(RENDER_TYPE_PARAM, new Integer JavaDoc(
289             JahiaEngine.RENDERTYPE_FORWARD));
290         toolBox.displayScreen(jParams, engineMap);
291     }
292
293     /**
294      * Retrieve the engine name.
295      *
296      * @return the engine name.
297      */

298     public final String JavaDoc getName () {
299         return ENGINE_NAME;
300     }
301
302     /**
303      * Get the selected page ID from the session.
304      *
305      * @param session the HTTP session
306      *
307      * @return The page ID in the session or -1 if none.
308      */

309     public int getSelectedPageID (HttpSession JavaDoc session)
310             throws JahiaException {
311         HashMap JavaDoc selectedPageParams = (HashMap JavaDoc) session.getAttribute (SESSION_PARAMS);
312         if (selectedPageParams == null) {
313             return -1;
314         }
315         Integer JavaDoc sourcePageID = (Integer JavaDoc) selectedPageParams.get (SOURCE_PAGE_ID);
316         if (sourcePageID == null) {
317             return -1;
318         }
319         return sourcePageID.intValue ();
320     }
321
322     /**
323      * Save the form parameters to the session. The following parameters are stored : -
324      * sourcePageID : the page selected. - copiedPageLevel : the page level for the copy page
325      * (not implemented yet).
326      *
327      * @param jParams The ParamBean
328      *
329      * @throws JahiaException
330      */

331     private void storeSelectedPageParameters (ParamBean jParams)
332             throws JahiaException {
333         HashMap JavaDoc selectedPageParams = (HashMap JavaDoc) jParams.getSession ().getAttribute (
334                 SESSION_PARAMS);
335         String JavaDoc sourcePageIDStr = jParams.getRequest ().getParameter (SOURCE_PAGE_ID);
336         if (sourcePageIDStr != null) {
337             Integer JavaDoc sourcePageID = Integer.valueOf (sourcePageIDStr);
338             selectedPageParams.put (SOURCE_PAGE_ID, sourcePageID);
339         }
340         String JavaDoc copiedPageLevelStr = jParams.getRequest ().getParameter (COPIED_PAGE_LEVEL);
341         if (copiedPageLevelStr != null) {
342             Integer JavaDoc copiedPageLevel = Integer.valueOf (copiedPageLevelStr);
343             selectedPageParams.put (COPIED_PAGE_LEVEL, copiedPageLevel);
344         }
345     }
346
347     private SelectPage_Engine () {
348         logger.debug ("***** Starting " + SelectPage_Engine.class.getName () + " engine *****");
349         toolBox = EngineToolBox.getInstance ();
350         possibleOperations.add (MOVE_OPERATION);
351         possibleOperations.add (LINK_OPERATION);
352         possibleOperations.add (COPY_OPERATION);
353         possibleOperations.add (SELECT_ANY_PAGE_OPERATION);
354     }
355 }
356
Popular Tags