Java tutorial
/* * Copyright 2016 OPEN TONE Inc. * * 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 jp.co.opentone.bsol.linkbinder.dao.impl; import jp.co.opentone.bsol.framework.core.SuppressTrace; import jp.co.opentone.bsol.framework.core.auth.AuthUser; import jp.co.opentone.bsol.framework.core.auth.AuthenticateException; import jp.co.opentone.bsol.framework.core.auth.AuthenticationParameter; import jp.co.opentone.bsol.framework.core.auth.ExpiredPasswordException; import jp.co.opentone.bsol.framework.core.dao.KeyDuplicateException; import jp.co.opentone.bsol.framework.core.dao.RecordNotFoundException; import jp.co.opentone.bsol.framework.core.dao.StaleRecordException; import jp.co.opentone.bsol.linkbinder.dao.AbstractLegacyDao; import jp.co.opentone.bsol.linkbinder.dao.UserDao; import jp.co.opentone.bsol.linkbinder.dto.ProjectUser; import jp.co.opentone.bsol.linkbinder.dto.SysUsers; import jp.co.opentone.bsol.linkbinder.dto.User; import jp.co.opentone.bsol.linkbinder.dto.condition.SearchUserCondition; import jp.co.opentone.bsol.linkbinder.util.ValueFormatter; import org.apache.commons.lang.StringUtils; import org.springframework.dao.DataIntegrityViolationException; import org.springframework.stereotype.Repository; import java.sql.SQLException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; /** * ??Dao. * * @author opentone * */ @Repository public class UserDaoImpl extends AbstractLegacyDao<User> implements UserDao { /** * SerialVersionUID. */ private static final long serialVersionUID = 368358941071595311L; /** * ????sqlMap.xml?namespace. */ private static final String NAMESPACE = "user"; /** * SQLID: ?????. */ private static final String SQL_FIND_BY_EMP_NO = "findByEmpNo"; /** * SQLID: ??????. */ private static final String SQL_FIND_PROJECT_USER = "findProjectUser"; /** * SQLID: ????. */ private static final String SQL_FIND_EMP_NO = "findEmpNo"; /** * SQLID: ??. */ private static final String SQL_AUTHENTICATE = "authenticate"; /** * SQLID: ????. */ private static final String SQL_COUNT = "count"; /** * SQLID: ????.(?). */ private static final String SQL_COUNT_CHECK = "countCheck"; /** * SQLID: ?????????. */ private static final String SQL_FIND_SEND_APPLY_USER = "findSendApplyUser"; /** * SQLID: ID????????. */ private static final String SQL_FIND_SYS_USER = "findBySysUserId"; /** * SQLID: ?. */ private static final String SQL_CREATE_USER = "createUsers"; /** * SQLID: ?. */ private static final String SQL_CREATE_PJ_USER = "createPjUsers"; /** * SQLID: ??. */ private static final String SQL_CREATE_SYSUSER = "createSysUser"; /** * SQLID: ?. */ private static final String SQL_UPDATE_USER = "updateUsers"; private static final String SQL_UPDATE_PJ_USER = "updatePjUsers"; /** * SQLID: ?. */ private static final String SQL_DELETE_USER = "deleteUsers"; private static final String SQL_DELETE_PJ_USER = "deletePjUsers"; /** * SQLID: ???(SYS_PJ)??. */ private static final String SQL_FIND_ALL = "findBySysUserAll"; /** * SQLID: ??????. */ private static final String SQLUSER_SETTING = "updateUsersSetting"; /** * SQLID: ???????(??). */ private static final String SQL_FIND_SYSUSER_COUNT = "findSysUserCount"; /** * SQLID: ??????(??). */ private static final String SQL_COUNT_EMAIL = "countEmail"; /** * ???????<code>pkg_auth.authenticate/code>??Oracle. * <p> * ORA-01403 No data found. * </p> */ private static final int ERROR_AUTHENTICATION_FAILED = 1403; /** * ???????????. * <p> * {@link #authenticate(String, String)}??. * </p> */ private static final String FLG_EXPIRED_PASSWORD = "2"; /** * ????. */ private static final List<String> FIELDS; static { FIELDS = new ArrayList<String>(); FIELDS.add("empNo"); FIELDS.add("nameE"); } /** * ???. */ public UserDaoImpl() { super(NAMESPACE); } /* * (non-Javadoc) * @see jp.co.opentone.bsol.linkbinder.dao.UserDao#findByEmpNo(java.lang.String) */ @Override public User findByEmpNo(String empNo) throws RecordNotFoundException { User record = (User) getSqlMapClientTemplate().queryForObject(getSqlId(SQL_FIND_BY_EMP_NO), empNo); if (record == null) { throw new RecordNotFoundException(empNo); } return record; } /* * (non-Javadoc) * @seejp.co.opentone.bsol.linkbinder.dao.UserDao#findProjectUser(jp.co.opentone.bsol.linkbinder.dto. * SearchUserCondition) */ @Override @SuppressWarnings("unchecked") public List<ProjectUser> findProjectUser(SearchUserCondition condition) { // ?? SearchUserCondition likeCondition = getLikeSearchCondition(condition, FIELDS); int skipResults = condition.getSkipNum(); int maxResults = condition.getPageRowNum(); return getSqlMapClientTemplate().queryForList(getSqlId(SQL_FIND_PROJECT_USER), likeCondition, skipResults, maxResults); } /* * (non-Javadoc) * @seejp.co.opentone.bsol.linkbinder.dao.UserDao#findEmpNo() */ @Override @SuppressWarnings("unchecked") public List<String> findEmpNo() { return getSqlMapClientTemplate().queryForList(getSqlId(SQL_FIND_EMP_NO)); } /* (non-Javadoc) * @see * jp.co.opentone.bsol.framework.auth.Authenticator#authenticate() */ @Override public AuthUser authenticate(String userId, @SuppressTrace // ????? String password) throws AuthenticateException, ExpiredPasswordException { AuthenticationParameter params = new AuthenticationParameter(); params.setUserId(userId); params.setPassword(null); // ? try { AuthenticationParameter idRecord = (AuthenticationParameter) getSqlMapClientTemplate() .queryForObject(getSqlId(SQL_AUTHENTICATE), params); if (idRecord == null) { throw new AuthenticateException(); } else { password = ValueFormatter.formatValueToHash(password, idRecord.getCreatedIdAt()); params.setPassword(password); } //??? AuthenticationParameter recorde = (AuthenticationParameter) getSqlMapClientTemplate() .queryForObject(getSqlId(SQL_AUTHENTICATE), params); if (recorde == null) { throw new AuthenticateException(); } } catch (DataIntegrityViolationException e) { handleExceptionAtAuthentication(e); } // TODO : ?? //checkExpiredPassword(params); // ?????. // ????? AuthUser u = new AuthUser(); u.setUserId(userId); return u; } /* (non-Javadoc) * @see jp.co.opentone.bsol.linkbinder.dao.UserDao#count(jp.co.opentone.bsol.linkbinder.dto.condition.SearchUserCondition) */ @Override public int count(SearchUserCondition condition) { // ?? SearchUserCondition likeCondition = getLikeSearchCondition(condition, FIELDS); return Integer .parseInt(getSqlMapClientTemplate().queryForObject(getSqlId(SQL_COUNT), likeCondition).toString()); } /* (non-Javadoc) * @see jp.co.opentone.bsol.linkbinder.dao.UserDao#countCheck( * jp.co.opentone.bsol.linkbinder.dto.condition.SearchUserCondition) */ @Override public int countCheck(SearchUserCondition condition) { return Integer.parseInt( getSqlMapClientTemplate().queryForObject(getSqlId(SQL_COUNT_CHECK), condition).toString()); } /** * ???????. * <p> * ????{@link AuthenticateException}???? * ????????????throw?. * </p> * @param e ????? * @throws AuthenticateException ?? */ private void handleExceptionAtAuthentication(DataIntegrityViolationException e) throws AuthenticateException { Throwable cause = e.getRootCause(); if (cause instanceof SQLException && ((SQLException) cause).getErrorCode() == ERROR_AUTHENTICATION_FAILED) { throw new AuthenticateException(); } else { throw e; } } /* * (non-Javadoc) * @see * jp.co.opentone.bsol.linkbinder.dao.UserDao#findSendApplyUser(jp.co.opentone.bsol.linkbinder.dto.condition * .SearchUserCondition) */ @Override @SuppressWarnings("unchecked") public List<User> findSendApplyUser(SearchUserCondition condition) { return getSqlMapClientTemplate().queryForList(getSqlId(SQL_FIND_SEND_APPLY_USER), condition); } @Override public void deleateUser(SysUsers user) throws KeyDuplicateException, StaleRecordException, RecordNotFoundException { int result = 0; if (0 < (result = getSqlMapClientTemplate().delete(getSqlId(SQL_DELETE_USER), user))) { if (!StringUtils.isEmpty(user.getPjId())) { result = getSqlMapClientTemplate().delete(getSqlId(SQL_DELETE_PJ_USER), user); } } else { throw new StaleRecordException(); } if (result == 0) { throw new StaleRecordException(); } } @Override public void updateUser(SysUsers user) throws KeyDuplicateException, StaleRecordException, RecordNotFoundException { int result = getSqlMapClientTemplate().update(getSqlId(SQL_UPDATE_USER), user); if (result != 1) { throw new StaleRecordException(); } } @Override public void updatePjUser(SysUsers user) throws KeyDuplicateException, StaleRecordException, RecordNotFoundException { int result = getSqlMapClientTemplate().update(getSqlId(SQL_UPDATE_PJ_USER), user); if (result != 1) { throw new StaleRecordException(); } } @Override public void createUser(SysUsers user) throws KeyDuplicateException { getSqlMapClientTemplate().insert(getSqlId(SQL_CREATE_USER), user); } @Override public void createPjUser(SysUsers user) throws KeyDuplicateException { getSqlMapClientTemplate().insert(getSqlId(SQL_CREATE_PJ_USER), user); } @Override public void create(SysUsers user) throws KeyDuplicateException { getSqlMapClientTemplate().insert(getSqlId(SQL_CREATE_SYSUSER), user); } @Override public String findBySysUserId(SysUsers user) throws RecordNotFoundException { String record = (String) getSqlMapClientTemplate().queryForObject(getSqlId(SQL_FIND_SYS_USER), user); if (StringUtils.isEmpty(record)) { throw new RecordNotFoundException(); } return record; } @Override public void updateUserSetting(SysUsers user) throws RecordNotFoundException, KeyDuplicateException, StaleRecordException { getSqlMapClientTemplate().update(getSqlId(SQLUSER_SETTING), user); } @Override @SuppressWarnings("unchecked") public List<User> findAll() { return getSqlMapClientTemplate().queryForList(getSqlId(SQL_FIND_ALL)); } @Override public int countSystemAdminUser() throws RecordNotFoundException { return Integer .parseInt(getSqlMapClientTemplate().queryForObject(getSqlId(SQL_FIND_SYSUSER_COUNT)).toString()); } @Override public int countEmail(String empNo, String mailAddress) { Map<String, String> param = new HashMap<>(); param.put("empNo", empNo); param.put("mailAddress", mailAddress); return Integer .parseInt(getSqlMapClientTemplate().queryForObject(getSqlId(SQL_COUNT_EMAIL), param).toString()); } }