<%@page contentType="text/html;charset=UTF-8" pageEncoding="ISO-8859-1" session="true"%>
<%@page errorPage="AdminException.jsp"
%><%@page import="com.fujitsu.iflow.model.workflow.WFSession"
%><%@page import="com.fujitsu.iflow.model.workflow.WFAdminSession"
%><%@page import="com.fujitsu.iflow.model.wfcore.WFSessionImpl"
%><%@page import="com.fujitsu.iflow.model.wfcore.WFAdminSessionImpl"
%><%@page import="com.fujitsu.iflow.model.workflow.Plan"
%><%@page import="com.fujitsu.iflow.model.workflow.WFObjectList"
%><%@page import="com.fujitsu.iflow.model.workflow.WFObjectFactory"
%><%@page import="com.fujitsu.iflow.server.dataobjects.ProcessDefinitionStatesEnum"
%><%@page import="com.fujitsu.iflow.server.dataobjects.FilterEnum"
%><%@page import="com.fujitsu.iflow.model.workflow.Filter"
%><%@page import="com.fujitsu.iflow.model.util.QuickForm"
%><%@page import="java.util.Date"
%><%@page import="java.util.Enumeration"
%><%@page import="java.text.SimpleDateFormat"
%><%@page import="com.fujitsu.iflow.model.util.URLEncoder"
%><%@page import="com.fujitsu.iflow.model.bpm.BPMAdmin"
%><%@page import="com.fujitsu.iflow.model.bpm.BPMSession"
%><%@page import="com.fujitsu.iflow.model.bpmcore.BPMAdminImpl"
%><%
    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;
    }

    BPMAdmin bpas = bps.getAdmin();
    WFAdminSessionImpl wfSession = ((BPMAdminImpl)bpas)._wfas;

    String[] bcolor = {"evenrow","oddrow"};
    int nextColor = 0;
    String rowColor = bcolor[nextColor];
    session.setAttribute("TabIndex","0");

    String pdSortedURL= "SortedPlanList.jsp";

    String sort = request.getParameter("sort");
    String sortBy ="";

    String IdSortedImage= "";
    String TmSortedImage= "";
    String StSortedImage= "";
    String OwSortedImage= "";

    if (sort != null) {
        if (sort.equals("planId"))
        {
            sortBy = " process defintion id.";
            IdSortedImage = " <img src=images/icons/d_sorted.gif border=0> ";
        }
        else if (sort.equals("planStatus"))
        {
            sortBy = " process defintion state.";
            StSortedImage = " <img src=images/icons/d_sorted.gif border=0> ";
        }
        else if (sort.equals("planOwner"))
        {
            sortBy = " process defintion owner.";
            OwSortedImage = " <img src=images/icons/d_sorted.gif border=0> ";
        }
        else if (sort.equals("planName"))
        {
            sortBy = " process defintion name.";
            TmSortedImage = " <img src=images/icons/d_sorted.gif border=0> ";
        }
    }
    else
    {
        IdSortedImage = " <img src=\"images/icons/d_sorted.gif\" border=0> ";
    }


    int _pageSize = 20;
    int selIdValue=1;
    long id = -1;
    long planId = -1;

    String pageOption = request.getParameter("wPageId");
    if (!(pageOption != null && pageOption.length() >= 1))
    {
        pageOption = "1";
    }

    //DO NOT MERGE THE ABOVE CODE INTO THE IF-LOOP below
    int wPageValue = Integer.parseInt(pageOption);
    if (wPageValue < 1)
        wPageValue = 1;


    Plan[] plans = getTemplates(FilterEnum._AllProcessDefinitions, sort, wPageValue, _pageSize, wfSession);
    // stupid habit of returning null for empty set
    if (plans == null) {
        plans = new Plan[0];
    }

