Java tutorial
/** * <p>??: UserMgrManager.java</p> * <p>???Manager</p> * <p>?: Copyright (c) 2010 Beijing Cosmosource Co. Ltd.</p> * <p>?: Cosmosource Beijing Office</p> * <p>All right reserved.</p> * @ 2010-8-30 ?03:21:23 * @ WXJ * @ V1.0 * <p> </p> * */ package com.cosmosource.common.service; import java.sql.SQLException; import java.util.ArrayList; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; import org.apache.commons.codec.digest.DigestUtils; import org.apache.commons.lang.StringUtils; import org.dom4j.Document; import org.dom4j.DocumentHelper; import org.dom4j.Element; import org.springframework.orm.ibatis.SqlMapClientCallback; import com.cosmosource.app.common.service.CommonManager; import com.cosmosource.app.entity.NjhwTscard; import com.cosmosource.app.entity.NjhwUsersExp; import com.cosmosource.app.entity.NjhwUsersPlatenum; import com.cosmosource.app.entity.Users; import com.cosmosource.base.service.BaseManager; import com.cosmosource.base.service.Page; import com.cosmosource.base.util.Constants; import com.cosmosource.base.util.DateUtil; import com.cosmosource.base.util.PropertiesUtil; import com.cosmosource.base.util.StringUtil; import com.cosmosource.base.util.Struts2Util; import com.cosmosource.common.entity.TAcOrg; import com.cosmosource.common.entity.TAcUser; import com.ibatis.sqlmap.client.SqlMapExecutor; /** * @??: ? DAOCRUD?? * * @ WXJ */ public class UserMgrManager extends BaseManager { private CommonManager commonManager; public CommonManager getCommonManager() { return commonManager; } public void setCommonManager(CommonManager commonManager) { this.commonManager = commonManager; } /** * ? * * @param entity */ public void saveUser(TAcUser entity) { if (StringUtil.isBlank(entity.getPassword())) { entity.setPassword(DigestUtils.md5Hex(PropertiesUtil.getResourcesProperty("default.user.loginname"))); entity.setPlainpassword(PropertiesUtil.getResourcesProperty("default.user.loginname")); entity.setPwdModifyTime(new Date()); entity.setPwdWrongCount(0L); } dao.saveOrUpdate(entity); } /** * ? * * @param page * @param model * @return */ public Page<TAcUser> queryUsers(final Page<TAcUser> page, final TAcUser model) { List lPara = new ArrayList(); StringBuilder sbHql = new StringBuilder(); sbHql.append("select t from TAcUser t ").append("where t.orgid=? "); lPara.add(model.getOrgid()); if (StringUtil.isNotBlank(model.getUsercode())) { sbHql.append(" and t.usercode like ? "); lPara.add("%" + model.getUsercode() + "%"); } if (StringUtil.isNotBlank(model.getUsername())) { sbHql.append("and t.username like ? "); lPara.add("%" + model.getUsername() + "%"); } sbHql.append(" order by t.userid "); return dao.findPage(page, sbHql.toString(), lPara.toArray()); } public void deleteUsers(String[] ids) { dao.deleteByIds(TAcUser.class, ids); } /** * @??: ?xml? * @param nodeId * @param orgid * @return */ public String getOrgTreeData(String orgid, String ctx, String type) { if ("init".equals(type)) { Document doc = DocumentHelper.createDocument(); Element root = doc.addElement("tree"); root.addAttribute("id", "0"); TAcOrg org = (TAcOrg) dao.findById(TAcOrg.class, new Long(orgid)); Element el = root.addElement("item"); el.addAttribute("text", org.getOrgname()); el.addAttribute("id", org.getOrgid() + ""); // el.addAttribute("open", "1"); el.addAttribute("child", "1"); Element elx = el.addElement("userdata"); elx.addAttribute("name", "url"); elx.addText(ctx + "/common/userMgr/list.act?nodeId=" + org.getOrgid() + "&orgtype=" + org.getOrgtype()); // getOrgTreeDoc(new Long(orgid), el, ctx, org.getOrgtype()); return doc.asXML(); } else { Element root = DocumentHelper.createElement("tree"); root.addAttribute("id", orgid); List<TAcOrg> list = dao .findByHQL("select t from TAcOrg t where t.parentid=" + orgid + " order by orgcode "); if (list.size() <= 500) { for (TAcOrg org : list) { Element el = root.addElement("item"); el.addAttribute("text", org.getOrgname()); el.addAttribute("id", org.getOrgid() + ""); if ("1".equals(org.getIsbottom())) { el.addAttribute("child", "1"); } else { el.addAttribute("child", "0"); } // if(org.getParentid()==0){ // el.addAttribute("open", "1"); // } Element elx = el.addElement("userdata"); elx.addAttribute("name", "url"); if (!org.getOrgtype().equals("3")) { elx.addText(ctx + "/common/userMgr/list.act?nodeId=" + org.getOrgid() + "&orgtype=" + org.getOrgtype()); } else { elx.addText(ctx + "/common/userMgr/orgFrame.act?nodeId=" + org.getOrgid()); } } } return root.asXML(); } } /** * @??: DOC * @param orgId * @param root */ public void getOrgTreeDoc(Long orgId, Element root, String ctx, String orgtype) { List<TAcOrg> list = dao.findByHQL("select t from TAcOrg t where t.parentid=" + orgId); if (!orgtype.equals("3")) { for (TAcOrg org : list) { Element el = root.addElement("item"); el.addAttribute("text", org.getOrgname()); el.addAttribute("id", org.getOrgid() + ""); if (org.getParentid() == 0) { el.addAttribute("open", "1"); } Element elx = el.addElement("userdata"); elx.addAttribute("name", "url"); if (!org.getOrgtype().equals("3")) { elx.addText(ctx + "/common/userMgr/list.act?nodeId=" + org.getOrgid() + "&orgtype=" + org.getOrgtype()); } else { elx.addText(ctx + "/common/userMgr/orgFrame.act?nodeId=" + org.getOrgid()); } if ("1".equals(org.getIsbottom())) { getOrgTreeDoc(org.getOrgid(), el, ctx, org.getOrgtype()); } } } } /** * ???. * * @return loginName?oldLoginNametrue. */ public boolean isLoginNameUnique(String newLoginName, String oldLoginName) { return dao.isPropertyUnique(TAcUser.class, "loginname", newLoginName, oldLoginName); } /** * * * * @??: TODO(?) * @param user * add by sjy 2010-12-23 */ public void updateUser(TAcUser user) { if (user != null) { dao.saveOrUpdate(user); } } /** * * @??: ?id * @param loginname * @return add by sjy 2010-12-24 update by sjy 2011-1-7 * ??id,???? */ public TAcUser getOrgidByLoginname(String loginname) { TAcUser user = new TAcUser(); List userList = dao.findByProperty(TAcUser.class, "loginname", loginname); if (userList != null && userList.size() >= 1) { user = (TAcUser) userList.get(0); } return user; } /** * ????MD5?5??3?? * * @return */ public String CreatePass() { String strmd = DigestUtils.md5Hex(DateUtil.date2Str(new Date(), "yyyyMMdd")); strmd = strmd.substring(0, 5); int rand = new Double(Math.random() * 3220000).intValue(); String num = String.valueOf(rand).substring(0, 3); return strmd + num; } /** * ? * * @param page * @param model * @return */ public Page<TAcOrg> queryOrgs(final Page<TAcOrg> page, final TAcOrg model) { List lPara = new ArrayList(); StringBuilder sbHql = new StringBuilder(); sbHql.append("select t from TAcOrg t ").append("where t.parentid=? "); lPara.add(model.getParentid()); if (StringUtil.isNotBlank(model.getOrgcode())) { sbHql.append(" and t.orgcode like ? "); lPara.add("%" + model.getOrgcode() + "%"); } if (StringUtil.isNotBlank(model.getOrgname())) { sbHql.append("and t.orgname like ? "); lPara.add("%" + model.getOrgname() + "%"); } if (StringUtil.isNotBlank(model.getTaxno())) { sbHql.append("and t.taxno like ? "); lPara.add("%" + model.getTaxno() + "%"); } if (StringUtil.isNotBlank(model.getTaxname())) { sbHql.append("and t.taxname like ? "); lPara.add("%" + model.getTaxname() + "%"); } sbHql.append(" order by t.orgid "); return dao.findPage(page, sbHql.toString(), lPara.toArray()); } // /** // * ?User?roleList. // */ // public void initUser(TAcUser user) { // dao.initProxyObject(user.getRoleList()); // } public void findOut() { List list = dao.findByHQL("from TAcUser t "); System.out.println("?" + list.size()); } // /** * // * @??:?? // * @param user // * add by sjy 2010-12-23 // */ // public void saveLotusQa(TLotusVendorQa entity){ // if(entity!=null){ // List list = dao.findByProperty(TLotusVendorQa.class, "loginname", // entity.getLoginname()); // if(list!=null&&list.size()>0){ // // // }else{ // dao.saveOrUpdate(entity); // } // // // } // } /** * @??:? * @WXJ * @2012-6-5 * @param loginname * ?? * @param state * ? * @return */ public int updateUserState(final String loginname, final String status) { Integer result = new Integer(0); if (StringUtils.isNotEmpty(loginname)) { result = (Integer) sqlDao.getSqlMapClientTemplate().execute(new SqlMapClientCallback() { public Object doInSqlMapClient(SqlMapExecutor executor) throws SQLException { Map<String, Object> map = new HashMap<String, Object>(); map.put("loginname", loginname); map.put("status", status); return executor.update("CommonSQL.updateUserStatus", map); } }); } return result; } /** * @??:? * @WXJ * @2012-6-5 * @param loginname * ?? * @return */ public int updatePasswordWrongCountZero(final String loginname) { Integer result = new Integer(0); if (StringUtils.isNotEmpty(loginname)) { result = (Integer) sqlDao.getSqlMapClientTemplate().execute(new SqlMapClientCallback() { public Object doInSqlMapClient(SqlMapExecutor executor) throws SQLException { Map<String, Object> map = new HashMap<String, Object>(); map.put("loginname", loginname); return executor.update("CommonSQL.clearPasswordWrongCount", map); } }); } return result; } /** * @??:?? * @WXJ * @2012-6-8 * @param loginname * ?? * @return */ public int updateUserUnLock(final String loginname) { Integer result = new Integer(0); if (StringUtils.isNotEmpty(loginname)) { result = (Integer) sqlDao.getSqlMapClientTemplate().execute(new SqlMapClientCallback() { public Object doInSqlMapClient(SqlMapExecutor executor) throws SQLException { Map<String, Object> map = new HashMap<String, Object>(); map.put("loginname", loginname); return executor.update("CommonSQL.updateUserUnLock", map); } }); } return result; } /** * @??:? * @qiyanqiang * @2013-05-10 * @return */ @SuppressWarnings("unchecked") public HashMap searchPersonInfo(Map map) { List<HashMap> list = sqlDao.findList("CommonSQL.searchPersonIfo", map); if (list != null && list.size() > 0) return list.get(0); else return null; } /** * @??:?? * @qiyanqiang * @2013-05-10 * @return */ @SuppressWarnings("unchecked") public HashMap searchCardInfo(Map map) { List<HashMap> list = sqlDao.findList("CommonSQL.searchCardsIfo", map); if (list != null && list.size() > 0) return list.get(0); else return null; } /** * @??:?? * @qiyanqiang * @2013-05-10 * @return */ public void savePersonInfo(NjhwUsersExp object, String carNums, String isFastenCarNums) { dao.saveOrUpdate(object); // NjhwUsersPlatenum // ?, ? dao.batchExecute("delete from NjhwUsersPlatenum t where t.userid = ?", object.getUserid()); if (StringUtil.isNotEmpty(carNums)) { String[] carNumList = carNums.split(","); String[] isFastens = isFastenCarNums.split(","); for (int i = 0; i <= carNumList.length - 1; i++) { NjhwUsersPlatenum plateNum = new NjhwUsersPlatenum(); plateNum.setNupPn(carNumList[i].toString()); plateNum.setUserid(object.getUserid()); plateNum.setNupFlag(isFastens[i]); plateNum.setInsertDate(DateUtil.getSysDate()); plateNum.setInsertId( Long.parseLong(Struts2Util.getSession().getAttribute(Constants.USER_ID).toString())); dao.save(plateNum); } } dao.flush(); } /** * @??:?? * @hj * @2013-07-19 * @return */ public void updateUserExp(NjhwUsersExp object) { dao.saveOrUpdate(object); } /** * * @title: findNjhwTscardByCardId * @description: ???? * @author qiyanqiang * @param userId * @return * @date 2013-5-11 * @throws */ @SuppressWarnings("unchecked") public NjhwTscard findNjhwTscardByCardId(String cardId) { List<NjhwTscard> list = dao.findByProperty(NjhwTscard.class, "cardId", cardId); if (list != null && list.size() >= 1) { return list.get(0); } else { return null; } } /** * * @title: findUsersPlatenum * @description: ID ? * @author qiyanqiang * @param * @return * @date 2013-5-11 * @throws */ @SuppressWarnings("unchecked") public NjhwUsersPlatenum findUsersPlatenum(Long userid) { List<NjhwUsersPlatenum> list = dao.findByProperty(NjhwUsersPlatenum.class, "userid", userid); if (list != null && list.size() >= 1) { return list.get(0); } else { return null; } } /** * * @title: findUsersExpById * @description: ID * @author qiyanqiang * @param * @return * @date 2013-5-11 * @throws */ @SuppressWarnings("unchecked") public NjhwUsersExp findUsersExpById(Long userid) { List<NjhwUsersExp> list = dao.findByProperty(NjhwUsersExp.class, "userid", userid); if (list != null && list.size() >= 1) { return list.get(0); } else { return null; } } /** * @description: ????? * @author qiyanqiang * @param * @return * @date 2013-5-11 * @throws */ public void saveUserTel(Object object) { System.err.println(); dao.saveOrUpdate(object); } /** * @description:??1?? * @author qiyanqiang * @param * @return * @date 2013-5-11 * @throws */ @SuppressWarnings("null") public boolean findUserName(Long userId, String loginId) { boolean res = false; List<Users> users = dao.findByHQL(" select u from Users u where u.userid!=? and u.loginUid=?", userId, loginId); if (users.size() < 1) { res = true; } return res; } /** * @description:??? * @author qiyanqiang * @param * @return * @date 2013-5-11 * @throws */ public void saveLoginUser(Object object) { System.err.println(); dao.saveOrUpdate(object); } /** * @description: id * @author qiyanqiang * @param * @return * @date 2013-5-11 * @throws */ public Users getUsersById(Long userId) { try { return (Users) dao.findById(Users.class, userId); } catch (Exception e) { e.printStackTrace(); } return null; } /** * * @??: * @param map * @author qiyanqiang * @ 2013- 05 -17 */ @SuppressWarnings("unchecked") public List searchUserLicensePlate(Map map) { List<HashMap> list = sqlDao.findList("CommonSQL.searchUserLicensePlate", map); if (list != null && list.size() > 0) return list; else return null; } /** * * @??: ???? * @param map * @author qiyanqiang * @ 2013- 05 -17 */ @SuppressWarnings("unchecked") public List searchAllFacility(Map map) { List<HashMap> list = sqlDao.findList("CommonSQL.searchAllFacility", map); if (list != null && list.size() > 0) return list; else return null; } /** * @description: * @author qyq * @param HashMap map * @return */ public int modifyCardIsLosted(String optType, String cityCard) { int num = 0; List<NjhwTscard> list = super.dao.findByProperty(NjhwTscard.class, "cardId", cityCard); if (null != list && list.size() > 0) { NjhwTscard tsCard = null; for (int i = 0; i < list.size(); i++) { tsCard = list.get(i); // ???? if (tsCard == null) num = 1; else { // ?? if ("confirmLosted".equals(optType)) { tsCard.setSystemLosted(NjhwTscard.SYSTEM_LOSTED_LOSTED); // //??? // commonManager.delAuthority(cityCard); } else if ("cancelLosted".equals(optType)) { tsCard.setSystemLosted(NjhwTscard.SYSTEM_LOSTED_UNLOSTED); // //?? // commonManager.addAuthority(cityCard); } dao.update(tsCard); dao.flush(); } } } return num; } /** * @description:????? * @author hj * @param String cardId * @return boolean */ @SuppressWarnings({ "rawtypes", "unchecked" }) public boolean checkAdmin(String cardId) { Long userid = (Long) Struts2Util.getSession().getAttribute(Constants.USER_ID); String orgId = Struts2Util.getSession().getAttribute(Constants.ORG_ID).toString(); Map<String, Object> condtion = new HashMap<String, Object>(); condtion.put("orgId", orgId); condtion.put("cardId", cardId); List<Map> listMap = sqlDao.findList("PersonnelUnitSQL.isUnitAdmin", condtion); Long useridCompare = 0l; if (null == listMap || listMap.size() == 0) { useridCompare = (Long) listMap.get(0).get("USERID"); } return useridCompare.equals(userid); } }