<%@page contentType="text/html;charset=UTF-8" pageEncoding="ISO-8859-1" session="true"
%><%@page errorPage="Exception.jsp"
%><%@page import="com.fujitsu.iflow.common.UdaNameSanitizer"
%><%@page import="com.fujitsu.iflow.model.bpm.BPMSession"
%><%@page import="com.fujitsu.iflow.model.util.QuickForm"
%><%@page import="com.fujitsu.iflow.model.util.QuickFormOperation"
%><%@page import="com.fujitsu.iflow.model.workflow.DataItem"
%><%@page import="com.fujitsu.iflow.model.workflow.FormRef"
%><%@page import="com.fujitsu.iflow.model.workflow.NodeInstance"
%><%@page import="com.fujitsu.iflow.model.workflow.Plan"
%><%@page import="com.fujitsu.iflow.model.workflow.ProcessInstance"
%><%@page import="com.fujitsu.iflow.model.workflow.WFObjectFactory"
%><%@page import="com.fujitsu.iflow.model.workflow.WorkItem"
%><%@page import="java.io.File"
%><%@page import="java.io.Writer"
%><%@page import="java.io.PrintWriter"
%><%@page import="java.lang.Long"
%><%@page import="com.fujitsu.iflow.model.util.URLEncoder"
%><%@page import="java.net.URL"
%><%@page import="java.text.ParseException"
%><%@page import="java.text.DateFormat"
%><%@page import="java.util.Date"
%><%@page import="java.util.Hashtable"
%><%@page import="java.util.Locale"
%><%
    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;
    }

    // change this variable if you save this file to a new name.
    String nameOfThisPage = "TemplateCommands.jsp";

    String pdIdStr = request.getParameter("pdId");
    if (pdIdStr == null) {
        throw new Exception(nameOfThisPage + " needs a pdId parameter to tell it which process definition to work on.");
    }

    long piId = 0;
    try {
        piId = Long.parseLong(pdIdStr);
    }
    catch (Exception e) {
        throw new Exception(nameOfThisPage + " pdId parameter must be an integer value instead of ("+pdIdStr+")");
    }

    Plan plan = bps.getPlan(piId);
    boolean isEdit = plan.isInEditMode();


    String mode = request.getParameter("mode");
    if (mode == null) {
        throw new Exception("This page requires a 'mode' parameter");
    }

    try {
    // ============= put your custom form page code below this line =================

        if (mode.equals("Edit")) {
            if (isEdit) {
                throw new Exception("Can not 'StartEdit' when you are already editing.");
            }
            plan.startEdit();
            response.sendRedirect("ProcessDefinition.jsp?planID=" + pdIdStr);
        }
        else if (mode.equals("Cancel")) {
            if (!isEdit) {
                throw new Exception("Can not 'CancelEdit' when you are not editing.");
            }
            plan.cancelEdit();
            response.sendRedirect("ProcessDefinition.jsp?planID=" + pdIdStr);
        }
        else if (mode.equals("Save")) {
            if (!isEdit) {
                throw new Exception("Can not 'CommitEdit' when you are not editing.");
            }
            plan.commitEdit();
            response.sendRedirect("ProcessDefinition.jsp?planID=" + pdIdStr+"&isEditable=true");
        }
        else if (mode.equals("Delete")) {
            if (isEdit) {
                throw new Exception("Can not 'Delete process definition' when you are not editing.");
            }
             bps.deletePlan(piId);
         response.sendRedirect("TemplateList.jsp");
        }
        else if (mode.equals("Copy")) {
		response.sendRedirect("TemplateCommandsView.jsp?Command=Copy&pdId="+ pdIdStr);
      }else if (mode.equals("CopyWithName")) {
            String newName = request.getParameter("newName");
            if(newName == null) throw new Exception("Please enter new name of process definition");
            plan.saveAs(newName);
            response.sendRedirect("TemplateList.jsp");

      }else {
            throw new Exception("Don't understand the mode parameter ("+mode+").");
        }
    }
    catch (Exception e) {
        throw new Exception ("Unable tohandle edit command '"+nameOfThisPage+
               "' for process definition id="+pdIdStr+" because: "+e.toString());
    }

%>