%>
<html>
<head>
<title>Interstage Business Process Manager | Process Definition List</title>
<jsp:include page="StyleSheet.jsp" flush="true"/>
<script language="javascript" src="styles/clockscripts.js"></script>
<script>
function setSelection(wiForm, selPage)
{
    wiForm.wPageId.value = selPage
    location.href='SortedPlanList.jsp?sort=' + wiForm.sort.value + '&wPageId=' + selPage;

}

function setPlanId(val, id, adForm)
{
    adForm.planId.value= id
    adForm.selected.value= val
    adForm.submit()
}

function gotoPlanId()
{
    if (document.adForm.templateId.value == "" || isNaN(document.adForm.templateId.value))
    {
        alert("Please enter a valid Process Definition ID.");
        document.adForm.templateId.focus();
        document.adForm.templateId.select();
        return false;
    }
    location.href="PlanSummary.jsp?planId=" + document.adForm.templateId.value;
}

function planCopy(id)
{
    location.href="CopyPlan.jsp?planId=" + id;
}

function importXPDL(id)
{
    location.href="ImportXPDL.jsp?type=xpdl";
}

function importXML(id)
{
    location.href="ImportXPDL.jsp?type=xml";
}

function archivedPlans()
{
    location.href="ArchivedPlans.jsp";
}

 function getSelected(opt) {
    var selected = new Array();
    var index = 0;
    if(document.adForm.pnum.value == 1)
    {
      if (opt.checked)
      {
          index = selected.length;
      selected[index] = new Object;
      selected[index].value = opt.value;
      selected[index].index = 0;
      }
    }
    else
    {
      for (var intLoop = 0; intLoop < opt.length; intLoop++) {
         if ((opt[intLoop].selected) ||
        (opt[intLoop].checked)) {
        index = selected.length;
        selected[index] = new Object;
        selected[index].value = opt[intLoop].value;
        selected[index].index = intLoop;
         }
      }
    }
    return selected;
 }

 function outputSelected(val) {
    var opt = document.adForm.selIds
    var sel = getSelected(opt);
    var strSel = "";
    for (var item in sel)
       strSel += sel[item].value + ",";
    var test = strSel.substring(0,strSel.length-1);
    var selected;
    if (val == 7) {
        selected = 'delete';
    }
    else
    {
        selected = 'archive';
    }
    if(test == "")
    {
        alert("Select Process Definition id's to " + selected + " multiple Process Definitions.");
    }
    else
    {
        var answer = confirm("Are you sure you want to " + selected + " Process Definition with id: " + test +"?")
        if (answer){
            location.href="PlanSummary.jsp?planId=" + test + "&selected=" + val;
        }
    }
 }

function disableEnterKey()
{
var code = window.event.keyCode;
    if (code == 13)
    {
        window.event.keyCode = 0;
    }
}

</script>
</head>

<body class="adminBodyClass">
<center>

<jsp:include page="AdminHeader.jsp" flush="true"/>
<br>
<form name="adForm" method="get" action="PlanSummary.jsp" onKeyPress="disableEnterKey()">
  <input type="hidden" name="wPageId" value="<%= wPageValue %>">
  <input type="hidden" name="selId" value="<%= selIdValue %>">
  <input type="hidden" name="planId" value="-1">
  <input type="hidden" name="selected" value="1">
  <input type="hidden" name="sort" value=<%= sort %>>


<table border="0" cellpadding="0" cellspacing="0" class="tableClass" width="90%" align="center">
<tr>
<td align="right">
<table border="0" cellspacing="0" cellpadding="0">
<tr>
<td>
    <table border="0" cellspacing="0" cellpadding="0" align="right" >
      <tr background="images/buttons/com_button_bg.gif">
        <td width="15"><a href="ArchivedPlans.jsp"><img src="images/buttons/but_package.gif" border="0"></a></td>
        <td background="images/buttons/com_button_bg.gif" nowrap><a href="ArchivedPlans.jsp"><div align="center" class="buttonlink"> Archived Process Definitions</div></a></td>
        <td width="9"><div align="right"><img src="images/buttons/com_button_right.gif" width="9" height="24"></div></td>
      </tr>
    </table>
