@Override public void unLink(String specific) throws AfException { if (isNew()) { throw new AfException("this object is new, you can not unlink it"); } NodeService nodeService = ServiceHelper.getNodeService(afSession); NodeRef parent = getSpecifiedNode(specific); if (parent == null || !(nodeService.exists(parent))) { return; } List<ChildAssociationRef> parents = nodeService.getParentAssocs(nodeRef); for (ChildAssociationRef ref : parents) { if (ref.getParentRef().getId().equals(parent.getId())) { if (!ref.isPrimary()) { // not primary,delete nodeService.removeChildAssociation(ref); return; } else { // primary, it's not a good coding.f**k it! String rootId = AFCHelper.getNodeRefByPath(afSession, "/").getId(); if (parents.size() <= 1) { if (rootId.equals(ref.getParentRef().getId())) { return; } else { moveTo("/", null); nodeService.removeChildAssociation(ref); return; } } else { // set the 2nd as the primary.moveTo for (ChildAssociationRef anotherP : parents) { if (!anotherP.equals(ref)) { String scndPId = anotherP.getParentRef().getId(); nodeService.removeChildAssociation(anotherP); moveTo(scndPId, null); nodeService.removeChildAssociation(ref); return; } } } } } } }