use of com.axelor.db.Model in project axelor-open-suite by axelor.
the class MailServiceMessageImpl method send.
@Override
public void send(final MailMessage message) throws MailException {
final EmailAccount emailAccount = mailAccountService.getDefaultSender();
if (emailAccount == null) {
super.send(message);
return;
}
Preconditions.checkNotNull(message, "mail message can't be null");
final Model related = findEntity(message);
final MailSender sender = getMailSender(emailAccount);
final Set<String> recipients = recipients(message, related);
if (recipients.isEmpty()) {
return;
}
final MailMessageRepository messages = Beans.get(MailMessageRepository.class);
final MailBuilder builder = sender.compose().subject(getSubject(message, related));
for (String recipient : recipients) {
builder.to(recipient);
}
for (MetaAttachment attachment : messages.findAttachments(message)) {
final Path filePath = MetaFiles.getPath(attachment.getMetaFile());
final File file = filePath.toFile();
builder.attach(file.getName(), file.toString());
}
final MimeMessage email;
try {
builder.html(template(message, related));
email = builder.build(message.getMessageId());
final Set<String> references = new LinkedHashSet<>();
if (message.getParent() != null) {
references.add(message.getParent().getMessageId());
}
if (message.getRoot() != null) {
references.add(message.getRoot().getMessageId());
}
if (!references.isEmpty()) {
email.setHeader("References", Joiner.on(" ").skipNulls().join(references));
}
} catch (MessagingException | IOException e) {
throw new MailException(e);
}
// send email using a separate process to void thread blocking
executor.submit(new Callable<Boolean>() {
@Override
public Boolean call() throws Exception {
send(sender, email);
return true;
}
});
}
use of com.axelor.db.Model in project axelor-open-suite by axelor.
the class StringTool method getIdListString.
/**
* Retrieve an ID list string from a collection of model objects. If the collection is empty, this
* method will return "0".
*
* @param collection
* @return
*/
public static String getIdListString(Collection<? extends Model> collection) {
List<Long> idList = new ArrayList<>();
String idString;
if (CollectionUtils.isEmpty(collection)) {
idString = "0";
} else {
for (Model item : collection) {
if (item != null) {
idList.add(item.getId());
}
}
idString = idList.stream().map(l -> l.toString()).collect(Collectors.joining(","));
}
return idString;
}
use of com.axelor.db.Model in project axelor-open-suite by axelor.
the class FullContextHelper method save.
@Transactional
public static FullContext save(Object object) {
Model model = null;
if (object instanceof Model) {
model = (Model) model;
} else if (object instanceof FullContext) {
model = (Model) ((FullContext) object).getTarget();
}
if (model != null) {
boolean active = JPA.em().getTransaction().isActive();
if (!active) {
JPA.em().getTransaction().begin();
}
FullContext wkfContext = new FullContext(JpaRepository.of(EntityHelper.getEntityClass(model)).save(model));
if (!active) {
JPA.em().getTransaction().commit();
}
return wkfContext;
}
return null;
}
use of com.axelor.db.Model in project axelor-open-suite by axelor.
the class FullContextHelper method filter.
public static List<FullContext> filter(String modelName, String queryStr, Object... params) {
List<FullContext> wkfEntities = new ArrayList<FullContext>();
if (params == null) {
params = new Object[] { null };
}
Query<? extends Model> query = createQuery(modelName, queryStr, null, params);
for (Model model : query.fetch()) {
wkfEntities.add(new FullContext(model));
}
return wkfEntities;
}
use of com.axelor.db.Model in project axelor-open-suite by axelor.
the class FullContextHelper method filter.
public static List<FullContext> filter(String modelName, String queryStr, Map<String, Object> paramMap) {
List<FullContext> wkfEntities = new ArrayList<FullContext>();
Query<? extends Model> query = createQuery(modelName, queryStr, paramMap, null);
for (Model model : query.fetch()) {
wkfEntities.add(new FullContext(model));
}
return wkfEntities;
}
Aggregations