</td><td></td><td>&nbsp;</td><td>
    <table border="0" cellspacing="0" cellpadding="0">
      <tr background="images/buttons/com_button_bg.gif">
        <td width="15"><a href="ImportXPDL.jsp?type=xpdl"><img src="images/buttons/but_import_xpdl.gif" width="32" height="24" border="0"></a></td>
        <td background="images/buttons/com_button_bg.gif" nowrap><a href="ImportXPDL.jsp?type=xpdl"><div align="center" class="buttonlink">&nbsp;Import from XPDL</div></a></td>
        <td width="9"><div align="right"><img src="images/buttons/com_button_right.gif" width="9" height="24"></div></td>
      </tr>
    </table>
</td><td>&nbsp;</td><td>
    <table border="0" cellspacing="0" cellpadding="0">
      <tr background="images/buttons/com_button_bg.gif">
        <td width="15"><a href="ImportXPDL.jsp?type=xml"><img src="images/buttons/but_import_xml.gif" height="24" border="0"></a></td>
        <td background="images/buttons/com_button_bg.gif" nowrap><a href="ImportXPDL.jsp?type=xml"><div align="center" class="buttonlink">&nbsp;Import from XML</div></a></td>
        <td width="9"><div align="right"><img src="images/buttons/com_button_right.gif" width="9" height="24"></div></td>
      </tr>
    </table>
</td>
<td>&nbsp;</td>
<td>
    <table border="0" cellspacing="0" cellpadding="0">
      <tr background="images/buttons/com_button_bg.gif">
        <td width="15"><a href="RepositorySync.jsp"><img src="images/buttons/but_process_view.gif" width="32" height="24" border="0"></a></td>
        <td background="images/buttons/com_button_bg.gif" nowrap><a href="RepositorySync.jsp"><div align="center" class="buttonlink">&nbsp;Synchronize</div></a></td>
        <td width="9"><div align="right"><img src="images/buttons/com_button_right.gif" width="9" height="24"></div></td>
      </tr>
    </table>
</td>
</tr>
</table>
<br>
</td>
</tr>
</table>

<table border="0" cellpadding="0" cellspacing="0" class="tableClass" width="90%" align="center">
<tr>
<td>
Process Definition Id: <input type= "text" name ="templateId" value = "" size = 10><input type = "button" onClick="gotoPlanId()" value = "Goto" class="submitClass"></td>
<%
    String selOption = "setSelection(this.form, " + (wPageValue + 1) +")";
    String btnEnabled = "";
    if (wPageValue > 1 && _pageSize != 0)
        selOption = "setSelection(this.form, " + (wPageValue -1) +")";
    else {
        //selOption = "setSelection(this.form, 1)";
        btnEnabled = "disabled";
    }
%>
    <td align="right"><input value="Prev Page" <%= btnEnabled %> type="button" onClick="<%= selOption %>" class="submitClass">
<%
    btnEnabled = "";
    selOption = "setSelection(this.form, " + (wPageValue + 1) +")";
    if (plans.length < _pageSize || _pageSize == 0)
        btnEnabled = "disabled";
    else
    {
        Plan[] mplans = getTemplates(FilterEnum._AllProcessDefinitions, sort, wPageValue+1, _pageSize, wfSession);
        if(mplans == null || mplans.length == 0)
           btnEnabled = "disabled";
    }

%>
    <input value="Next Page" <%= btnEnabled %> type="button" onClick="<%= selOption %>" class="submitClass"></td>

</tr>
</table>


