<%@page contentType="text/html;charset=UTF-8" pageEncoding="ISO-8859-1" session="true"
%><%@page errorPage="Exception.jsp"
%>
<%@page import="com.fujitsu.iflow.common.ErrorMessage"%>
<%@page import="com.fujitsu.iflow.common.wfapp.WFAppComponent"%>
<%@page import="com.fujitsu.iflow.common.wfapp.WFAppConstants"%>
<%@page import="com.fujitsu.iflow.common.wfapp.WFAppManifest"%>
<%@page import="com.fujitsu.iflow.model.util.ModelException"%>
<%@page import="com.fujitsu.iflow.model.util.ModelInternalException"%>
<%@page import="com.fujitsu.iflow.model.util.WFAppInstaller"%>
<%@page import="com.fujitsu.iflow.model.workflow.WFObjectFactory"%>
<%@page import="com.fujitsu.iflow.model.workflow.WFSession"%>
<%@page import="com.fujitsu.iflow.model.workflow.DMS"%>
<%@page import="com.fujitsu.iflow.model.wfadapter.DocRepository"%>
<%@page import="java.util.Enumeration"%>
<%@page import="java.util.Hashtable"%>
<%@page import="java.util.StringTokenizer"%>

<%
    request.setCharacterEncoding("UTF-8");request.getParameterNames(); //Need dummy call for WL9.1

    WFSession wfSession = (WFSession)session.getAttribute("ModelsessionID");
    if (wfSession == null) {
        response.sendRedirect("Login.jsp");
        return;
    }
    String pwd = (String)session.getAttribute("password");
    DMS dms =  WFObjectFactory.getDMS(wfSession, wfSession.getUserName(), pwd);

%>
<%
    WFAppInstaller installer = new WFAppInstaller();
    WFAppManifest manifest = (WFAppManifest)session.getAttribute("WFAppManifest");
    String disableTriggers= (String)request.getParameter("DisableTriggers");
    String packagePath = (String)session.getAttribute("WFAppPackagePath");
    String componentOptions = (String)request.getParameter("ComponentOptions");
    StringTokenizer st = new StringTokenizer(componentOptions,",");
    Hashtable virtualToRealMappings = manifest.getVirtualToRealMapping();
    Hashtable tmpTable = new Hashtable();


    while (st.hasMoreTokens()) 
    {
       String componentOption = st.nextToken().trim();
       if(componentOption.indexOf("optionsc_") == 0)
       {
          String indexStr = componentOption.substring("optionsc_".length());
          WFAppComponent[] serverComponents = manifest.getServerComponents();
          int index = Integer.parseInt(indexStr);
          serverComponents[index].setOperation(Integer.parseInt((String)request.getParameter(componentOption)));
          String tmpLocation = serverComponents[index].getLocation();
          if(tmpLocation.indexOf("%IBPM_DMS_ROOT") == 0)
          {
             String tmpDmsRoot = tmpLocation.substring(0,tmpLocation.indexOf("%","%IBPM_DMS_ROOT".length())+1);
             tmpTable.put(tmpDmsRoot,virtualToRealMappings.get((String)request.getParameter("R_"+tmpDmsRoot)));
          }
       }
       else if(componentOption.indexOf("optioncc_") == 0)
       {
          String indexStr = componentOption.substring("optioncc_".length());
          WFAppComponent[] clientComponents = manifest.getClientComponents();
          int index = Integer.parseInt(indexStr);
          clientComponents[index].setOperation(Integer.parseInt((String)request.getParameter(componentOption)));
       }
      
    }
    
    for(Enumeration enum2 = tmpTable.keys() ; enum2.hasMoreElements() ;) 
    {
       String mkey = (String)enum2.nextElement();
       virtualToRealMappings.put(mkey,tmpTable.get(mkey));
    }
    manifest.setVirtualToRealMapping(virtualToRealMappings);

    if(disableTriggers != null && disableTriggers.equalsIgnoreCase("true") == true)
    {
       installer.disableTriggers();
    }
    Hashtable vToR = manifest.getVirtualToRealMapping();
    vToR.put(WFAppConstants.IBPM_PORTAL_ROOT_PATTERN, application.getRealPath("/").trim());
    manifest.setVirtualToRealMapping(vToR);

    installer.saveManifest(packagePath, manifest);
    
    
    installer.installWFApp(wfSession, dms, packagePath);
    response.sendRedirect("ImportWFAppPackageStatus.jsp");
%>



