com.rr.familyPlanning.ui.importexport.importExportController.java Source code

Java tutorial

Introduction

Here is the source code for com.rr.familyPlanning.ui.importexport.importExportController.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.rr.familyPlanning.ui.importexport;

import com.registryKit.client.clientManager;
import com.registryKit.client.customProgramFields;
import com.registryKit.client.dataElements;
import com.registryKit.client.engagementManager;
import com.registryKit.client.engagements;
import com.registryKit.client.programClientFields;
import com.registryKit.client.programEngagementFields;
import com.registryKit.exportTool.exportManager;
import com.registryKit.exportTool.progressBar;
import com.registryKit.exportTool.savedExportSites;
import com.registryKit.exportTool.savedExports;
import com.registryKit.program.programExportFields;
import com.registryKit.hierarchy.hierarchyManager;
import com.registryKit.hierarchy.programHierarchyDetails;
import com.registryKit.hierarchy.programOrgHierarchy;
import com.registryKit.importTool.importManager;
import com.registryKit.importTool.processStatus;
import com.registryKit.importTool.programImportAuditError;
import com.registryKit.importTool.programImportDroppedValues;
import com.registryKit.importTool.programImportEntityAuditDetails;
import com.registryKit.importTool.programImportError;
import com.registryKit.importTool.programImportLogFileView;
import com.registryKit.importTool.programImportType;
import com.registryKit.importTool.programImport;
import com.registryKit.importTool.programImportTypeField;
import com.registryKit.importTool.programImportTypeReportField;
import com.registryKit.program.programManager;
import com.registryKit.reference.fileManager;
import com.registryKit.reference.fileSystem;
import com.registryKit.survey.surveyManager;
import com.registryKit.user.User;
import com.registryKit.user.userActivityLog;
import com.registryKit.user.userManager;
import com.registryKit.user.userProgramModules;
import com.registryKit.reference.referenceManager;
import com.rr.familyPlanning.ui.security.decryptObject;
import com.rr.familyPlanning.ui.security.encryptObject;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.springframework.web.servlet.view.RedirectView;

/**
 *
 * @author chadmccue
 */
@Controller
@RequestMapping("/import-export")
public class importExportController {

    private static Integer moduleId = 4;

    @Autowired
    private userManager usermanager;

    @Autowired
    private surveyManager surveyManager;

    @Autowired
    private hierarchyManager hierarchymanager;

    @Autowired
    private exportManager exportManager;

    @Autowired
    private programManager programmanager;

    @Autowired
    private engagementManager engagementmanager;

    @Autowired
    private clientManager clientmanager;

    @Autowired
    private importManager importmanager;

    @Autowired
    private referenceManager referencemanaager;

    @Autowired
    private fileManager filemanager;

    @Value("${programId}")
    private Integer programId;

    @Value("${topSecret}")
    private String topSecret;

    private static boolean allowImport = false;
    private static boolean allowExport = false;
    private static boolean allowDelete = false;

    @RequestMapping(value = "", method = RequestMethod.GET)
    public ModelAndView importExport(HttpSession session, RedirectAttributes redirectAttr) throws Exception {

        /* Get a list of completed surveys the logged in user has access to */
        User userDetails = (User) session.getAttribute("userDetails");

        /* Get user permissions */
        userProgramModules modulePermissions = usermanager.getUserModulePermissions(programId, userDetails.getId(),
                moduleId);

        if (userDetails.getRoleId() == 2) {
            allowImport = true;
            allowExport = true;
            allowDelete = true;
        } else {
            allowImport = modulePermissions.isAllowImport();
            allowExport = modulePermissions.isAllowExport();
            allowDelete = modulePermissions.isAllowDelete();
        }

        /** Log Here **/
        userActivityLog ual = new userActivityLog();
        ual.setSystemUserId(userDetails.getId());
        ual.setMapping("");
        ual.setRequestMethod("GET");
        ual.setMethodAccessed("importExport");
        ual.setModuleId(moduleId);
        ual.setProgramId(programId);

        if (allowImport == true) {
            ModelAndView mav = new ModelAndView(new RedirectView("/import-export/import"));
            ual.setMiscNotes("View:" + "/import-export/import");
            usermanager.saveUserActivityLog(ual);
            return mav;

        } else if (allowExport == true) {
            ModelAndView mav = new ModelAndView(new RedirectView("/import-export/export"));
            ual.setMiscNotes("View:" + "/import-export/export");
            usermanager.saveUserActivityLog(ual);
            return mav;

        } else {
            ModelAndView mav = new ModelAndView(new RedirectView("/import-export/export"));
            ual.setMiscNotes("View:" + "/import-export/export");
            usermanager.saveUserActivityLog(ual);
            return mav;
        }

    }

    /**
     * The '/import' GET request will display the import page.
     *
     * @param session
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/import", method = RequestMethod.GET)
    public ModelAndView importFiles(HttpSession session) throws Exception {

        ModelAndView mav = new ModelAndView();

        /* Get a list of completed surveys the logged in user has access to */
        User userDetails = (User) session.getAttribute("userDetails");
        userProgramModules modulePermissions = usermanager.getUserModulePermissions(programId, userDetails.getId(),
                moduleId);

        if (userDetails.getRoleId() == 2) {
            allowImport = true;
            allowExport = true;
            allowDelete = true;
        } else {
            allowImport = modulePermissions.isAllowImport();
            allowExport = modulePermissions.isAllowExport();
            allowDelete = modulePermissions.isAllowDelete();
        }
        /** since a PUT can potentially contain sites that users do not have access to, 
        * role 3 users can only see files they uploaded.
        */
        List<programImport> piList;

        encryptObject encrypt = new encryptObject();
        Map<String, String> map;

        Map<Integer, processStatus> psMap = new HashMap<Integer, processStatus>();
        List<processStatus> psList = importmanager.getAllProcessStatus();
        for (processStatus ps : psList) {
            psMap.put(ps.getId(), ps);
        }
        if (userDetails.getRoleId() == 2) {
            //we get all the imports in the system
            piList = importmanager.getProgramImports(programId, 0);
        } else {
            //piList = importmanager.getProgramImports(programId, userDetails.getId());
            piList = importmanager.getdProgramImportsForUserAllowedTypes(programId, userDetails.getId(), null);
        }
        String piIds = "";
        for (programImport pi : piList) {
            piIds = piIds + pi.getId() + " ";
            pi.setProcessStatus(psMap.get(pi.getStatusId()));
            //Encrypt the use id to pass in the url
            map = new HashMap<String, String>();
            map.put("id", Integer.toString(pi.getId()));
            map.put("topSecret", topSecret);

            String[] encrypted = encrypt.encryptObject(map);
            pi.setEncryptedId(encrypted[0]);
            pi.setEncryptedSecret(encrypted[1]);

        }

        mav.setViewName("/import");
        mav.addObject("allowImport", allowImport);
        mav.addObject("allowExport", allowExport);
        mav.addObject("allowDelete", allowDelete);
        mav.addObject("importList", piList);

