<%@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.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";

    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, 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)
{
    location.href='AdminPlanList.jsp?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.");
        return false;
    }
    location.href="PlanSummary.jsp?planId=" + document.adForm.templateId.value;
}

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 planCopy(id)
{
    location.href="CopyPlan.jsp?planId=" + id;
}

 function getSelected(opt) {
    var selected = new Array();
    var index = 0;
    if(document.adForm.selIdsNum.value == 1)
    {
       selected[1] = new Object;
       selected[1].value = opt.value;
       selected[1].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 test;
    var opt = document.adForm.selIds
    var sel = getSelected(opt);
    var strSel = "";
    for (var item in sel)
       strSel += sel[item].value + ",";
    test = strSel.substring(0,strSel.length-1);
    if(test == "")
    {
        var selected;
        if (val == 7) {
            selected = 'deleting';
        }
        else
        {
            selected = 'archiving';
        }
        alert("Select process definition id's for " + selected + " multiple process definitions.");
    }
    else
    {
        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">



<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">
      <tr background="images/buttons/com_button_bg.gif">
        <td width="15"><img src="images/buttons/but_import_xpdl.gif" width="32" height="24"></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</span></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"><img src="images/buttons/but_import_xml.gif" height="24"></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</span></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= "textbox" 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 = "";
    Plan[] nextPlans = getTemplates(FilterEnum._AllProcessDefinitions, wPageValue+1, _pageSize, wfSession);
    if (plans.length < _pageSize || _pageSize == 0 || nextPlans == null || nextPlans.length == 0)
        btnEnabled = "disabled";
     else
         selOption = "setSelection(this.form, " + (wPageValue + 1) +")";
%>
    <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">
    <td colspan="8" bgcolor="#EEEEEE" style="filter:progid:DXImageTransform.Microsoft.Gradient(startColorStr='#FFFFFF', endColorStr='#6699CC', gradientType='1')"><font color="#000000" size="2">All Process definitions</font></td>
</tr>
    <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></a></td>
    <th><img src="images/buttons/sort_icon.gif"><a href="<%=pdSortedURL%>?sort=planName"><font color="#FFFFFF"> Process definition Name </font></a></th>
    <th> Version </th>
    <th><img src="images/buttons/sort_icon.gif"><a href="<%=pdSortedURL%>?sort=planStatus"><font color="#FFFFFF"> State </font></a></th>
    <th><img src="images/buttons/sort_icon.gif"><a href="<%=pdSortedURL%>?sort=planOwner"><font color="#FFFFFF"> Owner </font></a></th>
    </tr>
<INPUT TYPE=HIDDEN NAME=selIdsNum  VALUE="<%=plans.length%>">
<%
for (int i = 0; i < plans.length; i++)
{
    Plan plan = plans[i];
    id = plan.getId();
    boolean isEditable = true;
    if(plan.getState() == Plan.STATE_PUBLISHED){
        isEditable = false;
    }
    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();
    String pdURL =   "ProcessDefinition.jsp?planId="+plan.getId()+ "&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 nowrap>
            <table border="0" width="100%" cellpadding="0" cellspacing="0"><tr><td nowrap>
            <a href="<%=pdURL%>"><font face="arial" size="2"><% QuickForm.writeHtml(out, planName); %></font></a></td>
            <td align="right">
<table border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td align="center"><img src="images/buttons/details_small_icon.gif" width="20" height="17"></td>
    <td rowspan="2">&nbsp;</td>
    <td align="center"><div align="center"><img src="images/buttons/copy_small_icon.gif" width="20" height="17" border="0"></div></td>
    <td rowspan="2">&nbsp;</td>
    <td><div align="center"><img src="images/buttons/view_small_icon.gif" width="17" height="17"></div></td>
    <td rowspan="2">&nbsp;</td>
    <td rowspan="2"><a href="AdminXPDLGenerator.jsp?planId=<%= id %>"><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"><div 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"><img src="images/buttons/but_delete.gif"></td>
    <td background="images/buttons/com_button_bg.gif" nowrap><a href="javascript:onClick=outputSelected('7')"><div align="center" class="buttonlink"> Delete</span></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"><img src="images/buttons/but_package.gif"></td>
    <td background="images/buttons/com_button_bg.gif" nowrap><a href="javascript:onClick=outputSelected('8')"><div align="center" class="buttonlink"> Archive</span></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>
<table border="0" cellspacing="0" cellpadding="0" align="right" >
  <tr background="images/buttons/com_button_bg.gif">
    <td width="15"><img src="images/buttons/but_package.gif"></td>
    <td background="images/buttons/com_button_bg.gif" nowrap><a href="ArchivedPlans.jsp"><div align="center" class="buttonlink"> Archived Process definitions</span></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>
    </tr>
</table>
<%
}
%>

</form>
</center>
</html>

<%!

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

        try {
            wfObjectList = WFObjectFactory.getWFObjectList(wfSession);
            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 WFAdminSessionImpl getiFlowSession(Object modelObject)
    {
            return (WFAdminSessionImpl)(modelObject);
    }


%>
