--- /dev/null
+/*
+ * Copyright © FYLAB and the Conseil Régional d'Île-de-France, 2009
+ * This file is part of L'Interface Libre et Interactive de l'Enseignement (Lilie).
+ *
+ * Lilie is free software. You can redistribute it and/or modify since
+ * you respect the terms of either (at least one of the both license) :
+ * - under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ * - the CeCILL-C as published by CeCILL-C; either version 1 of the
+ * License, or any later version
+ *
+ * There are special exceptions to the terms and conditions of the
+ * licenses as they are applied to this software. View the full text of
+ * the exception in file LICENSE.txt in the directory of this software
+ * distribution.
+ *
+ * Lilie is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * Licenses for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * and the CeCILL-C along with Lilie. If not, see :
+ * <http://www.gnu.org/licenses/> and
+ * <http://www.cecill.info/licences.fr.html>.
+ */
+
+
+
+
+
+package org.lilie.services.eliot.notes.scolarite
+
+import org.lilie.services.eliot.scolarite.RelPeriodeService
+import org.lilie.services.eliot.scolarite.Periode
+import org.lilie.services.eliot.exception.SauvegardeEchoueException
+import org.lilie.services.eliot.scolarite.RelPeriodeServiceService
+
+class NotesRelPeriodeServiceService extends RelPeriodeServiceService {
+
+ static transactional = true
+
+ /**
+ * Get relations
+ * @param periode
+ * @param ordre
+ * @return
+ */
+ List<RelPeriodeService> getRels(Periode periode, Integer ordre) {
+ return RelPeriodeService.withCriteria {
+ eq('periode', periode)
+ eq('ordre', ordre)
+ }
+ }
+
+ /**
+ * Change l'ordre d'un service.
+ * Gére le reordonencement si necessaire.
+ * @param rel
+ * @param ordre
+ * @return true si reordonencement necessaire
+ * @author msan
+ */
+ Boolean internalChangeOrdre(RelPeriodeService rel, Integer ordre)
+ throws SauvegardeEchoueException {
+ Periode periode = rel.periode
+ Boolean ordreChange = false
+
+ // Vérifie si un rel avec le même ordre existe pour la période donné
+ List<RelPeriodeService> relsMemeOrdre = RelPeriodeService.withCriteria {
+ eq('periode', periode)
+ eq('ordre', ordre)
+ ne('id', rel.id)
+ }
+
+ if (relsMemeOrdre?.size() > 0) {
+ // rels avec le même ordre existent - besoin de reordonner
+ ordreChange = true
+ // bouge toutes les rels >=ordre par 1
+ List<RelPeriodeService> relsAbouger = RelPeriodeService.withCriteria {
+ eq('periode', periode)
+ ge('ordre', ordre)
+ ne('id', rel.id)
+ }
+ // peut-on faire un update batch, pour éviter n requêtes ?
+ relsAbouger.each { RelPeriodeService relAbouger ->
+ relAbouger.ordre = relAbouger.ordre + 1
+ relAbouger.save()
+ if (relAbouger.hasErrors()) {
+ throw new SauvegardeEchoueException(relAbouger)
+ }
+ }
+ rel.ordre = ordre
+ } else {
+ rel.ordre = ordre
+ }
+ rel.save(flush: true)
+ if (rel.hasErrors()) {
+ throw new SauvegardeEchoueException(rel)
+ }
+
+ return ordreChange
+ }
+
+}