<%--
  Copyright 2003 Sun Microsystems, Inc. All rights reserved 
  Use is subject to license terms.
 --%>
<%-- @(#)viewDiscussionDisplay.jsp --%>
<%@page language= "java" import="com.sun.portal.providers.jsp.JSPProvider, 
				com.sun.portal.search.soif.SOIF,
				com.sun.portal.search.soif.SOIFInputStream,
				com.sun.portal.search.providers.SearchContext,
				com.sun.portal.discussions.providers.Comments,
				com.sun.portal.search.util.DateParser,
				com.sun.portal.search.util.Encoder, java.lang.*, 
				java.util.*, java.text.*" %> 
				
<%@page errorPage="error.jsp" %> 
<%@ taglib uri="/tld/jx.tld" prefix="jx" %> 
<%@ taglib uri="/tld/search.tld" prefix="search" %> 
<%@ taglib uri="/tld/desktop.tld" prefix="dt" %>

<%-- Get portal config information and portal desktop theme values --%> 
<%@ include file="declare.jsp" %>

<STYLE type="text/css"> 
    .discMessage{font-family: <%=tFontFace%>; color: <%=tFontColor%>; font-size:x-small; font-weight: bold;} 
    .discTitle{font-family: <%=tFontFace%>; font-size:small; font-weight: bold;}
    .discTitleHeader{font-family: <%=tFontFace%>; font-size:medium; font-weight: bold;}
    .discDate{font-family: <%=tFontFace%>; color: <%=tFontColor%>; font-size:small;}
    .discRating{font-family: <%=tFontFace%>; font-size:small; font-weight: bold;}
    .discDesc{font-family: <%=tFontFace%>; color: <%=tFontColor%>; font-size:small;}
    .discFilter{font-family: <%=tFontFace%>; color: <%=tBorderColor%>; font-size:small;}
    .discNav{font-family: <%=tFontFace%>; font-size:small;}
    .discBarLbl{font-family: <%=tFontFace%>; color:<%=tTitleFontColor%>; font-size:small; font-weight: bold;}
    .discBarFont{font-family: <%=tFontFace%>; color: <%=tTitleFontColor%>; font-size:small;}
    .discButton{background: <%=tTitleBarColor%>; color:<%=tTitleFontColor%>; font-family: <%=tFontFace%>; font-size:x-small; font-weight: bold;}
    .discSearchButton{background: <%=tTabColor%>; color:<%=tTabFontColor%>; font-family: <%=tFontFace%>; font-size:x-small; font-weight: bold;}
</STYLE>

<!-- Beginning of about search results -->
<search:getNoHits id= "noHits"/>

<table cellpadding="0" cellspacing="0" border="0" width="100%" bgcolor="<%=tBgColor%>">
<tr><td colspan="3" img="<dt:scontent/>/images/spacer.gif" width="1" height="10"></td>
</tr>
<jx:choose>
<jx:when test="$noHits">
    <tr>
    <td><img src="<dt:scontent/>/images/spacer.gif" width="1" height="1"></td>
    <td class="discMessage">Discussion not found.</td>
    <td><img src="<dt:scontent/>/images/spacer.gif" width="1" height="1"></td>
    </tr>
    <tr><td colspan="3" img="<dt:scontent/>/images/spacer.gif" width="1" height="10"></td>
    </tr>
</jx:when> 

<jx:otherwise> 
    <!-- Beginning of display results --> 
    <search:getResult id="resultstream"/> 
    <jx:declare id="resultstream" type="SOIFInputStream"/>

    <%-- Get the comments object and pass the search results. cluster and sort the comments --%>
    <% String did = request.getParameter("did"); %>
    <% Comments comments = new Comments(); %>
    <% comments.clusterComments(resultstream, did); %>
    <% pageContext.setAttribute("comments", comments, PageContext.REQUEST_SCOPE); %>

    <jsp:include page="viewDiscussionHeader.jsp" flush="true"/>
    <jsp:include page="viewDiscussionBar.jsp" flush="true"/>

    <% ArrayList a = (ArrayList)comments.getComments(); %>

    <tr><td colspan="3" img="<dt:scontent/>/images/spacer.gif" width="1" height="20"></td>
    </tr>
    <tr>
	<td><img src="<dt:scontent/>/images/spacer.gif" width="1" height="1"></td>
        <td class="discMessage">
	<% if (a.size() == 0) { %>
	    No comments in discussion 
	<% } else if (a.size() == 1) { %> 
	    1 comment in discussion
	<% } else { %>
	   <%=a.size()%> comments in discussion
        <% } %>
	</td>
	<td><img src="<dt:scontent/>/images/spacer.gif" width="1" height="1"></td>
    </tr>

    <tr>
    <td><img src="<dt:scontent/>/images/spacer.gif" width="1" height= "1"></td>
    <td bgcolor="<%=tTitleBarColor%>"><img src="<dt:scontent/>/images/spacer.gif" width="1" height="1"></td>
    <td><img src="<dt:scontent/>/images/spacer.gif" width="1" height="1"></td>
    </tr>

    <tr><td colspan="3" img="<dt:scontent/>/images/spacer.gif" width="1" height="15"></td>
    </tr>
    <tr><td colspan="3">
    <% 
    int prevDepth = 1; 
    int parentDepth = 0;
    Integer pd = (Integer)pageContext.findAttribute("parentDepth");
    if (pd != null) {
       parentDepth = pd.intValue();
    } %>

    <!-- Display comments -->
    <% for (int i = 0; i < a.size(); i++) { 
	SOIF cmt = (SOIF)a.get(i); 
	if (cmt != null && cmt.getURL() != null) { %>
	    <% if (i == 0) { %>
	        <UL>
	    <% } %>
	    <jx:if test="$threaded">
	      <%
		int d = comments.findDepth(cmt); 
		d = d-parentDepth; %>
		<% if (i == 0) { %>
		<% } else if (d == (prevDepth+1)) { %>
		    <UL>
		<% } else if (d < prevDepth) { %>
		   <% int indent = prevDepth - d; %>
		   <% for (int j=0; j<indent; j++) { %>
		       </UL>
		   <% } %>
		<% } else { %>
		<% } %>
		<% prevDepth = d; %>
            </jx:if>
	    <%
	    String title = cmt.getValue("title");
	    if (title == null) title = "";

	    // Get the integer comment rating and convert it to user-friendly text
	    Integer e =(Integer)pageContext.getAttribute("filter", PageContext.REQUEST_SCOPE);
	    int filter = 0;
	    if (e != null) {
	       filter = e.intValue();
	    }
	    String pkrating = cmt.getValue("rd-rating");
	    int rtg = 4;
	    if (pkrating != null) { 
		rtg = Integer.parseInt(pkrating); 
		if (pkrating.equals("-1")) { 
		    pkrating = "Irrelevant"; 
		} else if (pkrating.equals("0")) { 
		    pkrating = "Routine"; 
		} else if (pkrating.equals("1")) { 
		    pkrating = "Interesting"; 
		} else if (pkrating.equals("2")) { 
		    pkrating = "Important"; 
		} else if (pkrating.equals("3")) { 
		    pkrating = "Must Read"; 
		} else { 
		    pkrating = "Routine"; 
		}
	    } 
	    %>

	    <LI>
	    <%-- Display title --%>
	    <% if (rtg >= filter) { %>
		<a href="<%=dpurl%>?<%=channelName%>_dmode=vl&did=<%=Encoder.urlEncode(cmt.getURL())%>"><span class="discTitle"><%=title%></span></a> -
            <% } else { %>
		<span class="discFilter"><b><%=title%></b>-</span>
	    <% } %>
	   
	    <%-- Display author --%>
	    <% String athr = cmt.getValue("author"); %>
	    <% if ((athr != null) && (athr.equals("") || athr.equals("authlessanonymous"))) 
			athr = (String)pageContext.findAttribute("anonymousAuthor"); %>
	    <% if (rtg >= filter) { %>
		<span class="discTitle"><%=athr%></span>
	    <% } else { %>
	        <span class="discFilter"><%=athr%></span>
	    <% } %>

	    <%-- Display date --%>
	    <% String lastPost = cmt.getValue("rd-last-changed"); 
	    if (lastPost != null) {
		DateFormat dft = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.SHORT, locale); 
		try {
		    Date d = DateParser.parse(lastPost); %>
		    <% if (rtg >= filter) { %>
			<span class="discDate">on <%=dft.format(d)%></span>
		    <% } else { %>
			<span class="discFilter">on <%=dft.format(d)%></span>
		    <% }
                } catch (Exception ex) {
		} 
	    } %>

	    <%-- Display rating --%>
	    <% if (rtg >= filter) { %>
		<span class="discRating">(<%=pkrating%>)</span>
	    <% } else { %>
		<span class="discFilter">(<%=pkrating%>)</span>
	    <% } %>


	    <%-- Display description, optional --%>
	    <jx:if test="$expand">
		<%
		String desc = cmt.getValue("description");
		if (desc != null && rtg >= filter) {
		%>
		    <br>
		    <span class="discDesc"><%=SearchContext.htmlEncode(desc)%></span>
		    <jx:set var="rating">
		       <%=rtg%>
		    </jx:set>
		    <jx:set var="rid">
		       <%=cmt.getURL()%>
		    </jx:set>
		    <br>
		    <a href="<%=dpurl%>?<%=channelName%>_dmode=cmt&ref_id=<%=Encoder.urlEncode(cmt.getURL())%>&database=<%=dbname%>&t=<%=Encoder.urlEncode(title)%>"><span class="discNav">Post Reply</span></a>
		    <%@ include file="rating.jsp"%>
		<% } %>
	   </jx:if>
	   </LI>
	<% } %>
    <% } %>
    <% for (int j=0; j<prevDepth;j++) { %>
	</UL>
    <% } %>
    </td>
    </tr>
    <tr><td colspan="3" img="<dt:scontent/>/images/spacer.gif" width="1" height="20"></td>
    </tr>

    <%-- no pagination at present. requires caching 
    <search:getTotalPages id="total2"/>
    <%@ include file="pageFooter.jsp" %>
    --%>
</jx:otherwise>
</jx:choose>
</table>
