fr.mael.microrss.dao.impl.UserDaoImpl.java Source code

Java tutorial

Introduction

Here is the source code for fr.mael.microrss.dao.impl.UserDaoImpl.java

Source

/*
   Copyright  2013 Mael Le Guvel
   This work is free. You can redistribute it and/or modify it under the
   terms of the Do What The Fuck You Want To Public License, Version 2,
   as published by Sam Hocevar. See the COPYING file for more details.
*/
package fr.mael.microrss.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import fr.mael.microrss.dao.UserDao;
import fr.mael.microrss.domain.Article;
import fr.mael.microrss.domain.Category;
import fr.mael.microrss.domain.Feed;
import fr.mael.microrss.domain.User;
import fr.mael.microrss.domain.UserConfig;

@Repository
public class UserDaoImpl extends GenericDaoImpl<User> implements UserDao {

    @Override
    public User loadByUsername(String username) {
        StringBuffer queryString = new StringBuffer("from User ");
        queryString.append("where username = :name ");
        Query q = getSessionFactory().getCurrentSession().createQuery(queryString.toString());
        q.setString("name", username);
        return (User) q.uniqueResult();
    }

    @Override
    public boolean userHasArticle(User user, Article article) {
        StringBuffer query = new StringBuffer("select count(ua) from UserArticle ua ");
        query.append("where ua.user = :user and ua.article = :article ");
        Query q = getSession().createQuery(query.toString());
        q.setEntity("user", user);
        q.setEntity("article", article);
        return check(q.uniqueResult());
    }

    private boolean check(Object o) {
        return (o != null && o instanceof Long && (Long) o != 0);
    }

    @Override
    public boolean userHasFeed(User user, Feed feed) {
        StringBuffer query = new StringBuffer("select count(uf) from UserFeed uf ");
        query.append("where uf.user = :user and uf.feed = :feed ");
        Query q = getSession().createQuery(query.toString());
        q.setEntity("user", user);
        q.setEntity("feed", feed);
        return check(q.uniqueResult());
    }

    @Override
    public boolean userHasCategory(User user, Category category) {
        StringBuffer query = new StringBuffer("select count(cat) from Category cat ");
        query.append("where cat.user = :user and cat = :cat ");
        Query q = getSession().createQuery(query.toString());
        q.setEntity("user", user);
        q.setEntity("cat", category);
        return check(q.uniqueResult());
    }

    @Override
    public List<User> usersForFeed(Feed feed) {
        StringBuffer query = new StringBuffer("select user from UserFeed uf ");
        query.append("inner join uf.user user ");
        query.append("where uf.feed = :feed ");
        Query q = getSession().createQuery(query.toString());
        q.setEntity("feed", feed);
        return q.list();
    }

    @Override
    public boolean userHasUserArticle(User user, Integer userArticle) {
        StringBuffer query = new StringBuffer("select count(ua) from UserArticle ua ");
        query.append("where ua.user = :user and ua.id = :id ");
        Query q = getSession().createQuery(query.toString());
        q.setEntity("user", user);
        q.setInteger("id", userArticle);
        return check(q.uniqueResult());
    }

    @Override
    public boolean userHasLabel(User user, Integer userLabelId) {
        StringBuffer query = new StringBuffer("select count(ul) from UserLabel ul ");
        query.append("where ul.user = :user and ul.id = :id");
        Query q = getSession().createQuery(query.toString());
        q.setEntity("user", user);
        q.setInteger("id", userLabelId);
        return check(q.uniqueResult());
    }

    @Override
    public UserConfig getConfig(User user) {
        StringBuffer query = new StringBuffer("from UserConfig ");
        query.append("where user = :user");
        Query q = getSession().createQuery(query.toString());
        q.setEntity("user", user);
        return (UserConfig) q.uniqueResult();
    }

    @Override
    public void updateConfig(User user, String name, String value) {
        StringBuffer query = new StringBuffer("update UserConfig set " + name + " = :value ");
        query.append("where user = :user");
        Query q = getSession().createQuery(query.toString());
        q.setString("value", value);
        q.setEntity("user", user);
        q.executeUpdate();
    }
}