@Override public Organization moveOrgBefore(Organization org, String otherId) { Organization parent = org.getParent(); if (parent == null) throw new ServiceException("当前部门已经是根部门,无法移动位置!"); List<Organization> brothers = parent.getChildren(); Organization other = this.getOther(brothers, otherId); if (other == null) throw new ServiceException("当前部门不存在id=" + otherId + "的兄弟部门!"); if (other.equals(org)) return org; int oldIndex = brothers.indexOf(org); int newIndex = brothers.indexOf(other); if (oldIndex != -1) { if (newIndex < oldIndex) { brothers.remove(oldIndex); brothers.add(newIndex, org); } else { brothers.add(newIndex, org); brothers.remove(oldIndex); } } else { throw new ServiceException("当前部门不存在!"); } for (int i = 0; i < brothers.size(); i++) { Organization brother = brothers.get(i); brother.setPosition(i); dao.update(brother); } return org; }
private void traverseOrganizationTree(Organization organization) { if (null == organization) { return; } JpaHelper.initialize(organization.getPrincipal()); for (Organization child : organization.getChildren()) { traverseOrganizationTree(child); } }
@Override public Organization moveToLast(Organization org) { List<Organization> brothers = org.getChildren(); int p = brothers.indexOf(org); brothers.remove(p); brothers.add(org); for (int i = p; i < brothers.size(); i++) { Organization o = brothers.get(i); o.setPosition(i); dao.update(o, "position"); } return org; }
@Override public Organization addOrg(Organization org, String parentId) { Organization parent = parentId == null ? null : dao.get(parentId); if (parent != null) { parent.addChildren(org); org.setParent(parent); org.setPosition(parent.getChildren().size() - 1); } save(org); if (org.getShouldBeforeAt() != null && org.getShouldBeforeAt().equals("-1") == false) { this.moveOrgBefore(org, org.getShouldBeforeAt()); } return org; }