依赖于会话的业务(服务)层

时间:2017-05-25 00:12:02

标签: java spring web-services java-ee architecture

我目前正在开发一个使用spring作为框架的java / jee应用程序,我的应用程序是架构是3层的app.I有一个前端部分,通过休息调用与后端项目进行通信。在后端项目中,我有服务这将成为商务逻辑,但基本上服务是单身,但在我的情况下,服务取决于会话,所以我该如何解决?我可以让服务的勺子会话是那个逻辑吗?

这是我的服务代码:

package biz.picosoft.services;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.activiti.engine.HistoryService;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.history.HistoricProcessInstance;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.apache.chemistry.opencmis.client.api.CmisObject;
import org.apache.chemistry.opencmis.client.api.Folder;
import org.apache.chemistry.opencmis.client.api.Session;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import biz.picosoft.daoImpl.DocumentDaoImpl;
import biz.picosoft.daoImpl.FolderDaoImpl;
import biz.picosoft.mains.TestDao;

@Service
public class CourriersArrivésImpl implements CourriersServices {

    ProcessEngine processEngine;
    Session session;
    RuntimeService runtimeService;
    TaskService taskService;

    @Override
    // this method create a mail process and attach its file to it by calling
    // the attach file method
    // and then attach the folder of the mail

    public ProcessInstance créerCourrier(Map<String, Object> proprietésCourrier) {
        System.out.println("prop here" + proprietésCourrier);
        RuntimeService runtimeService = processEngine.getRuntimeService();
        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("courriersArrivés",
                proprietésCourrier);
        if ((boolean) proprietésCourrier.get("isValidated") != false) {
            @SuppressWarnings("unchecked")
            List<File> listePiécesJointes = (List<File>) proprietésCourrier.get("listePiécesJointes");
            if (listePiécesJointes != null) {
                FolderDaoImpl folderDaoImpl = new FolderDaoImpl(session);
                String idCourrierArrivéFolder = attachFiles(listePiécesJointes,
                        (String) proprietésCourrier.get("expéditeur"), processInstance.getId());
                List<CmisObject> lostOfFolderChildrens = folderDaoImpl.getAllChildrens((Folder) folderDaoImpl.getFolderById( idCourrierArrivéFolder));
                proprietésCourrier.put("idCourrierArrivéFolder", idCourrierArrivéFolder);
                proprietésCourrier.replace ("listePiécesJointes", lostOfFolderChildrens);
                runtimeService.setVariables(processInstance.getId(), proprietésCourrier);

            }
        } else {
            traiterCourrier(processInstance.getId(), proprietésCourrier);
        }
        // TODO Do not forget redirection with dispatcher
        this.taskService.complete(
                this.taskService.createTaskQuery().processInstanceId(processInstance.getId()).list().get(0).getId());
        // add the groups to ldap and affect réviserCourrier to BO

        taskService.addCandidateGroup(
                taskService.createTaskQuery().processInstanceId(processInstance.getId()).list().get(0).getId(),
                "Bureau d'ordre");

        return processInstance;
    }

    @Override
    public void réviser(String idProcess, boolean isValidated) {

        RuntimeService runtimeService = processEngine.getRuntimeService();
        ProcessInstance processInstance = runtimeService.createProcessInstanceQuery().processInstanceId(idProcess)
                .singleResult();

        if (isValidated) {

            validerCourrier(processInstance.getId());

        } else {
            refuserCourrier(processInstance.getId());
        }

    }

    @Override
    public void validerCourrier(String idProcess) {
        Map<String, Object> proprietésCourrier = runtimeService.getVariables((idProcess));
        proprietésCourrier.replace("isValidated", true);
        this.taskService = processEngine.getTaskService();
        this.taskService.complete(
                this.taskService.createTaskQuery().processInstanceId(idProcess).list().get(0).getId(),
                proprietésCourrier);
        this.taskService.addCandidateGroup(
                this.taskService.createTaskQuery().processInstanceId(idProcess).list().get(0).getId(),
                proprietésCourrier.get("départmentId").toString());
    }

    @Override
    public void traiterCourrier(String idProcess, Map<String, Object> nouvellesProprietésCourrier) {

        RuntimeService runtimeService = processEngine.getRuntimeService();
        ProcessInstance processInstance = runtimeService.createProcessInstanceQuery().processInstanceId(idProcess)
                .singleResult();
        runtimeService.setVariables(processInstance.getId(), nouvellesProprietésCourrier);
        this.taskService.complete(
                this.taskService.createTaskQuery().processInstanceId(processInstance.getId()).list().get(0).getId(),
                nouvellesProprietésCourrier);
        if ((boolean) nouvellesProprietésCourrier.get("isFinished") != true) {
            taskService.addCandidateGroup(
                    taskService.createTaskQuery().processInstanceId(processInstance.getId()).list().get(0).getId(),
                    nouvellesProprietésCourrier.get("affectedTo").toString());
        }
    }

    @Override
    public void archiverCourrier(String idProcess) {

    }