<table bgcolor="#CCCCCC" border="0" cellpadding="4" cellspacing="1" class="tableClass" width="90%" align="center">

    <tr align="center" class="headrow">
    <th align="center">Select</th>
        <td><img src="images/buttons/sort_icon.gif"><a href="<%=pdSortedURL%>?sort=planId"><font color="#FFFFFF">
          Id </font><%=IdSortedImage%></a></td>
        <th><img src="images/buttons/sort_icon.gif"><a href="<%=pdSortedURL%>?sort=planName"><font color="#FFFFFF">
          Process Definition Name </font><%=TmSortedImage%></a></th>
    <th> Version </th>
        <th><img src="images/buttons/sort_icon.gif"><a href="<%=pdSortedURL%>?sort=planStatus"><font color="#FFFFFF">
          State </font><%=StSortedImage%></a></th>
        <th><img src="images/buttons/sort_icon.gif"><a href="<%=pdSortedURL%>?sort=planOwner"><font color="#FFFFFF">
          Owner </font><%=OwSortedImage%></a></th>
    </tr>
<input type="hidden" name=pnum value="<%= plans.length %>">
<%
for (int i = 0; i < plans.length; i++)
{
    Plan plan = plans[i];
    id = plan.getId();
    String version = plan.getVersion();
    String planName = plan.getName();
    String planState = null;
    if (plan.getState() == -1)
        planState = "ERROR";
    else
        planState = ProcessDefinitionStatesEnum.from_int(plan.getState()).toString();
    String planOwners = plan.getOwner();
    String[] planOwnersArray = {planOwners};
    //   String piURL = "ProcessInstance.jsp?piId="+pi.getId();
    boolean isEditable = true;
    if(plan.getState() == Plan.STATE_PUBLISHED){
        isEditable = false;
    }
    String pdURL = "ProcessDefinition.jsp?planId="+plan.getId() + "&amp;isEditable=" + isEditable;
    String summaryURL = "PlanSummary.jsp?planId="+id;
    rowColor = bcolor[nextColor];
    nextColor = (nextColor + 1)%2;
    String summary ="Summary";
    String Copy = "Copy";
%>
    <tr class="<%=rowColor%>">
        <td align="center"><INPUT TYPE=CHECKBOX NAME= selIds VALUE="<%= id %>"></td>
        <td><%= id %></td>
        <td>
            <table border="0" width="100%" cellpadding="0" cellspacing="0"><tr><td nowrap>
            <a href="ProcessDefinition.jsp?planId=<%=id%>&isEditable=true"><font face="arial" size="2">
            <% QuickForm.writeHtmlWithNBSPs(out, planName); %></font></a></td>
            <td align="right">
<table border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td align="center"><a href="PlanSummary.jsp?planId=<%=id%>" title="Click here to modify this Process Definition"><img src="images/buttons/details_small_icon.gif" width="20" height="17" border="0"></a></td>
    <td rowspan="2">&nbsp;</td>
    <td align="center"><div align="center"><a href="CopyPlan.jsp?planId=<%=id%>" title="Click here to copy this Process Definition"><img src="images/buttons/copy_small_icon.gif" width="20" height="17" border="0"></a></div></td>
    <td rowspan="2">&nbsp;</td>
    <td><div align="center"><a href="PDGraphic.jsp?planId=<%=id%>" title="Click here to view this Process Definition"><img src="images/buttons/view_small_icon.gif" width="17" height="17" border="0"></a></div></td>
    <td rowspan="2">&nbsp;</td>
    <td rowspan="2"><a href="ExportXPDL.jsp?planId=<%= id %>&type=xpdl"><img src="images/icons/small_xpdl.gif" border="0"></a></td>
  </tr>
  <tr>
    <td><a href="PlanSummary.jsp?planId=<%=id%>" title="Click here to modify this Process Definition"><div class="smalllinks">SUMMARY</div></a></td>
    <td><a href="CopyPlan.jsp?planId=<%=id%>" title="Click here to copy this Process Definition"><div align="center" class="smalllinks">COPY</div></a></td>
    <td><a href="PDGraphic.jsp?planId=<%=id%>" title="Click here to view this Process Definition"><div class="smalllinks">VIEW</div></a></td>
  </tr>
</table>
            </td></tr></table>
        </td>
        <td nowrap><%= version %></td>
        <td><%= planState %></td>
        <td>
        <% for(int j=0; j<planOwnersArray.length; j++){%>
        <% QuickForm.writeHtml(out, planOwnersArray[j]); %>
                  <% if(j!=planOwnersArray.length-1) out.print(","); %>

        <%}%>
        </td>
    </tr>
<%
}
%>
</table>

