public boolean moveEntry(User user,Entry entry,String direction,boolean isAdmin,Errors errors){
  if (direction == null) {
    Errors.add(errors,errorMessages.errorDirectionIsNull());
    return false;
  }
  if (entry == null) {
    Errors.add(errors,errorMessages.errorEntryIsNull());
    return false;
  }
  if (!canUserModifyEntry(user,entry,isAdmin)) {
    Errors.add(errors,errorMessages.errorUserIsNotEntitledToMoveTheEntry());
    return false;
  }
  final String parentId=entry.getParentId();
  String parentFirstChildId=null;
  String parentLastChildId=null;
  final Entry parent=getEntryById(parentId);
  if (parent == null) {
    Errors.add(errors,errorMessages.errorTheEntryHadNoParentSoItCouldNotBeMoved());
    return false;
  }
  parentFirstChildId=parent.getFirstChildId();
  parentLastChildId=parent.getLastChildId();
  if (direction.equals("before")) {
    final String previousId=entry.getPreviousSiblingId();
    if (previousId == null) {
      Errors.add(errors,errorMessages.errorThereIsNoEntryToMoveBefore());
      return false;
    }
    final Entry previous=getEntryById(previousId);
    final String previousPreviousId=previous.getPreviousSiblingId();
    if (previousPreviousId != null) {
      final Entry previousPrevious=getEntryById(previousPreviousId);
      if (previousPrevious != null) {
        previousPrevious.setNextSiblingId(entry.getId());
      }
    }
    final String nextId=entry.getNextSiblingId();
    if (nextId != null) {
      final Entry next=getEntryById(nextId);
      if (next != null) {
        next.setPreviousSiblingId(previousId);
      }
    }
    entry.setPreviousSiblingId(previousPreviousId);
    entry.setNextSiblingId(previousId);
    previous.setPreviousSiblingId(entry.getId());
    previous.setNextSiblingId(nextId);
    if (parentFirstChildId != null && previousId != null && parentFirstChildId.equals(previousId)) {
      parent.setFirstChildId(entry.getId());
    }
    if (parentLastChildId != null && parentLastChildId.equals(entry.getId())) {
      parent.setLastChildId(previousId);
    }
  }
 else   if (direction.equals("after")) {
    final String nextId=entry.getNextSiblingId();
    if (nextId == null) {
      Errors.add(errors,errorMessages.errorThereIsNoEntryToMoveAfter());
      return false;
    }
    final Entry next=getEntryById(nextId);
    final String nextNextId=next.getNextSiblingId();
    if (nextNextId != null) {
      final Entry nextNext=getEntryById(nextNextId);
      if (nextNext != null) {
        nextNext.setPreviousSiblingId(entry.getId());
      }
    }
    final String previousId=entry.getPreviousSiblingId();
    if (previousId != null) {
      final Entry previous=getEntryById(previousId);
      if (previousId != null) {
        previous.setNextSiblingId(nextId);
      }
    }
    entry.setPreviousSiblingId(nextId);
    entry.setNextSiblingId(nextNextId);
    next.setPreviousSiblingId(previousId);
    next.setNextSiblingId(entry.getId());
    if (parentFirstChildId != null && parentFirstChildId.equals(entry.getId())) {
      parent.setFirstChildId(nextId);
    }
    if (parentLastChildId != null && nextId != null && parentLastChildId.equals(nextId)) {
      parent.setLastChildId(entry.getId());
    }
  }
 else   if (direction.equals("left")) {
    final String parentParentId=parent.getParentId();
    if (parentParentId == null) {
      Errors.add(errors,errorMessages.errorThereIsNoParentOfTheParent());
      return false;
    }
    snipOutEntry(entry,parent,true);
    insertEntryAfter(entry,parent,true);
  }
 else   if (direction.equals("right")) {
    final String previousId=entry.getPreviousSiblingId();
    if (previousId == null) {
      Errors.add(errors,errorMessages.errorThereIsNoPreviousEntryToUseAsAParent());
      return false;
    }
    snipOutEntry(entry,parent,true);
    final Entry previous=getEntryById(previousId);
    final String previousLastChildId=previous.getLastChildId();
    if (previousLastChildId != null) {
      final Entry previousLastChild=getEntryById(previousLastChildId);
      previousLastChild.setNextSiblingId(entry.getId());
    }
    entry.setPreviousSiblingId(previousLastChildId);
    entry.setNextSiblingId(null);
    previous.setLastChildId(entry.getId());
    if (!previous.hasFirstChildId()) {
      previous.setFirstChildId(entry.getId());
    }
    previous.setLastChildId(entry.getId());
    entry.setParentId(previous.getId());
  }
 else {
    Errors.add(errors,errorMessages.errorTheDirectionIsInvalid());
    return false;
  }
  return true;
}