    // this method return all instances of courriers arrivés Process
    @Override
    public List<Map<String, Object>> getListCourriersArrivées() {
        String expéditeur;
        String société;
        String date;
        String objet;
        String idProcess;
        RuntimeService runtimeService = processEngine.getRuntimeService();
        List<ProcessInstance> listAllCourrierArrivé = runtimeService.createProcessInstanceQuery()
                .processDefinitionKey("courriersArrivés").list();
        List<Map<String, Object>> listVarsOfAllCourriers = new ArrayList<Map<String, Object>>();
        Map<String, Object> varsOfAnActiveProcess;
        for (int i = 0; i < listAllCourrierArrivé.size(); i++) {
            idProcess= listAllCourrierArrivé.get(i).getProcessInstanceId();
            varsOfAnActiveProcess = new HashMap<String, Object>();
            expéditeur = (String) runtimeService.getVariable(listAllCourrierArrivé.get(i).getProcessInstanceId(),
                    "expéditeur");
            société = (String) runtimeService.getVariable(listAllCourrierArrivé.get(i).getProcessInstanceId(),
                    "société");
            date = (String) runtimeService.getVariable(listAllCourrierArrivé.get(i).getProcessInstanceId(), "dateOut");
            objet = (String) runtimeService.getVariable(listAllCourrierArrivé.get(i).getProcessInstanceId(), "objet");
            varsOfAnActiveProcess.put("idProcess", idProcess);
            varsOfAnActiveProcess.put("expéditeur", expéditeur);
            varsOfAnActiveProcess.put("société", société);
            varsOfAnActiveProcess.put("date", date);
            varsOfAnActiveProcess.put("objet", objet);
            listVarsOfAllCourriers.add(varsOfAnActiveProcess);
        }
        return listVarsOfAllCourriers;

    }

    // this method attach files to a process and return the folder id
    // it checks also if necessary folder are already created or not if not it
    // will create it
    @Override
    public String attachFiles(List<File> listePiécesJointes, String expéditeur, String id) {
        Folder folderCourrier = null;
        if (listePiécesJointes != null) {
            DocumentDaoImpl documentDaoImpl = new DocumentDaoImpl();
            FolderDaoImpl folderDaoImpl = new FolderDaoImpl(this.session);
            Folder courriersArrivésFolderPerYear;
            Folder courriersArrivésFolder;

            try {
                try {
                    courriersArrivésFolder = (Folder) folderDaoImpl.getFolderByPath("/courriersArrivés");
                } catch (Exception myExction) {
                    courriersArrivésFolder = folderDaoImpl.createFolder((Folder) folderDaoImpl.getFolderByPath("/"),
                            "courriersArrivés");
                }
                courriersArrivésFolderPerYear = (Folder) folderDaoImpl.getFolderByPath(
                        courriersArrivésFolder.getPath() + "/" + Calendar.getInstance().get(Calendar.YEAR));
            } catch (Exception myExction) {

                courriersArrivésFolderPerYear = folderDaoImpl.createFolder(
                        (Folder) folderDaoImpl.getFolderByPath("/courriersArrivés"),
                        Integer.toString(Calendar.getInstance().get(Calendar.YEAR)));
            }
            folderCourrier = folderDaoImpl.createFolder(courriersArrivésFolderPerYear, expéditeur + id);
            for (int i = 0; i < listePiécesJointes.size(); i++) {
                try {
                    documentDaoImpl.inserte(listePiécesJointes.get(i), folderCourrier);
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }
            }

        }

        return folderCourrier.getId();
    }

    public ProcessEngine getProcessEngine() {
        return processEngine;
    }

    public void setProcessEngine(ProcessEngine processEngine) {
        this.processEngine = processEngine;
    }

    public CourriersArrivésImpl() {
        super();
        @SuppressWarnings("resource")
        ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("activit.cfg.xml");
        this.processEngine = (ProcessEngine) applicationContext.getBean("processEngine");
        this.runtimeService = processEngine.getRuntimeService();
        this.taskService = processEngine.getTaskService();
        @SuppressWarnings("resource")
        ApplicationContext ctx = new AnnotationConfigApplicationContext(TestDao.class);
        session = ctx.getBean(Session.class);

    }

    public Session getSession() {
        return session;
    }

    public void setSession(Session session) {
        this.session = session;
    }

