<%@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"
%><%@page import="com.fujitsu.iflow.model.util.QuickForm"
%><%@page import="com.fujitsu.iflow.common.JSConversions"
%><%
    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";


    //get the planId from request, if not available in request then get it from session
    String planId = request.getParameter("planId");

    if(sel != null)
    {
        int selValue = new Integer(sel).intValue();
        session.setAttribute("selected", sel);
        switch(selValue){
            case 7: returnMsg = "Delete";
                    break;
            case 8: returnMsg = "Archive";
                    break;

        }

    }
    else
    {
        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";
            }
            else if (planState.equals(ProcessDefinitionStatesEnum.from_int(ProcessDefinitionStatesEnum._Deleted).toString()))
            {
            state = "Deleted";
            }
            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)
        {
            //NEW CODE ADDED - BEGINS  
            	throw ex;
            //NEW CODE ADDED - ENDS  
            
            //returnMsg = "Could not retrieve Process Definition with id " + id + ".";
        }

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

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

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

     }



%>


<html>
<head>
<title>Interstage Business Process Manager | Process Definition</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, owner, adForm)
{
    planSumForm.planId.value= id
    planSumForm.selected.value= val
    planSumForm.owner.value= owner
    planSumForm.submit()
}

function gotoPlanList()
{
    document.location.href='SortedPlanList.jsp';
}


</script>

<center>

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

<%
if (!(returnMsg == ""))
{
%>

       <TABLE  border="0" cellpadding="2" cellspacing="1" class="tableClass" width="50%" 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="oddrow">
            <td><% writeAll(out, returnMsg,wfSession, planId); %></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()" value="Process Definition List" name="check" class="core-button">
            </td></tr>
          </TABLE>
<%
}
else
{
%>


    <table cellpadding=0 cellspacing=0 align="center" width="90%">
        <tr><td>&nbsp;</td></tr>

        <tr>

         <td align="right">

         <table border="0" cellspacing="0" cellpadding="0"><tr><td>
            <table border="0" cellspacing="0" cellpadding="0">
             <tr>
             <td><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><td>&nbsp;</td><td>
            <table border="0" cellspacing="0" cellpadding="0">
             <tr>
             <td><a href="PDGraphicBPMN.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="PDGraphicBPMN.jsp?planId=<%= id %>">
             <div align="center" class="buttonlink">BPMN</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 border="0" cellpadding="0" cellspacing="0" width="90%" align="center" bgcolor="#CCCCCC"><tr>
        <form name="planSumForm" method="post" action="PlanAction.jsp">
        <input type="hidden" name="planId" value="-1">
        <input type="hidden" name="selected" value="Edit">
<td>
<TABLE  border="0" cellpadding="3" cellspacing="1" class="tableClass" width="100%">
        <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><td class="headrow"> ID</td><td bgcolor="#FFFFFF"><%= id %></td></tr>
            <tr><td class="headrow"> Name</td><td bgcolor="#FFFFFF"><a href="<%=pdURL%>"><% QuickForm.writeHtml(out, name);%></a></td></tr>
        <tr>
        <td class="headrow" width="20%"> Description</td><td bgcolor="#FFFFFF"><% QuickForm.writeHtml(out, desc);%></td>
        </tr>
        <tr><td bgcolor="#FFFFFF" class="headrow"> State</td><td bgcolor="#FFFFFF"><%= 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 class="headrow"> Owner</td>
        <td bgcolor="#FFFFFF"><input type=text value ="<% QuickForm.writeHtml(out, planOwners);%>" name=owner size=30>
        
        <input type="button" onClick="setPlanOwner('2',<%= id %>, this.form.owner.value, this.form)" value="Modify Owner" name="check" class="core-button">
        </td>
        
        </td></tr>
        <tr><td class="headrow"> Version</td><td bgcolor="#FFFFFF"><%= version %></td></tr>
</TABLE>
</tr></table><br></form>
       <TABLE border="0" cellpadding="0" cellspacing="0" width="90%" align="center">
        <tr><td>
            

<!-- blue buttons -->
<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"><a href="javascript:onClick=setPlanId('1',<%= id %>, this.form)"><img src="images/buttons/but_delete.gif" border="0"></a></td>
        <td background="images/buttons/com_button_bg.gif" nowrap>
        <a href="javascript:onClick=setPlanId('1',<%= id %>, this.form)">
        <div align="center" class="buttonlink">&nbsp;Delete</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"><a href="javascript:onClick=setPlanId('5',<%= id %>, this.form)"><img src="images/buttons/but_package.gif" border="0"></a></td>
        <td background="images/buttons/com_button_bg.gif" nowrap>
        <a href="javascript:onClick=setPlanId('5',<%= id %>, this.form)">
        <div align="center" class="buttonlink">&nbsp;Archive</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>
          
<!-- end buttons -->
    </td><td align="right">            
<!-- blue buttons -->
<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"><a href="javascript:onClick=saveAsXML(<%= id %>)"><img src="images/buttons/but_save.gif" border="0"></a></td>
        <td background="images/buttons/com_button_bg.gif" nowrap><a href="javascript:onClick=saveAsXML(<%= id %>)"><div align="center" class="buttonlink">&nbsp;Save As XML</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"><a href="javascript:onClick=saveAsXPDL(<%= id %>)"><img src="images/buttons/but_save.gif" border="0"></a></td>
        <td background="images/buttons/com_button_bg.gif" nowrap><a href="javascript:onClick=saveAsXPDL(<%= id %>)"><div align="center" class="buttonlink">&nbsp;Save as XPDL</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>
<!-- end buttons -->           
            </td></tr>
<tr><td><br>
<% if(procInsts.equals("true")){
            %>          
            <input type="button" onClick="listPIs(<%= id %>)" value="List Process Instances" name="check" class="core-button">            
            <%
            }
            %> 
</td></tr>            
            </TABLE>

        <!-- Added Larer for changing the layout -->


<%
}
%>