<%
if (plans.length != 0)
{
%>

<br>
<table border="0" cellpadding="0" cellspacing="0" width="90%" align="center">
    <tr>
        <td>

<TABLE border="0" cellpadding=0 cellspacing=0>
        <tr>
            <td>
<table border="0" cellspacing="0" cellpadding="0">
  <tr background="images/buttons/com_button_bg.gif">
    <td width="15"><a href="javascript:onClick=outputSelected('7')"><img src="images/buttons/but_delete.gif" border="0"></a></td>
    <td background="images/buttons/com_button_bg.gif" nowrap><a href="javascript:onClick=outputSelected('7')"><div align="center" class="buttonlink"> Delete</div></a></td>
    <td width="9"><div align="right"><img src="images/buttons/com_button_right.gif" width="9" height="24"></div></td>
  </tr>
</table>

</td><td>&nbsp;</td><td>

<table border="0" cellspacing="0" cellpadding="0">
  <tr background="images/buttons/com_button_bg.gif">
    <td width="15"><a href="javascript:onClick=outputSelected('8')"><img src="images/buttons/but_package.gif" border="0"></a></td>
    <td background="images/buttons/com_button_bg.gif" nowrap><a href="javascript:onClick=outputSelected('8')"><div align="center" class="buttonlink"> Archive</div></a></td>
    <td width="9"><div align="right"><img src="images/buttons/com_button_right.gif" width="9" height="24"></div></td>
  </tr>
</table>

</td></tr></table>
</td><td>&nbsp;</td><td>
    </td></tr>
</table>
<%
}
%>
</form>

</center>
</body>
</html>

<%!

    public
    Plan[]
    getTemplates(
        int filterOption,
        String sort,
        int batchNumber,
        int batchSize,
        WFSession wfSession)
        throws Exception
    {
        WFObjectList wfObjectList = null;
        Plan[] planList = null;

        try {
            wfObjectList = WFObjectFactory.getWFObjectList(wfSession);
            if (sort != null) {
                if (sort.equals("planId"))
                {
                    wfObjectList.addSortOrder(WFObjectList.LISTFIELD_PLAN_ID, WFObjectList.SORTORDER_ASCENDING);
                }
                else if (sort.equals("planName"))
                {
                    wfObjectList.addSortOrder(WFObjectList.LISTFIELD_PLAN_NAME, WFObjectList.SORTORDER_ASCENDING);
                }
                else if (sort.equals("planStatus"))
                {
                    wfObjectList.addSortOrder(WFObjectList.LISTFIELD_PLAN_STATE, WFObjectList.SORTORDER_ASCENDING);
                }
                else if (sort.equals("planOwner"))
                {
                    wfObjectList.addSortOrder(WFObjectList.LISTFIELD_PLAN_OWNER, WFObjectList.SORTORDER_ASCENDING);
                }
            }
            Object[] objList = null;
            wfObjectList.openBatchedList(filterOption);
            objList = wfObjectList.getNextBatch(batchSize);
            if (batchNumber > 1) {
                for (int i=1; i<batchNumber; i++) {
                    objList = wfObjectList.getNextBatch(batchSize);
                }
            }

            if (objList != null && objList.length > 0) {
                planList = new Plan[objList.length];
                for (int i=0; i<objList.length; i++) {
                    planList[i] = (Plan)objList[i];
                }
            }
        }
        finally {
            if (wfObjectList != null)
                wfObjectList.closeList();
        }

        return planList;
    }

    public WFSessionImpl getiFlowSession(Object modelObject)
    {
            return (WFSessionImpl)(modelObject);
    }


%>
