Search in sources :

Example 11 with FullContext

use of com.axelor.apps.tool.context.FullContext in project axelor-open-suite by axelor.

the class WkfEmailServiceImpl method sendEmail.

@Override
public void sendEmail(WkfTaskConfig wkfTaskConfig, DelegateExecution execution) throws ClassNotFoundException, MessagingException, AxelorException, InstantiationException, IllegalAccessException, IOException {
    String title = wkfTaskConfig.getTaskEmailTitle();
    if (title == null) {
        return;
    }
    FullContext wkfContext = wkfUserActionService.getModelCtx(wkfTaskConfig, execution);
    if (wkfContext == null) {
        return;
    }
    String model = null;
    String tag = null;
    Long id = null;
    title = wkfUserActionService.processTitle(title, wkfContext);
    model = wkfContext.getTarget().getClass().getName();
    if (wkfContext.getTarget().getClass().equals(MetaJsonRecord.class)) {
        tag = (String) wkfContext.get("jsonModel");
        model = tag;
    } else {
        tag = wkfContext.getTarget().getClass().getSimpleName();
    }
    id = (Long) wkfContext.get("id");
    String url = createUrl(wkfContext, wkfTaskConfig.getDefaultForm());
    String activeNode = execution.getCurrentActivityName();
    Template template = Beans.get(TemplateRepository.class).findByName(wkfTaskConfig.getTemplateName());
    Message message = null;
    if (template != null) {
        url = "<a href=\"" + url + "\" >" + url + "</a>";
        message = Beans.get(TemplateMessageService.class).generateMessage(id, model, tag, template);
        message.setSubject(message.getSubject().replace("{{activeNode}}", activeNode));
        message.setContent(message.getContent().replace("{{activeNode}}", activeNode));
        message.setSubject(message.getSubject().replace("{{recordUrl}}", url));
        message.setContent(message.getContent().replace("{{recordUrl}}", url));
    } else {
        User user = null;
        if (wkfTaskConfig.getUserPath() != null) {
            user = wkfUserActionService.getUser(wkfTaskConfig.getUserPath(), wkfContext);
        }
        if (user == null || user.getEmail() == null) {
            return;
        }
        String content = String.format(EMAIL_CONTENT, user.getName(), activeNode, url, url);
        List<EmailAddress> toEmailAddressList = new ArrayList<EmailAddress>();
        EmailAddress emailAddress = Beans.get(EmailAddressRepository.class).findByAddress(user.getEmail());
        if (emailAddress == null) {
            emailAddress = new EmailAddress(user.getEmail());
        }
        toEmailAddressList.add(emailAddress);
        message = messageService.createMessage(model, id, title, content, null, null, toEmailAddressList, null, null, null, null, MessageRepository.MEDIA_TYPE_EMAIL, null, null);
    }
    messageService.sendByEmail(message);
}
Also used : TemplateRepository(com.axelor.apps.message.db.repo.TemplateRepository) User(com.axelor.auth.db.User) Message(com.axelor.apps.message.db.Message) EmailAddressRepository(com.axelor.apps.message.db.repo.EmailAddressRepository) FullContext(com.axelor.apps.tool.context.FullContext) ArrayList(java.util.ArrayList) EmailAddress(com.axelor.apps.message.db.EmailAddress) Template(com.axelor.apps.message.db.Template)

Example 12 with FullContext

use of com.axelor.apps.tool.context.FullContext in project axelor-open-suite by axelor.

the class WkfCommonServiceImpl method findProcessConfig.

protected WkfProcessConfig findProcessConfig(Model model, boolean isActive, int status) {
    List<WkfProcessConfig> configs = wkfProcessConfigRepository.all().filter("(self.metaModel.fullName = ?1 OR self.metaJsonModel.name = ?1) " + "AND self.wkfProcess.wkfModel.statusSelect = ?2 " + "AND self.wkfProcess.wkfModel.isActive is ?3 " + "AND (self.isStartModel is true OR self.processPath is not null)", getModelName(model), status, isActive).order("pathCondition").fetch();
    Map<String, Object> ctxMap = new HashMap<String, Object>();
    ctxMap.put(getVarName(model), new FullContext(model));
    for (WkfProcessConfig config : configs) {
        boolean condition = true;
        if (config.getPathCondition() != null) {
            condition = (boolean) evalExpression(ctxMap, config.getPathCondition());
        }
        if (condition) {
            return config;
        }
    }
    return null;
}
Also used : WkfProcessConfig(com.axelor.apps.bpm.db.WkfProcessConfig) HashMap(java.util.HashMap) FullContext(com.axelor.apps.tool.context.FullContext)

Example 13 with FullContext

use of com.axelor.apps.tool.context.FullContext in project axelor-open-suite by axelor.

the class WkfInstanceServiceImpl method startInstance.

