Package com.walters.sms

Source Code of com.walters.sms.UserService

package com.walters.sms;

import com.walters.common.args.CheckArg;
import com.walters.sms.domain.User;
import org.apache.log4j.Logger;
import org.hibernate.HibernateException;

public class UserService extends PersistenceService implements IUserService
{
    private static final Logger LOGGER = Logger.getLogger(UserService.class);
    private static final String USERNAME = "username";
    private static final String PASSWORD = "password";

    public User saveUser(User user) throws UsernameNotUniqueException
    {
        if (CheckArg.isNotNull(user))
        {
            final String username = user.getUsername();

            try
            {
                if (CheckArg.isNull(user.getId()) && hasUserWithUsername(username))
                {
                    throw new UsernameNotUniqueException(user.getUsername());
                }
                else
                {
                    user = save(user);
                }
            }
            catch (final DataAccessException ex)
            {
                throw new ApplicationException(String.format("Failed to save user: %s.", username), ex);
            }
        }

        return user;
    }

    public User findUserByUsernameAndPassword(final String username, final String password)
    {
        User user = null;

        if (CheckArg.isNotNull(username) && CheckArg.isNotNull(password))
        {
            try
            {
                user = super.findByCriteria(User.class, QueryParameter.with(USERNAME, username)
                        .and(PASSWORD, password).parameters());
            }
            catch (final HibernateException ex)
            {
                throw new ApplicationException(String.format("Failed to find user \"%s\" by username and password.",
                        username), ex);
            }
        }

        return user;
    }

    public void deleteAll()
    {
        try
        {
            deleteAll(User.class);
        }
        catch (final HibernateException ex)
        {
            throw new ApplicationException("Failed to delete all users.", ex);
        }
    }

    public boolean hasUserWithUsername(final String username)
    {
        int count = 0;

        if (CheckArg.isNotNull(username))
        {
            try
            {
                count = super.countByProperty(User.class, USERNAME, username);
            }
            catch (final HibernateException ex)
            {
                throw new ApplicationException("Failed to find user by username.", ex);
            }
        }

        return count > 0;
    }

    public int getUserCount()
    {
        int count;

        try
        {
            count = count(User.class);
        }
        catch (final HibernateException ex)
        {
            throw new ApplicationException("Failed to retrieve user count.", ex);
        }

        return count;
    }
}
TOP

Related Classes of com.walters.sms.UserService

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.