1 14 package org.jahia.engines.selectpage; 15 16 import java.util.ArrayList ; 17 import java.util.HashMap ; 18 import java.util.HashSet ; 19 import java.util.List ; 20 import java.util.Map ; 21 22 import javax.servlet.http.HttpSession ; 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 45 public class SelectPage_Engine implements JahiaEngine { 46 47 public static final String ENGINE_NAME = "selectpage"; 48 public static final String SESSION_PARAMS = "org.jahia.engines.SelectPage_Engine.sessionParams"; 49 50 public static final String OPERATION = "selectPageOperation"; 53 public static final String SOURCE_PAGE_ID = "sourcePageID"; 56 public static final String COPIED_PAGE_LEVEL = "copiedPageLevel"; 59 public static final String PARENT_PAGE_ID = "parentPageID"; 62 public static final String PAGE_ID = "pageID"; 68 69 private static final String SELECT_PAGE_JSP = "select_page"; 71 private static final String CLOSE_JSP = "selectPage_close"; 72 73 public static final String MOVE_OPERATION = "movePage"; 75 public static final String COPY_OPERATION = "copyPage"; 76 public static final String LINK_OPERATION = "linkJahiaPage"; 77 public static final String SELECT_ANY_PAGE_OPERATION = "selectAnyPage"; 79 80 private HashSet possibleOperations = new HashSet (); 81 82 private EngineToolBox toolBox; 83 private static SelectPage_Engine instance = null; 84 85 private final static String [] tabOptions = {"treeView", "flatView", "searchView"}; 86 private final static String [] tabOptionsBasic = {"treeView", "flatView"}; 87 private String lastDisplay; 88 89 private static final org.apache.log4j.Logger logger = 90 org.apache.log4j.Logger.getLogger (SelectPage_Engine.class); 91 92 93 96 public static synchronized SelectPage_Engine getInstance () { 97 if (instance == null) { 98 instance = new SelectPage_Engine (); 99 } 100 return instance; 101 } 102 103 110 public boolean authoriseRender (ParamBean jParams) { 111 return true; 112 } 113 114 124 public String renderLink (ParamBean jParams, Object anObject) 125 throws JahiaException { 126 HashMap params = (HashMap ) anObject; 127 String operation = (String ) params.get (OPERATION); 128 Integer parentPageID = (Integer ) params.get (PARENT_PAGE_ID); 129 Integer pageID = (Integer ) 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 constructDisplayParams (String display, String languageCode, 141 String operation, int parentPageID, int pageID) { 142 StringBuffer displayParams = new StringBuffer (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 168 public boolean needsJahiaData (ParamBean jParams) { 169 return false; 170 } 171 172 182 public void handleActions(ParamBean jParams, JahiaData jData) 183 throws JahiaException { 184 185 final PagesFilter pagesFilter = JahiaSiteMapService.getCurrentPagesFilter( 186 (JahiaField)((Map )jParams.getSession().getAttribute("jahia_session_engineMap")). 187 get("theField"), jParams); 188 189 HashMap engineMap = new HashMap (); 190 engineMap.put("tabOptions", (pagesFilter != null && pagesFilter.isSearchTabRequired() ? tabOptions : tabOptionsBasic)); 191 String actionScreen = jParams.getParameter("screen"); 192 if ("display".equals(actionScreen)) { 194 HashMap selectedPageParams = (HashMap ) jParams.getSession() 195 .getAttribute(SESSION_PARAMS); 196 if (selectedPageParams == null) { 197 selectedPageParams = new HashMap (); 198 selectedPageParams.put(SOURCE_PAGE_ID, new Integer (-1)); 199 selectedPageParams.put(COPIED_PAGE_LEVEL, new Integer (-1)); 200 jParams.getSession().setAttribute(SESSION_PARAMS, 201 selectedPageParams); 202 } else { 203 String selectPageOperation = (String ) selectedPageParams 204 .get(OPERATION); 205 if (!possibleOperations.contains(selectPageOperation)) { 206 engineMap.put("engineError", selectPageOperation); 207 } 208 } 209 String 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 ( 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 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 startPages = new ArrayList (); 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 String 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 ( 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 ( 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 storeSelectedPageParameters(jParams); 278 engineMap.put("jspSource", SELECT_PAGE_JSP); 279 jParams.getRequest().setAttribute("engineTitle", "Select Page"); } else if ("store".equals(actionScreen)) { 284 storeSelectedPageParameters(jParams); 286 engineMap.put("jspSource", CLOSE_JSP); 287 } 288 engineMap.put(RENDER_TYPE_PARAM, new Integer ( 289 JahiaEngine.RENDERTYPE_FORWARD)); 290 toolBox.displayScreen(jParams, engineMap); 291 } 292 293 298 public final String getName () { 299 return ENGINE_NAME; 300 } 301 302 309 public int getSelectedPageID (HttpSession session) 310 throws JahiaException { 311 HashMap selectedPageParams = (HashMap ) session.getAttribute (SESSION_PARAMS); 312 if (selectedPageParams == null) { 313 return -1; 314 } 315 Integer sourcePageID = (Integer ) selectedPageParams.get (SOURCE_PAGE_ID); 316 if (sourcePageID == null) { 317 return -1; 318 } 319 return sourcePageID.intValue (); 320 } 321 322 331 private void storeSelectedPageParameters (ParamBean jParams) 332 throws JahiaException { 333 HashMap selectedPageParams = (HashMap ) jParams.getSession ().getAttribute ( 334 SESSION_PARAMS); 335 String sourcePageIDStr = jParams.getRequest ().getParameter (SOURCE_PAGE_ID); 336 if (sourcePageIDStr != null) { 337 Integer sourcePageID = Integer.valueOf (sourcePageIDStr); 338 selectedPageParams.put (SOURCE_PAGE_ID, sourcePageID); 339 } 340 String copiedPageLevelStr = jParams.getRequest ().getParameter (COPIED_PAGE_LEVEL); 341 if (copiedPageLevelStr != null) { 342 Integer 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 |