/** * Fixes ordinal values if there is any duplicates * * @param definitionId * @param dao */ private void fixDuplicates(int definitionId, EventDefinitionCRFDAO dao) { ArrayList list = dao.findAllByEventDefinitionId(definitionId); int prevOrdinal = 0; boolean incrementNextOrdinal = false; for (int i = 0; i < list.size(); i++) { EventDefinitionCRFBean edc = (EventDefinitionCRFBean) list.get(i); if (i == 0) { if (edc.getOrdinal() != 0) { edc.setOrdinal(i); dao.update(edc); } continue; } if (incrementNextOrdinal) { edc.setOrdinal(i); dao.update(edc); continue; } if (edc.getOrdinal() != i) { edc.setOrdinal(i); dao.update(edc); incrementNextOrdinal = true; } } }
/** * Increases the ordinal for current object and decrease the ordinal of the previous one * * @param idCurrent * @param idPrevious * @param dao */ private void increase( int idCurrent, int idPrevious, int currOrdinal, int prevOrdinal, int defId, EventDefinitionCRFDAO dao) { EventDefinitionCRFBean current = (EventDefinitionCRFBean) dao.findByPK(idCurrent); EventDefinitionCRFBean previous = (EventDefinitionCRFBean) dao.findByPK(idPrevious); if (current.getOrdinal() == currOrdinal && previous.getOrdinal() == prevOrdinal) { if (idCurrent > 0) { int currentOrdinal = current.getOrdinal(); current.setOrdinal(currentOrdinal - 1); current.setUpdater((UserAccountBean) session.getAttribute("userBean")); dao.update(current); } if (idPrevious > 0) { int previousOrdinal = previous.getOrdinal(); previous.setOrdinal(previousOrdinal + 1); previous.setUpdater((UserAccountBean) session.getAttribute("userBean")); dao.update(previous); } ArrayList currOrdlist = dao.findAllByEventDefinitionIdAndOrdinal(defId, current.getOrdinal()); ArrayList prevOrdlist = dao.findAllByEventDefinitionIdAndOrdinal(defId, previous.getOrdinal()); if (currOrdlist.size() > 1 || prevOrdlist.size() > 1) { fixDuplicates(defId, dao); } } }