<%@page import="com.fujitsu.ibpm.dtm.*"
%><%@page import="com.fujitsu.iflow.model.wfadapter.DocRepository"
%><%@page import="com.fujitsu.iflow.model.wfadapter.Folder"
%><%@page import="com.fujitsu.iflow.model.bpmcore.BPMSessionImpl"
%><%@page import="com.fujitsu.iflow.model.workflow.*"
%><%@page import="com.fujitsu.iflow.model.bpm.BPMSession"
%><%@page import="java.util.*"
%><%@page import="java.io.*"
%>
<%
    request.setCharacterEncoding("UTF-8");

    String user = (String) session.getAttribute("userName");
    
    boolean dtmFileExists = false;
    boolean validDtm=false;
    // BEGIN DTM CODE - DO NOT Alter following Block Used By DTM Installer //
    String applPath = application.getRealPath("/").trim();
    if(!(applPath.endsWith("/") || applPath.endsWith("\\")))
        applPath = applPath + "/";
    String dtmFileName = applPath + "jsp/dtmconfig.properties";
    
    File dtmFile = new File(dtmFileName);
    dtmFileExists = dtmFile.exists();
    if(dtmFileExists)
    {
        String DTMUsers = DecisionTableConfig.getString("DecisionTableManagerUsers");
        if(DTMUsers!=null && DTMUsers.length()>0)
        {
            String[] result = DTMUsers.split(",");
            for (int x=0; x<result.length; x++)
            {
                if(user.equals(result[x]))
                {
                    validDtm = true;
                    break;
                }
            }
        }
    }
	BPMSession bpsSesion = (BPMSession) session.getAttribute("BPMSession");
 	if(bpsSesion.isAdmin())validDtm=true;
	
    // END DTM CODE //
//Check for valid DTM user
	if(!validDtm) throw new Exception("Access to DecisionTable Management System not allowed. Please contact your system administrator");
%>
<%
	WFSession wfSession = ((BPMSessionImpl)bpsSesion)._wfs;
    	boolean trace=false;
	String actionStr = request.getParameter("pageaction");
 	String fileName = DecisionTableManager.defaultFileName; // reads from the default file name
	String activeFileName = DecisionTableManager.activeFileName; // reads from the actvie file name
	Object fileNameObj = session.getAttribute("FileName");
	if(fileNameObj!=null) fileName = fileNameObj.toString();
	DecisionTableManager dtm = (DecisionTableManager) session.getAttribute("DecisionTableManager");
	String pwd = (String)session.getAttribute("password");
	DocRepository docRepository= getDocRepository(wfSession,pwd);
	if(dtm==null){
		dtm = new DecisionTableManager(activeFileName,docRepository);
		session.setAttribute("DecisionTableManager",dtm);
	}
    session.setAttribute("TabIndex","4");
%>
<%!
 	private
	    DocRepository  
	    getDocRepository(WFSession wfSession,String password)
	        throws Exception
	    {
	        DMS dms =  WFObjectFactory.getDMS(wfSession, wfSession.getUserName(), password);
	        return WFObjectFactory.getDocRepository(dms);
	    }

	%>