<%@page contentType="text/html;charset=UTF-8" pageEncoding="ISO-8859-1" session="true"
%><%@page errorPage="AdminException.jsp"
%><%@page import="com.fujitsu.iflow.model.workflow.WFAdminSession"
%><%@page import="com.fujitsu.iflow.model.wfcore.WFAdminSessionImpl"
%><%@page import="com.fujitsu.iflow.model.workflow.ProcessInstance"
%><%@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.ProcessInstanceStatesEnum"
%><%@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="java.util.Date"
%><%@page import="java.util.Enumeration"
%><%@page import="java.text.SimpleDateFormat"
%><%@page import="java.util.StringTokenizer"
%><%@page import="java.io.Writer"
%><%@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 returnMsg = "";

    String sel = request.getParameter("selected");

    long id = -1;
    Plan plan = null;
    String name = "";
    String desc = "";
    String version = "";
    String planState = "";
    String state = "";
    String planOwners = "";
    String pdURL = "";
    String planVersion = "";
    String procInsts = "false";

    String userId = request.getParameter("currentOwner");
    String planId = request.getParameter("planId");

    if (planId == null) {
    planId = (String) session.getAttribute("planId");
    }

    //DO NOT MERGE THE ABOVE CODE INTO THE IF-LOOP below
    id = Long.parseLong(planId);

    try {

    plan = WFObjectFactory.getPlan(id, wfSession);

    name = plan.getName();
    desc = plan.getDesc();
    desc = (desc == null || desc.length() <=0)?"":desc;
    version = plan.getVersion();
    planState = ProcessDefinitionStatesEnum.from_int(plan.getState()).toString();
    if (planState.equals(ProcessDefinitionStatesEnum.from_int(ProcessDefinitionStatesEnum._Draft).toString()))
    {
    state = "Draft";
    }
    else if (planState.equals(ProcessDefinitionStatesEnum.from_int(ProcessDefinitionStatesEnum._Published).toString()))
    {
    state = "Published";
    }
    else if (planState.equals(ProcessDefinitionStatesEnum.from_int(ProcessDefinitionStatesEnum._Obsoleted).toString()))
    {
    state = "Obsolete";
    }
    else if (planState.equals(ProcessDefinitionStatesEnum.from_int(ProcessDefinitionStatesEnum._Private).toString()))
    {
    state = "Private";
    }
    planOwners = plan.getOwner();
    planVersion = plan.getVersion();


    WFObjectList wfObjectList = null;
    ProcessInstance[] piList = null;

    try
    {
        wfObjectList = WFObjectFactory.getWFObjectList(wfSession);
        wfObjectList.addFilter(WFObjectList.LISTFIELD_PLAN_ID,WFObjectList.SQLOP_EQUALTO,"'" + planId + "'");
        Object[] objList = null;
        wfObjectList.openBatchedList(FilterEnum._AllProcesses);
        objList = wfObjectList.getNextBatch(10);

        if (objList != null && objList.length > 0) {
        procInsts = "true";
        }
    }
    finally {
        if (wfObjectList != null)
        wfObjectList.closeList();
    }
    }
    catch(Exception ex)
    {
    returnMsg = "Could not retrieve Process Definition with id " + id + ".";
    }

    boolean isEditable = true;
    if(plan.getState() == Plan.STATE_PUBLISHED){
       isEditable = false;
    }
    pdURL = "ProcessDefinition.jsp?planId="+id + "&isEditable=" + isEditable;

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

    session.setAttribute("TabIndex","3");




%>


<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>
</head>

<body class="adminBodyClass">
<script>

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

function saveAsXML(id)
{
    location.href="ExportXPDL.jsp?planId=" + id + "&type=xml"
}

function saveAsXPDL(id)
{
    location.href="ExportXPDL.jsp?planId=" + id +"&type=xpdl"
}

function listPIs(id)
{
    location.href="ListProcessInstances.jsp?planId=" + id
}

function setPlanOwner(val, id, userId, owner, adForm)
{
    planSumForm.planId.value= id;
    planSumForm.selected.value= val;
    planSumForm.owner.value= owner;
    planSumForm.userId.value= userId;
    planSumForm.submit();
}

</script>

<center>

<jsp:include page="AdminHeader.jsp" flush="true"/>

    <table cellpadding=2 cellspacing=1 class="tableclass" align="center" width="90%">
        <tr>&nbsp;</tr><tr><td align="left"></td>
        <td align="right">
        <table border="0" cellpadding="0" cellspacing="3"><tr>
        <td>
			<table border="0" cellspacing="0" cellpadding="0">
			  <tr>
				<td nowrap><a href="ExportXPDL.jsp?planId=<%= id %>&type=xpdl"><img src="images/icons/xpdl.gif" border="0"></a></td>
			  </tr>
			</table>
		</td>
		<td>
			<table border="0" cellspacing="0" cellpadding="0">
				<tr background="images/buttons/com_button_bg.gif">
					<td width="15"><a href="PDGraphic.jsp?planId=<%= id %>"><img src="images/buttons/but_graphical_view.gif" border="0"></a></td>
					<td background="images/buttons/com_button_bg.gif" nowrap><a href="PDGraphic.jsp?planId=<%= id %>"><div align="center" class="buttonlink">Snapshot</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></tr>
    </table>

<table width="90%" border="0" cellpadding="0" cellspacing="0" align="center"><tr><td>
        <form name="planSumForm" method="get" action="PlanOwnerAction.jsp">
          <input type="hidden" name="planId" value="-1">
          <input type="hidden" name="selected" value="-1">
          <input type="hidden" name="userId" value="<%= userId %>">