    @Override
    // this method will return vars of active process per user
    public List<Map<String, Object>> getListActiveCourriersArrivésParUser(String userName) {
        // list of vars of active process per user
        List<Map<String, Object>> listVarsOfActiveProcesPerUser = new ArrayList<Map<String, Object>>();
        // get the list active tasks per user
        List<Task> listTaskByProceeAndUser = this.taskService.createTaskQuery().processDefinitionKey("courriersArrivés")
                .taskCandidateUser(userName).list();
        System.out.println("hhhh" + listTaskByProceeAndUser);

        if (listTaskByProceeAndUser != null) {
            // this will hold the vars of one task of the list of active process
            // per user
            String idProcess;
            String expéditeur;
            String société;
            String date;
            String objet;
            Map<String, Object> varsOfAnActiveProcessPerUser;
            for (int i = 0; i < listTaskByProceeAndUser.size(); i++) {
                varsOfAnActiveProcessPerUser = new HashMap<String, Object>();
                idProcess= listTaskByProceeAndUser.get(i).getProcessInstanceId();
                expéditeur = (String) runtimeService.getVariable(listTaskByProceeAndUser.get(i).getProcessInstanceId(),
                        "expéditeur");
                société = (String) runtimeService.getVariable(listTaskByProceeAndUser.get(i).getProcessInstanceId(),
                        "société");
                date = (String) runtimeService.getVariable(listTaskByProceeAndUser.get(i).getProcessInstanceId(),
                        "dateOut");
                objet = (String) runtimeService.getVariable(listTaskByProceeAndUser.get(i).getProcessInstanceId(),
                        "objet");
                System.out.println("la soci" + société);
                varsOfAnActiveProcessPerUser.put("expéditeur", expéditeur);
                varsOfAnActiveProcessPerUser.put("date", date);
                varsOfAnActiveProcessPerUser.put("objet", objet);
                varsOfAnActiveProcessPerUser.put("société", société);

                listVarsOfActiveProcesPerUser.add(varsOfAnActiveProcessPerUser);

            }
        }
        return listVarsOfActiveProcesPerUser;
    }

    @Override

    public List<String> getListFinishedCourrierArrivéPerUser(String userId) {
        HistoryService historyService = this.processEngine.getHistoryService();
        List<String> listFinishedCourriersId = new ArrayList<>();
        List<HistoricProcessInstance> listFinishedCourriersArrivéInstances = historyService
                .createHistoricProcessInstanceQuery().processDefinitionKey("courriersArrivés").finished().list();

        for (int j = 0; j < listFinishedCourriersArrivéInstances.size(); j++) {
            listFinishedCourriersId.add(listFinishedCourriersArrivéInstances.get(j).getId());
        }

        System.out.println(historyService.createHistoricProcessInstanceQuery().processDefinitionKey("courriersArrivés")
                .finished().list().size());
        List<String> listFinishedCourriersInvolvedMrX = new ArrayList<>();
        for (int i = 0; i < historyService.createHistoricTaskInstanceQuery().taskCandidateUser(userId)
                .processDefinitionKey("courriersArrivés").finished().list().size(); i++) {
            listFinishedCourriersInvolvedMrX
                    .add(historyService.createHistoricTaskInstanceQuery().taskCandidateUser(userId)
                            .processDefinitionKey("courriersArrivés").finished().list().get(i).getProcessInstanceId());

        }

        return listFinishedCourriersInvolvedMrX;
    }

    @Override
    public List<Map<String, Object>> getListActiveCourrierArrivéParDirection(String directionName) {
        // TODO Auto-generated method stub

        // list of vars of active process per direction
        List<Map<String, Object>> listVarsOfActiveProcesPerDirection = new ArrayList<Map<String, Object>>();
        // get the list active tasks per user
        List<Task> listOfActiveTasksByDirection = this.taskService.createTaskQuery()
                .processDefinitionKey("courriersArrivés").taskCandidateGroup(directionName).list();

        if (listOfActiveTasksByDirection != null) {
            // this will hold the vars of one task of the list of active process
            // per direction
            Map<String, Object> varsOfAnActiveProcessPerUser;
            for (int i = 0; i < listOfActiveTasksByDirection.size(); i++) {
                varsOfAnActiveProcessPerUser = runtimeService
                        .getVariables(listOfActiveTasksByDirection.get(i).getProcessInstanceId());
                listVarsOfActiveProcesPerDirection.add(varsOfAnActiveProcessPerUser);
            }
        }
        return listVarsOfActiveProcesPerDirection;

    }

    @Override
    public void refuserCourrier(String idProcess) {
        // TODO Auto-generated method stub
        Map<String, Object> proprietésCourrier = runtimeService.getVariables((idProcess));
        proprietésCourrier.replace("isValidated", false);
        this.taskService = processEngine.getTaskService();
        this.taskService.complete(
                this.taskService.createTaskQuery().processInstanceId(idProcess).list().get(0).getId(),
                proprietésCourrier);
        this.taskService.addCandidateGroup(
                this.taskService.createTaskQuery().processInstanceId(idProcess).list().get(0).getId(),
                "ROLE_Secrétaire Générale");
    }

    public RuntimeService getRuntimeService() {
        return runtimeService;
    }

    public void setRuntimeService(RuntimeService runtimeService) {
        this.runtimeService = runtimeService;
    }

    public TaskService getTaskService() {
        return taskService;
    }

    public void setTaskService(TaskService taskService) {
        this.taskService = taskService;
    }

    @Override
    public File multipartToFile(MultipartFile multipart) {
        File convFile = new File(multipart.getOriginalFilename());
        try {
            multipart.transferTo(convFile);
        } catch (IllegalStateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return convFile;
    }

    @Override
    public Map<String, Object> getCourrierDetails(String idProcess) {
        Map<String, Object> courriersDetails = runtimeService.getVariables(idProcess);
        courriersDetails.put("idProcess", idProcess);

        return courriersDetails;
    }

}

0 个答案:

没有答案