<%@page contentType="text/html;charset=UTF-8" pageEncoding="ISO-8859-1" session="true"%>
<%@page errorPage="Exception.jsp"
%><%@page import="com.fujitsu.iflow.dbadapter.dataobjects.SearchStruct"
%><%@page import="com.fujitsu.iflow.dbadapter.dataobjects.SearchUdaStruct"
%><%@page import="com.fujitsu.iflow.model.bpm.BPMSession"
%><%@page import="com.fujitsu.iflow.model.bpmcore.BPMSessionImpl"
%><%@page import="com.fujitsu.iflow.model.util.QuickForm"
%><%@page import="com.fujitsu.iflow.model.util.URLEncoder"
%><%@page import="com.fujitsu.iflow.model.workflow.Filter"
%><%@page import="com.fujitsu.iflow.model.workflow.WFObjectFactory"
%><%@page import="com.fujitsu.iflow.model.workflow.WFObjectList"
%><%@page import="com.fujitsu.iflow.model.workflow.WFSession"
%><%@page import="com.fujitsu.iflow.model.workflow.WorkItem"
%><%@page import="com.fujitsu.iflow.server.dataobjects.FilterEnum"
%><%@page import="com.fujitsu.iflow.server.dataobjects.WorkItemStatesEnum"
%><%@page import="java.io.Writer"
%><%@page import="java.text.SimpleDateFormat"
%><%@page import="java.util.Date"
%><%@page import="java.util.Enumeration"
%><%@page import="java.text.DateFormat"
%><%
    request.setCharacterEncoding("UTF-8");request.getParameterNames(); //Need dummy call for WL9.1
    BPMSession bps = (BPMSession) session.getAttribute("BPMSession");
    if (bps == null) {
        %><jsp:include page="PasswordPanel.jsp" flush="true"/><%
        return;
    }
%>
<html>
<head>
<title>Interstage Business Process Manager | Advanced Search</title>
<jsp:include page="StyleSheet.jsp" flush="true"/>
<script>
function setDefault(myform)
{
    myform.submit();
}

function confirmDelete(myform,userId, queryType, queryName) {
    var answer = confirm("Are you sure you want to delete Search named " + queryName +"?")
    if (answer)
    {
        myform.action.value="delete";
        myform.submit();
        return false;
    }
}

function showDetail(detail)
{
    location.href=detail;
    return false;
}

</script>
</head>

<body class="bodyClass">
<center>
  <jsp:include page="Header.jsp" flush="true"/><br/>

<%
    boolean trace=false;
    String actionStr = request.getParameter("action");
    String qname = request.getParameter("qname");
    String qtype = request.getParameter("qtype");
    String quser = request.getParameter("quser");

    debug(trace, out, "\n qname="+qname);
    debug(trace, out, "\n qtype="+qtype);
    debug(trace, out, "\n quser="+quser);

    if (qtype == null) {
        qtype = "WI";
    }
    String dtlPage = null;
    String listPage = null;
    String title = null;
    if (qtype.equals(SearchStruct.TYPE_WI)) {
        listPage = "WorkList.jsp";
        dtlPage = "WorkListSearchDtl.jsp";
        title = "Advanced Workitem Search";
    }
    else if (qtype.equals(SearchStruct.TYPE_PI)) {
        listPage = "ProcessList.jsp";
        dtlPage = "ProcessSearchDtl.jsp";
        title = "Advanced Process Instance Search";
    }

    if (actionStr != null && actionStr.length() > 0) {
        if (actionStr.equals("delete")) {
            bps.deleteSearch(quser, qname, qtype);
        }
        else if (actionStr.equals("setDefault")) {
            SearchStruct s = bps.getSearch(quser, qname, qtype);
            if (!s.defaultQuery) {
              s.defaultQuery = true;
              bps.updateSearch(s);
            }
            else {
              s.defaultQuery = false;
              bps.updateSearch(s);
            }
        }
    }

%>
  <table width="100%" height="100%" border="0" cellpadding="0" cellspacing="0">
      <td valign="top">
        <table width="90%" border="0" cellpadding="2" cellspacing="2" align="center" class="tableclass">
          <tr>
            <td align="left">
              <form name="WLSearchList" method="get" action="<%=dtlPage%>">
                <input value="New Search..." type="submit" class="core-button">
              </form>
            </td>
          </tr>
          <tr>
            <td colspan="2">
              <TABLE bgcolor="#CCCCCC" border="0" cellpadding="4" cellspacing="1" class="tableClass" width="100%" align="center">
                <tr align="center" class="headrow">
                  <td bgcolor="#EEEEEE" style="filter:progid:DXImageTransform.Microsoft.Gradient(startColorStr='#FFFFFF', endColorStr='#6699CC', gradientType='1')"><font color="#000000" size="2"><%=title%></font></td>
                </tr>
              </table>
              <TABLE bgcolor="#CCCCCC" border="0" cellpadding="4" cellspacing="1" class="tableClass" width="100%" align="center">
                <tr align="center" class="headrow" bgcolor="#0066CC">
                  <th align="center"> Default </th>
                  <th> Name </th>
                  <th> Search Criteria Summary </th>
                  <th align="center"> Actions </th>
                </tr>
