Search in sources :

Example 71 with Model

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;
        }
    });
}
Also used : Path(java.nio.file.Path) LinkedHashSet(java.util.LinkedHashSet) EmailAccount(com.axelor.apps.message.db.EmailAccount) MessagingException(javax.mail.MessagingException) MailSender(com.axelor.mail.MailSender) IOException(java.io.IOException) MailBuilder(com.axelor.mail.MailBuilder) MessagingException(javax.mail.MessagingException) AddressException(javax.mail.internet.AddressException) IOException(java.io.IOException) MailException(com.axelor.mail.MailException) MailMessageRepository(com.axelor.mail.db.repo.MailMessageRepository) MimeMessage(javax.mail.internet.MimeMessage) Model(com.axelor.db.Model) MailException(com.axelor.mail.MailException) File(java.io.File) MetaAttachment(com.axelor.meta.db.MetaAttachment)

Example 72 with Model

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;
}
Also used : ArrayList(java.util.ArrayList) Model(com.axelor.db.Model)

Example 73 with Model

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;
}
Also used : Model(com.axelor.db.Model) Transactional(com.google.inject.persist.Transactional)

Example 74 with Model

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;
}
Also used : ArrayList(java.util.ArrayList) Model(com.axelor.db.Model)

Example 75 with Model

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;
}
Also used : ArrayList(java.util.ArrayList) Model(com.axelor.db.Model)

Aggregations

Model (com.axelor.db.Model)77 MetaModel (com.axelor.meta.db.MetaModel)22 AxelorException (com.axelor.exception.AxelorException)19 ArrayList (java.util.ArrayList)18 HashMap (java.util.HashMap)16 Context (com.axelor.rpc.Context)15 Transactional (com.google.inject.persist.Transactional)15 List (java.util.List)14 Mapper (com.axelor.db.mapper.Mapper)13 IOException (java.io.IOException)13 File (java.io.File)12 Map (java.util.Map)12 Property (com.axelor.db.mapper.Property)11 MetaJsonRecord (com.axelor.meta.db.MetaJsonRecord)9 JsonContext (com.axelor.rpc.JsonContext)8 MetaModelRepository (com.axelor.meta.db.repo.MetaModelRepository)7 Strings (com.google.common.base.Strings)7 HashSet (java.util.HashSet)7 FullContext (com.axelor.apps.tool.context.FullContext)6 Beans (com.axelor.inject.Beans)6