net.jforum.repository.PrivateMessageDao.java Source code

Java tutorial

Introduction

Here is the source code for net.jforum.repository.PrivateMessageDao.java

Source

/*
 * Copyright (c) JForum Team. All rights reserved.
 *
 * The software in this package is published under the terms of the LGPL
 * license a copy of which has been included with this distribution in the
 * license.txt file.
 *
 * The JForum Project
 * http://www.jforum.net
 */
package net.jforum.repository;

import net.jforum.entities.PrivateMessage;
import net.jforum.entities.PrivateMessageType;
import net.jforum.entities.User;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author Rafael Steil
 */
@Repository
public class PrivateMessageDao extends HibernateGenericDAO<PrivateMessage> implements Dao<PrivateMessage> {
    public PrivateMessageDao(Session session) {
        super(session);
    }

    @Override
    public void add(PrivateMessage entity) {
        PrivateMessage targetCopy = new PrivateMessage(entity);

        // First copy is to the sender's list
        entity.setType(PrivateMessageType.SENT);
        super.add(entity);

        // Second copy is the target
        targetCopy.setType(PrivateMessageType.NEW);
        super.add(targetCopy);
    }

    @SuppressWarnings("unchecked")
    public List<PrivateMessage> getFromInbox(User user) {
        return session.createCriteria(this.persistClass).add(Restrictions.eq("toUser", user))
                .add(Restrictions.disjunction().add(Restrictions.eq("type", PrivateMessageType.NEW))
                        .add(Restrictions.eq("type", PrivateMessageType.READ))
                        .add(Restrictions.eq("type", PrivateMessageType.UNREAD)))
                .setComment("privateMessageDAO.getFromInbox").list();
    }

    @SuppressWarnings("unchecked")
    public List<PrivateMessage> getFromSentBox(User user) {
        return session.createCriteria(this.persistClass).add(Restrictions.eq("fromUser", user))
                .add(Restrictions.eq("type", PrivateMessageType.SENT))
                .setComment("privateMessageDAO.getFromSentBox").list();
    }

    /**
     * This method will always throw UnsupportedOperationException
     *
     * @throws UnsupportedOperationException
     */
    @Override
    public void update(PrivateMessage entity) {
        throw new UnsupportedOperationException("Update is not supported for Private Messages");
    }
}