<%
   /**
    * Created by The eXo Platform SAS
    * Author : eXoPlatform
    *          hailt@exoplatform.com
    * Feb 21, 2012  
    */
import java.util.HashMap;
import java.util.List;
import org.apache.commons.lang.StringEscapeUtils;

import javax.jcr.Node;
import javax.portlet.PortletPreferences;

import org.exoplatform.services.wcm.publication.WCMComposer;

import org.exoplatform.webui.application.WebuiRequestContext;
import org.exoplatform.webui.application.portlet.PortletRequestContext;
import org.exoplatform.wcm.webui.clv.UICLVPortlet;
import org.exoplatform.portal.application.PortalRequestContext;

import org.exoplatform.web.url.navigation.NavigationResource;
import org.exoplatform.web.url.navigation.NodeURL;

import org.exoplatform.portal.webui.util.Util;
import org.exoplatform.wcm.webui.Utils;
import org.exoplatform.services.wcm.utils.WCMCoreUtils;
import org.exoplatform.wcm.webui.reader.ContentReader;


public void renderCategories() {
  renderHomeMenu();  
  renderCategoryMenu();
}

public void renderHomeMenu() {
  PortletRequestContext portletRequestContext = WebuiRequestContext.getCurrentInstance();
  PortletPreferences preferences = portletRequestContext.getRequest().getPreferences();
  def folderPath = preferences.getValue(UICLVPortlet.PREFERENCE_ITEM_PATH, null);
  def data = folderPath.split(":");
  def repository = data[0];
  def workspace = data[1];
  def itemPath = data[2];
    
  //render the Home menu
  def lblHome = _ctx.appRes("CategoryNavigationTemplate.label.home");
  
  //build the url
  PortalRequestContext portalRequestContext = Util.getPortalRequestContext();
  def portletId = portalRequestContext.getNodePath();
  NodeURL nodeURL = Util.getPortalRequestContext().createURL(NodeURL.TYPE);
  NavigationResource resource = new NavigationResource(portalRequestContext.getSiteType(), portalRequestContext.getSiteName(), portletId);
  nodeURL.setResource(resource);
  def homeURL = nodeURL.toString();
  
  %>
    <li><a href="$homeURL" class="TabLeft" rel="tooltip" data-placement="bottom" title="$lblHome">$lblHome</a></li>
  <%
}
    
private void renderCategoryMenu() {
  PortletRequestContext portletRequestContext = WebuiRequestContext.getCurrentInstance();
  PortletPreferences preferences = portletRequestContext.getRequest().getPreferences();
  def folderPath = preferences.getValue(UICLVPortlet.PREFERENCE_ITEM_PATH, null);
  def data = folderPath.split(":");
  def repository = data[0];
  def workspace = data[1];
  def itemPath = data[2];
    
  //render category menu
  WCMComposer wcmComposer = WCMCoreUtils.getService(WCMComposer.class);
  HashMap<String, String> filters = new HashMap<String, String>();
  filters = new HashMap<String, String>();
  filters.put(WCMComposer.FILTER_MODE, Utils.getCurrentMode());  
  
  PortalRequestContext portalRequestContext = Util.getPortalRequestContext();
  def selectedCategory = portalRequestContext.getRequestParameter("category-name");
  def selectedContent = portalRequestContext.getRequestParameter("content-id");
    
  def categories = uicomponent.getCategories(false);
  for (category in categories) {
    def path = category.getPath();
    def categoryName = category.getName();
    def categoryTitle = category.getTitle();
    
    def selectedCategoryClass = "";
    if (selectedCategory != null && selectedCategory.equals(categoryName)) {
      selectedCategoryClass = "SelectedCategory";
    }
      
    %>
      <li><a href="javascript:void(0);" id="$categoryName" class="TabLeft IconClose $selectedCategoryClass" rel="tooltip" data-placement="bottom" title="$categoryTitle" onclick="eXo.ecm.CategoryNavigation.toggleSubMenu(event)">$categoryTitle</a>
    <%
      
    //render sub menu  
    List<Node> contents = wcmComposer.getContents(workspace, path, filters, WCMCoreUtils.getUserSessionProvider());    
    if (contents != null && contents.size() > 0) {
      %>
        <ul style="display: block;" class="SubMenu">
      <%
      for (content in contents) {
        def nodePath = content.getPath();
        def nodeTitle = org.exoplatform.services.cms.impl.Utils.getTitle(content);
      
        //if content is a frozenNode, we'll get the original node 
        if (content.isNodeType("nt:frozenNode")) {
          String uuid = content.getProperty("jcr:frozenUuid").getString();
          Node originalNode = content.getSession().getNodeByUUID(uuid);
          nodePath = originalNode.getPath();
          nodeTitle = org.exoplatform.services.cms.impl.Utils.getTitle(originalNode);
        }
        nodeTitle = ContentReader.getXSSCompatibilityContent(nodeTitle);
        
        //build the url        
        def portletId = portalRequestContext.getNodePath();
        NodeURL nodeURL = Util.getPortalRequestContext().createURL(NodeURL.TYPE);
        NavigationResource resource = new NavigationResource(portalRequestContext.getSiteType(), portalRequestContext.getSiteName(), portletId);
        nodeURL.setResource(resource);

        def contentId = "/" + repository + "/" + workspace + nodePath;
        nodeURL.setQueryParameterValue("category", categoryTitle);
        nodeURL.setQueryParameterValue("category-name", categoryName);
        nodeURL.setQueryParameterValue("content-title", nodeTitle);
        nodeURL.setQueryParameterValue("content-id", contentId);
        def contentURL = nodeURL.toString();
        
        if (selectedContent != null && selectedContent.equals(contentId)) {
          %>
            <li class="SelectedContent"><a href="$contentURL" rel="tooltip" data-placement="bottom" title="$nodeTitle">$nodeTitle</a></li>
          <%
        } else {
          %>
            <li><a href="$contentURL" rel="tooltip" data-placement="bottom" title="$nodeTitle">$nodeTitle</a></li>
          <%
        }
      }
      
      %>
        </ul>
      <%
    }
    
    %>
      </li>
    <%
  }
  
}
  
  
%>
      
<div class="UIMicroSite">
  <div class="UIContainer">
    <div class="ColumLeft">
      <ul>
        <%
          renderCategories();
        %>
      </ul>
    </div>
  </div>
</div>