<%@page contentType="text/html;charset=UTF-8" pageEncoding="ISO-8859-1" session="true"%>
<%@page errorPage="Exception.jsp?dtm=true"
%><%@page import="com.fujitsu.iflow.model.bpm.BPMSession"
%><%@page import="com.fujitsu.iflow.model.bpmcore.BPMSessionImpl"
%><%@page import="com.fujitsu.iflow.model.util.QuickForm"
%><%@page import="com.fujitsu.iflow.model.util.URLEncoder"
%><%@page import="com.fujitsu.iflow.model.workflow.WFSession"
%><%@page import="com.fujitsu.iflow.model.workflow.*"
%><%@page import="com.fujitsu.iflow.model.wfadapter.Document"
%><%@page import="com.fujitsu.iflow.model.wfadapter.DocRepository"
%><%@page import="com.fujitsu.iflow.model.wfadapter.Folder"
%><%@page import="java.io.Writer"
%><%@page import="java.util.*"
%><%@page import="java.io.FileOutputStream"
%><%@page import="java.util.Iterator"
%><%@page import="java.util.Enumeration"
%><%@page import="java.text.DateFormat"
%><%@page import="java.io.FileOutputStream"
%><%@page import="java.io.InputStream"
%><%@page import="java.io.ObjectInputStream"
%><%@page import="java.io.ObjectOutputStream"
%><%@page import="java.io.OutputStream"
%>

<%-- calls the debug() function --%>
<%@ include file="Debug.jsp" %>
<%  //checks the session is valid or not
    request.setCharacterEncoding("UTF-8");
    BPMSession bps = (BPMSession) session.getAttribute("BPMSession");
    if (bps == null) {
        %><jsp:include page="PasswordPanel.jsp" flush="true"/><%
        return;
    }
%>
<%@ include file="dtmCommon.jsp" %>
<%@ include file="setDecisionTableManager.jsp" %>

<html>
<head>
<title>Interstage Business Process Manager | Decision Table Management</title>
<link href="styles/dtm.css" rel="stylesheet" type="text/css">

</head>
<body class="bodyClass">
<center>
 <jsp:include page="DTMHeader.jsp" flush="true"/><br/>
<!-- calls methods for checking validity of user inputs -->
<script type="text/javascript" language="javascript" src="jscript/check.js"> </script>

