@Override public void addCarouselImage(CarouselImage carouselImage) { // We use explicit transactions, since we are going to execute more than // 1 query in the same transaction Ebean.beginTransaction(); try { // We set atomatically the caroueselImage id, in order to be the // first one carouselImage.setId((long) 1); List<CarouselImage> carouselImages = finder.orderBy().desc("id").findList(); for (CarouselImage ci : carouselImages) { // We insert a copy of the ci in the next position CarouselImage ciCopy = (CarouselImage) ci._ebean_createCopy(); ciCopy.setId(ci.getId() + 1); ciCopy.save(); // Then we delete the ci ci.delete(); } // Then we save carouselImage.save(); Ebean.commitTransaction(); } finally { Ebean.endTransaction(); } }
@Override public void deleteCarouselImage(Long id) throws NotFoundException { // We use explicit transactions, since we are going to execute more than // 1 query in the same transaction Ebean.beginTransaction(); try { // We see if the carouselImage really exists if (finder.byId(id) == null) throw new NotFoundException("La imagen no existe asi que no se puede borrar"); // We delete it finder.byId(id).delete(); // Now we need to reorder all the carouselImages for (CarouselImage ci : finder.orderBy("id").findList()) { if (ci.getId() > id) { // We insert a copy of the ci in the previous position CarouselImage ciCopy = (CarouselImage) ci._ebean_createCopy(); ciCopy.setId(ci.getId() - 1); ciCopy.save(); // Then we delete the ci ci.delete(); } } Ebean.commitTransaction(); } finally { Ebean.endTransaction(); } }