</center>
</body>
</html>

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

    public void writeAll(Writer out, String select, WFAdminSessionImpl wfSession, String planIds)throws Exception
         {
             StringTokenizer st = new StringTokenizer(planIds, ",");
             String planIdsChanged = "";
             String planIdsFailed = "";
             long id = -1;
             
               String[] bcolor = {"oddrow","evenrow"};
               int nextColor = 0;
               String rowColor = bcolor[nextColor];
               Plan plan = null;
               String name = "";
               String tempExp = "";
               boolean stat = true;
               
               while (st.hasMoreTokens()){
               	try{
               		id = Long.parseLong(st.nextToken());
                             if(select.equalsIgnoreCase("delete"))
                             	wfSession.deletePlan(id);
                             else if(select.equalsIgnoreCase("archive"))
                             	wfSession.archivePlan(id);
                             planIdsChanged = planIdsChanged + id + ", ";
                             
                     }
               	catch (Exception ex) {
                             planIdsFailed = planIdsFailed + id + ", ";
                             tempExp = "";
                             if(stat == true)
                             	if (!(planIdsFailed.equals("") || planIdsFailed.equals(null)))
               	              	{  
               	                        out.write("<table bgcolor=\"#CCCCCC\" border=\"0\" cellpadding=\"1\" cellspacing=\"1\" class=\"tableClass\" width=\"100%\"><tr class=\"headrow\"><td colspan=\"3\">");
               	                        out.write(select);
               	                        out.write(" Failed </td></tr><tr><td width=\"50%\" align=\"center\"><b> Process Definition Name </td><td width=\"50%\" align=\"center\"><b> Process Definition ID </td> <td align=\"center\"><b>Reason</td></tr>");
               	                        stat = false;  
               	              	}
                             	
                             try{
                             	plan = WFObjectFactory.getPlan(id, wfSession);
                             	name = plan.getName();
                             }
                             catch(Exception e){
                             	tempExp = "{Unable to retrieve Process Definition name }";
                             }
                             tempExp += ex.getMessage();                        
                             tempExp = "Unable to "+select+" Process Definition with ID : "+id+" {" +tempExp+"}";
                             String te = JSConversions.quote4JS(tempExp);			  
                             tempExp = te.substring(1,te.length()-1); //removed double quotes		  	  
     	  		  	  
                             rowColor = bcolor[nextColor];
                             nextColor = (nextColor + 1)%2;	      			  
     	      
                             out.write("<tr class=\"");
                             out.write(rowColor);
                             out.write("\"><td align=\"center\">&nbsp;");
                             out.write(name);
                             out.write("</td><td align=\"center\">&nbsp;");
                             out.write(Long.toString(id));
                             out.write("</td><td align=\"center\"><input type=\"button\" onClick=\"getException('");
                             QuickForm.writeHtml(out, tempExp);
                             out.write("',");
                             out.write(Long.toString(id));
                             out.write(")\" value=\"Exception\" name=\"exception\" class=\"core-button\"></td></tr>"); 
                             
     	                    }
     	                 } 
     	                 
			if(stat == false)
	               		out.write("</table>");
		
                        if (!((planIdsChanged.equals("")) || (planIdsChanged.equals(null))))
           	             	{
               	                    if (stat == false)
               	                      	out.write("<br>");
				                             		
                        	out.write("<table bgcolor=\"#CCCCCC\" border=\"0\" cellpadding=\"1\" cellspacing=\"1\" class=\"tableClass\" width=\"100%\"><tr class=\"headrow\"><th colspan=\"2\">");
                        	out.write(select);
                        	out.write(" Successful </th></tr><tr class=\"evenrow\"><td><b>Process Definition ID's : " ); 
				out.write(planIdsChanged.substring(0,planIdsChanged.length() -2));     	                               	
                 		out.write("</td></tr></table>");
                 		}
                 
    }


%>
<script TYPE="text/javascript">

function getException(exception,id)
{
	var url = 'PlanException.jsp?exception=' + exception + '&id=' + id + '&newWindow=true';
	var w = '650';
	var h = '600';
	var winl = (screen.width-w)/2;
	var wint = (screen.height-h)/2;
	var settings  ='height='+h+',';
	settings +='width='+w+',';
	settings +='top='+wint+',';
	settings +='left='+winl+',';
	settings +='scrollbars='+'yes'+',';
	settings +='resizable=0';  
	window.open(url,"ProcessSummaryException",settings);
	
}

</script>