<%
/**
 * 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;
%>
<% 
  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="" 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="" 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="" 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="" 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) {          
          found = true;
          if (even) rowClass = "EvenRow";  
          else rowClass =  "OddRow"; 
          even = !even;
          String nodePath = row.getJcrPath();
          try {
            node = uicomponent.getNodeByPath(nodePath);
          } catch(Exception e) {
            continue;
          }
          if (node.getPrimaryNodeType().getName().equals("nt:resource")) {
            node = node.getParent();
            nodePath = node.getPath();
          }

          String excerpt = row.getRepExcerpt();
          excerpt = StringEscapeUtils.unescapeHtml(excerpt);
//          excerpt += row.getValue("rep:spellcheck()") == null ? "NNUULLLL" : 
//          	row.getValue("rep:spellcheck()").getString();
          String score = row.getJcrScore();
			if (node != null && !node.getPrimaryNodeType().getName().equals("exo:comments")) {		  
      %>
          <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")%>: <%=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="" 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="" 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>