<%--
  Copyright 2003 Sun Microsystems, Inc. All rights reserved 
  Use is subject to license terms.
 --%>
<%-- @(#)ratingProcess.jsp --%>
<%@page language= "java" import="com.sun.portal.search.demo.Search, 
				com.sun.portal.search.soif.*, 
				com.sun.portal.search.soif.SOIFOutputStream, 
				com.sun.portal.search.soif.SOIFInputStream, 
				java.net.*, java.lang.*, 
				java.util.Random, java.util.Date" %> 
<%@page errorPage="error.jsp" %>

<%@ taglib uri="/tld/jx.tld" prefix="jx" %>
<%@ taglib uri="/tld/desktop.tld" prefix="dt" %>
<%@ taglib uri="/tld/desktopProviderContext.tld" prefix="dtpc" %>

<%
String rid, database, rating;
rid = request.getParameter("rid");
if (rid == null || (rid.equals(""))) {
    throw new Exception("Discussions: Cannot rate document");
}

// from view discussion and feedback form
rating = request.getParameter("rtg");
if (rating == null || rating.equals("")) {
    throw new Exception("Discussions: Cannot rate document");
}

database = request.getParameter("database");
if (database == null) {
    throw new Exception("Discussions: Cannot rate document");
} 
    
SOIF parentrd = new SOIF();
String parentRid=""; 
int parentRating=0, parentNumRating=0, parentPeakRating=0, parentSumRating=0;
%>

<dt:obtainChannel channel="$JSPProvider">
<dtpc:providerContext>
    <dtpc:getStringProperty key="searchServer" id="searchServer"/>
    <dtpc:getSessionID id="accessToken"/>

    <jx:declare id="searchServer" type="java.lang.String"/>
    <jx:declare id="accessToken" type="java.lang.String"/>

    <%
    String query = "URL = "+rid;
    Search search = new Search(query, "","",1,5,"search",database,searchServer,accessToken);
    search.doQuery(1,5);
    SOIFInputStream is = search.getResultStream();

    if (is != null) {
	parentrd = is.readSOIF();
	if (parentrd == null) 
	   throw new Exception("Discussions: parent RD not found in "+database+" database");

	// extract parent reference id field
	if (parentrd.getValue("rd-rating") != null) { 
	    parentRating = Integer.parseInt(parentrd.getValue("rd-rating")); 
	}
	if (parentrd.getValue("rd-num-rating") != null) { 
	    parentNumRating = Integer.parseInt(parentrd.getValue("rd-num-rating")); 
	}
	if (parentrd.getValue("rd-sum-rating") != null) { 
	    parentSumRating = Integer.parseInt(parentrd.getValue("rd-sum-rating")); 
	}
	if (parentrd.getValue("rd-peak-rating") != null) { 
	    parentPeakRating = Integer.parseInt(parentrd.getValue("rd-peak-rating")); 
	}

    } else {
       throw new Exception("Discussions: Failed to read inputstream");
    } 

    int ir = Integer.parseInt(rating); 

    parentNumRating = parentNumRating+1; 
    parentSumRating = parentSumRating+ir; 
    float tmp = (((float)parentSumRating) / (parentNumRating+1)); 
    parentRating = (int)Math.round(tmp); 
    if (parentRating > parentPeakRating) parentPeakRating = parentRating; 

    parentrd.replace("rd-rating", parentRating+""); 
    parentrd.replace("rd-peak-rating", parentPeakRating+""); 
    parentrd.replace("rd-sum-rating", parentSumRating+""); 
    parentrd.replace("rd-num-rating", parentNumRating+""); 

    try {
	URLConnection pc = new URL((String)pageContext.findAttribute("searchServer")).openConnection();
	pc.setAllowUserInteraction(true);
	pc.setUseCaches(false);
	pc.setDoOutput(true);
	pc.setDoInput(true);
	SOIFOutputStream sos = new SOIFOutputStream(pc.getOutputStream()); 
	pc.connect();

	SOIF hdr = new SOIF("RDMHEADER", "-");
	hdr.insert("rdm-type", "rd-submit-request");
	hdr.insert("rdm-access-token", accessToken);
	hdr.insert("submit-database", database);
	sos.write(hdr); 

	SOIF req = new SOIF("Request", "-");
	req.insert("submit-type", "persistent"); 
	req.insert("submit-operation", "update"); 
	req.insert("submit-view", "rd-rating,rd-sum-rating,rd-peak-rating,rd-num-rating");
	sos.write(req);
	sos.write(parentrd);
	sos.close();

	SOIFInputStream sis = new SOIFInputStream(pc.getInputStream());
	SOIF s; 
	while ((s = sis.readSOIF()) != null)  { %>
	    soif: <%=s%> <br>
	<% }
    } catch (Exception e) {
	throw new Exception("Discussions: Cannot submit comments. Failed to connect to "+searchServer);
    } %>
</dtpc:providerContext>
</dt:obtainChannel>

<% if (rid != null && (!rid.equals(""))) { %> 
    <jsp:forward page="viewDiscussion.jsp"/> 
<% } else { %> 
    <jsp:forward page="fullDiscussion.jsp"/> 
<% } %> 

