@SuppressWarnings("unchecked") @Override public void deleteAll(Object object, Collection ids) throws Exception { List<?> list = super.findByIds(object.getClass(), ids); if (null != list) { for (Iterator<?> iterator = list.iterator(); iterator.hasNext(); ) { SysCompanyUser ele = (SysCompanyUser) iterator.next(); ele.setDeleteFlag(Constants.STATUS_Y); // ele.setStatus(Constants.STATUS_D); ele.setSuperiorId(null); // setChildSuperiorIdToNull(ele.getId()); deleteSysCompanyUserRel(ele.getId()); } super.saveOrUpdateAll(list); } }
private void setChildSuperiorIdToNull(Integer parentUserId) throws Exception { List<?> rels = findSysCompanyUserRel(new SysCompanyUserRel(new SysCompanyUserRelId(parentUserId, null))); // set child superior id to null if (null != rels && rels.size() > 0) { List<Integer> childIds = new ArrayList<Integer>(); for (Iterator<?> iterator = rels.iterator(); iterator.hasNext(); ) { SysCompanyUserRel ele = (SysCompanyUserRel) iterator.next(); childIds.add(ele.getId().getChildUserId()); } List<?> childList = super.findByIds(SysCompanyUser.class, childIds); for (Iterator<?> iterator = childList.iterator(); iterator.hasNext(); ) { SysCompanyUser ele = (SysCompanyUser) iterator.next(); ele.setSuperiorId(null); } super.saveOrUpdateAll(childList); } }
private void getUserSuperiorIds(Set<Integer> set, Integer id) throws Exception { SysCompanyUser _obj = (SysCompanyUser) getObject(SysCompanyUser.class, id); if (null != _obj && null != _obj.getSuperiorId() && _obj.getSuperiorId() > 0 && _obj.getId() != _obj.getSuperiorId()) { set.add(_obj.getSuperiorId()); getUserSuperiorIds(set, _obj.getSuperiorId()); } }
@Override public void saveOrUpdate(Object object) throws Exception { SysCompanyUser sysCompUser = (SysCompanyUser) object; if (null != sysCompUser.getId() && sysCompUser.getId() > 0) { // for update setChildSuperiorIdToNull(sysCompUser.getId()); deleteSysCompanyUserRel(sysCompUser.getId()); } // for add or update super.saveOrUpdate(sysCompUser); Set<Integer> set = new HashSet<Integer>(); getUserSuperiorIds(set, sysCompUser.getId()); // if (set.size() > 0) { Set<SysCompanyUserRel> rels = new HashSet<SysCompanyUserRel>(); for (Iterator<?> iterator = set.iterator(); iterator.hasNext(); ) { Integer ele = (Integer) iterator.next(); rels.add(new SysCompanyUserRel(new SysCompanyUserRelId(ele, sysCompUser.getId()))); } super.saveOrUpdateAll(rels); } }