        /** Log Here **/
        userActivityLog ual = new userActivityLog();
        ual.setSystemUserId(userDetails.getId());
        ual.setMapping("/import");
        ual.setRequestMethod("GET");
        ual.setMethodAccessed("importFiles");
        ual.setModuleId(moduleId);
        ual.setProgramId(programId);
        ual.setMiscNotes(piIds);
        usermanager.saveUserActivityLog(ual);

        return mav;
    }

    /**
     * The '/import-export/getImportForm.do' GET request will 
     * return the overlay of an import form
     * form.
     *
     * @param session
     * @param subfolder
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/getImportForm.do", method = RequestMethod.GET)
    public @ResponseBody ModelAndView getImportForm(HttpSession session) throws Exception {

        ModelAndView mav = new ModelAndView();
        mav.setViewName("/importExport/importForm");

        /* Get a list of completed surveys the logged in user has access to */
        User userDetails = (User) session.getAttribute("userDetails");

        //recheck permission
        userProgramModules modulePermissions = new userProgramModules();

        if (userDetails.getRoleId() != 2) {
            modulePermissions = usermanager.getUserModulePermissions(programId, userDetails.getId(), moduleId);
        } else {
            modulePermissions.setProgramId(programId);
        }
        //get list of PUT types for user
        List<programImportType> putList = importmanager.getProgramImportTypes(userDetails, modulePermissions);

        mav.addObject("programImports", new programImport());
        mav.addObject("putList", putList);

        /** Log Here **/
        userActivityLog ual = new userActivityLog();
        ual.setSystemUserId(userDetails.getId());
        ual.setMapping("/getImportForm.do");
        ual.setRequestMethod("GET");
        ual.setMethodAccessed("getImportForm");
        ual.setModuleId(moduleId);
        ual.setProgramId(programId);
        usermanager.saveUserActivityLog(ual);

        return mav;
    }

    /**
     * The '/export' GET request will display the export page.
     *
     * @param session
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/export", method = RequestMethod.GET)
    public ModelAndView export(HttpSession session) throws Exception {

        /* Get a list of completed surveys the logged in user has access to */
        User userDetails = (User) session.getAttribute("userDetails");

        ModelAndView mav = new ModelAndView();
        mav.setViewName("/export");
        mav.addObject("allowImport", allowImport);
        mav.addObject("allowExport", allowExport);
        mav.addObject("allowDelete", allowDelete);

        String exportIds = "";
        programOrgHierarchy level1 = hierarchymanager.getProgramOrgHierarchyBydspPos(1, programId);
        programOrgHierarchy level2 = hierarchymanager.getProgramOrgHierarchyBydspPos(2, programId);
        programOrgHierarchy level3 = hierarchymanager.getProgramOrgHierarchyBydspPos(3, programId);

        Integer userId;
        if (userDetails.getRoleId() == 2) {
            userId = 0;
        } else {
            userId = userDetails.getId();
        }

        List<programHierarchyDetails> level1Items = hierarchymanager.getProgramHierarchyItems(level1.getId(),
                userId);
        mav.addObject("level1Items", level1Items);

        mav.addObject("level1Name", level1.getName());
        mav.addObject("level2Name", level2.getName());
        mav.addObject("level3Name", level3.getName());

        List<savedExports> savedExports = exportManager.getSavedExports(userId, programId);

        if (savedExports != null && savedExports.size() > 0) {
            String registryName = programmanager.getProgramById(programId).getProgramName().replaceAll(" ", "-")
                    .toLowerCase();

            fileSystem dir = new fileSystem();
            dir.setDir(registryName, "exportFiles");
            Iterator<savedExports> itr = savedExports.iterator();
            while (itr.hasNext()) {

                savedExports export = itr.next();
                exportIds = exportIds + export.getId() + " ";
                File newFile = new File(dir.getDir() + export.getExportFile());

                if (!newFile.exists()) {
                    itr.remove();
                }
            }
        }

        mav.addObject("allowImport", allowImport);
        mav.addObject("allowExport", allowExport);
        mav.addObject("allowDelete", allowDelete);
        mav.addObject("savedExports", savedExports);

        /** Log Here **/
        userActivityLog ual = new userActivityLog();
        ual.setSystemUserId(userDetails.getId());
        ual.setMapping("/export");
        ual.setRequestMethod("GET");
        ual.setMethodAccessed("export");
        ual.setModuleId(moduleId);
        ual.setProgramId(programId);
        ual.setMiscNotes("Tier 1:" + level1.getName() + "^^^^^" + "Tier 2:" + level2.getName() + "^^^^^" + "Tier 3:"
                + level3.getName() + "^^^^^" + "exportIds:" + exportIds);
        usermanager.saveUserActivityLog(ual);

        return mav;

    }

    /**
     * The 'getOrganizationSites' GET request will return the view containing the table for the list of client based on the search parameters.
     * 
     * @param request
     * @param response
     * @param session
     * @param searchString  The string containing the list of search parameters.
     * @return
     * @throws Exception 
     */
    @RequestMapping(value = "getOrganizationSites", method = RequestMethod.GET)
    @ResponseBody
    public ModelAndView getOrganizationSites(HttpSession session,
            @RequestParam(value = "entityValue", required = false) Integer entityValue,
            @RequestParam(value = "level", required = true) Integer level) throws Exception {

        ModelAndView mav = new ModelAndView();
        mav.setViewName("/importExport/siteList");

        User userDetails = (User) session.getAttribute("userDetails");

        List<programHierarchyDetails> sites = hierarchymanager.getProgramOrgHierarchyItemsWithClientTotal(programId,
                level, entityValue, userDetails.getId());

        mav.addObject("sites", sites);

        /** Log Here **/
        userActivityLog ual = new userActivityLog();
        ual.setSystemUserId(userDetails.getId());
        ual.setMapping("getOrganizationSites");
        ual.setRequestMethod("GET");
        ual.setMethodAccessed("getOrganizationSites");
        ual.setModuleId(moduleId);
        ual.setProgramId(programId);
        ual.setMiscNotes("entityValue:" + entityValue + "^^^^^" + "level:" + level);
        usermanager.saveUserActivityLog(ual);

        return mav;
    }

    /**
     * 
     * @param surveyId
     * @param session
     * @param request
     * @return
     * @throws Exception 
     */
    @RequestMapping(value = "/getExportModal.do", method = RequestMethod.GET)
    public ModelAndView getExportModal(HttpSession session, HttpServletRequest request) throws Exception {

        ModelAndView mav = new ModelAndView();
        mav.setViewName("/importExport/exportModal");

        savedExports exportDetails = new savedExports();
        exportDetails.setExportType(1);

        Random rand = new Random();
        exportDetails.setUniqueId(rand.nextInt(50000000));

        User userDetails = (User) session.getAttribute("userDetails");

        programOrgHierarchy level1 = hierarchymanager.getProgramOrgHierarchyBydspPos(1, programId);
        programOrgHierarchy level2 = hierarchymanager.getProgramOrgHierarchyBydspPos(2, programId);
        programOrgHierarchy level3 = hierarchymanager.getProgramOrgHierarchyBydspPos(3, programId);

        Integer userId;
        if (userDetails.getRoleId() == 2) {
            userId = 0;
        } else {
            userId = userDetails.getId();
        }

        List<programHierarchyDetails> level1Items = hierarchymanager.getProgramHierarchyItems(level1.getId(),
                userId);
        mav.addObject("level1Items", level1Items);

        mav.addObject("level1Name", level1.getName());
        mav.addObject("level2Name", level2.getName());
        mav.addObject("level3Name", level3.getName());

        mav.addObject("exportDetails", exportDetails);
        mav.addObject("showDateRange", true);

        /** Log Here **/
        userActivityLog ual = new userActivityLog();
        ual.setSystemUserId(userDetails.getId());
        ual.setMapping("/getExportModal.do");
        ual.setRequestMethod("GET");
        ual.setMethodAccessed("getExportModal");
        ual.setModuleId(moduleId);
        ual.setProgramId(programId);
        ual.setMiscNotes("Tier 1:" + level1.getName() + "^^^^^" + "Tier 2:" + level2.getName() + "^^^^^" + "Tier 3:"
                + level3.getName() + "^^^^^" + "exportDetails:" + exportDetails.getId());
        usermanager.saveUserActivityLog(ual);

        return mav;
    }

    /**
     * 
     * @param surveyId
     * @param session
     * @param request
     * @return
     * @throws Exception 
     */
    @RequestMapping(value = "/saveParticipantExport.do", method = RequestMethod.POST)
    public ModelAndView saveExport(@ModelAttribute(value = "exportDetails") savedExports exportDetails,
            @RequestParam List<Integer> selectedSites, BindingResult errors, HttpSession session,
            HttpServletRequest request) throws Exception {

        if (errors.hasErrors()) {
            for (ObjectError error : errors.getAllErrors()) {
                System.out.println(error.getDefaultMessage());
            }
        }

        User userDetails = (User) session.getAttribute("userDetails");

        /** Log Here **/
        userActivityLog ual = new userActivityLog();
        ual.setSystemUserId(userDetails.getId());
        ual.setMapping("/saveParticipantExport.do");
        ual.setRequestMethod("POST");
        ual.setMethodAccessed("saveExport");
        ual.setModuleId(moduleId);
        ual.setProgramId(programId);

        SimpleDateFormat datesearchFormat = new SimpleDateFormat("MM/dd/yyyy");
        Date startDate = datesearchFormat.parse(exportDetails.getStartDate());
        Date endDate = datesearchFormat.parse(exportDetails.getEndDate());

        datesearchFormat.applyPattern("yyyy-MM-dd");

        String realStartDate = datesearchFormat.format(startDate);
        String realEndDate = datesearchFormat.format(endDate);

        String exportFileName = "";

        String registryName = programmanager.getProgramById(programId).getProgramName().replaceAll(" ", "-")
                .toLowerCase();

        /* Get the client engagements */
        List<engagements> engagements = engagementmanager.getEngagementByMultipleEntity(programId, selectedSites,
                realStartDate, realEndDate);

        Integer exportId = 0;

        /* Loop through sessions here */
        if (engagements != null && engagements.size() > 0) {

            exportDetails.setProgramId(programId);
            exportDetails.setSystemUserId(userDetails.getId());
            exportDetails.setSelectedDateRange(exportDetails.getStartDate() + " - " + exportDetails.getEndDate());
            exportDetails.setDownloadType(1);
            exportDetails.setTotalRecords(engagements.size());

            exportId = exportManager.saveExport(exportDetails);

            if (selectedSites != null && !"".equals(selectedSites)) {
                StringBuilder selectedSiteNames = new StringBuilder();
                for (Integer site : selectedSites) {
                    programHierarchyDetails siteDetails = hierarchymanager.getProgramHierarchyItemDetails(site);

                    savedExportSites exportSite = new savedExportSites();
                    exportSite.setExportId(exportId);
                    exportSite.setSiteName(siteDetails.getName());
                    exportSite.setSiteId(site);

                    exportManager.saveExportSite(exportSite);
                }
            }

            progressBar newProgressBar = new progressBar();
            newProgressBar.setExportId(exportDetails.getUniqueId());
            newProgressBar.setPercentComplete(0);

            exportManager.saveProgessBar(newProgressBar);

            DateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmssS");
            Date date = new Date();
            String fileName = "";
            String delimiter = ",";

            if (exportDetails.getExportType() == 1) {
                fileName = new StringBuilder().append("clientExport").append(dateFormat.format(date)).append(".csv")
                        .toString();
                delimiter = ",";
            } else if (exportDetails.getExportType() == 2) {
                fileName = new StringBuilder().append("clientExport").append(dateFormat.format(date)).append(".txt")
                        .toString();
                delimiter = ",";
            } else if (exportDetails.getExportType() == 3) {
                fileName = new StringBuilder().append("clientExport").append(dateFormat.format(date)).append(".txt")
                        .toString();
                delimiter = "|";
            } else if (exportDetails.getExportType() == 4) {
                fileName = new StringBuilder().append("clientExport").append(dateFormat.format(date)).append(".txt")
                        .toString();
                delimiter = "\t";
            }

            /* Create new export file */
            InputStream inputStream = null;
            OutputStream outputStream = null;

            fileSystem dir = new fileSystem();
            dir.setDir(registryName, "exportFiles");

            File newFile = new File(dir.getDir() + fileName);

            /* Create the empty file in the correct location */
            try {

                if (newFile.exists()) {
                    int i = 1;
                    while (newFile.exists()) {
                        int iDot = fileName.lastIndexOf(".");
                        newFile = new File(dir.getDir() + fileName.substring(0, iDot) + "_(" + ++i + ")"
                                + fileName.substring(iDot));
                    }
                    fileName = newFile.getName();
                    newFile.createNewFile();
                } else {
                    newFile.createNewFile();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

            /* Read in the file */
            FileInputStream fileInput = null;
            File file = new File(dir.getDir() + fileName);
            fileInput = new FileInputStream(file);

            exportFileName = fileName;

            FileWriter fw = null;

            try {
                fw = new FileWriter(file, true);
            } catch (IOException ex) {
                Logger.getLogger(exportManager.class.getName()).log(Level.SEVERE, null, ex);
            }

            exportDetails.setExportFile(exportFileName);
            exportManager.saveExport(exportDetails);

            StringBuilder exportRow = new StringBuilder();
            exportRow.append("CLIENT IDENTIFIER").append(delimiter);
            exportRow.append("DOB").append(delimiter);
            exportRow.append("SEX").append(delimiter); /* P */
            exportRow.append("VISIT DATE").append(delimiter); /* P */
            exportRow.append("WEIGHT").append(delimiter); /* P */
            exportRow.append("HEIGHT").append(delimiter); /* P */
            exportRow.append("BP SYSTOLIC").append(delimiter); /* P */
            exportRow.append("BP DIASTOLIC").append(delimiter); /* P */

            exportRow.append(System.getProperty("line.separator"));

            fw.write(exportRow.toString());

            Integer participantId = 0;

            List<programExportFields> exportFields = programmanager.getProgramExportFields(programId);

            List<String> tableinfo = new ArrayList<String>();

            Integer fieldId = 0;
            Integer customFieldId = 0;
            Integer validationValue = 0;
            String fieldTypeValue = "";
            Integer crosswalkId = 0;

            for (programExportFields field : exportFields) {

                if (field.getFieldType() == 1) {
                    fieldTypeValue = "1";
                    programClientFields fieldDetails = clientmanager.getClientFieldDetails(programId,
                            field.getFieldId());
                    fieldId = fieldDetails.getFieldId();
                    customFieldId = fieldDetails.getCustomfieldId();
                    validationValue = fieldDetails.getValidationId();
                    crosswalkId = fieldDetails.getCrosswalkId();
                } else {
                    fieldTypeValue = "2";
                    programEngagementFields fieldDetails = engagementmanager.getEngagementFieldDetails(programId,
                            field.getFieldId());
                    fieldId = fieldDetails.getFieldId();
                    customFieldId = fieldDetails.getCustomfieldId();
                    validationValue = fieldDetails.getValidationId();
                    crosswalkId = fieldDetails.getCrosswalkId();
                }

                if (fieldId > 0) {
                    dataElements fieldDetails = clientmanager.getFieldDetails(fieldId);
                    tableinfo.add(fieldTypeValue + "-" + fieldDetails.getSaveToTableName() + "-"
                            + fieldDetails.getSaveToTableCol() + "-" + validationValue + "-" + crosswalkId);
                }

                else if (customFieldId > 0) {
                    customProgramFields fieldDetails = clientmanager.getCustomFieldDetails(customFieldId);
                    tableinfo.add(fieldTypeValue + "-" + fieldDetails.getSaveToTable() + "-"
                            + fieldDetails.getSaveToTableCol() + "-" + validationValue + "-" + crosswalkId);
                }
            }

            Integer totalDone = 0;
            float percentComplete;

            progressBar exportProgressBar = exportManager.getProgressBar(exportDetails.getUniqueId());

            for (engagements engagement : engagements) {

                exportRow = new StringBuilder();

                programHierarchyDetails hierarchyDetails = hierarchymanager.getProgramHierarchyItemDetails(
                        clientmanager.getClientEntities(engagement.getProgramPatientId()).getEntity3Id());

                String siteDisplayId = "";

                if (hierarchyDetails.getDisplayId() != null) {
                    siteDisplayId = hierarchyDetails.getDisplayId();
                }

                //exportRow.append(siteDisplayId).append(delimiter);

                if (tableinfo != null & tableinfo.size() > 0) {
                    for (String table : tableinfo) {
                        String[] tableInfoArray = table.split("-");
                        String fieldValue = "";
                        String fieldType = tableInfoArray[0];
                        String tablename = tableInfoArray[1];
                        String tableCol = tableInfoArray[2];
                        String validation = tableInfoArray[3];
                        String crosswalk = tableInfoArray[4];

                        if ("1".equals(fieldType)) {
                            fieldValue = clientmanager.getTableData(tablename, tableCol,
                                    engagement.getProgramPatientId());
                        } else {
                            fieldValue = engagementmanager.getTableData(tablename, tableCol, engagement.getId(),
                                    engagement.getProgramPatientId());
                        }

                        if ("55".equals(crosswalk) && "0".equals(fieldValue)) {
                            fieldValue = "2";
                        }

                        /* If date format to correct display format */
                        if ("4".equals(validation)) {
                            if ("dob".equals(tableCol)) {
                                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                                Date dob = format.parse(fieldValue);
                                Date today = new Date();

                                format.applyPattern("yyyy");
                                String dobAsString = format.format(dob);
                                String todayAsString = format.format(today);

                                Integer age = Integer.parseInt(todayAsString) - Integer.parseInt(dobAsString);
                                exportRow.append(age).append(delimiter);
                            } else {
                                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                                Date formatedDate = format.parse(fieldValue);
                                format.applyPattern("MM/dd/yy");
                                exportRow.append(format.format(formatedDate)).append(delimiter);
                            }
                        } else {
                            exportRow.append(fieldValue).append(delimiter);
                        }
                    }
                }

                exportRow.append(System.getProperty("line.separator"));
                fw.write(exportRow.toString());

                //Update progress bar
                totalDone = totalDone + 1;
                percentComplete = ((float) totalDone) / engagements.size();
                exportProgressBar.setPercentComplete(Math.round(percentComplete * 100));
                exportManager.saveProgessBar(exportProgressBar);

            }

            fw.close();
        }

        ModelAndView mav = new ModelAndView();

        /* If no results are found */
        if (exportFileName.isEmpty()) {
            mav.setViewName("/importExport/exportModal");

            savedExports newexportDetails = new savedExports();
            exportDetails.setExportType(1);

            programOrgHierarchy level1 = hierarchymanager.getProgramOrgHierarchyBydspPos(1, programId);
            programOrgHierarchy level2 = hierarchymanager.getProgramOrgHierarchyBydspPos(2, programId);
            programOrgHierarchy level3 = hierarchymanager.getProgramOrgHierarchyBydspPos(3, programId);

            Integer userId;
            if (userDetails.getRoleId() == 2) {
                userId = 0;
            } else {
                userId = userDetails.getId();
            }

            List<programHierarchyDetails> level1Items = hierarchymanager.getProgramHierarchyItems(level1.getId(),
                    userId);
            mav.addObject("level1Items", level1Items);

            mav.addObject("level1Name", level1.getName());
            mav.addObject("level2Name", level2.getName());
            mav.addObject("level3Name", level3.getName());

            mav.addObject("exportDetails", newexportDetails);
            mav.addObject("showDateRange", true);
            mav.addObject("noresults", true);
            ual.setMiscNotes("mav view:" + mav.getViewName() + "^^^^^" + "No Results.");

        } else {
            if (exportDetails.getUniqueId() > 0) {
                /* Delete progress bar entry */
                exportManager.deleteProgressBar(exportDetails.getUniqueId());
            }

            mav.setViewName("/importExport/exportDownloadModal");
            mav.addObject("exportFileName", exportFileName);
            ual.setMiscNotes("mav view:" + mav.getViewName() + "^^^^^exportFileName:" + exportFileName
                    + "^^^^^exportDetails:" + exportDetails.getId());

        }

        usermanager.saveUserActivityLog(ual);

        return mav;
    }

    /**
     * The '/updateProgressBar.do' request will return the current value of export progress bar.
     *
     * @param uniqueId  The unique export id
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/updateProgressBar.do", method = RequestMethod.GET)
    @ResponseBody
    public Integer updateProgressBar(HttpSession session, @RequestParam Integer uniqueId) throws Exception {

        /** Log Here **/
        userActivityLog ual = new userActivityLog();
        if (session.getAttribute("userDetails") != null) {
            User userDetails = (User) session.getAttribute("userDetails");
            ual.setSystemUserId(userDetails.getId());
        }
        ual.setMapping("/updateProgressBar.do");
        ual.setRequestMethod("GET");
        ual.setMethodAccessed("updateProgressBar");
        ual.setModuleId(moduleId);
        ual.setProgramId(programId);

        if (uniqueId != null && !"".equals(uniqueId) && uniqueId > 0) {
            progressBar exportProgressBar = exportManager.getProgressBar(uniqueId);

            if (exportProgressBar != null) {
                ual.setMiscNotes(
                        "exportProgressBar:" + exportProgressBar.getId() + "^^^^^" + "uniqueId:" + uniqueId);
                usermanager.saveUserActivityLog(ual);

                return exportProgressBar.getPercentComplete();
            } else {
                ual.setMiscNotes("exportProgressBar:null");
                usermanager.saveUserActivityLog(ual);
                return 0;
            }
        } else {
            ual.setMiscNotes("uniqueId:null");
            usermanager.saveUserActivityLog(ual);
            return 0;
        }

    }

    /**
     * The '/deleteExport' GET request will change the status of the selected client .
     * 
     * @param exportId The encrypted url value containing the selected user id
     * @return
     * @throws Exception 
     */
    @RequestMapping(value = "/deleteExport", method = RequestMethod.GET)
    @ResponseBody
    public boolean deleteExport(HttpSession session,
            @RequestParam(value = "exportId", required = false) Integer exportId) throws Exception {

        /** Log Here **/
        userActivityLog ual = new userActivityLog();
        User userDetails = (User) session.getAttribute("userDetails");
        ual.setSystemUserId(userDetails.getId());
        ual.setMapping("/deleteExport");
        ual.setRequestMethod("GET");
        ual.setMethodAccessed("deleteExport");
        ual.setModuleId(moduleId);
        ual.setProgramId(programId);

        if (exportId != null) {
            ual.setRelatedId(exportId);
            ual.setRelatedIdCol("exportId");
            exportManager.deleteExport(exportId);
        }

        usermanager.saveUserActivityLog(ual);

        return true;
    }

    @RequestMapping(value = "/saveImportForm.do", method = RequestMethod.POST)
    public @ResponseBody ModelAndView saveImportForm(
            @ModelAttribute(value = "programImport") programImport programImport,
            @RequestParam(value = "uploadFile", required = false) List<MultipartFile> uploadFiles,
            RedirectAttributes redirectAttr, HttpSession session

    ) throws Exception {

        /* Get a list of completed surveys the logged in user has access to */
        User userDetails = (User) session.getAttribute("userDetails");
        MultipartFile uploadFile = uploadFiles.get(0);
        programImport.setProgramId(programId);
        List<programImportError> pieList = importmanager.saveSubmitFile(uploadFile, programImport, userDetails);

        redirectAttr.addFlashAttribute("pieList", pieList);
        ModelAndView mav = new ModelAndView(new RedirectView("/import-export/import"));

        /** Log Here **/
        userActivityLog ual = new userActivityLog();
        ual.setSystemUserId(userDetails.getId());
        ual.setMapping("/saveImportForm.do");
        ual.setRequestMethod("POST");
        ual.setMethodAccessed("saveImportForm");
        ual.setModuleId(moduleId);
        ual.setRelatedId(programImport.getId());
        ual.setRelatedIdCol("programImportId");
        ual.setProgramId(programId);
        ual.setMiscNotes("uploadFile:" + uploadFile.getOriginalFilename());
        usermanager.saveUserActivityLog(ual);

        return mav;

    }

    /**
     * The '/import-export/getImportForm.do' GET request will 
     * return the overlay of an import form
     * form.
     *
     * @param session
     * @param subfolder
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/getStatusDetails.do", method = RequestMethod.GET)
    public @ResponseBody ModelAndView getStatusDetails(HttpSession session,
            @RequestParam(value = "statusId", required = false) Integer statusId) throws Exception {

        ModelAndView mav = new ModelAndView();
        mav.setViewName("/importExport/statusDetails");
        processStatus statusDetails = importmanager.getProcessStatusById(statusId);
        mav.addObject("statusDetails", statusDetails);

        /** Log Here **/
        userActivityLog ual = new userActivityLog();
        User userDetails = (User) session.getAttribute("userDetails");
        ual.setSystemUserId(userDetails.getId());
        ual.setMapping("/saveImportForm.do");
        ual.setRequestMethod("POST");
        ual.setMethodAccessed("saveImportForm");
        ual.setModuleId(moduleId);
        ual.setRelatedId(statusId);
        ual.setRelatedIdCol("statusId");
        ual.setProgramId(programId);
        usermanager.saveUserActivityLog(ual);

        return mav;
    }

    /**
     * The '/auditReport' GET request will display the created csv report
     * 
     * @param i The encrypted url value containing the selected user id
     * @param v The encrypted url value containing the secret decode key
     * @return
     * @throws Exception 
     */
    @RequestMapping(value = "/auditReport", method = { RequestMethod.GET, RequestMethod.POST })
    public ModelAndView viewReport(HttpServletResponse response, HttpSession session, HttpServletRequest request,
            @RequestParam(value = "i", required = false) String i,
            @RequestParam(value = "v", required = false) String v,
            @RequestParam(value = "formFileName", required = false) String formFileName,
            @RequestParam(value = "formDate", required = false) String formDate) throws Exception {

        ModelAndView mav = new ModelAndView();
        mav.setViewName("/auditReport");

        boolean hasPermission = false;
        /* Get a list of completed surveys the logged in user has access to */
        User userDetails = (User) session.getAttribute("userDetails");

        /* Get user permissions */
        userProgramModules modulePermissions = usermanager.getUserModulePermissions(programId, userDetails.getId(),
                moduleId);

        if (userDetails.getRoleId() == 2) {
            allowImport = true;
            allowExport = true;
            allowDelete = true;
        } else {
            allowImport = modulePermissions.isAllowImport();
            allowExport = modulePermissions.isAllowExport();
            allowDelete = modulePermissions.isAllowDelete();
        }

        mav.addObject("allowImport", allowImport);
        mav.addObject("allowExport", allowExport);
        mav.addObject("allowDelete", allowDelete);

        int importId = 0;

        /** make sure it belongs to user **/
        int userId = 0;
        if (userDetails.getRoleId() != 2) {
            userId = userDetails.getId();
        }

        /** if querying by date - if we either return a table with sites break down
         *  or return the piList **/
        if (formDate != null) {
            mav.addObject("selectedDate", formDate);
            List<programImport> importList = new ArrayList<programImport>();
            if (userDetails.getRoleId() == 2) {
                importList = importmanager.getProgramImportByImportDate(programId, userId, Arrays.asList(52),
                        formDate);
            } else {
                importList = importmanager.getdProgramImportsForUserAllowedTypesByDate(programId, userId,
                        Arrays.asList(52), formDate);
            }

            if (importList.size() == 1) {
                importId = importList.get(0).getId();
            } else {
                encryptObject encrypt = new encryptObject();
                Map<String, String> map;

                Map<Integer, processStatus> psMap = new HashMap<Integer, processStatus>();
                List<processStatus> psList = importmanager.getAllProcessStatus();
                for (processStatus ps : psList) {
                    psMap.put(ps.getId(), ps);
                }
                for (programImport pi : importList) {
                    pi.setProcessStatus(psMap.get(pi.getStatusId()));
                    //Encrypt the use id to pass in the url
                    map = new HashMap<String, String>();
                    map.put("id", Integer.toString(pi.getId()));
                    map.put("topSecret", topSecret);

                    String[] encrypted = encrypt.encryptObject(map);
                    pi.setEncryptedId(encrypted[0]);
                    pi.setEncryptedSecret(encrypted[1]);

                }

                mav.addObject("importList", importList);
            }
        } else if (formFileName != null) {
            if (userDetails.getRoleId() == 2) {
                hasPermission = true;
            } else {
                hasPermission = importmanager.checkImportPermission(0, formFileName, userId);
            }
            programImport piDetails = new programImport();
            if (hasPermission) {
                piDetails = importmanager.getProgramImportByAssignedName(formFileName, 0);
                importId = piDetails.getId();
            }

        } else if (i != null && v != null) {
            /* Decrypt the url */
            decryptObject decrypt = new decryptObject();
            Object obj = decrypt.decryptObject(i, v);
            String[] result = obj.toString().split((","));
            importId = Integer.parseInt(result[0].substring(4));
        }

        /** get pilist **/

        List<programImport> piList = importmanager.getdProgramImportsForUserAllowedTypes(programId, userId,
                Arrays.asList(52));

        mav.addObject("piList", piList);

        /** get date list **/
        List<programImport> piDateList = importmanager.getProgramImportDate(programId, userId, Arrays.asList(52));
        mav.addObject("piDateList", piDateList);

        if (importId != 0) {
            //check user permission to batch
            if (userDetails.getRoleId() == 2) {
                hasPermission = true;
            } else {
                hasPermission = importmanager.checkImportPermission(importId, null, userId);
            }
            programImport piDetails = new programImport();

            if (hasPermission) {
                piDetails = importmanager.getProgramImportById(importId, 0);
                piDetails.setUploadedAsProgramUploadTypeName(
                        importmanager.getImportTypeById(piDetails.getUploadedAsProgramUploadTypeId()).getName());
                //Encrypt the use id to pass in the url
                encryptObject encrypt = new encryptObject();
                Map<String, String> map;
                map = new HashMap<String, String>();
                map.put("id", Integer.toString(piDetails.getId()));
                map.put("topSecret", topSecret);

                String[] encrypted = encrypt.encryptObject(map);
                piDetails.setEncryptedId(encrypted[0]);
                piDetails.setEncryptedSecret(encrypted[1]);

                mav.addObject("pi", piDetails);
                mav.addObject("selectedImport", piDetails.getAssignedFileName());

                //we generate the audit reports here.  We display sites and arrow for drop down
                List<programOrgHierarchy> hierarchies = hierarchymanager.getProgramOrgHierarchy(programId);
                String entityCol = importmanager.getEntityCol(importId, hierarchies);
                programImportType pit = importmanager.getImportTypeById(piDetails.getProgramUploadTypeId());
                //we check to see if it is a child program upload type, if so we use the fields set up for parents
                programImportType processPIT = pit;
                if (pit.getParentProgramUploadTypeId() != 0) {
                    processPIT = importmanager.getImportTypeById(pit.getParentProgramUploadTypeId());
                }
                programImportTypeField entityDetails = importmanager
                        .getFieldDetailsByTableAndColumn(processPIT.getId(), "programpatiententities", entityCol);

                List<programHierarchyDetails> associatedWithList = new ArrayList<programHierarchyDetails>();

                List<programImportAuditError> nullAuditErrorList = importmanager.checkImportForNullEntity(importId);
                //check if there are nulls
                if (nullAuditErrorList.size() > 0) {
                    programHierarchyDetails nullTier = new programHierarchyDetails();
                    nullTier.setId(0);
                    nullTier.setName("Invalid");
                    nullTier.setDisplayId("");
                    List<programImportEntityAuditDetails> detailList = new ArrayList<programImportEntityAuditDetails>();
                    detailList.add(importmanager.getAuditDetailForEntity(nullTier, new programHierarchyDetails(),
                            entityDetails, importId, entityCol));
                    detailList.get(0).setEntityId(null);
                    detailList.get(0).setEntityName("Invalid");

                    nullTier.setEntityAuditDetails(detailList);
                    associatedWithList.add(nullTier);
                }

                List<programHierarchyDetails> tierWithEntityList = new ArrayList<programHierarchyDetails>(
                        importmanager.getEntityDetailsForAuditReports(null, importId, entityDetails,
                                "associatedwith", entityCol));
                //we get level 2 name in audit reports
                //we loop and look for level 3 and we calculate totals 
                for (programHierarchyDetails topTier : tierWithEntityList) {
                    topTier.setEntityAuditDetails(
                            importmanager.getAuditDetail(topTier, entityDetails, importId, entityCol));
                }

                //get reportable fields
                programImportTypeReportField reportHeaders = importmanager.getAuditReportHeader(processPIT.getId());

                //get dropped values
                List<programImportDroppedValues> droppedValues = importmanager.getDroppedValues(piDetails);

                associatedWithList.addAll(tierWithEntityList);
                mav.addObject("reportHeaders", reportHeaders);
                mav.addObject("droppedValues", droppedValues);
                mav.addObject("associatedWithList", associatedWithList);
                mav.addObject("topTierLabel", hierarchies.get(hierarchies.size() - 2).getName());
                mav.addObject("entityLabel", hierarchies.get(hierarchies.size() - 1).getName());
            }
        }

        /** Log Here **/
        userActivityLog ual = new userActivityLog();
        ual.setSystemUserId(userDetails.getId());
        ual.setMapping("/auditReport.do");
        ual.setRequestMethod(request.getMethod());
        ual.setMethodAccessed("viewReport");
        ual.setModuleId(moduleId);
        ual.setRelatedId(importId);
        ual.setRelatedIdCol("programUploadId");
        ual.setProgramId(programId);
        if (formFileName != null) {
            ual.setMiscNotes("hasPermission:" + hasPermission + "^^^^^formFileName:" + formFileName
                    + "^^^^^formDate:" + formDate);
        }
        usermanager.saveUserActivityLog(ual);

        return mav;
    }

    @RequestMapping(value = "/DLFile", method = { RequestMethod.GET })
    public void DLReport(@RequestParam String i, @RequestParam String v,
            @RequestParam(value = "f", required = true) String f, HttpSession session, HttpServletResponse response)
            throws Exception {

        boolean hasPermission = false;
        /* Get a list of completed surveys the logged in user has access to */
        User userDetails = (User) session.getAttribute("userDetails");

        /* Get user permissions */
        userProgramModules modulePermissions = usermanager.getUserModulePermissions(programId, userDetails.getId(),
                moduleId);

        if (userDetails.getRoleId() == 2) {
            allowImport = true;
            allowExport = true;
            allowDelete = true;
        } else {
            allowImport = modulePermissions.isAllowImport();
            allowExport = modulePermissions.isAllowExport();
            allowDelete = modulePermissions.isAllowDelete();
        }

        int importId = 0;

        programImportLogFileView logFileView = new programImportLogFileView();
        logFileView.setSystemUserId(userDetails.getId());
        logFileView.setProgramUploadId(importId);
        logFileView.setViewAction("Access /DLFile - " + f);

        /** Log Here **/
        userActivityLog ual = new userActivityLog();
        ual.setSystemUserId(userDetails.getId());
        ual.setMapping("//DLFile");
        ual.setRequestMethod("GET");
        ual.setMethodAccessed("DLReport");
        ual.setModuleId(moduleId);
        ual.setRelatedId(importId);
        ual.setRelatedIdCol("programUploadId");
        ual.setProgramId(programId);
        String miscNotes = "filename:" + f;

        if (!allowImport) {
            // no permission
            logFileView.setProgramUploadId(importId);
            logFileView.setViewAction("Not Allowed To Import /DLFile");
            importmanager.saveProgramImportLogFileView(logFileView);
            ual.setMiscNotes(miscNotes + "^^^^^No Permission:Not Allowed To Import /DLFile");
            usermanager.saveUserActivityLog(ual);
            throw new Exception("Not Allowed to Access Import - " + importId);
        } else {
            if (i != null && v != null) {
                /* Decrypt the url */
                decryptObject decrypt = new decryptObject();
                Object obj = decrypt.decryptObject(i, v);
                String[] result = obj.toString().split((","));
                importId = Integer.parseInt(result[0].substring(4));
            }
            if (importId != 0) {

                /** make sure user is allowed to access this file **/
                int userId = 0;
                if (userDetails.getRoleId() != 2) {
                    userId = userDetails.getId();
                }

                //check user permission to batch
                if (userDetails.getRoleId() == 2) {
                    hasPermission = true;
                } else {
                    hasPermission = importmanager.checkImportPermission(importId, null, userId);
                }

                if (hasPermission) {
                    programImport pi = importmanager.getProgramImportById(importId, 0);

                    //we grab the file from /importFiles

                    /* f of it = importTypes, user's original file
                     * f of ai = archivesIn, user's converted file
                     * */
                    String folderName = "importFiles";
                    String fileExt = pi.getInFileExt();
                    if (f.equals("ai")) {
                        folderName = "archivesIn";
                        fileExt = pi.getOutFileExt();
                    }
                    String filePath = importmanager.getFilePath(programId, folderName);
                    String filePath1 = importmanager.getFilePath(programId, "loadFiles");
                    String fileName = pi.getAssignedFileName() + "." + fileExt;
                    String fileOut = filePath + pi.getAssignedFileName() + "_dec." + fileExt;

                    File fout = new File(fileOut);
                    try {
                        File file = new File(filePath + fileName);
                        boolean fileExists = false;
                        if (file.exists()) {
                            fileExists = true;
                        } else {
                            file = new File(filePath1 + fileName);
                            filePath = filePath1;
                            if (file.exists()) {
                                fileExists = true;
                            }
                        }

                        if (!fileExists) {
                            ual.setMiscNotes(miscNotes + "^^^^^File is missing:" + filePath + fileName);
                            usermanager.saveUserActivityLog(ual);
                            throw new Exception("Error with File " + filePath + fileName);
                        } else {
                            //decrypt file
                            if (!fout.exists()) {
                                filemanager.decodeTo64(file, fout);
                            }
                        }
                    } catch (Exception e) {
                        try {
                            //update file to error
                            ual.setMiscNotes(miscNotes + "^^^^^File is missing:" + filePath + fileName);
                            usermanager.saveUserActivityLog(ual);
                            throw new Exception("File does not exists " + filePath + fileName);
                        } catch (Exception ex1) {
                            ual.setMiscNotes(miscNotes + "^^^^^File is missing:" + filePath + fileName);
                            usermanager.saveUserActivityLog(ual);
                            throw new Exception("File does not exists " + filePath + fileName + ex1);
                        }
                    }

                    try {
                        // get your file as InputStream
                        InputStream is = new FileInputStream(fileOut);
                        // copy it to response's OutputStream
                        String dlFileName = fileName;
                        if (f.equalsIgnoreCase("it")) {
                            dlFileName = pi.getUploadedFileName();
                        }
                        String mimeType = "application/octet-stream";
                        response.setContentType(mimeType);
                        response.setHeader("Content-Transfer-Encoding", "binary");
                        response.setHeader("Content-Disposition", "attachment;filename=\"" + dlFileName + "\"");
                        org.apache.commons.io.IOUtils.copy(is, response.getOutputStream());
                        response.flushBuffer();
                        is.close();

                        if (fout.exists()) {
                            fout.delete();
                        }

                    } catch (IOException ex) {
                        ual.setMiscNotes(miscNotes + "^^^^^File output errored:" + filePath + fileName);
                        usermanager.saveUserActivityLog(ual);
                        ex.printStackTrace();
                        System.out
                                .println("Error writing file to output stream. Filename was '{}'" + fileName + ex);

                    }
                }

                //we log user
                logFileView.setProgramUploadId(importId);
                importmanager.saveProgramImportLogFileView(logFileView);
                ual.setMiscNotes(miscNotes + "^^^^^Viewed file:" + f);
                usermanager.saveUserActivityLog(ual);

            } else {
                // no permission
                logFileView.setProgramUploadId(importId);
                logFileView.setViewAction("Access Denied /DLFile");
                importmanager.saveProgramImportLogFileView(logFileView);
                ual.setMiscNotes(miscNotes + "^^^^^Access Denied /DLFile:" + f);
                usermanager.saveUserActivityLog(ual);

                throw new Exception("invalid import file download - " + importId);
            }

        }
    }

    @RequestMapping(value = "/clearFile", method = { RequestMethod.GET })
    public ModelAndView clearFile(@RequestParam String i, @RequestParam String v,
            @RequestParam(value = "a", required = true) Integer clearAction, HttpSession session,
            HttpServletResponse response) throws Exception {

        boolean hasPermission = false;
        /* Get a list of completed surveys the logged in user has access to */
        User userDetails = (User) session.getAttribute("userDetails");

        /* Get user permissions */
        userProgramModules modulePermissions = usermanager.getUserModulePermissions(programId, userDetails.getId(),
                moduleId);

        if (userDetails.getRoleId() == 2) {
            allowImport = true;
            allowExport = true;
            allowDelete = true;
        } else {
            allowImport = modulePermissions.isAllowImport();
            allowExport = modulePermissions.isAllowExport();
            allowDelete = modulePermissions.isAllowDelete();
        }

        int importId = 0;

        programImportLogFileView logFileView = new programImportLogFileView();
        logFileView.setSystemUserId(userDetails.getId());
        logFileView.setProgramUploadId(importId);
        logFileView.setViewAction("Access /clearFile - " + clearAction);

        /** Log Here **/
        userActivityLog ual = new userActivityLog();
        ual.setSystemUserId(userDetails.getId());
        ual.setMapping("/clearFile");
        ual.setRequestMethod("GET");
        ual.setMethodAccessed("clearFile");
        ual.setModuleId(moduleId);
        ual.setProgramId(programId);
        ual.setRelatedIdCol("importId");

        if (!allowImport) {
            // no permission
            logFileView.setProgramUploadId(importId);
            logFileView.setViewAction("Not Allowed to Access Import");
            ual.setRelatedId(importId);
            ual.setMiscNotes("clearAction:" + clearAction + "^^^^^Not Allowed to Access Import");
            usermanager.saveUserActivityLog(ual);
            importmanager.saveProgramImportLogFileView(logFileView);
            throw new Exception("Not Allowed to Access Import - " + importId);
        } else {
            if (i != null && v != null) {
                /* Decrypt the url */
                decryptObject decrypt = new decryptObject();
                Object obj = decrypt.decryptObject(i, v);
                String[] result = obj.toString().split((","));
                importId = Integer.parseInt(result[0].substring(4));
            }
            if (importId != 0) {

                /** make sure user is allowed to access this file **/
                int userId = 0;
                if (userDetails.getRoleId() != 2) {
                    userId = userDetails.getId();
                }

                //only role2 users can clear files
                if (userDetails.getRoleId() == 2) {
                    hasPermission = true;
                } else {
                    hasPermission = false;
                }

                if (hasPermission) {
                    programImport pi = importmanager.getProgramImportById(importId, 0);

                    //we grab the file from /importFiles

                    /* a of 1 = clear db
                     * a of 2 = clear db & file
                     * a of 3 = DNP
                     * a of 4 = CNH - clear and hold
                     * */
                    importmanager.clearImport(pi, userDetails, clearAction);

                    ual.setRelatedId(importId);
                    ual.setMiscNotes("clearAction:" + clearAction);
                    usermanager.saveUserActivityLog(ual);

                } else {
                    logFileView.setProgramUploadId(importId);
                    logFileView.setViewAction("Do not have permission to clear files");
                    importmanager.saveProgramImportLogFileView(logFileView);
                    ual.setRelatedId(importId);
                    ual.setMiscNotes("clearAction:" + clearAction + "^^^^^Do not have permission to clear files");
                    usermanager.saveUserActivityLog(ual);
                    throw new Exception("Not Allowed to Clear Import - " + importId);
                }
            }
        }
        ModelAndView mav = new ModelAndView(new RedirectView("/import-export/import"));
        return mav;

    }

    @RequestMapping(value = "/setCompleted", method = { RequestMethod.GET })
    public ModelAndView setCompleted(@RequestParam String i, @RequestParam String v, HttpSession session,
            HttpServletResponse response) throws Exception {

        boolean hasPermission = false;
        /* Get a list of completed surveys the logged in user has access to */
        User userDetails = (User) session.getAttribute("userDetails");

        /* Get user permissions */
        userProgramModules modulePermissions = usermanager.getUserModulePermissions(programId, userDetails.getId(),
                moduleId);

        if (userDetails.getRoleId() == 2) {
            allowImport = true;
            allowExport = true;
            allowDelete = true;
        } else {
            allowImport = modulePermissions.isAllowImport();
            allowExport = modulePermissions.isAllowExport();
            allowDelete = modulePermissions.isAllowDelete();
        }

        int importId = 0;

        programImportLogFileView logFileView = new programImportLogFileView();
        logFileView.setSystemUserId(userDetails.getId());
        logFileView.setProgramUploadId(importId);
        logFileView.setViewAction("Access /setCompleted");

        /** Log Here **/
        userActivityLog ual = new userActivityLog();
        ual.setSystemUserId(userDetails.getId());
        ual.setMapping("/setCompleted");
        ual.setRequestMethod("GET");
        ual.setMethodAccessed("setCompleted");
        ual.setModuleId(moduleId);
        ual.setRelatedIdCol("programUploadId");
        ual.setProgramId(programId);

        if (!allowImport) {
            // no permission
            logFileView.setProgramUploadId(importId);
            logFileView.setViewAction("Not Allowed to Access Import");
            importmanager.saveProgramImportLogFileView(logFileView);
            ual.setRelatedId(importId);
            ual.setMiscNotes("hasPermission:" + hasPermission + "^^^^^allowImport:" + allowImport);
            usermanager.saveUserActivityLog(ual);

            throw new Exception("Not Allowed to Access Import - " + importId);
        } else {
            if (i != null && v != null) {
                /* Decrypt the url */
                decryptObject decrypt = new decryptObject();
                Object obj = decrypt.decryptObject(i, v);
                String[] result = obj.toString().split((","));
                importId = Integer.parseInt(result[0].substring(4));
            }
            if (importId != 0) {

                /** make sure user is allowed to access this file **/
                int userId = 0;
                if (userDetails.getRoleId() != 2) {
                    userId = userDetails.getId();
                }

                //only role2 users can clear files
                if (userDetails.getRoleId() == 2) {
                    hasPermission = true;
                } else {
                    hasPermission = false;
                }

                if (hasPermission) {
                    programImport pi = importmanager.getProgramImportById(importId, 0);
                    importmanager.setProgramImportToCompleted(pi);

                    ual.setRelatedId(importId);
                    ual.setMiscNotes("hasPermission:" + hasPermission);
                    usermanager.saveUserActivityLog(ual);

                } else {
                    logFileView.setProgramUploadId(importId);
                    logFileView.setViewAction("Do not have permission to set program import to completed status");
                    importmanager.saveProgramImportLogFileView(logFileView);

                    ual.setRelatedId(importId);
                    ual.setMiscNotes("hasPermission:" + hasPermission
                            + "^^^^^Do not have permission to set program import to completed status.");
                    usermanager.saveUserActivityLog(ual);

                    throw new Exception("Not Allowed to Clear Import - " + importId);
                }
            }
        }
        ModelAndView mav = new ModelAndView(new RedirectView("/import-export/import"));
        return mav;

    }

}