Search in sources :

Example 1 with Message

use of org.jbei.ice.storage.model.Message in project ice by JBEI.

the class MessageDAO method retrieveMessages.

public List<Message> retrieveMessages(Account account, List<Group> groups, int start, int count) {
    try {
        StringBuilder builder = new StringBuilder();
        builder.append("select id from message m where m.id in ").append("(select message_id from message_destination_accounts where account_id = ").append(account.getId()).append(")");
        if (groups != null && !groups.isEmpty()) {
            builder.append(" OR m.id in (select message_id from message_destination_groups where group_id in (");
            int size = groups.size();
            int i = 0;
            for (Group group : groups) {
                builder.append(group.getId());
                if (i < size - 1)
                    builder.append(", ");
                i += 1;
            }
            builder.append("))");
        }
        NativeQuery query = currentSession().createNativeQuery(builder.toString());
        query.setFirstResult(start);
        query.setMaxResults(count);
        List list = query.list();
        Set<Long> set = new HashSet<>();
        for (Object object : list) {
            Number number = (Number) object;
            set.add(number.longValue());
        }
        if (set.isEmpty())
            return new ArrayList<>();
        CriteriaQuery<Message> criteriaQuery = getBuilder().createQuery(Message.class);
        Root<Message> from = criteriaQuery.from(Message.class);
        criteriaQuery.where(from.get("id").in(set));
        criteriaQuery.orderBy(getBuilder().desc(from.get("dateSent")));
        return currentSession().createQuery(criteriaQuery).list();
    } catch (HibernateException he) {
        Logger.error(he);
        throw new DAOException(he);
    }
}
Also used : Group(org.jbei.ice.storage.model.Group) Message(org.jbei.ice.storage.model.Message) HibernateException(org.hibernate.HibernateException) DAOException(org.jbei.ice.storage.DAOException) NativeQuery(org.hibernate.query.NativeQuery) ArrayList(java.util.ArrayList) List(java.util.List) HashSet(java.util.HashSet)

Example 2 with Message

use of org.jbei.ice.storage.model.Message in project ice by JBEI.

the class Messages method replyTo.

public boolean replyTo(long mid, MessageInfo info) {
    // check if user has permission to access referenced message
    Message parent = dao.get(mid);
    // create new message and set as parent
    Message message = new Message();
    message.setDateSent(new Date());
    message.setFromEmail(this.userId);
    message.setMessage(info.getMessage());
    message.setTitle(info.getTitle());
    message.setDestinationAccounts(new HashSet<>(parent.getDestinationAccounts()));
    message.setParent(parent);
    return dao.create(message) != null;
}
Also used : Message(org.jbei.ice.storage.model.Message) Date(java.util.Date)

Example 3 with Message

use of org.jbei.ice.storage.model.Message in project ice by JBEI.

the class Messages method send.

/**
 * Sends message contained in the MessageInfo to the specified recipients. It some of the
 * recipients do not exist, the routine does its best to deliver as many as possible
 *
 * @param info details of message including recipient(s)
 * @return false if the message fails to be sent to all the intended recipients
 */
public boolean send(MessageInfo info) {
    if (info == null || info.getAccounts().isEmpty() && info.getUserGroups().isEmpty())
        return false;
    boolean success = true;
    Message message = new Message();
    message.setDateSent(new Date());
    message.setFromEmail(this.userId);
    message.setMessage(info.getMessage());
    message.setTitle(info.getTitle());
    if (info.getAccounts() != null) {
        for (AccountTransfer accountTransfer : info.getAccounts()) {
            Account account = accountDAO.getByEmail(accountTransfer.getEmail());
            if (account == null) {
                success = false;
                continue;
            }
            message.getDestinationAccounts().add(account);
        }
    }
    if (info.getUserGroups() != null) {
        for (UserGroup userGroup : info.getUserGroups()) {
            Group group = DAOFactory.getGroupDAO().get(userGroup.getId());
            if (group == null) {
                Logger.warn("Could not retrieve group with id " + userGroup.getId() + " to send message");
                success = false;
                continue;
            }
            message.getDestinationGroups().add(group);
        }
    }
    if (!success)
        return false;
    if (message.getDestinationAccounts().isEmpty() && message.getDestinationGroups().isEmpty())
        return false;
    return dao.create(message) != null;
}
Also used : Account(org.jbei.ice.storage.model.Account) Group(org.jbei.ice.storage.model.Group) UserGroup(org.jbei.ice.lib.dto.group.UserGroup) Message(org.jbei.ice.storage.model.Message) Date(java.util.Date) AccountTransfer(org.jbei.ice.lib.account.AccountTransfer) UserGroup(org.jbei.ice.lib.dto.group.UserGroup)

