/* (non-Javadoc)
   * @see org.apache.cocoon.portal.profile.ProfileManager#register(org.apache.cocoon.portal.layout.Layout)
   */
  public void register(Layout layout) {
    PortalService service = null;
    try {
      service = (PortalService) this.manager.lookup(PortalService.ROLE);
      final String layoutKey = service.getDefaultLayoutKey();

      Map layoutMap = (Map) service.getAttribute("Layout-Map:" + layoutKey);
      if (layoutMap == null) {
        layout = (Layout) service.getAttribute("Layout:" + layoutKey);
        if (layout != null) {
          layoutMap = new HashMap();
          this.cacheLayouts(layoutMap, layout);
          service.setAttribute("Layout-Map:" + layoutKey, layoutMap);
        }
      }

      if (layoutMap != null) {
        layoutMap.put(layout.getId(), layout);
      }

    } catch (ServiceException e) {
      throw new CascadingRuntimeException("Unable to lookup portal service.", e);
    } finally {
      this.manager.release(service);
    }
  }
  /* (non-Javadoc)
   * @see org.apache.cocoon.portal.profile.ProfileManager#storeProfile(org.apache.cocoon.portal.layout.Layout, java.lang.String)
   */
  public void storeProfile(Layout rootLayout, String layoutKey) {
    PortalService service = null;

    try {
      service = (PortalService) this.manager.lookup(PortalService.ROLE);
      if (null == layoutKey) {
        layoutKey = service.getDefaultLayoutKey();
      }

      final String layoutAttributeKey = "Layout:" + layoutKey;

      service.setAttribute(layoutAttributeKey, rootLayout);
    } catch (Exception ce) {
      throw new CascadingRuntimeException("Exception during loading of profile.", ce);
    } finally {
      this.manager.release(service);
    }
  }
  /* (non-Javadoc)
   * @see org.apache.cocoon.portal.profile.ProfileManager#getPortalLayout(java.lang.String, java.lang.String)
   */
  public Layout getPortalLayout(String layoutKey, String layoutID) {
    PortalService service = null;
    ServiceSelector adapterSelector = null;

    try {
      service = (PortalService) this.manager.lookup(PortalService.ROLE);
      LayoutFactory factory = service.getComponentManager().getLayoutFactory();
      CopletFactory copletFactory = service.getComponentManager().getCopletFactory();

      adapterSelector = (ServiceSelector) this.manager.lookup(CopletAdapter.ROLE + "Selector");

      if (null == layoutKey) {
        layoutKey = service.getDefaultLayoutKey();
      }

      final String layoutAttributeKey = "Layout:" + layoutKey;
      final String layoutObjectsAttributeKey = "Layout-Map:" + layoutKey;

      Layout layout = (Layout) service.getAttribute(layoutAttributeKey);
      if (layout == null) {
        layout = this.loadProfile(layoutKey, service, copletFactory, factory, adapterSelector);
      }

      if (layoutID != null) {
        // now search for a layout
        Map layoutMap = (Map) service.getAttribute(layoutObjectsAttributeKey);
        if (layoutMap == null) {
          layoutMap = new HashMap();
          this.cacheLayouts(layoutMap, layout);
          service.setAttribute(layoutObjectsAttributeKey, layoutMap);
        }
        if (layoutMap != null) {
          return (Layout) layoutMap.get(layoutID);
        }
      }

      return layout;
    } catch (Exception ce) {
      throw new CascadingRuntimeException("Exception during loading of profile.", ce);
    } finally {
      this.manager.release(service);
      this.manager.release(adapterSelector);
    }
  }