protected void startInstance(WkfProcessConfig wkfProcessConfig, Model model) throws AxelorException {
    WkfProcessConfig previousConfig = wkfService.findOldProcessConfig(model);
    if (previousConfig != null && previousConfig.getProcessPath() != null && addRelatedInstance(model, previousConfig)) {
        return;
    }
    log.debug("Start instance for the model: {}, id: {}", model.getClass().getName(), model.getId());
    WkfProcess wkfProcess = wkfProcessConfig.getWkfProcess();
    ProcessEngine engine = engineService.getEngine();
    RuntimeService runTimeService = engine.getRuntimeService();
    ProcessInstantiationBuilder builder = runTimeService.createProcessInstanceById(wkfProcess.getProcessId());
    Map<String, Object> modelMap = new HashMap<String, Object>();
    modelMap.put(wkfService.getVarName(model), new FullContext(model));
    builder.setVariables(wkfService.createVariables(modelMap));
    ProcessInstance processInstance = builder.executeWithVariablesInReturn();
    WkfInstance instance = wkfInstanceRepository.findByInstnaceId(model.getProcessInstanceId());
    if (instance != null) {
        instance.setModelId(model.getId());
        instance.setModelName(model.getClass().getName());
        instance.setProcessInstanceId(processInstance.getId());
    }
    model.setProcessInstanceId(processInstance.getId());
}
Also used : WkfProcess(com.axelor.apps.bpm.db.WkfProcess) WkfProcessConfig(com.axelor.apps.bpm.db.WkfProcessConfig) RuntimeService(org.camunda.bpm.engine.RuntimeService) HashMap(java.util.HashMap) FullContext(com.axelor.apps.tool.context.FullContext) WkfInstance(com.axelor.apps.bpm.db.WkfInstance) HistoricProcessInstance(org.camunda.bpm.engine.history.HistoricProcessInstance) ProcessInstance(org.camunda.bpm.engine.runtime.ProcessInstance) ProcessInstantiationBuilder(org.camunda.bpm.engine.runtime.ProcessInstantiationBuilder) ProcessEngine(org.camunda.bpm.engine.ProcessEngine)

Example 14 with FullContext

use of com.axelor.apps.tool.context.FullContext in project axelor-open-suite by axelor.

the class WkfInstanceServiceImpl method addRelatedInstance.

private boolean addRelatedInstance(Model model, WkfProcessConfig wkfProcessConfig) throws AxelorException {
    log.debug("Find related instance for the model: {}, id: {}, path: {}", model.getClass().getName(), model.getId(), wkfProcessConfig.getProcessPath());
    Object object = wkfService.findRelatedRecord(model, wkfProcessConfig.getProcessPath());
    if (object != null && object instanceof FullContext) {
        FullContext relatedModel = (FullContext) object;
        log.debug("Related instance found with processInstanceId: {}", relatedModel.get("processInstanceId"));
        model.setProcessInstanceId((String) relatedModel.get("processInstanceId"));
        return true;
    } else {
        WkfProcessConfig oldProcessConfig = wkfService.findOldProcessConfig(model);
        if (oldProcessConfig == null || oldProcessConfig.getProcessPath() == null || oldProcessConfig.equals(wkfProcessConfig)) {
            return false;
        }
        return addRelatedInstance(model, oldProcessConfig);
    }
}
Also used : WkfProcessConfig(com.axelor.apps.bpm.db.WkfProcessConfig) FullContext(com.axelor.apps.tool.context.FullContext)

Example 15 with FullContext

use of com.axelor.apps.tool.context.FullContext in project axelor-open-suite by axelor.

the class WkfUserActionServiceImpl method createUserAction.

@Override
@Transactional
public void createUserAction(WkfTaskConfig wkfTaskConfig, DelegateExecution execution) {
    String title = wkfTaskConfig.getTaskEmailTitle();
    if (title == null) {
        return;
    }
    try {
        FullContext wkfContext = getModelCtx(wkfTaskConfig, execution);
        if (wkfContext != null) {
            title = processTitle(title, wkfContext);
        }
        TeamTask teamTask = new TeamTask(title);
        teamTask.setStatus("new");
        if (wkfTaskConfig.getDeadlineFieldPath() != null) {
            teamTask.setTaskDate(getDeadLineDate(wkfTaskConfig.getDeadlineFieldPath(), wkfContext));
        }
        if (teamTask.getTaskDate() == null) {
            teamTask.setTaskDate(LocalDate.now());
        }
        String userPath = getUserPath(wkfTaskConfig, execution.getProcessDefinitionId());
        if (userPath != null) {
            teamTask.setAssignedTo(getUser(userPath, wkfContext));
        }
        String url = Beans.get(WkfEmailService.class).createUrl(wkfContext, wkfTaskConfig.getDefaultForm());
        teamTask.setDescription(String.format(DESCRIPTION, execution.getCurrentActivityName(), url, url));
        teamTaskRepository.save(teamTask);
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }
}
Also used : FullContext(com.axelor.apps.tool.context.FullContext) TeamTask(com.axelor.team.db.TeamTask) Transactional(com.google.inject.persist.Transactional)

Aggregations

FullContext (com.axelor.apps.tool.context.FullContext)18 HashMap (java.util.HashMap)8 Model (com.axelor.db.Model)7 WkfProcessConfig (com.axelor.apps.bpm.db.WkfProcessConfig)6 MetaJsonRecord (com.axelor.meta.db.MetaJsonRecord)6 Map (java.util.Map)4 GroovyScriptHelper (com.axelor.script.GroovyScriptHelper)3 WkfInstance (com.axelor.apps.bpm.db.WkfInstance)2 WkfProcess (com.axelor.apps.bpm.db.WkfProcess)2 Mapper (com.axelor.db.mapper.Mapper)2 Context (com.axelor.rpc.Context)2 Transactional (com.google.inject.persist.Transactional)2 File (java.io.File)2 FileWriter (java.io.FileWriter)2 IOException (java.io.IOException)2 Bindings (javax.script.Bindings)2 SimpleBindings (javax.script.SimpleBindings)2 ProcessEngine (org.camunda.bpm.engine.ProcessEngine)2 ObjectValue (org.camunda.bpm.engine.variable.value.ObjectValue)2 WkfContextHelper (com.axelor.apps.bpm.context.WkfContextHelper)1