<table width="100%" height="100%" border="0" cellpadding="0" cellspacing="0">
      <td valign="top">
        <form name="main" method="post">
		<table width="95%" border="0" cellpadding="2" cellspacing="2" align="center" class="tableclass">
          <tr>
              <td colspan="2"> 
                <table  border="0" cellpadding="4" cellspacing="1" class="tableClass" width="100%" align="center">
                  <tr> 
                    <td align="left" height="35" valign="top" width="30%"> 
                      <table border="0" cellpadding=0 cellspacing=0>
                        <tr> 
                          <td align="right"> 
                            <%
							 if(isActive){
			   if(toBeSaved){
			  %>
                            <table border="0" cellspacing="0" cellpadding="0">
                              <tr background="images/buttons/com_button_bg.gif"> 
                                <td width="15"><img src="images/buttons/but_save.gif"></td>
                                <td background="images/buttons/com_button_bg.gif" nowrap><a href="DecisionTableManagement.jsp?pageaction=saveDecisionTableManager">
							      <div align="center" class="buttonlink"> Save</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> 
                            <%
			 
			   if(toBeApproved){
			  %>
                            <table border="0" cellspacing="0" cellpadding="0">
                              <tr background="images/buttons/com_button_bg.gif"> 
                                <td width="15"><img src="images/dtm/check_mark.gif"></td>
                                <td background="images/buttons/com_button_bg.gif" nowrap><a href="DecisionTableManagement.jsp?pageaction=applyChangesToDecisionTableManager"> 
                                  <div align="center" class="buttonlink"> Apply 
                                    Changes</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"><img src="images/buttons/but_graphical_view.gif" width="28" height="24"></td>
                                <td background="images/buttons/com_button_bg.gif" nowrap><a href="DecisionTableManagement.jsp?pageaction=requestApprovalToDecisionTableManager"> 
                                  <div align="center" class="buttonlink"> Request 
                                    Approval</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>
                    <td align="center" height="35" valign="top" width="30%"> 
                      <%
					  if(isActive){  %>
                      <table border="0" cellspacing="0" cellpadding="0" align="center">
                        <tr background="images/buttons/com_button_bg.gif"> 
                          <td width="15"><img src="images/buttons/but_view.gif" width="25" height="24"></td>
                          <td background="images/buttons/com_button_bg.gif" nowrap> 
							<a href="javascript:onClick=confirmViewActive(<%=toBeSaved%>)"> 
                            <div align="center" class="buttonlink"> View Active 
                              Decision Tables 
						    </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>
                      <%}else{%>
                      <table border="0" cellspacing="0" cellpadding="0" align="center">
                        <tr background="images/buttons/com_button_bg.gif"> 
                          <td width="15"><img src="images/buttons/but_edit.gif" width="26" height="24"></td>
                          <td background="images/buttons/com_button_bg.gif" nowrap> 
                           <a href="DecisionTableManagement.jsp?pageaction=editDecisionTables"> 
                            <div align="center" class="buttonlink"> Manage Decision Tables 
                            </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 align="right" height="35" valign="top"> 
                      <table border="0" cellpadding=0 cellspacing=0>
                        <tr> 
                          <td align="right"> 
                            <table border="0" cellspacing="0" cellpadding="0">
                             <tr background="images/buttons/com_button_bg.gif"><% if(isActive){%>
                                <td width="15"><img src="images/dtm/but_import.gif" width="27" height="24"></td>
                                <td background="images/buttons/com_button_bg.gif" nowrap><a href="ImportDecisionTable.jsp"> 
                                  <div align="center" class="buttonlink"> Load 
                                    Decision Tables</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"><img src="images/dtm/but_export.gif" width="27" height="24"></td>
                                <td background="images/buttons/com_button_bg.gif" nowrap> 
                                  <a href="DtmExportAction.jsp"> 
                                  <div align="center" class="buttonlink"> Export 
                                    Decision Tables</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>
                <br>
                <TABLE bgcolor="#CCCCCC" border="0" cellpadding="4" cellspacing="1" class="tableClass" width="100%" align="center">
				<%if(isActive){  %>
                  <tr align="center" class="headrow"> 
                    <td bgcolor="#ffffff"  align="right"> 
                      <table border="0" cellspacing="0" cellpadding="0" align="right">
                        <tr background="images/buttons/com_button_bg.gif"> 
                          <td width="15"><img src="images/buttons/but_table_view.gif" width="32" height="24"></td>
                          <td background="images/buttons/com_button_bg.gif" nowrap>  
                            <a href="NewDecisionTable.jsp"> 
                            <div align="center" class="buttonlink"> Add Decision Table</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><%}%>
                  <tr align="center" class="headrow"> 
                    <td bgcolor="#EEEEEE" style="filter:progid:DXImageTransform.Microsoft.Gradient(startColorStr='#FFFFFF', endColorStr='#6699CC', gradientType='1')" align="right"> 
                      <div align="left"><font color="#000000" size="2"> <%=tableTitle%>
                        <div align="right"></div>
                        </font></div>
                    </td>
                  </tr>
                </table>
                <TABLE bgcolor="#CCCCCC" border="0" cellpadding="4" cellspacing="1" class="tableClass" width="100%" align="center">
                  <tr align="center" class="headrow" bgcolor="#0066CC">
                  <% if(!isActive){  %>
                    <%}%>
                    <th align="center" width="10%">Enable</th>
                  <th> Name </th>
                  <th width="50%"> Description</th>
                    <th align="center" width="20%"> Action</th>
                </tr>
<%
     String[] bcolor = {"evenrow","oddrow"};
    int nextColor = 0;
    String rowColor = bcolor[nextColor];
	int decisionTableIndex = 0;
   		Iterator itr = decisionTables.iterator();
		while (itr.hasNext())
		{
			DecisionTable decisionTable = (DecisionTable) itr.next();
	        debug(trace, out, "\n certeria="+decisionTable.toString());
 			rowColor = bcolor[nextColor];
            nextColor = (nextColor + 1)%2;
		%>
                  <tr align="center" class="<%=rowColor%>" valign="top"> 
                  <% 
				  if(!isActive){%><% } %>
                    <td width="10%" align="center"> 
                    <input type="checkbox" <% if(!isActive){  out.println("disabled"); }%>name="Enable<%=decisionTable.getId()%>" onClick="enableDecisionTable(<%=decisionTable.getId()%>)" <%if(decisionTable.isEnabled()){%>checked <%}%>value="checkbox">                  </td>
                    <td width="20%"><% if(isActive){%>
					<a href="<%=editUrl%>?decisionTableId=<%=decisionTable.getId()%>"><%}QuickForm.writeHtmlWithNBSPs(out, decisionTable.getName()); %></a>
					
					</td>
                    <td width="50%"><%QuickForm.writeHtml(out, decisionTable.getDescription());%></td>
                    <td width="20%"> 
                      <table width="100%" cellpadding="0" cellspacing="0" border="0" class="tableclass">
                      <tr>
                        <td align="center">&nbsp; </td>
                        <td align="center"> 
                          <table border="0" cellspacing="0" cellpadding="0">
                            <tr>
							<% 
							if(!isActive){
							%>
							    <td align="center"><a href="<%=editUrl%>?decisionTableId=<%=decisionTable.getId()%>&pageaction=viewDecisionTable"><img src="images/buttons/view_small_icon.gif" width="17" height="17" border="0"></a></td>
                                <td align="center">&nbsp;</td>
                                <% }else{%>
							  <td align="center"><a href="<%=editUrl%>?decisionTableId=<%=decisionTable.getId()%>"><img src="images/buttons/edit_small_icon.gif" width="17" height="17" border="0"></a></td>
							  
							  <td align="center">&nbsp;</td>
							  <td align="center"><a href="NewDecisionTable.jsp?pageaction=edit&decisionTableId=<%=decisionTable.getId()%>"><img src="images/dtm/but_rename.gif" width="12" height="12" border="0"></a></td>
                              
                              <td align="center">&nbsp;</td>
                              <td align="center"><a href="javascript:onClick=confirmDelete('<%=decisionTable.getId()%>','<%QuickForm.writeHtmlWithNBSPs(out, decisionTable.getName());%>')"><img src="images/dtm/delete.gif" width="12" height="12" border="0"></a></td>
                             <% }%>
							 </tr>
                            <tr> 
							<%
							if(!isActive){
							%>
                                <td class="smalllinks" align="center"><a href="<%=editUrl%>?decisionTableId=<%=decisionTable.getId()%>&pageaction=viewDecisionTable">VIEW</a></td>
                                <td class="smalllinks" align="center">&nbsp;</td>
                                <% }else{%>
							  <td class="smalllinks" align="center">
							  <a href="<%=editUrl%>?decisionTableId=<%=decisionTable.getId()%>"> EDIT&nbsp; </a></td>
							  <td class="smalllinks" align="center">&nbsp;</td>
							  <td class="smalllinks" align="center">
							  <a href="NewDecisionTable.jsp?pageaction=edit&decisionTableId=<%=decisionTable.getId()%>"> RENAME</a>							  </td>
                                <td class="smalllinks" align="center">&nbsp;</td>
                                <td class="smalllinks" align="center">&nbsp;<a href="javascript:onClick=confirmDelete('<%=decisionTable.getId()%>','<%QuickForm.writeHtmlWithNBSPs(out, decisionTable.getName());%>')">DELETE</a>
			<script language="javascript">
				function confirmDelete(decisionTableId,decisionTableName){
				 var answer = confirm("Are you sure you want to delete decisionTable: " + decisionTableName +" ?")
   				 if (answer){
    			    location.href="deleteDecisionTable.jsp?pageaction=delete&decisionTableId=" + decisionTableId ;
 				   }
				}
						</script>								</td>
                            <% }%></tr>
                          </table>                        </td>
                      </tr>
                    </table>                  </td>
                </tr>
<%
decisionTableIndex++;
    }
%>
              </TABLE>
            </td>
          </tr>
        </table>
        
          <br>
        </form>
      </td>
      </tr>
    </table>
</center>
</body>
</html>
<%!

	 private String replaceWithBackSlash(String str)
	    {
	        return replace(str, "\\", "/");
	    }

	 private String replace(String str, String original, String replacement)
	    {
	        StringBuffer sb = new StringBuffer(str);
	        for (int i=0; i<sb.length(); i++)
	        {
	            if (sb.substring(i,i+1).equals(original))
	                sb.replace(i,i+1,replacement);
	        }
	        return sb.toString();
	    }
	//This function is used to create a temporary file in server
	private void createDocument(DocRepository docRepository,String clientPath,String fileName)throws Exception
	{
	try{
		File inFile= docRepository.checkOut(fileName,false);
	
		
			List tempDecisionTables = new ArrayList();
			FileInputStream fstream = new FileInputStream(inFile);
			OutputStream  fout= new FileOutputStream(clientPath);
			ObjectInputStream istream = new ObjectInputStream(fstream);
			Object obj = istream.readObject();
			tempDecisionTables = (ArrayList) obj;
			ByteArrayOutputStream bos = new ByteArrayOutputStream();
			ObjectOutputStream oos = new ObjectOutputStream( bos);
			oos.writeObject(tempDecisionTables);
			oos.flush();
			byte buffer [] = bos.toByteArray();  
			fout.write(buffer);
			
			istream.close();
			fstream.close();
			fout.close();
			bos.close();
			oos.close();
			}
			catch(Exception e){
				throw e;
			}	
	}
%>