
<%@page import="java.util.*" %>
<sql:setDataSource driver="${initParam.dataSourceDriver}" url="${initParam.dataSourceUrl}" user="${initParam.dataSourceUser}" password="${initParam.dataSourcePassword}"/>

<c:set var="hostname"><%=getURI(serviceBean.serviceItem.attributeSets).getHost()%></c:set>

<c:choose>
<c:when test="${param.action=='addPCD'}">
  <sql:update>
  INSERT INTO printer(printer_id,hostname,printer_group_id,
  name,location)
  VALUES(printer_seq.nextval,?,?,
  (SELECT department_id FROM printergroup WHERE printer_group_id=?),?,?)
  <sql:param value="${hostname}"/>
  <sql:param value="${param.pcdGroup}"/>
  <sql:param value="${param.pcdName}"/>
  <sql:param value="${param.pcdLocation}"/>
  </sql:update>
</c:when>
<c:when test="${param.action=='updatePCD'}">
  <sql:update>
  UPDATE printer set printer_group_id=?,
  name=?,location=?
  WHERE printer_id=?
  <sql:param value="${param.pcdGroup}"/>
  <sql:param value="${param.pcdName}"/>
  <sql:param value="${param.pcdLocation}"/>
  <sql:param value="${param.pcdID}"/>
  </sql:update>
</c:when>
<c:when test="${param.action=='removePCD'}">
  <sql:update>
  DELETE from printer WHERE printer_id=?
  <sql:param value="${param.pcdID}"/>
  </sql:update>
</c:when>
</c:choose>

<sql:query var="rs">
  SELECT printer_id,printer_group_id, name, location
  FROM printer
  WHERE hostname=?
  <sql:param value="${hostname}"/>
</sql:query>
<table border=1>
<tr><td>Name</td><td>Location</td><td>Group</td><td></td></tr>
<FORM ACTION="<c:out value='${alias}'/>/selector.jsp?view=configure" METHOD=POST>
<c:forEach items="${rs.rows}" var="row">
<c:set var="pcdRow" value="${row}"/>
</c:forEach>
<sql:query var="rs">
SELECT printer_group_id, z.name printer_group, y.name department
FROM printergroup z,department y
WHERE z.department_id=y.department_id
ORDER BY y.name, z.name
</sql:query>

<tr>
<c:choose>
<c:when test="${pcdRow!=null}">
    <INPUT TYPE=HIDDEN NAME="action" VALUE="updatePCD">
    <c:set var="id" value="${pcdRow.printer_id}"/>
    <INPUT TYPE=HIDDEN NAME="pcdID" VALUE='<c:out value="${id}"/>'>
    <td><input type=text name=pcdName value='<c:out value="${pcdRow.name}"/>'></td>
    <td><input type=text name=pcdLocation value='<c:out value="${pcdRow.location}"/>'></td>
    <td>
<select name=pcdGroup>
  <c:forEach items="${rs.rows}" var="row">
  <c:set var="pcdRow" value="${row}"/>
  <option 
<c:if test="${row.printer_group_id==pcdRow.printer_group_id}">SELECTED</c:if>
value="<c:out value='${row.printer_group_id}'/>">
  <c:out value="${row.department} - ${row.printer_group}"/></option>
  </c:forEach>
</select>
    </td>
    <td><input type=submit value="update">
        <a href="<c:out value='${alias}'/>/selector.jsp?view=configure&action=removePCD&pcdID=<c:out value='${id}'/>">remove</a>
    </td>
</c:when>
<c:otherwise>
    <INPUT TYPE=HIDDEN NAME="action" VALUE="addPCD">
    <td><input type=text name=pcdName></td>
    <td><input type=text name=pcdLocation></td>
    <td>
<select name=pcdGroup>
  <option>choose one</option>
  <c:forEach items="${rs.rows}" var="row">
  <c:set var="pcdRow" value="${row}"/>
  <option value="<c:out value='${row.printer_group_id}'/>">
  <c:out value="${row.department} - ${row.printer_group}"/></option>
  </c:forEach>
</select>
    </td>
    <td><input type=submit value="set">
    </td>
</c:otherwise>
</c:choose>
<tr>
</FORM>
</table>


<%
Object[] methodPropertyGetParams = new Object[] {null};
pageContext.setAttribute("methodPropertyGetParams", methodPropertyGetParams);
Object[] methodPropertySetParams = new Object[] {null, null};
pageContext.setAttribute("methodPropertySetParams", methodPropertySetParams);
pageContext.setAttribute("serviceBean", serviceBean);

serviceBean.setMethodName("getPropertyKeys");
serviceBean.setMethodParamClasses(null);
serviceBean.setMethodParams(null);
String[] keys = (String[] )serviceBean.getMethodResult();
%>
<c:if test="${param.action=='save'}">
    <%
    serviceBean.setMethodName("setProperty");
    serviceBean.setMethodParamClasses("java.lang.String,java.lang.String");
    serviceBean.setMethodParams(methodPropertySetParams);
    for(int i=0; i<keys.length; i++) {
        String key = keys[i];
        methodPropertySetParams[0] = key;
        if(request.getParameter(key)==null) continue;
        methodPropertySetParams[1] = request.getParameter(key);
        serviceBean.getMethodResult();
    }
    %>
</c:if>
<table border=0>
    <%
    serviceBean.setMethodName("getProperty");
    serviceBean.setMethodParamClasses("java.lang.String");
    serviceBean.setMethodParams(methodPropertyGetParams);
//    String[] keys = serviceBean.getPropertyKeys();
    for(int i=0; i<keys.length; i++) {
        String key = keys[i];
        methodPropertyGetParams[0] = key;
        %>
        <FORM ACTION="<c:out value='${alias}'/>/selector.jsp?view=configure" METHOD=GET>
        <tr>
            <td><%=key%></td>
            <td>
                <INPUT TYPE=HIDDEN name="service" value="edu.columbia.print.ninja.core.service.Admin">
                <INPUT TYPE=HIDDEN name="sid" value="<%=request.getParameter("sid")%>">
                <INPUT TYPE=HIDDEN name="type" value="HTML">
                <INPUT TYPE=HIDDEN name="view" value="configure">
                <INPUT TYPE=HIDDEN NAME="action" VALUE="save">
                <INPUT TYPE="text" NAME="<%=key%>" SIZE=32 VALUE="<%=serviceBean.getMethodResult()%>">
            </td>
            <td>
                <INPUT TYPE=SUBMIT VALUE="Save">
            </td>
        </tr>
        </FORM>
        <%
    }
    %>
</table>
