/* (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#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();
  }
  /* (non-Javadoc)
   * @see org.apache.cocoon.portal.profile.ProfileManager#unregister(org.apache.cocoon.portal.coplet.CopletInstanceData)
   */
  public void unregister(CopletInstanceData coplet) {
    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);

      copletInstanceDataManager.getCopletInstanceData().remove(coplet.getId());

    } catch (ServiceException e) {
      throw new CascadingRuntimeException("Unable to lookup portal service.", e);
    } finally {
      this.manager.release(service);
    }
  }