MentaTutorials

Rev

Rev 16 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
16 soliveira 1
package org.menta.service.impl;
2
 
3
import org.menta.dao.UserDAO;
4
import org.menta.exception.LoginException;
5
import org.menta.exception.LoginException.Type;
6
import org.menta.model.User;
7
import org.menta.service.UserService;
8
import org.mentawai.mail.Letter;
9
import org.mentawai.mail.SimpleEmail;
10
import org.mentawai.mail.TextLetter;
11
 
12
/**
13
 *
14
 * Service that do some business logic before access DAO layer.
19 soliveira 15
 *
16 soliveira 16
 */
17
public class UserServiceImpl implements UserService {
18
 
19 soliveira 19
        private final UserDAO userDAO;
20
 
21
        public UserServiceImpl(final UserDAO userDAO) {
16 soliveira 22
                this.userDAO = userDAO;
23
        }
19 soliveira 24
 
16 soliveira 25
        @Override
19 soliveira 26
        public User findByUsername(final String username) {
27
                if (username == null) {
28
                        throw new IllegalArgumentException("Username cannot be null");
29
                }
30
 
16 soliveira 31
                return userDAO.findByUsername(username);
32
        }
33
 
19 soliveira 34
        @Override
35
        public User load(final int id) {
36
                if (id < 1) {
37
                        return null;
38
                }
39
 
16 soliveira 40
                return userDAO.load(id);
41
        }
42
 
43
        @Override
19 soliveira 44
        public void save(final User u) {
16 soliveira 45
 
19 soliveira 46
                if (u.getUsername() == null || u.getUsername().isEmpty()) {
16 soliveira 47
                        throw new IllegalStateException("Username cannot be null");
19 soliveira 48
                }
49
 
50
                if (u.getId() > 0) {
16 soliveira 51
                        userDAO.update(u);
19 soliveira 52
 
53
                }
54
                else {
16 soliveira 55
                        userDAO.insert(u);
19 soliveira 56
 
16 soliveira 57
                        // Send email to any new user with password
58
                        sendWelcomeMail(u);
59
                }
19 soliveira 60
 
16 soliveira 61
        }
62
 
19 soliveira 63
        private void sendWelcomeMail(final User u) {
64
 
65
                final Letter welcome = new TextLetter("welcome.txt");
16 soliveira 66
                welcome.setAttribute("username", u.getUsername());
67
                welcome.setAttribute("password", u.getPassword());
19 soliveira 68
 
16 soliveira 69
                try {
19 soliveira 70
 
71
                        final String subject = welcome.getSubject(u.getLocale());
72
                        final String body = welcome.getText(u.getLocale());
73
 
16 soliveira 74
                        SimpleEmail.sendLater(u.getUsername(), u.getEmail(), subject, body);
19 soliveira 75
 
76
                }
77
                catch (final Exception e) {
78
 
16 soliveira 79
                        System.err.println("Error sending email to: " + u.getEmail());
19 soliveira 80
 
16 soliveira 81
                        e.printStackTrace();
82
                }
19 soliveira 83
 
16 soliveira 84
        }
85
 
86
        @Override
19 soliveira 87
        public User login(final User user) throws LoginException {
88
 
89
                final User userFinded = userDAO.findByUsername(user.getUsername());
90
 
91
                if (userFinded == null) {
16 soliveira 92
                        throw new LoginException(Type.USERNAME_NOTFOUND);
93
                }
19 soliveira 94
 
16 soliveira 95
                if (!userFinded.getPassword().equals(user.getPassword())) {
96
                        throw new LoginException(Type.WRONG_PASSWORD);
97
                }
19 soliveira 98
 
16 soliveira 99
                return userFinded;
100
        }
101
}