package com.vci.server.framework.systemConfig.stafforgmanage.passwordStrategy; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import org.hibernate.HibernateException; import org.hibernate.type.Type; import com.vci.server.base.persistence.dao.BaseService; import com.vci.server.base.persistence.dao.HibernateCallback; import com.vci.server.base.persistence.dao.HibernateCallbackExt; import com.vci.server.base.persistence.dao.HibernateTemplate; import com.vci.server.base.persistence.dao.JDBCCallback; import com.vci.server.base.persistence.dao.JDBCRunType; import com.vci.server.base.persistence.dao.JDBCTemplate; public class PasswordStrategyService extends BaseService{ public boolean savePasswordStrategy(final PasswordStrategy entity ) { return (Boolean)new HibernateTemplate().runExt(new HibernateCallbackExt() { public Object execute(Connection conn) throws HibernateException, SQLException{ PasswordStrategyDaoImpl impl = new PasswordStrategyDaoImpl(); String sql = "from PasswordStrategy "; boolean isDefault = entity.getIsDefault(); List list = impl.findEntites(sql, new String[]{}); if(!isDefault) { boolean isDef = false; for(PasswordStrategy obj : list) { if(obj.getIsDefault()) { isDef = true; break; } } entity.setUserEntity(userEntity); if(isDef) { impl.saveOrUpdate(entity); }else { entity.setIsDefault(true); impl.saveOrUpdate(entity); } }else { for(PasswordStrategy obj : list) { if(obj.getIsDefault()) { String hsql = "update PasswordStrategy p set p.isDefault = 0 where p.id in ?"; impl.deleteQueryObject(hsql, new String[]{obj.getId()}, userEntity); } } impl.saveOrUpdate(entity); } saveCombinationPasswordstrategy(conn,entity); return true; } }); } public boolean updatePasswordStrategy(final PasswordStrategy entity) { return (Boolean)new HibernateTemplate().runExt(new HibernateCallbackExt() { public Object execute(Connection conn) throws HibernateException, SQLException{ PasswordStrategyDaoImpl impl = new PasswordStrategyDaoImpl(); String sql = "from PasswordStrategy "; PasswordStrategy info = impl.getById(entity.getId()); boolean isDefault = entity.getIsDefault(); List list = impl.findEntites(sql, new String[]{}); if(!isDefault) { boolean isDef = false; for(PasswordStrategy obj : list) { if(obj.getIsDefault() && !(obj.getId().equals(entity.getId()))) { isDef = true; break; } } if(isDef) { }else { info.setIsDefault(true); } }else { for(PasswordStrategy obj : list) { if(obj.getIsDefault() && !obj.getId().equals(entity.getId())) { String hsql = "update PasswordStrategy p set p.isDefault = 0 where p.id in ?"; impl.deleteQueryObject(hsql, new String[]{obj.getId()}, userEntity); } } info.setIsDefault(true); } info.setId(entity.getId()); info.setName(entity.getName()); info.setPasswordLen(entity.getPasswordLen()); info.setPasswordMaxLen(entity.getPasswordMaxLen()); info.setCharTypes(entity.getCharTypes()); info.setRequiredType(entity.getRequiredType()); info.setOverdueDay(entity.getOverdueDay()); info.setRemideDay(entity.getRemideDay()); info.setLockTime(entity.getLockTime()); info.setRetryTime(entity.getRetryTime()); info.setDesc(entity.getDesc()); info.setUserEntity(userEntity); impl.saveOrUpdate(info); //saveCombinationPasswordstrategy(conn,entity,combinationIds); return true; } }); } /** * 创建密码策略和密码组合方式的关系 *

Description:

* * @author wangxl * @time 2013-1-4 * @param conn * @param entity * @param combinationIds */ private void saveCombinationPasswordstrategy(Connection conn , PasswordStrategy entity){ String sql1 = "delete PLCOMBINATIONPASSWORDSTRATEGY ud where ud.PLPASSWORDSTRATEGYUID = ?"; PreparedStatement pstmt = null; PreparedStatement pstmt1 = null; try { pstmt = conn.prepareStatement(sql1); pstmt.setString(1, entity.getId()); pstmt.execute(); } catch (SQLException e1) { e1.printStackTrace(); } finally { if (pstmt != null){ try { pstmt.close(); } catch (SQLException e) { e.printStackTrace(); } } } // for (String combinationId : combinationIds){ // try { // String hql = "insert into PLCOMBINATIONPASSWORDSTRATEGY values (? , ?)"; // pstmt1 = conn.prepareStatement(hql); // pstmt1.setString(1, entity.getId()); // pstmt1.setString(2, combinationId); // pstmt1.execute(); // }catch (SQLException e1) { // e1.printStackTrace(); // } finally { // if (pstmt1 != null){ // try { // pstmt1.close(); // } catch (SQLException e) { // e.printStackTrace(); // } // } // } // } } public PasswordStrategy getPasswordStrBysecrtId(final String secretId) { return (PasswordStrategy)new HibernateTemplate().run(new HibernateCallback() { public Object execute() throws HibernateException { PasswordStrategyDaoImpl impl = new PasswordStrategyDaoImpl(); String hql = "select {u.*} from PPASSWORDSTRATEGY u where u.puid in(select o.PPWSTATEGYID from PSECRETSTATEGY o where o.PSECRETID=?) order by u.pname "; Object[] values = new Object[1]; values[0] = secretId; List list = impl.findEntites(hql, values, "u", PasswordStrategy.class); if(list.size() > 0) { return list.get(0); } return new PasswordStrategy(); } }); } public PasswordStrategy getPasswordObjByName(final String name) { return (PasswordStrategy)new HibernateTemplate().run(new HibernateCallback() { public Object execute() throws HibernateException { PasswordStrategyDaoImpl impl = new PasswordStrategyDaoImpl(); String hql = "select {u.*} from PLPASSWORDSTRATEGY u where u.pname=?"; Object[] values = new Object[1]; values[0] = name; List list = impl.findEntites(hql, values, "u", PasswordStrategy.class); if(list.size() > 0) { return list.get(0); }else { return null; } } }); } public PasswordStrategy getPasswordObjByUserId(final String userId) { return (PasswordStrategy)new HibernateTemplate().run(new HibernateCallback() { public Object execute() throws HibernateException { PasswordStrategyDaoImpl impl = new PasswordStrategyDaoImpl(); String hql = "select {u.*} from PLPASSWORDSTRATEGY u where u.PLUID in(select o.PLPASSWORDSTRATEGYUID from PLUSERPASSWORDSTRATEGY o where o.pluseruid = ?) order by u.PNAME "; Object[] values = new Object[1]; values[0] = userId; List list = impl.findEntites(hql, values, "u", PasswordStrategy.class); if(list.size() > 0) { return list.get(0); }else { String sql = "from PasswordStrategy u where u.isDefault =1"; return impl.findEntity(sql); } } }); } public List getPasswordObjById(final String id) { return (List)new HibernateTemplate().run(new HibernateCallback() { public Object execute() throws HibernateException { PasswordStrategyDaoImpl impl = new PasswordStrategyDaoImpl(); String hsql = " from PasswordStrategy u where u.id = '"+id+"'"; return impl.findEntities(hsql); } }); } @SuppressWarnings("unchecked") public List getPasswordStrategyList() { return (List)new HibernateTemplate().run(new HibernateCallback() { public Object execute() throws HibernateException { PasswordStrategyDaoImpl impl = new PasswordStrategyDaoImpl(); String sql = "from PasswordStrategy p order by p.name"; return impl.findEntities(sql); } }); } @SuppressWarnings("unchecked") public List getPasswordStrategyList(final int pageNo,final int pageSize) { return (List)new HibernateTemplate().run(new HibernateCallback() { public Object execute() throws HibernateException { PasswordStrategyDaoImpl impl = new PasswordStrategyDaoImpl(); int V_PAGE_ROWNUM_START = (pageNo - 1) * pageSize; int V_PAGE_ROWNUM_END = (pageNo ) * pageSize; StringBuffer sb = new StringBuffer(); sb.append(" SELECT * "); sb.append(" FROM (SELECT ROW_.*, ROWNUM RN "); sb.append(" FROM (SELECT * from PLPASSWORDSTRATEGY R "); sb.append(" ORDER BY R.PNAME )"); sb.append(" ROW_)"); sb.append(" WHERE RN <= "); sb.append( V_PAGE_ROWNUM_END); sb.append(" AND RN > "); sb.append(V_PAGE_ROWNUM_START); Object[] values = new Object[0]; List list = impl.findEntites(sb.toString(), values, "R", PasswordStrategy.class); return list; } }); } /***查询密码策略总数*/ public int getPasswordStrategyTotal() { int res = 0; String sql = "SELECT COUNT(*) FROM PLPASSWORDSTRATEGY "; res = (Integer)new JDBCTemplate().run(new JDBCCallback( new HibernateTemplate().getSessionConnection(), sql, JDBCRunType.SQL, 0, false, false,new Object[]{}) { @Override public Object execute(ResultSet rst) throws SQLException { int ress = 0; while(rst.next()){ ress = rst.getInt(1); } return ress; } }); return res; } public boolean deletePasswordStrategyById(final String id) { return (Boolean)new HibernateTemplate().run(new HibernateCallback() { public Object execute() throws HibernateException { PasswordStrategyDaoImpl impl = new PasswordStrategyDaoImpl(); PasswordStrategy entity = impl.getById(id); entity.setUserEntity(userEntity); impl.delete(entity); return true; } }); } public int checkPasswordStrategyIsquotedCount(final String id ){ return (Integer)new HibernateTemplate().run(new HibernateCallback() { public Object execute() throws HibernateException { int count = 0; PasswordStrategyDaoImpl impl = new PasswordStrategyDaoImpl(); String hsql = "select count(*) from PLUSERPASSWORDSTRATEGY ps where ps.PLPASSWORDSTRATEGYUID = '"+id+"'"; List list = new ArrayList(); Object[] values = new Object[0]; Map map = new HashMap(); list = impl.findEntitesBySQL(hsql, values,map); for (int i = 0 ; i < list.size(); i ++){ count += Integer.valueOf(String.valueOf(list.get(i))).intValue(); } return count; } }); } }