<%@page contentType="text/html;charset=UTF-8" pageEncoding="ISO-8859-1" session="true"
%><%@page errorPage="Exception.jsp"
%><%@page import="com.fujitsu.iflow.model.bpm.BPMSession"
%><%@page import="com.fujitsu.iflow.model.bpmcore.BPMSessionImpl"
%><%@page import="com.fujitsu.iflow.model.wfadapter.DocRepository"
%><%@page import="com.fujitsu.iflow.model.wfadapter.Document"
%><%@page import="com.fujitsu.iflow.model.workflow.DMS"
%><%@page import="com.fujitsu.iflow.model.workflow.WFObjectFactory"
%><%@page import="com.fujitsu.iflow.model.workflow.WFSession"
%><%@page import="com.fujitsu.iflow.model.workflow.WorkItem"
%><%@page import="java.io.File"
%><%@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;
    }

%><jsp:useBean id="myUpload" scope="page" class="com.fujitsu.iflow.model.util.Upload" /><%

    String formName = request.getParameter("Forms");
    if (formName == null)
        formName = "";
    myUpload.initialize(pageContext);
    myUpload.upload();
    String fileName = myUpload.getFiles().getFile(0).getFileName();
    String documentName = request.getParameter("documentName");
    long taskId = Long.parseLong(request.getParameter("taskId"));
    WorkItem task = bps.getWorkItem(taskId);

    String formUrlParam = "?taskId=" + taskId;
    if (formName != null && formName.trim().length() > 0) {
        formUrlParam = formUrlParam + "&Forms="+formName;
    }
    if ( fileName == null || fileName.trim().equals("") )
    {
        String message = "<br/><table border=\"0\" cellpadding=\"4\" cellspacing=\"1\" class=\"tableClass\" width=\"450\" align=\"center\" bgcolor=\"#CCCCCC\" align=\"center\"><tr><td class=\"headrow\"><b>Warning</b></td></tr><tr><td bgcolor=\"#FFFFFF\"><font class=\"arialfonts\"><br/><blockquote/>You need to locate the document that you have edited on" +
                " your machine before pressing \"Check in\" button.";
        session.setAttribute("message", message);
        request.setAttribute("message", message);
        request.setAttribute("nextPage", "");
        request.setAttribute("nextLink", "");
%>
        <jsp:forward page="Error.jsp"/>
<%
    }
    else
    {
        String root = application.getRealPath("/").trim();
        if (root.endsWith(File.separator))
            root = root + "attachments";
        else
            root = root + File.separator + "attachments";
        myUpload.save(root);
        boolean checkedIn = false;

        String documentPathOnClientMachine = root + File.separator + fileName;
        String pwd = (String)session.getAttribute("password");
        DocRepository docRepository = getDocRepository(bps, pwd);
        if(checkedIn = isDocCheckedOut(docRepository, documentPathOnClientMachine))
            docRepository.checkIn(documentPathOnClientMachine);
        if ( checkedIn )
        {
            String message = "<br/><table border=\"0\" cellpadding=\"4\" cellspacing=\"1\" class=\"tableClass\" width=\"450\" align=\"center\" bgcolor=\"#CCCCCC\" align=\"center\"><tr><td class=\"headrow\"><b>Information</b></td></tr><tr><td bgcolor=\"#FFFFFF\"><font class=\"arialfonts\"><br/><blockquote/>The document " + myUpload.getFiles().getFile(0).getFileName() +
                  " was successfully checked in.</blockquote/></td></tr></table>";
            session.setAttribute("message", message);
            response.sendRedirect(response.encodeRedirectURL("WorkItem.jsp"
              + formUrlParam + "&documentName=" + documentName));
        }
        else
        {
            String message = "<br/><table border=\"0\" cellpadding=\"4\" cellspacing=\"1\" class=\"tableClass\" width=\"450\" align=\"center\" bgcolor=\"#CCCCCC\" align=\"center\"><tr><td class=\"headrow\"><b>Warning</b></td></tr><tr><td bgcolor=\"#FFFFFF\"><font class=\"arialfonts\"><br/><blockquote/>The document " + myUpload.getFiles().getFile(0).getFileName() +
                    " could not be checked in. Make sure that the document is checked out for editing before checking in.</blockquote/></td></tr></table>";
            session.setAttribute("message", message);

            String workItemPage = response.encodeRedirectURL("WorkItem.jsp"
                                  + formUrlParam + "&documentName=" + documentName);
            request.setAttribute("message", message);
            request.setAttribute("nextPage", workItemPage);
            request.setAttribute("nextLink", "Return to WorkItem page");
%>
        <jsp:forward page="Error.jsp"/>
<%
        }
    }
%>


<%!
    public
    DocRepository
    getDocRepository(
        BPMSession bps,
        String password)
        throws Exception
    {
        DocRepository docRepository = null;
        Properties sysProp = System.getProperties();
        String docRoot = sysProp.getProperty("user.home");
        WFSession wfSession = ((BPMSessionImpl)bps)._wfs;
        DMS dms =  WFObjectFactory.getDMS(wfSession, wfSession.getUserName(), password);
        docRepository = WFObjectFactory.getDocRepository(dms, docRoot);

        return docRepository;
    }

    private
    boolean
    isDocCheckedOut(
        DocRepository docRep,
        String docPath)
    {
        int index = docPath.lastIndexOf(File.separator);
        int end = docPath.lastIndexOf(".");
        String documentName = docPath.substring(index + 1, end);
        Document[] document = null;
        try{
          document = docRep.getLockedDocs();
        }
        catch(Exception ex){
          return false;
        }
        for ( int i = 0; i < document.length; i++ )
        {
            if ( document[i] != null && document[i].getPath().indexOf(documentName) != -1 )
                return true;
        }
        return false;
    }
%>
