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);
}
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;
}
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());
}
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);
}
}
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();
}
}
Aggregations