<%@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.workflow.DataItem"
%><%@page import="com.fujitsu.iflow.model.workflow.Plan"
%><%@page import="com.fujitsu.iflow.model.workflow.ProcessInstance"
%><%@page import="com.fujitsu.iflow.model.workflow.ProcessInstantiator"
%><%@page import="java.io.Writer"
%><%@page import="java.math.BigDecimal"
%><%@page import="java.text.DateFormat"
%><%@page import="java.text.ParseException"
%><%@page import="java.util.Date"
%><%@page import="java.util.Enumeration"
%><%@page import="java.util.Locale"
%><%@page import="java.util.Properties"
%><%
    request.setCharacterEncoding("UTF-8");request.getParameterNames(); //Need dummy call for WL9.1

    BPMSession bps = (BPMSession) session.getAttribute("BPMSession");
    if (bps == null) {
        response.sendRedirect("Login.jsp");
        return;
    }

    Plan plan = (Plan)session.getAttribute("plan");
    String operation = request.getParameter("operation");
    if (operation == null || operation.length() <= 0) {
        operation = "Invalid";
    }

    ProcessInstantiator processInstantiator =
            (ProcessInstantiator)session.getAttribute("processInstantiator");
    if ( processInstantiator == null ) {
        throw new Exception("Null processInstantiator");
    }

    // set new process name
    String processName = request.getParameter("procName");
    if ( processName == null || processName.trim().equals("") )
        processName = plan.getName();
    processInstantiator.setName(processName);

    // set new process description
    String procDescr = request.getParameter("procDesc");
    processInstantiator.setDescription(procDescr);

    //set new process data values
    DataItem data[] = processInstantiator.getDataItems();
    Properties dataProp = null;
    if(data != null)
    {
        dataProp = new Properties();
        for(int i=0; i<data.length; i++)
        {
            String udaName = data[i].getName();
            String udaIdentifier = UdaNameSanitizer.getSanitizedString(udaName);
            String formFieldName = "uda_"+udaIdentifier;
            String udaVal = request.getParameter(formFieldName);
			//added for QFE
			String[] udaValues = request.getParameterValues(formFieldName);
			if(udaValues != null)
			{
			out.println(udaValues.length);
			if(udaValues.length == 1)
			{
               if (udaVal == null) {
                        continue;
                 }
               String udaType = data[i].getType();
               if (!typeMatches(udaName, udaVal, udaType) )
               {
                String message = "The value of the attribute " + udaName +
                        " does not match its type ("+udaType+"); Posted value is: "+udaVal;
                request.setAttribute("message", message);
                throw new Exception(message);
               }
			   dataProp.put(udaName,udaVal);
			 }else if(udaValues.length >1)
			 {
			    udaVal = "";
				for(int index =0;index<udaValues.length;index++)
                  {
                   udaVal += udaValues[index];
                   if(index<udaValues.length-1)
                                udaVal +="&sep;";
                  }
			    dataProp.put(udaName,udaValues);
			 }
			 }
            //dataProp.put(udaName,udaVal);
        }
        processInstantiator.setDataItemValues(dataProp);
    }


    if (operation.equalsIgnoreCase("Delete Selected")) {
        String[] delAttach = request.getParameterValues("attach1");
        if (delAttach != null && delAttach.length > 0) {
            for (int i=0; i<delAttach.length; i++) {
                processInstantiator.removeAttachment(delAttach[i]);
            }
        }
        session.setAttribute("reuseProcInstantiator", new Boolean(true));
        String splanId = (String) session.getAttribute("planID");
        response.sendRedirect(response.encodeRedirectURL("ProcessInstantiator.jsp?planToProcess=" + splanId));
    }
    else if (operation.equalsIgnoreCase("Add")) {
        response.sendRedirect(response.encodeRedirectURL("AddAttachment.jsp"));
    }
    else if (operation.equalsIgnoreCase("Start"))
    {
        long processID = -1;

        ProcessInstance processInstance =
                    processInstantiator.startNewProcess();

        session.setAttribute("processInstantiator",processInstantiator);

        processID = processInstance.getId();

        String message = "Process Instance " + "\""+ processInstance.getName()+ " (id = " +
                        processID + ")" + "\""+ " was successfully created and started.";
        session.setAttribute("message", message);
        session.setAttribute("reuseProcInstantiator", new Boolean(false));
        response.sendRedirect(response.encodeRedirectURL("ProcessSuccess.jsp"));
    }
    else {
        request.setAttribute("message", "Invalid option specified : " + operation);
        request.setAttribute("nextPage", "ProcessInstantiator.jsp");
        request.setAttribute("nextLink", "Process");
        response.sendRedirect(response.encodeRedirectURL("Error.jsp"));
    }
%>

<%!
    public
    boolean
    isBoolean(
        String text)
    {
        boolean isMatch = false;
        if (text != null && text.trim().length()>0)
        {
            if (text.trim().equalsIgnoreCase("true") ||
                text.trim().equalsIgnoreCase("false"))
            {
                isMatch = true;
            }
        }
        return isMatch;
    }

    public
    boolean
    isInteger(
        String text)
    {
        boolean isMatch = false;
        if (text != null && text.trim().length()>0)
        {
            try
            {
                Integer i = Integer.valueOf(text);
                isMatch = true;
            }
            catch(NumberFormatException e)
            {
                isMatch = false; //unnecessary
            }
        }
        return isMatch;
    }

    public
    boolean
    isFloat(
        String text)
    {
        boolean isMatch = false;
        if (text != null && text.trim().length()>0)
        {
            try
            {
                Float f = Float.valueOf(text);
                isMatch = true;
            }
            catch(NumberFormatException e)
            {
                isMatch = false; //unnecessary
            }
        }
        return isMatch;
    }

    public
    boolean
    isLong(
        String text)
    {
        boolean isMatch = false;
        if (text != null && text.trim().length()>0)
        {
            try
            {
                Long l = Long.valueOf(text);
                isMatch = true;
            }
            catch(NumberFormatException e)
            {
                isMatch = false; //unnecessary
            }
        }
        return isMatch;
    }

    public
    boolean
    isDate(String text)
    {
        try {
            long l = Long.parseLong(text);
            return true;
        }
        catch (NumberFormatException e1){
        }
        return false;
    }

    public
    boolean
    isBigDecimal(
        String text)
    {
        boolean isMatch = false;
        if (text != null && text.trim().length()>0)
        {
            try
            {
                BigDecimal l = new BigDecimal(text);
                isMatch = true;
            }
            catch(NumberFormatException e)
            {
                isMatch = false; //unnecessary
            }
        }
        return isMatch;
    }

    public
    boolean
    isString(
        String text)
    {
        return true;
    }

   /**
    *   Utility method to check whether a given attribute value matches the
    *   tye type of of the attribute name. This method is mainly used in validating
    *   a form when a user modifies the values of the user defined attributes of
    *   a process.
    */
    public
    boolean
    typeMatches(
        String attributeName,
        String attributeValue,
        String udaType)
        throws Exception
    {
        boolean bMatch = false;
        if (attributeName == null ||
            attributeValue == null ||
            udaType == null) {
            return false;
        }

        if (udaType.equals(DataItem.TYPE_STRING)) {
            return isString(attributeValue);
        }
        else if (udaType.equals(DataItem.TYPE_INTEGER)) {
            return isInteger(attributeValue);
        }
        else if (udaType.equals(DataItem.TYPE_BOOLEAN)) {
            return isBoolean(attributeValue);
        }
        else if (udaType.equals(DataItem.TYPE_FLOAT)) {
            return isFloat(attributeValue);
        }
        else if (udaType.equals(DataItem.TYPE_LONG)) {
            return isLong(attributeValue);
        }
        else if (udaType.equals(DataItem.TYPE_DATE)) {
            return isDate(attributeValue);
        }
        else if (udaType.equals(DataItem.TYPE_BIGDECIMAL)) {
            return isBigDecimal(attributeValue);
        }
        return bMatch;
    }
%>