<TABLE  border="0" cellpadding="2" cellspacing="1" class="tableClass" width="100%" align="center" bgcolor="#CCCCCC" >
        <tr align="center" class="headrow">
            <td colspan="3" bgcolor="#EEEEEE" style="filter:progid:DXImageTransform.Microsoft.Gradient(startColorStr='#FFFFFF', endColorStr='#6699CC', gradientType='1')"><font color="#000000" size="2">Process Definition Summary</font></td>
            </tr>
        <tr><br></tr>
            <tr><td colspan="1" class="headrow"> ID</td><td bgcolor="#FFFFFF" colspan="2"><%= id %></td></tr>
            <tr><td colspan="1" class="headrow"> Name</td><td bgcolor="#FFFFFF" colspan="2"><a href="<%=pdURL%>"><%= name %></td></tr>
        <tr>
        <td colspan="1" rowspan= "1" class="headrow"> Description</td><td bgcolor="#FFFFFF" colspan="2"><%= desc %></td>
        </tr>
        <tr><td colspan="1" class="headrow"> State</td><td bgcolor="#FFFFFF" colspan="1"><%= planState %>
        <%
        if ((state.equals("Draft")) || (state.equals("Obsolete"))){
        %>
        <input type="button" onClick="setPlanId('3',<%= id %>, this.form)" value="Publish" name="check" class="core-button">
        <%
        }
        else if ((state.equals("Published"))){
        %>
        <input type="button" onClick="setPlanId('4',<%= id %>, this.form)" value="Obsolete" name="check" class="core-button">
        <%
        }
        %>
        </td></tr>

        <tr><td colspan="1" class="headrow"> Owner</td>
        <td bgcolor="#FFFFFF" colspan="1"><input type=text value =<%= planOwners %> name=owner size=30>
        <% if(!procInsts.equals("true")){
        %>
        <input type="button" onClick="setPlanOwner('2',<%= id %>, '<%= userId %>',this.form.owner.value, this.form)" value="Modify Owner" name="check" class="core-button">
        </td>
            <%
            }
            %>
        </tr>
        <tr><td colspan="1" class="headrow"> Version</td><td bgcolor="#FFFFFF" colspan="2"><%= version %></td></tr>
        
</TABLE>
<TABLE border="0" cellpadding="2" cellspacing="1" class="tableClass" width="100%">

        <tr><td><br>
        <% if(!procInsts.equals("true")){
            %>
        
        <input type="button" onClick="setPlanId('1',<%= id %>, this.form)" value="Delete" name="check" class="core-button">
        <input type="button" onClick="setPlanId('5',<%= id %>, this.form)" value="Archive" name="check" class="core-button">
        <%
            }
         %>
         <input type="button" onClick="saveAsXML(<%= id %>)" value="Save As XML" name="check" class="core-button">
         <input type="button" onClick="saveAsXPDL(<%= id %>)" value="Save As XPDL" name="check" class="core-button">            
            <% if(procInsts.equals("true")){
            %>
         <input type="button" onClick="listPIs(<%= id %>)" value="List Process Instances" name="check" class="core-button">
            <%
            }
            %>
	</td>
        </tr>
        </TABLE>
</td>
            </tr>
        </table>
        
        <!-- Added Larer for changing the layout -->
      </td></tr>
  </table></form>

</center>
</html>

<%!
    public WFAdminSessionImpl getiFlowSession(Object modelObject)
    {
            return (WFAdminSessionImpl)(modelObject);
    }


    public String archiveMultiplePlans(WFAdminSessionImpl wfSession, String planIds) throws Exception
    {
        StringTokenizer st = new StringTokenizer(planIds, ",");
        String planIdsArchived = "";
        String planIdsFailed = "";
        String message = "";
        long id = -1;
        while (st.hasMoreTokens()){
            try{
                id = Long.parseLong(st.nextToken());
                wfSession.archivePlan(id);
                planIdsArchived = planIdsArchived + id + ", ";
            }
            catch (Exception ex) {
                planIdsFailed = planIdsFailed + id + ", ";
            }
        }

        if (!((planIdsArchived.equals("")) || (planIdsArchived.equals(null))))
        {
            message = "Process Definition with ID's " + planIdsArchived.substring(0,planIdsArchived.length() -2) + " have been successfully archived. ";
        }

    if (!(planIdsFailed.equals("") || planIdsFailed.equals(null)))
    {
        message = message + "Archival of Process Definition with ID's " + planIdsFailed.substring(0,planIdsFailed.length()-2) + " failed.";
    }

        return message;
    }

    public String deleteMultiplePlans(WFAdminSessionImpl wfSession, String planIds) throws Exception
    {
        StringTokenizer st = new StringTokenizer(planIds, ",");
        String planIdsDeleted = "";
        String planIdsFailed = "";
        long id = -1;
        String message = "";

        while (st.hasMoreTokens()){
            try{
                id = Long.parseLong(st.nextToken());
                wfSession.deletePlan(id);
                planIdsDeleted = planIdsDeleted + id + ", ";
            }
            catch (Exception ex) {
                planIdsFailed = planIdsFailed + id + ", ";
            }
        }

        if (!((planIdsDeleted.equals("")) || (planIdsDeleted.equals(null))))
        {
            message = "Process Definition with ID's " + planIdsDeleted.substring(0,planIdsDeleted.length() -2) + " have been successfully deleted. ";
        }

    if (!(planIdsFailed.equals("") || planIdsFailed.equals(null)))
    {
        message = message + "Deletion of Process Definition with ID's " + planIdsFailed.substring(0,planIdsFailed.length()-2) + " failed.";
    }

        return message;
    }

%>