public boolean moveEntry ( User user , Entry entry , String direction , boolean isAdmin , Errors errors ) { if ( direction == null ) { Errors . add ( errors , errorMessages . errorDirectionIsNull ( ) ) ; return false ; } if ( entry == null ) { Errors . add ( errors , errorMessages . errorEntryIsNull ( ) ) ; return false ; } if ( ! canUserModifyEntry ( user , entry , isAdmin ) ) { Errors . add ( errors , errorMessages . errorUserIsNotEntitledToMoveTheEntry ( ) ) ; return false ; } final String parentId = entry . getParentId ( ) ; String parentFirstChildId = null ; String parentLastChildId = null ; final Entry parent = getEntryById ( parentId ) ; if ( parent == null ) { Errors . add ( errors , errorMessages . errorTheEntryHadNoParentSoItCouldNotBeMoved ( ) ) ; return false ; } parentFirstChildId = parent . getFirstChildId ( ) ; parentLastChildId = parent . getLastChildId ( ) ; if ( direction . equals ( "before" ) ) { final String previousId = entry . getPreviousSiblingId ( ) ; if ( previousId == null ) { Errors . add ( errors , errorMessages . errorThereIsNoEntryToMoveBefore ( ) ) ; return false ; } final Entry previous = getEntryById ( previousId ) ; final String previousPreviousId = previous . getPreviousSiblingId ( ) ; if ( previousPreviousId != null ) { final Entry previousPrevious = getEntryById ( previousPreviousId ) ; if ( previousPrevious != null ) { previousPrevious . setNextSiblingId ( entry . getId ( ) ) ; } } final String nextId = entry . getNextSiblingId ( ) ; if ( nextId != null ) { final Entry next = getEntryById ( nextId ) ; if ( next != null ) { next . setPreviousSiblingId ( previousId ) ; } } entry . setPreviousSiblingId ( previousPreviousId ) ; entry . setNextSiblingId ( previousId ) ; previous . setPreviousSiblingId ( entry . getId ( ) ) ; previous . setNextSiblingId ( nextId ) ; if ( parentFirstChildId != null && previousId != null && parentFirstChildId . equals ( previousId ) ) { parent . setFirstChildId ( entry . getId ( ) ) ; } if ( parentLastChildId != null && parentLastChildId . equals ( entry . getId ( ) ) ) { parent . setLastChildId ( previousId ) ; } } else if ( direction . equals ( "after" ) ) { final String nextId = entry . getNextSiblingId ( ) ; if ( nextId == null ) { Errors . add ( errors , errorMessages . errorThereIsNoEntryToMoveAfter ( ) ) ; return false ; } final Entry next = getEntryById ( nextId ) ; final String nextNextId = next . getNextSiblingId ( ) ; if ( nextNextId != null ) { final Entry nextNext = getEntryById ( nextNextId ) ; if ( nextNext != null ) { nextNext . setPreviousSiblingId ( entry . getId ( ) ) ; } } final String previousId = entry . getPreviousSiblingId ( ) ; if ( previousId != null ) { final Entry previous = getEntryById ( previousId ) ; if ( previousId != null ) { previous . setNextSiblingId ( nextId ) ; } } entry . setPreviousSiblingId ( nextId ) ; entry . setNextSiblingId ( nextNextId ) ; next . setPreviousSiblingId ( previousId ) ; next . setNextSiblingId ( entry . getId ( ) ) ; if ( parentFirstChildId != null && parentFirstChildId . equals ( entry . getId ( ) ) ) { parent . setFirstChildId ( nextId ) ; } if ( parentLastChildId != null && nextId != null && parentLastChildId . equals ( nextId ) ) { parent . setLastChildId ( entry . getId ( ) ) ; } } else if ( direction . equals ( "left" ) ) { final String parentParentId = parent . getParentId ( ) ; if ( parentParentId == null ) { Errors . add ( errors , errorMessages . errorThereIsNoParentOfTheParent ( ) ) ; return false ; } snipOutEntry ( entry , parent , true ) ; insertEntryAfter ( entry , parent , true ) ; } else if ( direction . equals ( "right" ) ) { final String previousId = entry . getPreviousSiblingId ( ) ; if ( previousId == null ) { Errors . add ( errors , errorMessages . errorThereIsNoPreviousEntryToUseAsAParent ( ) ) ; return false ; } snipOutEntry ( entry , parent , true ) ; final Entry previous = getEntryById ( previousId ) ; final String previousLastChildId = previous . getLastChildId ( ) ; if ( previousLastChildId != null ) { final Entry previousLastChild = getEntryById ( previousLastChildId ) ; previousLastChild . setNextSiblingId ( entry . getId ( ) ) ; } entry . setPreviousSiblingId ( previousLastChildId ) ; entry . setNextSiblingId ( null ) ; previous . setLastChildId ( entry . getId ( ) ) ; if ( ! previous . hasFirstChildId ( ) ) { previous . setFirstChildId ( entry . getId ( ) ) ; } previous . setLastChildId ( entry . getId ( ) ) ; entry . setParentId ( previous . getId ( ) ) ; } else { Errors . add ( errors , errorMessages . errorTheDirectionIsInvalid ( ) ) ; return false ; } return true ; }
api method . moves an entry before , after , left or right .
