/** This command is used to get the navigationtitle for a sitenode in a certain language. */
  public String doGetPageNavigationTitle() throws Exception {
    DatabaseWrapper dbWrapper = new DatabaseWrapper(CastorDatabaseService.getDatabase());
    // Database db = CastorDatabaseService.getDatabase();

    beginTransaction(dbWrapper.getDatabase());

    try {
      Principal principal = (Principal) this.getHttpSession().getAttribute("infogluePrincipal");
      if (principal == null) {
        try {
          Map arguments = new HashMap();
          arguments.put("j_username", CmsPropertyHandler.getAnonymousUser());
          arguments.put("j_password", CmsPropertyHandler.getAnonymousPassword());

          principal = ExtranetController.getController().getAuthenticatedPrincipal(arguments);
        } catch (Exception e) {
          throw new SystemException(
              "There was no anonymous user found in the system. There must be - add the user anonymous/anonymous and try again.",
              e);
        }
      }

      this.nodeDeliveryController =
          NodeDeliveryController.getNodeDeliveryController(
              getSiteNodeId(), getLanguageId(), getContentId());
      this.integrationDeliveryController =
          IntegrationDeliveryController.getIntegrationDeliveryController(
              getSiteNodeId(), getLanguageId(), getContentId());
      TemplateController templateController =
          getTemplateController(
              dbWrapper,
              getSiteNodeId(),
              getLanguageId(),
              getContentId(),
              getRequest(),
              (InfoGluePrincipal) principal,
              false);
      this.navigationTitle = templateController.getPageNavTitle(this.getSiteNodeId());

      closeTransaction(dbWrapper.getDatabase());
    } catch (Exception e) {
      logger.error("An error occurred so we should not complete the transaction:" + e, e);
      rollbackTransaction(dbWrapper.getDatabase());
      throw new SystemException(e.getMessage());
    }

    return "navigationTitle";
  }
  /**
   * This method simulates a call to a page so all castor caches fills up before we throw the old
   * page cache.
   *
   * @param db
   * @param siteNodeId
   * @param languageId
   * @param contentId
   */
  public void recache(DatabaseWrapper dbWrapper, Integer siteNodeId)
      throws SystemException, Exception {
    logger.info("recache starting..");

    HttpHelper helper = new HttpHelper();
    String recacheUrl =
        CmsPropertyHandler.getRecacheUrl()
            + "?siteNodeId="
            + siteNodeId
            + "&refresh=true&isRecacheCall=true";
    String response = helper.getUrlContent(recacheUrl, 30000);

    String recacheBaseUrl = CmsPropertyHandler.getRecacheUrl().replaceAll("/ViewPage.action", "");
    String pathsToRecacheOnPublishing = CmsPropertyHandler.getPathsToRecacheOnPublishing();
    if (pathsToRecacheOnPublishing.indexOf("pathsToRecacheOnPublishing") == -1) {
      String[] pathsToRecacheOnPublishingArray = pathsToRecacheOnPublishing.split(",");
      for (int i = 0; i < pathsToRecacheOnPublishingArray.length; i++) {
        recacheUrl =
            recacheBaseUrl
                + pathsToRecacheOnPublishingArray[i]
                + "?refresh=true&isRecacheCall=true";
        logger.info("calling recacheUrl:" + recacheUrl);
      }
    }

    LanguageVO masterLanguageVO =
        LanguageDeliveryController.getLanguageDeliveryController()
            .getMasterLanguageForSiteNode(dbWrapper.getDatabase(), siteNodeId);
    if (masterLanguageVO == null)
      throw new SystemException("There was no master language for the siteNode " + siteNodeId);

    Integer languageId = masterLanguageVO.getLanguageId();
    if (languageId == null) languageId = masterLanguageVO.getLanguageId();

    Integer contentId = new Integer(-1);

    Principal principal = (Principal) CacheController.getCachedObject("userCache", "anonymous");
    if (principal == null) {
      Map arguments = new HashMap();
      arguments.put("j_username", CmsPropertyHandler.getAnonymousUser());
      arguments.put("j_password", CmsPropertyHandler.getAnonymousPassword());

      principal =
          ExtranetController.getController()
              .getAuthenticatedPrincipal(dbWrapper.getDatabase(), arguments);

      if (principal != null) CacheController.cacheObject("userCache", "anonymous", principal);
    }

    FakeHttpSession fakeHttpServletSession = new FakeHttpSession();
    FakeHttpServletResponse fakeHttpServletResponse = new FakeHttpServletResponse();
    FakeHttpServletRequest fakeHttpServletRequest = new FakeHttpServletRequest();
    fakeHttpServletRequest.setParameter("siteNodeId", "" + siteNodeId);
    fakeHttpServletRequest.setParameter("languageId", "" + languageId);
    fakeHttpServletRequest.setParameter("contentId", "" + contentId);
    fakeHttpServletRequest.setRequestURI("ViewPage.action");

    fakeHttpServletRequest.setAttribute("siteNodeId", "" + siteNodeId);
    fakeHttpServletRequest.setAttribute("languageId", "" + languageId);
    fakeHttpServletRequest.setAttribute("contentId", "" + contentId);

    fakeHttpServletRequest.setServletContext(DeliverContextListener.getServletContext());

    BrowserBean browserBean = new BrowserBean();
    // this.browserBean.setRequest(getRequest());

    NodeDeliveryController nodeDeliveryController =
        NodeDeliveryController.getNodeDeliveryController(siteNodeId, languageId, contentId);
    IntegrationDeliveryController integrationDeliveryController =
        IntegrationDeliveryController.getIntegrationDeliveryController(
            siteNodeId, languageId, contentId);
    TemplateController templateController =
        getTemplateController(
            dbWrapper,
            siteNodeId,
            languageId,
            contentId,
            new FakeHttpServletRequest(),
            (InfoGluePrincipal) principal,
            false,
            browserBean,
            nodeDeliveryController,
            integrationDeliveryController);

    DeliveryContext deliveryContext =
        DeliveryContext.getDeliveryContext(/*(InfoGluePrincipal)this.principal*/ );
    // deliveryContext.setRepositoryName(repositoryName);
    deliveryContext.setSiteNodeId(siteNodeId);
    deliveryContext.setContentId(contentId);
    deliveryContext.setLanguageId(languageId);
    deliveryContext.setPageKey("" + System.currentTimeMillis());
    // deliveryContext.setSession(new Session(fakeHttpServletSession));
    // deliveryContext.setInfoGlueAbstractAction(null);
    deliveryContext.setHttpServletRequest(fakeHttpServletRequest);
    deliveryContext.setHttpServletResponse(fakeHttpServletResponse);

    templateController.setDeliveryContext(deliveryContext);

    // We don't want a page cache entry to be created
    deliveryContext.setDisablePageCache(true);

    SiteNodeVO siteNodeVO = templateController.getSiteNode(siteNodeId);
    SiteNodeVO rootSiteNodeVO =
        templateController.getRepositoryRootSiteNode(siteNodeVO.getRepositoryId());

    recurseSiteNodeTree(
        rootSiteNodeVO.getId(), languageId, templateController, principal /*, dbWrapper*/, 1, 0);

    List templates =
        ContentController.getContentController()
            .getContentVOWithContentTypeDefinition("HTMLTemplate", dbWrapper.getDatabase());
    Iterator templatesIterator = templates.iterator();
    {
      ContentVO template = (ContentVO) templatesIterator.next();

      String templateString =
          templateController.getContentAttribute(template.getId(), languageId, "Template", true);
    }

    RepositoryVO repository =
        RepositoryDeliveryController.getRepositoryDeliveryController()
            .getMasterRepository(dbWrapper.getDatabase());

    RepositoryDeliveryController.getRepositoryDeliveryController()
        .getRepositoryVOListFromServerName(
            dbWrapper.getDatabase(), "localhost", "8080", repository.getName());

    logger.info("recache stopped..");
  }
  private void recurseSiteNodeTree(
      Integer siteNodeId,
      Integer languageId,
      TemplateController templateController,
      Principal principal /*, DatabaseWrapper dbWrapper*/,
      int maxLevel,
      int currentLevel)
      throws Exception {
    SiteNode siteNode =
        SiteNodeController.getController()
            .getSiteNodeWithId(siteNodeId, templateController.getDatabase(), true);
    SiteNodeVO siteNodeVO = templateController.getSiteNode(siteNodeId);

    templateController.getContentAttribute(
        siteNodeVO.getMetaInfoContentId(), languageId, "Title", true);
    templateController.getContentAttribute(
        siteNodeVO.getMetaInfoContentId(), languageId, "NavigationTitle", true);
    templateController.getContentAttribute(
        siteNodeVO.getMetaInfoContentId(), languageId, "NiceURIName", true);
    templateController.getContentAttribute(
        siteNodeVO.getMetaInfoContentId(), languageId, "Description", true);
    templateController.getContentAttribute(
        siteNodeVO.getMetaInfoContentId(), languageId, "ComponentStructure", true);

    List childPages = templateController.getChildPages(siteNodeId);

    templateController.getRepositoryRootSiteNode(siteNodeVO.getRepositoryId());
    templateController.getParentSiteNode(siteNodeVO.getId());

    Collection childSiteNodes = siteNode.getChildSiteNodes();

    /*
       List childSiteNodeIds = new ArrayList();
       Iterator childSiteNodesIterator = childSiteNodes.iterator();
    while(childSiteNodesIterator.hasNext())
       {
        SiteNode childSiteNode = (SiteNode)childSiteNodesIterator.next();
        childSiteNodeIds.add(childSiteNode.getSiteNodeId());
       }

       templateController.commitDatabase();

    Iterator childSiteNodeIdsIterator = childSiteNodeIds.iterator();
    while(childSiteNodeIdsIterator.hasNext())
       {
        Integer childSiteNodeId = (Integer)childSiteNodeIdsIterator.next();
        recurseSiteNodeTree(childSiteNodeId, languageId, templateController, principal);
       }

       */

    Iterator childSiteNodesIterator = childSiteNodes.iterator();
    while (childSiteNodesIterator.hasNext()) {
      SiteNode childSiteNode = (SiteNode) childSiteNodesIterator.next();
      if (maxLevel > currentLevel)
        recurseSiteNodeTree(
            childSiteNode.getId(),
            languageId,
            templateController,
            principal,
            maxLevel,
            currentLevel + 1);
    }

    Repository repository =
        RepositoryController.getController()
            .getRepositoryWithId(siteNodeVO.getRepositoryId(), templateController.getDatabase());
    Collection languages = repository.getRepositoryLanguages();
    Iterator languagesIterator = languages.iterator();
    while (languagesIterator.hasNext()) {
      RepositoryLanguage repositoryLanguage = (RepositoryLanguage) languagesIterator.next();
      LanguageDeliveryController.getLanguageDeliveryController()
          .getLanguageIfSiteNodeSupportsIt(
              templateController.getDatabase(),
              repositoryLanguage.getLanguage().getId(),
              siteNodeId);
    }

    Integer contentId = new Integer(-1);

    FakeHttpSession fakeHttpServletSession = new FakeHttpSession();
    FakeHttpServletResponse fakeHttpServletResponse = new FakeHttpServletResponse();
    FakeHttpServletRequest fakeHttpServletRequest = new FakeHttpServletRequest();
    fakeHttpServletRequest.setParameter("siteNodeId", "" + siteNodeId);
    fakeHttpServletRequest.setParameter("languageId", "" + languageId);
    fakeHttpServletRequest.setParameter("contentId", "" + contentId);
    fakeHttpServletRequest.setRequestURI("ViewPage.action");

    fakeHttpServletRequest.setAttribute("siteNodeId", "" + siteNodeId);
    fakeHttpServletRequest.setAttribute("languageId", "" + languageId);
    fakeHttpServletRequest.setAttribute("contentId", "" + contentId);

    fakeHttpServletRequest.setServletContext(DeliverContextListener.getServletContext());

    BrowserBean browserBean = new BrowserBean();
    // this.browserBean.setRequest(getRequest());

    NodeDeliveryController nodeDeliveryController =
        NodeDeliveryController.getNodeDeliveryController(siteNodeId, languageId, contentId);
    IntegrationDeliveryController integrationDeliveryController =
        IntegrationDeliveryController.getIntegrationDeliveryController(
            siteNodeId, languageId, contentId);
    TemplateController subTemplateController =
        getTemplateController(
            templateController.getDatabaseWrapper(),
            siteNodeId,
            languageId,
            contentId,
            new FakeHttpServletRequest(),
            (InfoGluePrincipal) principal,
            false,
            browserBean,
            nodeDeliveryController,
            integrationDeliveryController);

    DeliveryContext deliveryContext =
        DeliveryContext.getDeliveryContext(/*(InfoGluePrincipal)this.principal*/ );
    // deliveryContext.setRepositoryName(repositoryName);
    deliveryContext.setSiteNodeId(siteNodeId);
    deliveryContext.setContentId(contentId);
    deliveryContext.setLanguageId(languageId);
    deliveryContext.setPageKey("" + System.currentTimeMillis());
    // deliveryContext.setSession(new Session(fakeHttpServletSession));
    // deliveryContext.setInfoGlueAbstractAction(null);
    deliveryContext.setHttpServletRequest(fakeHttpServletRequest);
    deliveryContext.setHttpServletResponse(fakeHttpServletResponse);

    subTemplateController.setDeliveryContext(deliveryContext);

    // We don't want a page cache entry to be created
    deliveryContext.setDisablePageCache(true);

    SiteNodeVO rootSiteNodeVO =
        templateController.getRepositoryRootSiteNode(siteNodeVO.getRepositoryId());

    String pagePath = subTemplateController.getCurrentPagePath();

    CacheController.cacheObject("newPagePathCache", deliveryContext.getPageKey(), pagePath);
  }