/* (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#getCopletInstanceData(org.apache.cocoon.portal.coplet.CopletData)
   */
  public List getCopletInstanceData(CopletData data) {
    List coplets = new ArrayList();
    PortalService service = null;
    String attribute = null;
    try {
      service = (PortalService) this.manager.lookup(PortalService.ROLE);
      final String layoutKey = service.getDefaultLayoutKey();

      attribute = "CopletInstanceData:" + layoutKey;
      CopletInstanceDataManager copletInstanceDataManager =
          (CopletInstanceDataManager) service.getAttribute(attribute);

      Iterator iter = copletInstanceDataManager.getCopletInstanceData().values().iterator();
      while (iter.hasNext()) {
        CopletInstanceData current = (CopletInstanceData) iter.next();
        if (current.getCopletData().equals(data)) {
          coplets.add(current);
        }
      }
      return coplets;
    } 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#getCopletData(java.lang.String)
   */
  public CopletData getCopletData(String copletDataId) {
    PortalService service = null;
    String attribute = null;
    try {
      service = (PortalService) this.manager.lookup(PortalService.ROLE);
      final String layoutKey = service.getDefaultLayoutKey();

      attribute = "CopletInstanceData:" + layoutKey;
      CopletInstanceDataManager copletInstanceDataManager =
          (CopletInstanceDataManager) service.getAttribute(attribute);

      Iterator i = copletInstanceDataManager.getCopletInstanceData().values().iterator();
      boolean found = false;
      CopletInstanceData current = null;
      while (!found && i.hasNext()) {
        current = (CopletInstanceData) i.next();
        found = current.getCopletData().getId().equals(copletDataId);
      }
      if (found) {
        return current.getCopletData();
      }
      return null;
    } 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#getCopletInstanceDatas()
  */
 public Collection getCopletInstanceDatas() {
   PortalService service = null;
   try {
     service = (PortalService) this.manager.lookup(PortalService.ROLE);
     final String layoutKey = service.getDefaultLayoutKey();
     CopletInstanceDataManager manager =
         (CopletInstanceDataManager) service.getAttribute("CopletInstanceData:" + layoutKey);
     return manager.getCopletInstanceData().values();
   } catch (Exception e) {
     throw new CascadingRuntimeException("Error in getCopletInstanceDatas.", 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#unregister(org.apache.cocoon.portal.layout.Layout)
   */
  public void unregister(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) {
        layoutMap.remove(layout.getId());
      }

    } 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#getCopletInstanceData(java.lang.String)
   */
  public CopletInstanceData getCopletInstanceData(String copletID) {
    PortalService service = null;
    String attribute = null;
    try {
      service = (PortalService) this.manager.lookup(PortalService.ROLE);
      final String layoutKey = service.getDefaultLayoutKey();

      attribute = "CopletInstanceData:" + layoutKey;
      CopletInstanceDataManager copletInstanceDataManager =
          (CopletInstanceDataManager) service.getAttribute(attribute);

      return copletInstanceDataManager.getCopletInstanceData(copletID);
    } 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#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);
    }
  }
  /* (non-Javadoc)
   * @see org.apache.cocoon.portal.profile.ProfileManager#logout()
   */
  public void logout() {
    PortalService service = null;
    ServiceSelector adapterSelector = null;
    try {
      adapterSelector = (ServiceSelector) this.manager.lookup(CopletAdapter.ROLE + "Selector");
      service = (PortalService) this.manager.lookup(PortalService.ROLE);
      final String layoutKey = service.getDefaultLayoutKey();

      CopletInstanceDataManager copletInstanceDataManager =
          (CopletInstanceDataManager) service.getAttribute("CopletInstanceData:" + layoutKey);
      if (copletInstanceDataManager != null) {
        Iterator iter = copletInstanceDataManager.getCopletInstanceData().values().iterator();
        while (iter.hasNext()) {
          CopletInstanceData cid = (CopletInstanceData) iter.next();
          CopletAdapter adapter = null;
          try {
            adapter =
                (CopletAdapter)
                    adapterSelector.select(
                        cid.getCopletData().getCopletBaseData().getCopletAdapterName());
            adapter.logout(cid);
          } finally {
            adapterSelector.release(adapter);
          }
        }
      }

      service.removeAttribute("CopletData:" + layoutKey);
      service.removeAttribute("CopletInstanceData:" + layoutKey);
      service.removeAttribute("Layout:" + layoutKey);
    } catch (ServiceException e) {
      throw new CascadingRuntimeException("Unable to lookup portal service.", e);
    } finally {
      this.manager.release(service);
      this.manager.release(adapterSelector);
    }
    super.logout();
  }