public void saveProduct(X p) throws TreeNodeException { /* parent check */ if (p.getParent() == null) throw new TreeNodeException("Need to append to a parent"); containerNodeDao.startTransaction(); final NodeContainer<X> parent = containerNodeDao.getContainerByNodeUID(p.getParent().getUID()); if (parent == null) throw new TreeNodeException("Need to append to a saved parent"); /* load nodes to be updated */ List<NodeContainer<X>> rightMost = containerNodeDao.getRightMostProducts(parent.getRight() - 1); List<NodeContainer<X>> leftMost = containerNodeDao.getLeftMostProducts(parent.getRight() - 1); List<NodeContainer<X>> toBeUpdated = new ArrayList<NodeContainer<X>>(); int incr = (1 + services.countDescendants(p)) * 2; /* setting increment (total nodes to be added) */ for (NodeContainer<X> r : rightMost) { r.setRight(r.getRight() + incr); } toBeUpdated.addAll(rightMost); for (NodeContainer<X> r : leftMost) { int incLeft = r.getLeft() + incr; if (toBeUpdated.contains(r)) rightMost.get(rightMost.indexOf(r)).setLeft(incLeft); else { r.setLeft(incLeft); toBeUpdated.add(r); } } containerNodeDao.saveOrUpdateProducts(toBeUpdated); /* add new nodes */ NodeContainer<X> rightDesc = containerNodeDao.getMaxRightDescendant(parent); int start = Math.max(parent.getLeft() + 1, (rightDesc != null) ? rightDesc.getRight() : 0 + 1); toBeUpdated.addAll(services.getNodeContainerList(p, start)); containerNodeDao.saveOrUpdateProducts(toBeUpdated); containerNodeDao.commitTransaction(); }