1 16 package org.apache.cocoon.portal.impl; 17 18 import java.util.ArrayList ; 19 import java.util.List ; 20 import java.util.Map ; 21 22 import org.apache.avalon.framework.context.Context; 23 import org.apache.avalon.framework.context.ContextException; 24 import org.apache.avalon.framework.context.Contextualizable; 25 import org.apache.avalon.framework.logger.AbstractLogEnabled; 26 import org.apache.avalon.framework.service.ServiceException; 27 import org.apache.avalon.framework.service.ServiceManager; 28 import org.apache.avalon.framework.service.Serviceable; 29 import org.apache.avalon.framework.thread.ThreadSafe; 30 import org.apache.avalon.framework.CascadingRuntimeException; 31 import org.apache.avalon.framework.component.Component; 32 import org.apache.avalon.framework.configuration.Configurable; 33 import org.apache.avalon.framework.configuration.Configuration; 34 import org.apache.cocoon.portal.PortalService; 35 import org.apache.cocoon.portal.layout.Layout; 36 import org.apache.cocoon.portal.layout.CompositeLayout; 37 import org.apache.cocoon.portal.layout.Item; 38 import org.apache.cocoon.portal.layout.NamedItem; 39 import org.apache.cocoon.portal.event.impl.ChangeAspectDataEvent; 40 import org.apache.cocoon.util.HashMap; 41 import org.apache.cocoon.environment.Request; 42 import org.apache.cocoon.environment.ObjectModelHelper; 43 import org.apache.cocoon.components.ContextHelper; 44 45 57 public class PageLabelManager 58 extends AbstractLogEnabled 59 implements ThreadSafe, Serviceable, Configurable, Contextualizable, Component { 60 61 public static final String ROLE = PageLabelManager.class.getName(); 62 63 64 protected ServiceManager manager; 65 66 protected Context context; 67 protected String aspectName = null; 68 private String requestParameterName; 69 private boolean nonStickyTabs; 70 private boolean marshallEvents; 71 72 protected static final String LABEL_ARRAY = PageLabelManager.class.getName() + "A"; 73 protected static final String LABEL_MAP = PageLabelManager.class.getName() + "M"; 74 protected static final String EVENT_MAP = PageLabelManager.class.getName() + "E"; 75 private static final String DEFAULT_REQUEST_PARAMETER_NAME = "pageLabel"; 76 77 78 81 public void service(ServiceManager manager) throws ServiceException { 82 this.manager = manager; 83 } 84 85 88 public void configure(Configuration config) 89 { 90 this.requestParameterName = 91 config.getChild("parameterName").getValue(DEFAULT_REQUEST_PARAMETER_NAME); 92 this.aspectName = config.getChild("aspectName").getValue("tab"); 93 this.nonStickyTabs = 94 Boolean.valueOf(config.getChild("nonStickyTabs").getValue("false")).booleanValue(); 95 this.marshallEvents = 96 Boolean.valueOf(config.getChild("marshallEvents").getValue("false")).booleanValue(); 97 } 98 99 102 public void contextualize(Context context) throws ContextException { 103 this.context = context; 104 } 105 106 110 public String getCurrentLabel() { 111 String [] labels = getLabels(); 112 113 return labels[0]; 114 } 115 116 120 public String getPreviousLabel() { 121 String [] labels = getLabels(); 122 123 return labels[1]; 124 } 125 126 130 public String setCurrentLabel() { 131 final Request request = 132 ObjectModelHelper.getRequest(ContextHelper.getObjectModel(this.context)); 133 String value = request.getParameter(this.requestParameterName); 134 String [] labels = getLabels(); 135 136 if (value != null) { 137 labels[1] = labels[0]; 138 labels[0] = value; 139 } 140 return labels[0]; 141 } 142 143 147 public String getRequestParameterName() { 148 return this.requestParameterName; 149 } 150 151 152 156 public Map getPageEventMap() 157 { 158 PortalService service = null; 159 try { 160 service = (PortalService) this.manager.lookup(PortalService.ROLE); 161 Map map = (Map ) service.getAttribute(EVENT_MAP); 162 if (null == map) { 163 map = new HashMap(); 164 service.setAttribute(EVENT_MAP, map); 165 } 166 167 return map; 168 } 169 catch (ServiceException ce) { 170 throw new CascadingRuntimeException("Unable to lookup component.", ce); 171 } 172 finally { 173 this.manager.release(service); 174 } 175 } 176 177 184 public List getPageLabelEvents(String pageLabel) { 185 PortalService service = null; 186 try { 187 service = (PortalService) this.manager.lookup(PortalService.ROLE); 188 Map map = (Map ) service.getAttribute(LABEL_MAP); 189 if (null == map) { 190 map = initializeLabels(service); 191 service.setAttribute(LABEL_MAP, map); 192 } 193 194 List list = (List ) map.get(pageLabel); 195 196 if (list == null) { 197 list = new ArrayList (); 198 map.put(pageLabel, list); 199 } 200 201 return list; 202 } 203 catch (ServiceException ce) { 204 throw new CascadingRuntimeException("Unable to lookup component.", ce); 205 } 206 finally { 207 this.manager.release(service); 208 } 209 } 210 211 214 public boolean isMarshallEvents() { 215 return this.marshallEvents; 216 } 217 218 221 private String [] getLabels() { 222 PortalService service = null; 223 try { 224 service = (PortalService) this.manager.lookup(PortalService.ROLE); 225 String [] labels = (String []) service.getAttribute(LABEL_ARRAY); 226 if (null == labels) { 227 labels = new String [2]; 228 service.setAttribute(LABEL_ARRAY, labels); 229 } 230 return labels; 231 } 232 catch (ServiceException ce) { 233 throw new CascadingRuntimeException("Unable to lookup component.", ce); 234 } 235 finally { 236 this.manager.release(service); 237 } 238 } 239 240 246 private Map initializeLabels(PortalService service) { 247 Map map = new HashMap(); 248 249 Layout portalLayout = service.getEntryLayout(null); 250 if (portalLayout == null) { 251 portalLayout = 252 service.getComponentManager().getProfileManager().getPortalLayout(null, null); 253 } 254 255 if (portalLayout instanceof CompositeLayout) { 256 populate((CompositeLayout) portalLayout, map, "", new ArrayList ()); 257 } 258 259 return map; 260 } 261 262 263 271 private List populate(CompositeLayout layout, Map map, String name, List parentEvents) { 272 List lhList = null; 273 for (int j = 0; j < layout.getSize(); j++) { 274 Item tab = layout.getItem(j); 275 ChangeAspectDataEvent event = 276 new ChangeAspectDataEvent(layout, this.aspectName, new Integer (j)); 277 StringBuffer label = new StringBuffer (name); 278 if (label.length() > 0) { 279 label.append("."); 280 } 281 label.append((tab instanceof NamedItem) ? ((NamedItem) tab).getName() : 282 Integer.toString(j)); 283 List events = new ArrayList (parentEvents); 284 events.add(event); 285 Layout child = tab.getLayout(); 286 List allEvents = null; 287 if (child != null && child instanceof CompositeLayout) { 288 allEvents = populate((CompositeLayout) child, map, label.toString(), events); 289 } 290 if (this.nonStickyTabs) { 291 if (lhList == null) 294 { 295 if (allEvents != null) 296 { 297 lhList = allEvents; 298 } 299 else 300 { 301 lhList = events; 302 } 303 } 304 if (allEvents != null) { 305 map.put(label.toString(), allEvents); 306 } else { 307 map.put(label.toString(), events); 308 } 309 } else { 310 map.put(label.toString(), events); 311 } 312 } 313 return lhList; 314 } 315 } | Popular Tags |