<%
    // display the retrieved searches.
    debug(trace, out, " bps.getUserName = " + bps.getUserName() + " type :" + qtype);
    SearchStruct[] searches = bps.getAllSearchs(bps.getUserName(), qtype);

    String[] bcolor = {"evenrow","oddrow"};
    int nextColor = 0;
    String rowColor = bcolor[nextColor];

    for (int i=0; i < searches.length ; i++) {

        SearchStruct search = searches[i];
        debug(trace, out, "\n query="+search.convertToXML());

        String checked = "";
        if (search.defaultQuery) {
            checked = "checked";
        }
        String query = getQueryString(search);
        rowColor = bcolor[nextColor];
        nextColor = (nextColor + 1)%2;

        StringBuffer summary = new StringBuffer();
        summary.append("Predefined Filter = ");
        summary.append(getFilterTitleFromId(Integer.parseInt(search.preDefFilter)));

        if (search.pdNames != null && search.pdNames.length() > 0) {
            summary.append(", Process Definition = ");
            summary.append(search.pdNames);
        }
        if (search.piNames != null && search.piNames.length() > 0) {
            summary.append(", Process Instance ");
            summary.append(search.piOp);
            summary.append(" ");
            summary.append(search.piNames);
        }
        if (search.wiNames != null && search.wiNames.length() > 0) {
            summary.append(", Workitem ");
            summary.append(search.wiOp);
            summary.append(" ");
            summary.append(search.wiNames);
        }
        if (search.assignees != null && search.assignees.length() > 0) {
            if (qtype.equals(SearchStruct.TYPE_PI)) {
                summary.append(", Owner(s) ");
            }
            else {
                summary.append(", Assignee ");
            }
            summary.append(search.asnOp);
            summary.append(" ");
            summary.append(search.assignees);
        }
%>
                <form name="radioForm" method="post" action="AdvSearch.jsp">
                <tr align="center" class="<%=rowColor%>">
                  <td align="center" valign="center"> 
                    
                      <input type="hidden" name="qname" value="<%=search.queryName%>"/>
                      <input type="hidden" name="qtype" value="<%=search.queryType%>"/>
                      <input type="checkbox" name="Default" <%=checked%> onclick="setDefault(this.form)"/>
                      <input type="hidden" name="quser" value="<%=search.userId%>"/>
                      <input type="hidden" name="def" value="<%=search.defaultQuery%>"/>
                      <input type="hidden" name="action" value="setDefault"/>
                    
                  </td>
                  <td>
                    <a href="<%=listPage%>?<%=query%>">
                      <%=search.queryName%>
                    </a>
                  </td>
                  <td width="70%"><%=summary.toString()%>
                  </td>
                  <td>
                    <TABLE border="0" cellpadding="4" cellspacing="1" class="tableClass" width="100%" align="center">
                      <tr align="center" >
                        <td>
                          <a href="" onclick="return showDetail('<%=dtlPage%>?<%=query%>')">Details</a>
                        </td>
                        <td>
                          <label for="Delete" class=linkcolor onclick="confirmDelete(this.form,'<%=search.userId%>', '<%=search.queryType%>', '<%=search.queryName %>')" >Delete</label>
                        </td>
                      </tr>
                    </table>
                  </td>
                </tr>
                </form>
<%
    }
%>
                </form>
              </TABLE>
            </td>
          </tr>
        </table>
      </td>
      </tr>
    </table>
</center>
</body>
</html>
<%!
    public
    String
    getQueryString(
      SearchStruct s)
    {
        StringBuffer sb = new StringBuffer();
        sb.append("srchnm=" + s.queryName);
        sb.append("&");
        sb.append("pg=" + s.pageSize);
        sb.append("&");
        sb.append("filterId=" + s.preDefFilter);
        sb.append("&");
        sb.append("sort=" + s.sortBy);
        sb.append("&");
        sb.append("asc=" + s.sortOrder);
        sb.append("&");
        sb.append("pin=" + s.piNames);
        sb.append("&");
        sb.append("piop=" + s.piOp);
        sb.append("&");
        sb.append("win=" + s.wiNames);
        sb.append("&");
        sb.append("wiop=" + s.wiOp);
        sb.append("&");
        sb.append("asn=" + s.assignees);
        sb.append("&");
        sb.append("asnop=" + s.asnOp);
        sb.append("&");
        sb.append("pd=" + s.pdNames);
        sb.append("&filterUdas=");
        if (s.udaFilter != null && s.udaFilter.length >0) {
            for (int i=0; i < s.udaFilter.length; i++) {
                SearchUdaStruct suda = s.udaFilter[i];
                if (i>0) {
                    sb.append(",");
                }
                sb.append(suda.name + " (" + suda.type + ") " + suda.operation + " " + suda.value);
            }
        }
        sb.append("&");
        sb.append("dspUda=" + s.disUdas);
        sb.append("&");
        sb.append("dspFields=" + s.disColums);
        sb.append("&");
        sb.append("hFrmCrt=" + s.createdStartDate);
        sb.append("&");
        sb.append("hToCrt=" + s.createdEndDate);
        sb.append("&");
        sb.append("hFrmDue=" + s.dueDateStarts);
        sb.append("&");
        sb.append("hToDue=" + s.dueDateEnds);
        sb.append("&");
        sb.append("pgNum=1");
        return sb.toString();
    }

    public
    String
    getFilterTitleFromId(
        int filterId)
    {
        String filterTitle = "My Active Activities";
        switch(filterId){
            case Filter.MyActiveWorkItems:
                filterTitle = "My Active Activities";
                break;
            case Filter.MyAcceptedWorkItems:
                filterTitle = "My Accepted Activities";
                break;
            case Filter.MyWorkItems:
                filterTitle = "My Activities";
                break;
            case Filter.MyDeclinedWorkItems:
                filterTitle = "My Declined Activities";
                break;
            case Filter.MyCompletedWorkItems:
                filterTitle = "My Completed Activities";
                break;
            case Filter.AllInactiveWorkItems:
                filterTitle = "All Inactive Activities";
                break;
            case Filter.AllWorkItems:
                filterTitle = "All Activities";
                break;
        }
        return filterTitle;
    }

    public void debug (boolean trace, Writer out, String msg) throws Exception
    {
        if (trace) {
            System.out.println(msg);
        }
    }
%>