Example 4 with Message

use of org.jbei.ice.storage.model.Message in project ice by JBEI.

the class Messages method get.

public Results<MessageInfo> get(int start, int limit) {
    Account account = accountDAO.getByEmail(this.userId);
    List<Group> groups = DAOFactory.getGroupDAO().retrieveMemberGroups(account);
    List<Message> messages = new ArrayList<>(dao.retrieveMessages(account, groups, start, limit));
    Results<MessageInfo> results = new Results<>();
    for (Message message : messages) {
        Account from = accountDAO.getByEmail(message.getFromEmail());
        if (from == null)
            continue;
        MessageInfo info = new MessageInfo();
        info.setId(message.getId());
        // get from details
        AccountTransfer accountTransfer = new AccountTransfer();
        accountTransfer.setEmail(from.getEmail());
        accountTransfer.setFirstName(account.getFirstName());
        accountTransfer.setLastName(account.getLastName());
        info.setFrom(accountTransfer);
        info.setTitle(message.getTitle());
        info.setRead(message.isRead());
        info.setSent(message.getDateSent().getTime());
        results.getData().add(info);
    }
    int totalSize = dao.retrieveMessageCount(account, groups);
    results.setResultCount(totalSize);
    return results;
}
Also used : Account(org.jbei.ice.storage.model.Account) Group(org.jbei.ice.storage.model.Group) UserGroup(org.jbei.ice.lib.dto.group.UserGroup) Message(org.jbei.ice.storage.model.Message) Results(org.jbei.ice.lib.dto.common.Results) ArrayList(java.util.ArrayList) AccountTransfer(org.jbei.ice.lib.account.AccountTransfer) MessageInfo(org.jbei.ice.lib.dto.message.MessageInfo)

Example 5 with Message

use of org.jbei.ice.storage.model.Message in project ice by JBEI.

the class Messages method markMessageAsRead.

/**
 * Marks a message as read and returns the number of unread messages
 *
 * @param id identifier for message to be marked as read
 * @return number of unread messages after marking message as read
 */
public int markMessageAsRead(long id) {
    Message message = dao.retrieveMessage(id);
    message.setRead(true);
    message.setDateRead(new Date());
    dao.update(message);
    return 0;
}
Also used : Message(org.jbei.ice.storage.model.Message) Date(java.util.Date)

Aggregations

Message (org.jbei.ice.storage.model.Message)5 Date (java.util.Date)3 Group (org.jbei.ice.storage.model.Group)3 ArrayList (java.util.ArrayList)2 AccountTransfer (org.jbei.ice.lib.account.AccountTransfer)2 UserGroup (org.jbei.ice.lib.dto.group.UserGroup)2 Account (org.jbei.ice.storage.model.Account)2 HashSet (java.util.HashSet)1 List (java.util.List)1 HibernateException (org.hibernate.HibernateException)1 NativeQuery (org.hibernate.query.NativeQuery)1 Results (org.jbei.ice.lib.dto.common.Results)1 MessageInfo (org.jbei.ice.lib.dto.message.MessageInfo)1 DAOException (org.jbei.ice.storage.DAOException)1