<%@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.Properties"
%><%@page import="java.util.Enumeration"
%><%@page import="java.util.StringTokenizer"
%><%@page import="java.text.SimpleDateFormat"
%><%@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 = null;
    boolean browseDMS = false;

    String url ="";

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

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

    String owner = request.getParameter("owner");

    String path = request.getParameter("path");
    String fileName = request.getParameter("fileName");

    String pwd = (String)session.getAttribute("password");

    String sel = request.getParameter("selected");
    if(sel != null)
    {
        int selValue = new Integer(sel).intValue();
        session.setAttribute("selected", sel);
        switch(selValue){
            case 1: returnMsg = deletePlan(wfSession, Long.parseLong(planId));
                    break;
            case 2: returnMsg = modifyOwner(wfSession, Long.parseLong(planId), owner);
                    break;
            case 3: returnMsg = publishPlan(wfSession, Long.parseLong(planId));
                    break;
            case 4: returnMsg = obsoletePlan(wfSession, Long.parseLong(planId));
                    break;
            case 5: returnMsg = archivePlan(wfSession, Long.parseLong(planId));
                    break;
            case 6: browseDMS = true;
                    break;
            case 7: returnMsg = modifyMultiplePlans(wfSession, planId, owner);
                    break;
        }
    }

    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, actionForm)
{
    actionForm.planId.value= id
    actionForm.selected.value= val
    actionForm.submit()
}


function gotoPlanList(id)
{
    document.location.href='UserPlanList.jsp?userId=' + id;
}


</script>
<center>

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

<%

if(browseDMS == true) {
url = "ExportXPDL.jsp?planId=" + planId + "&type=xml";
response.sendRedirect(response.encodeRedirectURL(url));
}
%>

       <TABLE  border="0" cellpadding="2" cellspacing="1" class="tableClass" width="75%" align="center">
            <tr><td>&nbsp;</td></tr>
            <tr><td align="center">
            <img src="images/icons/info_excep.gif" border="0"><br/></td></tr>
            <tr><td>&nbsp;</td></tr>
        <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"><%= returnMsg %></font></td>
            </tr><tr><td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td></tr>
            <tr><td>&nbsp;</td></tr>
            <tr><td align ="center">
            <input type="button" onClick="gotoPlanList('<%= userId %>')" value="Process Definition List" name="check" class="core-button">
            </td></tr>
          </TABLE>
 </center>
</html>

<%!
    public String deletePlan(WFAdminSessionImpl wfSession, long id) throws Exception
    {
        wfSession.deletePlan(id);
        return "Process Definition with ID " + id + " has been successfully deleted";
    }

    public String modifyOwner(WFAdminSessionImpl wfSession, long id, String owner) throws Exception
    {
        Plan plan = WFObjectFactory.getPlan(id, wfSession);
        plan.startEdit();
        plan.setOwner(owner);
        plan.commitEdit();
        return "Owner for Process Definition with ID " + id + " has been successfully modified to " + owner;
    }

    public String archivePlan(WFAdminSessionImpl wfSession, long id) throws Exception
    {
        wfSession.archivePlan(id);
        return "Process Definition with ID " + id + " has been successfully archived";
    }

    public String publishPlan(WFAdminSessionImpl wfSession, long id) throws Exception
    {
        wfSession.publishPlan(id);
        return "Process Definition with ID " + id + " has been successfully published";
    }

    public String obsoletePlan(WFAdminSessionImpl wfSession, long id) throws Exception
    {
        wfSession.obsoletePlan(id);
        return "Process Definition with ID " + id + " is now obsolete ";
    }

    public String copyPlan(WFAdminSessionImpl wfSession, long id) throws Exception
    {
        Plan plan = WFObjectFactory.getPlan(id, wfSession);
        long newID = wfSession.createPlanVersion(plan.getName(), "", plan.getVersion());
        return "New Process Definition for " + plan.getName() + " with ID " + newID + " created";
    }

    public String modifyMultiplePlans(WFAdminSessionImpl wfSession, String id, String owner) throws Exception
    {
        String message = "";
        String successIds = "";
        String failedIds = "";
        /*
        StringTokenizer st = new StringTokenizer(owner, ",");
        String owners[] = new String[st.countTokens()];
        for (int i=0;i<owners.length;i++)
        {
            owners[i] = st.nextToken();
        }
        */
        StringTokenizer stIds = new StringTokenizer(id, ",");
        String plans[] = new String[stIds.countTokens()];
        long[] planIds = new long[plans.length];
        for (int i=0;i<planIds.length;i++)
        {
            planIds[i] = Long.parseLong(stIds.nextToken());
            try
            {
                Plan plan = WFObjectFactory.getPlan(planIds[i], wfSession);
            plan.startEdit();
            plan.setOwner(owner);
            plan.commitEdit();
            successIds = successIds + planIds[i] + ", ";
        }
        catch(Exception ex)
        {
            failedIds = failedIds + planIds[i] + ", ";
        }
        }

        if (!((successIds.equals("")) || (successIds.equals(null))))
        {
            message = "Owner(s) for Process Definition with ID's " + successIds.substring(0,successIds.length() -2) + " have been successfully modified to " + owner + ".";
        }

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

        return message;
    }

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



%>
