📄 accountdaobean.java
字号:
/*
* Copyright 2006 Borys Burnayev
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.rdacorp.petstore.business.dao.impl;
import java.util.Collection;
import javax.ejb.PostConstruct;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.EntityNotFoundException;
import javax.persistence.NoResultException;
import javax.persistence.NonUniqueResultException;
import javax.persistence.PersistenceContext;
import com.rdacorp.petstore.business.dao.AccountDao;
import com.rdacorp.petstore.business.dao.LoginException;
import com.rdacorp.petstore.business.dao.PasswordException;
import com.rdacorp.petstore.business.util.MessageSource;
import com.rdacorp.petstore.business.util.impl.MessageSourceImpl;
import com.rdacorp.petstore.domain.Account;
import com.rdacorp.petstore.domain.Language;
import com.rdacorp.petstore.domain.State;
import com.rdacorp.petstore.domain.UserProfile;
/**
* @author Borys Burnayev
*/
@Stateless(name = "AccountDao")
public class AccountDaoBean implements AccountDao {
@PersistenceContext(unitName = "manager1")
protected EntityManager em;
MessageSource messageSource;
@PostConstruct
public void init() {
messageSource = new MessageSourceImpl("exceptions");
}
public UserProfile login(String login, String password) throws LoginException, PasswordException {
UserProfile profile = null;
try {
profile = (UserProfile) em.createQuery("from UserProfile up where up.login = :login").setParameter("login",
login).getSingleResult();
}
catch (NoResultException e) {
throw new LoginException(messageSource.getMessage("EXC-000", login));
}
assert profile != null;
if (!profile.getPassword().equals(password))
throw new PasswordException(messageSource.getMessage("EXC-001"));
return profile;
}
public Account createAccount(Account account) {
em.persist(account);
return account;
}
public Account updateAccount(Account account) {
return em.merge(account);
}
public Collection<State> getStates() {
return em.createQuery("from State").getResultList();
}
public Collection<Language> getLanguages() {
return em.createQuery("from Language").getResultList();
}
public Account findAccount(String login) {
UserProfile profile = null;
try {
profile = (UserProfile) em.createQuery("from UserProfile up where up.login = :login").setParameter("login",
login).getSingleResult();
}
catch (NoResultException e) {
// profile of null is OK;
}
catch (NonUniqueResultException e) {
// profile of null is OK;
}
return profile != null ? profile.getPerson().getAccount() : null;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -