📄 accountserviceimp.java
字号:
/*
* Copyright 2003-2005 the original author or authors.
* 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.jdon.jivejdon.service.imp.account;
import org.apache.log4j.Logger;
import com.jdon.container.visitor.data.SessionContext;
import com.jdon.container.visitor.data.SessionContextAcceptable;
import com.jdon.controller.events.EventModel;
import com.jdon.controller.model.PageIterator;
import com.jdon.controller.service.Stateful;
import com.jdon.jivejdon.Constants;
import com.jdon.jivejdon.auth.ResourceAuthorization;
import com.jdon.jivejdon.dao.AccountDao;
import com.jdon.jivejdon.dao.AccountProfileDao;
import com.jdon.jivejdon.model.Account;
import com.jdon.jivejdon.model.AccountProfile;
import com.jdon.jivejdon.model.auth.Role;
import com.jdon.jivejdon.service.AccountService;
import com.jdon.jivejdon.service.util.SessionContextUtil;
import com.jdon.jivejdon.util.ToolsUtil;
import com.jdon.util.Debug;
/**
* @author <a href="mailto:banqiao@jdon.com">banq</a>
*
*/
public class AccountServiceImp implements AccountService, Stateful, SessionContextAcceptable {
private final static Logger logger = Logger.getLogger(AccountServiceImp.class);
private final static String module = AccountServiceImp.class.getName();
private AccountDao accountDao;
private AccountProfileDao accountProfileDao;
protected SessionContext sessionContext;
protected SessionContextUtil sessionContextUtil;
protected ResourceAuthorization resourceAuthorization;
public AccountServiceImp(AccountDao accountDao, AccountProfileDao accountProfileDao,
SessionContextUtil sessionContextUtil,
ResourceAuthorization resourceAuthorization) {
this.accountDao = accountDao;
this.accountProfileDao = accountProfileDao;
this.sessionContextUtil = sessionContextUtil;
this.resourceAuthorization = resourceAuthorization;
}
public Account getloginAccount(){
return sessionContextUtil.getLoginAccount(sessionContext);
}
public Account getAccountByName(String username){
Account account = getloginAccount();
if (resourceAuthorization.isAdmin(account))// if now is Admin
try {
account = accountDao.getAccountByName(username);
}catch(Exception ex){
ex.printStackTrace();
}
return account;
}
public Account getAccount(Long userId) {
return accountDao.getAccount(userId);
}
/* (non-Javadoc)
* @see com.jdon.framework.samples.jpetstore.service.AccountService#insertAccount(com.jdon.framework.samples.jpetstore.domain.Account)
*/
public void createAccount(EventModel em) {
Account account = (Account) em.getModelIF();
Debug.logVerbose("createAccount username=" + account.getUsername(), module);
try {
if (accountDao.getAccountByEmail(account.getEmail()) != null) {
em.setErrors(Constants.EMAIL_EXISTED);
return;
}
if (accountDao.getAccountByName(account.getUsername()) != null) {
em.setErrors(Constants.USERNAME_EXISTED);
return;
}
//setup the role is User
account.setRoleName(Role.USER);
account.setPassword(ToolsUtil.hash(account.getPassword()));
//创建时间使用long字符串
long dateTime = System.currentTimeMillis();
String savetime = ToolsUtil.getDateTimeDisp(Long.toString(dateTime));
account.setCreationDate(savetime);
AccountProfile accountProfile = new AccountProfile();
account.setAccountProfile(accountProfile);
accountProfile.setAccount(account);
accountDao.createAccount(account);
} catch (Exception e) {
Debug.logError(" createAccount error : " + e, module);
em.setErrors(Constants.ERRORS);
}
}
/* (non-Javadoc)
* @see com.jdon.framework.samples.jpetstore.service.AccountService#updateAccount(com.jdon.framework.samples.jpetstore.domain.Account)
*/
public void updateAccount(EventModel em) {
Debug.logVerbose("enter updateAccount", module);
Account accountInput = (Account) em.getModelIF();
try {
Account account = getloginAccount();
if (account == null) {
Debug.logError("this user not login", module);
return;
}
if (resourceAuthorization.isOwner(account, accountInput)) {
Account accountInSessin = accountDao.getAccount(account.getUserId());
//merge
accountInSessin.setUsername(accountInput.getUsername());
accountInSessin.setPassword(ToolsUtil.hash(accountInput.getPassword()));
accountInSessin.setEmail(accountInput.getEmail());
accountInSessin.setEmailVisible(accountInput.isEmailVisible());
accountDao.updateAccount(accountInSessin);
}
} catch (Exception daoe) {
Debug.logError(" updateAccount error : " + daoe, module);
em.setErrors(Constants.ERRORS);
}
}
public void deleteAccount(EventModel em) {
Account account = (Account) em.getModelIF();
account= getAccountByName(account.getUsername());
account.setUsername("anonymous");
long dateTime = System.currentTimeMillis();
account.setPassword(ToolsUtil.hash(Long.toString(dateTime)));
account.setRoleName("Anonymous");
try {
accountDao.updateAccount(account);
} catch (Exception e) {
em.setErrors(Constants.ERRORS);
}
}
/* (non-Javadoc)
* @see com.jdon.jivejdon.service.AccountService#getAccounts(int, int)
*/
public PageIterator getAccounts(int start, int count) {
PageIterator pageIterator = new PageIterator();
try {
pageIterator = accountDao.getAccounts(start, count);
}catch(Exception ex){
logger.error(ex);
}
return pageIterator;
}
/**
* @return Returns the sessionContext.
*/
public SessionContext getSessionContext() {
return sessionContext;
}
/**
* @param sessionContext The sessionContext to set.
*/
public void setSessionContext(SessionContext sessionContext) {
this.sessionContext = sessionContext;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -