<%
/**
 * Created by The eXo Platform SARL
 * Author : Dang Van Minh
 *          minh.dang@exoplatform.com
 * Apr 17, 2007 3:37:43 PM
 */
%>
<%
  import org.exoplatform.ecm.webui.utils.Utils;
  import org.exoplatform.services.jcr.impl.core.JCRPath;
  import org.exoplatform.services.jcr.impl.core.SessionImpl;
  import javax.jcr.Node;
  import javax.jcr.Session;
  import org.apache.commons.lang.StringEscapeUtils;
  import java.util.ArrayList;
  import org.exoplatform.services.jcr.util.Text;
  import org.exoplatform.wcm.webui.reader.ContentReader;
%>
<%
  def resultList = uicomponent.getCurrentList();
  if(resultList == null) return;
  long searchTime = uicomponent.getSearchTime()/1000;
  int resultSize = uicomponent.getUIPageIterator().getAvailable();
  def rcontext = _ctx.getRequestContext();
  rcontext.getJavascriptManager().importJavascript('eXo.ecm.UIJCRExplorer','/ecmexplorer/javascript/');
  rcontext.getJavascriptManager().importJavascript('eXo.ecm.ECMUtils','/ecm-wcm-extension/javascript/');
  java.text.DateFormat dateFormat = uicomponent.getSimpleDateFormat();
%>
<div class="UISearchResult" id="$uicomponent.id">
  <table class="UIGrid" style="width: 97%">
    <thead>
      <tr>
        <th width="45px">
          <%
            if (uicomponent.iconType.equals("BlueUpArrow")) {
          %>
            <div onclick="<%=uicomponent.event("SortASC", Utils.formatNodeName("type"))%>" title="<%=_ctx.appRes("UISearchResult.title.typeASC")%>">
              <%=_ctx.appRes("UISearchResult.header.type")%>
              <img src="/eXoResources/skin/sharedImages/Blank.gif" alt="<%=_ctx.appRes("UISearchResult.title.typeASC")%>" class="BlueUpArrow16x16Icon" />
            </div>
          <%
            } else if (uicomponent.iconType.equals("BlueDownArrow")) {
          %>
          <div onclick="<%=uicomponent.event("SortDESC", Utils.formatNodeName("type"))%>" title="<%=_ctx.appRes("UISearchResult.title.typeDESC")%>">
            <%=_ctx.appRes("UISearchResult.header.type")%>
            <img src="/eXoResources/skin/sharedImages/Blank.gif" alt="<%=_ctx.appRes("UISearchResult.title.typeDESC")%>" class="BlueDownArrow16x16Icon" />
          </div>
          <%
            } else {
          %>
            <div onclick="<%=uicomponent.event("SortASC", Utils.formatNodeName("type"))%>" title="<%=_ctx.appRes("UISearchResult.title.typeASC")%>">
              <%=_ctx.appRes("UISearchResult.header.type")%>
            </div>
          <% } %>
        </th>
        <th><%=_ctx.appRes("UISearchResult.header.name")%></th>
        <th style="width:80px;">
          <%
            if (uicomponent.iconScore.equals("BlueUpArrow")) {
          %>
            <a href="<%=uicomponent.event("SortASC", Utils.formatNodeName("score"))%>" title="<%=_ctx.appRes("UISearchResult.title.scoreASC")%>">
              <%=_ctx.appRes("UISearchResult.header.score")%>
              <img src="/eXoResources/skin/sharedImages/Blank.gif" alt="<%=_ctx.appRes("UISearchResult.title.scoreASC")%>" class="BlueUpArrow16x16Icon"/>
            </a>
          <%
            } else if (uicomponent.iconScore.equals("BlueDownArrow")) {
          %>
          <a href="<%=uicomponent.event("SortDESC", Utils.formatNodeName("score"))%>" title="<%=_ctx.appRes("UISearchResult.title.scoreDESC")%>">
            <%=_ctx.appRes("UISearchResult.header.score")%>
            <img src="/eXoResources/skin/sharedImages/Blank.gif" alt="<%=_ctx.appRes("UISearchResult.title.scoreDESC")%>" class="BlueDownArrow16x16Icon"/>
          </a>
          <%
            } else {
          %>
            <a href="<%=uicomponent.event("SortASC", Utils.formatNodeName("score"))%>" title="<%=_ctx.appRes("UISearchResult.title.scoreASC")%>">
              <%=_ctx.appRes("UISearchResult.header.score")%>
            </a>
          <% } %>
        <th style="width:80px;"><%=_ctx.appRes("UISearchResult.header.action")%></th>
      </tr>
    </thead>
    <tbody>
    <%

    def rowClass = null ;
      boolean even = true ;
      boolean found = false ;
      Node node = null;
	  def listAddedResult = new ArrayList<Node>();
      if (resultList != null && resultList.size() > 0) {
	    def count = 0;
        for (row in resultList) {

          String nodePath = row.getJcrPath();
          try {
            node = uicomponent.getNodeByPath(nodePath);
            if (node==null) {
              continue;
            };
          } catch(Exception e) {
            continue;
          }
          if (node.getPrimaryNodeType().getName().equals("nt:resource")) {
            node = node.getParent();
            nodePath = node.getPath();
          }

          if (node.getPrimaryNodeType().getName().equals("exo:comments")) {
            node = node.getParent().getParent();
            nodePath = node.getPath();
          }

        found = true;
        if (even) rowClass = "EvenRow";
        else rowClass =  "OddRow";
        even = !even;
        String excerpt = row.getRepExcerpt();
        excerpt = StringEscapeUtils.unescapeHtml(excerpt);
        String score = row.getJcrScore();
			if (node != null) {
      %>
          <tr class="$rowClass">
            <td>
              <div class="default16x16Icon ItemIcon <%=Utils.getNodeTypeIcon(node, "16x16Icon")%>" style="margin:auto"><span></span></div>
            </td>
            <td>
              <div class="Text" title="<%=dateFormat.format(uicomponent.getDateCreated(node))%>">
                <%=Utils.getTitleWithSymlink(node)%>
              </div>
              <div>$excerpt</div>
              <div><%=_ctx.appRes("UISearchResult.header.path")%>: <%=ContentReader.getXSSCompatibilityContent(node.getPath())%></div>
              <div><%=_ctx.appRes("UISearchResult.header.dateCreated")%>: <%=dateFormat.format(uicomponent.getDateCreated(node))%></div>
            </td>
            <td><div class="ActionContainer">$score</div></td>
            <td>
              <div class="ActionContainer">
                <img onclick="<%=uicomponent.event("View", Utils.formatNodeName(node.getPath()))%>" alt="<%=_ctx.appRes("UISearchResult.tooltip.View")%>" title="<%=_ctx.appRes("UISearchResult.tooltip.View")%>" src="/eXoResources/skin/sharedImages/Blank.gif" class="View16x16Icon"/>
                <img src="/eXoResources/skin/sharedImages/Blank.gif" onclick="<%=uicomponent.event("OpenFolder", Utils.formatNodeName(node.getPath()))%>" title="<%=_ctx.appRes("UISearchResult.tooltip.OpenFolder")%>" alt="<%=_ctx.appRes("UISearchResult.tooltip.OpenFolder")%>" class="OpenFolder16x16Icon"/>
              </div>
            </td>
          </tr>
    <%
		  }
        }
      } else {
    %>
      <tr class="EvenRow">
        <td colspan="4" style="text-align:center;font-style:italic; color: #EF5604;"><%=_ctx.appRes("UISearchResult.msg.empty")%></td>
      </tr>
    <%}%>
    </tbody>
  </table>
  <% if(uicomponent.getUIPageIterator().getAvailablePage() > 1){ %>
  <div style="margin:1px 0px 10px 0px;">
    <%_ctx.renderUIComponent(uicomponent.getUIPageIterator())%>
  </div>
  <% } %>
</div>