/* (non-Javadoc)
   * @see org.jasig.portal.portlet.registry.IPortletWindowRegistry#getPortletWindowId(java.lang.String)
   */
  @Override
  public PortletWindowIdImpl getPortletWindowId(
      HttpServletRequest request, String portletWindowId) {
    Validate.notNull(portletWindowId, "portletWindowId can not be null");

    final String[] portletWindowIdParts = ID_PART_SEPERATOR_PATTERN.split(portletWindowId);

    final String entityIdStr;
    final String instanceId;
    if (portletWindowIdParts.length == 1) {
      entityIdStr = portletWindowIdParts[0];
      instanceId = null;
    } else if (portletWindowIdParts.length == 2) {
      entityIdStr = portletWindowIdParts[0];
      instanceId = portletWindowIdParts[1];
    } else {
      throw new IllegalArgumentException(
          "Provided portlet window ID '" + portletWindowId + "' is not valid");
    }

    final IPortletEntity portletEntity =
        this.portletEntityRegistry.getPortletEntity(request, entityIdStr);
    if (portletEntity == null) {
      throw new IllegalArgumentException(
          "No parent IPortletEntity found for id '"
              + entityIdStr
              + "' from portlet window id: "
              + portletWindowId);
    }

    return createPortletWindowId(instanceId, portletEntity.getPortletEntityId());
  }
  protected IPortletWindow wrapPortletWindowData(
      HttpServletRequest request, PortletWindowData portletWindowData) {
    final IPortletEntityId portletEntityId = portletWindowData.getPortletEntityId();
    final IPortletEntity portletEntity =
        this.portletEntityRegistry.getPortletEntity(request, portletEntityId);
    if (portletEntity == null) {
      return null;
    }

    final IPortletDefinition portletDefinition = portletEntity.getPortletDefinition();
    final PortletDefinition portletDescriptor =
        this.portletDefinitionRegistry.getParentPortletDescriptor(
            portletDefinition.getPortletDefinitionId());
    if (portletDescriptor == null) {
      return null;
    }

    final IPortletWindow portletWindow =
        new PortletWindowImpl(portletDescriptor, portletEntity, portletWindowData);

    logger.trace(
        "Wrapping PortletWindowData {} as IPortletWindow", portletWindow.getPortletWindowId());

    return portletWindow;
  }
 protected String getPortletFname(HttpServletRequest request, IPortletWindowId portletWindowId) {
   final IPortletWindow portletWindow =
       this.portletWindowRegistry.getPortletWindow(request, portletWindowId);
   final IPortletEntity portletEntity = portletWindow.getPortletEntity();
   final IPortletDefinition portletDefinition = portletEntity.getPortletDefinition();
   return portletDefinition.getFName();
 }
  @Override
  public IPortletWindow getOrCreateDefaultPortletWindow(
      HttpServletRequest request, IPortletDefinitionId portletDefinitionId) {
    Validate.notNull(request, "HttpServletRequest cannot be null");
    Validate.notNull(portletDefinitionId, "portletDefinition cannot be null");

    final IPortletEntity portletEntity =
        this.portletEntityRegistry.getOrCreateDefaultPortletEntity(request, portletDefinitionId);
    final IPortletEntityId portletEntityId = portletEntity.getPortletEntityId();

    return this.getOrCreateDefaultPortletWindow(request, portletEntityId);
  }
 protected IPortletWindowId getErrorPortletWindowId(HttpServletRequest request, String fname) {
   final IUserInstance userInstance = this.userInstanceManager.getUserInstance(request);
   final IUserPreferencesManager preferencesManager = userInstance.getPreferencesManager();
   final IUserLayoutManager userLayoutManager = preferencesManager.getUserLayoutManager();
   final String errorPortletSubscribeId = userLayoutManager.getSubscribeId(fname);
   final IPortletEntity errorPortletEntity =
       this.portletEntityRegistry.getOrCreatePortletEntity(
           request, userInstance, errorPortletSubscribeId);
   final IPortletWindow portletWindow =
       this.portletWindowRegistry.getOrCreateDefaultPortletWindow(
           request, errorPortletEntity.getPortletEntityId());
   return portletWindow.getPortletWindowId();
 }
 /**
  * Initializes a newly created {@link PortletWindow}, the default implementation sets up the
  * appropriate {@link WindowState} and {@link javax.portlet.PortletMode}
  */
 protected void initializePortletWindowData(
     HttpServletRequest request, PortletWindowData portletWindowData) {
   final IStylesheetDescriptor stylesheetDescriptor = getThemeStylesheetDescriptor(request);
   final IPortletEntityId portletEntityId = portletWindowData.getPortletEntityId();
   final IPortletEntity portletEntity =
       this.portletEntityRegistry.getPortletEntity(request, portletEntityId);
   final WindowState entityWindowState = portletEntity.getWindowState(stylesheetDescriptor);
   if (persistentWindowStates.contains(entityWindowState)) {
     portletWindowData.setWindowState(entityWindowState);
   } else if (entityWindowState != null) {
     // Set of persistent window states must have changed, nuke the old value
     this.logger.warn(
         "PortletEntity.windowState="
             + entityWindowState
             + " but that state is not in the set of persistent WindowStates. PortletEntity.windowState will be set to null");
     portletEntity.setWindowState(stylesheetDescriptor, null);
     this.portletEntityRegistry.storePortletEntity(request, portletEntity);
   }
 }
  @Override
  public IPortletWindow getOrCreateDefaultPortletWindowByFname(
      HttpServletRequest request, String fname) {
    Validate.notNull(request, "HttpServletRequest cannot be null");
    Validate.notNull(fname, "fname cannot be null");

    final IUserInstance userInstance = this.userInstanceManager.getUserInstance(request);
    final IPortletEntity portletEntity =
        this.portletEntityRegistry.getOrCreatePortletEntityByFname(request, userInstance, fname);
    if (portletEntity == null) {
      return null;
    }

    final IPortletWindow portletWindow =
        this.getOrCreateDefaultPortletWindow(request, portletEntity.getPortletEntityId());
    logger.trace("Found portlet window {} for portlet definition fname {}", portletWindow, fname);

    return portletWindow;
  }
  @Override
  public IPortletWindow getOrCreateDefaultPortletWindowByLayoutNodeId(
      HttpServletRequest request, String subscribeId) {
    Validate.notNull(request, "HttpServletRequest cannot be null");
    Validate.notNull(subscribeId, "subscribeId cannot be null");

    final IUserInstance userInstance = this.userInstanceManager.getUserInstance(request);
    final IPortletEntity portletEntity =
        this.portletEntityRegistry.getOrCreatePortletEntity(request, userInstance, subscribeId);
    if (portletEntity == null) {
      logger.debug(
          "No portlet entity found for id {}, no IPortletWindow will be returned.", subscribeId);
      return null;
    }
    logger.trace("Found portlet entity {} for id {}", portletEntity, subscribeId);

    final IPortletWindow portletWindow =
        this.getOrCreateDefaultPortletWindow(request, portletEntity.getPortletEntityId());
    logger.trace("Found portlet window {} for layout node {}", portletWindow, subscribeId);

    return portletWindow;
  }
  @Override
  public void storePortletWindow(HttpServletRequest request, IPortletWindow portletWindow) {
    if (isDisablePersistentWindowStates(request)) {
      return;
    }

    final IUserInstance userInstance = this.userInstanceManager.getUserInstance(request);
    final IPerson person = userInstance.getPerson();
    if (person.isGuest()) {
      // Never persist things for the guest user, just rely on in-memory storage
      return;
    }

    final IStylesheetDescriptor themeStylesheetDescriptor =
        this.getThemeStylesheetDescriptor(request);

    final WindowState windowState = portletWindow.getWindowState();

    final IPortletEntity portletEntity = portletWindow.getPortletEntity();
    final WindowState entityWindowState = portletEntity.getWindowState(themeStylesheetDescriptor);

    // If the window and entity states are different
    if (windowState != entityWindowState && !windowState.equals(entityWindowState)) {
      final WindowState defaultWindowState = this.getDefaultWindowState(themeStylesheetDescriptor);

      // If a window state is set and is one of the persistent states set it on the entity
      if (!defaultWindowState.equals(windowState) && persistentWindowStates.contains(windowState)) {
        portletEntity.setWindowState(themeStylesheetDescriptor, windowState);
      }
      // If not remove the state from the entity
      else if (entityWindowState != null) {
        portletEntity.setWindowState(themeStylesheetDescriptor, null);
      }

      // Persist the modified entity
      this.portletEntityRegistry.storePortletEntity(request, portletEntity);
    }
  }
  @Override
  @RequestCache(keyMask = {false})
  public Set<IPortletWindow> getAllPortletWindows(HttpServletRequest request) {
    final IUserInstance userInstance = this.userInstanceManager.getUserInstance(request);
    final IUserPreferencesManager preferencesManager = userInstance.getPreferencesManager();
    final IUserLayoutManager userLayoutManager = preferencesManager.getUserLayoutManager();
    final Set<String> allSubscribedChannels = userLayoutManager.getAllSubscribedChannels();

    final Set<IPortletWindow> allLayoutWindows =
        new LinkedHashSet<IPortletWindow>(allSubscribedChannels.size());

    for (final String channelSubscribeId : allSubscribedChannels) {
      final IPortletEntity portletEntity =
          this.portletEntityRegistry.getOrCreatePortletEntity(
              request, userInstance, channelSubscribeId);

      final IPortletEntityId portletEntityId = portletEntity.getPortletEntityId();
      final Set<IPortletWindow> portletWindows =
          this.getAllPortletWindowsForEntity(request, portletEntityId);
      allLayoutWindows.addAll(portletWindows);
    }

    return allLayoutWindows;
  }
  @Override
  @RequestCache(keyMask = {false})
  public Set<IPortletWindow> getAllLayoutPortletWindows(HttpServletRequest request) {
    final IUserInstance userInstance = this.userInstanceManager.getUserInstance(request);
    final IUserPreferencesManager preferencesManager = userInstance.getPreferencesManager();
    final IUserLayoutManager userLayoutManager = preferencesManager.getUserLayoutManager();
    final Set<String> allSubscribedChannels = userLayoutManager.getAllSubscribedChannels();

    final Set<IPortletWindow> allLayoutWindows =
        new LinkedHashSet<IPortletWindow>(allSubscribedChannels.size());

    for (final String channelSubscribeId : allSubscribedChannels) {
      final IPortletEntity portletEntity =
          this.portletEntityRegistry.getOrCreatePortletEntity(
              request, userInstance, channelSubscribeId);
      if (portletEntity == null) {
        this.logger.debug(
            "No portlet entity found for layout node {} for user {}",
            channelSubscribeId,
            userInstance.getPerson().getUserName());
        continue;
      }

      final IPortletEntityId portletEntityId = portletEntity.getPortletEntityId();
      final IPortletWindow portletWindow =
          this.getOrCreateDefaultPortletWindow(request, portletEntityId);
      if (portletWindow == null) {
        this.logger.debug("No portlet window found for {}", portletEntity);
        continue;
      }

      allLayoutWindows.add(portletWindow);
    }

    return allLayoutWindows;
  }
  @Override
  public IPortletWindow getOrCreateStatelessPortletWindow(
      HttpServletRequest request, IPortletWindowId basePortletWindowId) {
    // Need the basePortletWindowId to be an instance of PortletWindowIdImpl so that we can extract
    // the entity ID
    if (!(basePortletWindowId instanceof PortletWindowIdImpl)) {
      final String basePortletWindowIdStr = basePortletWindowId.getStringId();
      basePortletWindowId = this.getPortletWindowId(request, basePortletWindowIdStr);
    }

    // Get the entity ID for the portlet window
    final IPortletEntityId portletEntityId =
        ((PortletWindowIdImpl) basePortletWindowId).getPortletEntityId();

    // Create the stateless ID
    final PortletWindowIdImpl statelessPortletWindowId =
        this.createPortletWindowId(STATELESS_PORTLET_WINDOW_ID, portletEntityId);

    // See if there is already a request cached stateless window
    IPortletWindow statelessPortletWindow =
        this.getPortletWindow(request, statelessPortletWindowId);
    if (statelessPortletWindow != null) {
      return statelessPortletWindow;
    }

    // Lookup the base portlet window to clone the stateless from
    final IPortletWindow basePortletWindow = this.getPortletWindow(request, basePortletWindowId);

    final PortletWindowCache<PortletWindowData> statelessPortletWindowDataMap =
        this.getStatelessPortletWindowDataMap(request, true);

    // If no base to clone from lookup the entity and pluto definition data
    if (basePortletWindow == null) {
      final IPortletEntity portletEntity =
          this.portletEntityRegistry.getPortletEntity(request, portletEntityId);
      if (portletEntity == null) {
        throw new IllegalArgumentException(
            "No IPortletEntity could be found for "
                + portletEntity
                + " while creating stateless portlet window for "
                + basePortletWindowId);
      }

      final IPortletDefinition portletDefinition = portletEntity.getPortletDefinition();
      final IPortletDefinitionId portletDefinitionId = portletDefinition.getPortletDefinitionId();
      final PortletDefinition portletDescriptor =
          this.portletDefinitionRegistry.getParentPortletDescriptor(portletDefinitionId);

      final PortletWindowData portletWindowData =
          new PortletWindowData(statelessPortletWindowId, portletEntityId);
      statelessPortletWindowDataMap.storeWindow(portletWindowData);

      statelessPortletWindow =
          new StatelessPortletWindowImpl(portletWindowData, portletEntity, portletDescriptor);
    }
    // Clone the existing base window
    else {
      final PortletWindowData portletWindowData =
          new PortletWindowData(statelessPortletWindowId, portletEntityId);
      portletWindowData.setExpirationCache(basePortletWindow.getExpirationCache());
      portletWindowData.setPortletMode(basePortletWindow.getPortletMode());
      portletWindowData.setWindowState(basePortletWindow.getWindowState());
      portletWindowData.setPublicRenderParameters(basePortletWindow.getPublicRenderParameters());
      portletWindowData.setRenderParameters(basePortletWindow.getRenderParameters());

      statelessPortletWindowDataMap.storeWindow(portletWindowData);

      final IPortletEntity portletEntity = basePortletWindow.getPortletEntity();
      final PortletDefinition portletDescriptor =
          basePortletWindow.getPlutoPortletWindow().getPortletDefinition();
      statelessPortletWindow =
          new StatelessPortletWindowImpl(portletWindowData, portletEntity, portletDescriptor);
    }

    // Cache the stateless window in the request
    final PortletWindowCache<IPortletWindow> portletWindowMap = this.getPortletWindowMap(request);
    portletWindowMap.storeWindow(statelessPortletWindow);

    return statelessPortletWindow;
  }