import org.lilie.services.eliot.notes.AbstractNotesController
import org.lilie.services.eliot.notes.avis.AvisService
import org.lilie.services.eliot.notes.avis.TypeAvisService
+import org.lilie.services.eliot.notes.ActionEnum
+import org.lilie.services.eliot.notes.NotesFonction
+import org.lilie.services.eliot.notes.NotesDroitService
class AvisController extends AbstractNotesController {
AvisService avisService
TypeAvisService typeAvisService
+ NotesDroitService notesDroitService
def loadAsArbre = {
+ notesDroitService.verifieAutorisationByEtablissement(
+ securiteSession,
+ etablissementCourant(),
+ ActionEnum.CONSULTATION,
+ (List) (NotesFonction.DIRECTION_ASSIMILE)
+ )
+
List result =
typeAvisService.
findAllForEtablissement(securiteSession, etablissementCourant())
}
def supprime = {
+ notesDroitService.verifieAutorisationByEtablissement(
+ securiteSession,
+ etablissementCourant(),
+ ActionEnum.SUPPRESSION,
+ (List) (NotesFonction.DIRECTION_ASSIMILE)
+ )
+
Map result = tryCatch {
Avis avis = Avis.get(params.id)
- avisService.supprime(avis)
+ avisService.supprime(securiteSession, avis)
}
render result as JSON
}
def cree = {
+ notesDroitService.verifieAutorisationByEtablissement(
+ securiteSession,
+ etablissementCourant(),
+ ActionEnum.CREATION,
+ (List) (NotesFonction.DIRECTION_ASSIMILE)
+ )
+
Map result = tryCatch{
- Integer typeAvisId = Integer.parseInt(params.typeAvisId)
String libelle = params.libelle
- if (!typeAvisId || !libelle){
- throw new IllegalArgumentException("L'avis (id type : $params.typeAvisId," +
+ String typeAvisIdStr = params.typeAvisId
+ if (!typeAvisIdStr || !libelle){
+ throw new IllegalArgumentException("L'avis (id type : $typeAvisIdStr," +
" libellé : $libelle) n'a pas pu être créé car l'un de ces" +
" attributs est non défini")
}
- return [avis: avisService.cree(typeAvisId, libelle)]
+ // Cherche le type
+ TypeAvis typeAvis = TypeAvis.get(typeAvisIdStr)
+ return [avis: avisService.cree(securiteSession, typeAvis, libelle)]
}
JSON.use(ParametrageAvisConfig.toString()){
}
def modifieLibelle = {
+ notesDroitService.verifieAutorisationByEtablissement(
+ securiteSession,
+ etablissementCourant(),
+ ActionEnum.MODIFICATION,
+ (List) (NotesFonction.DIRECTION_ASSIMILE)
+ )
+
Map result = tryCatch {
Avis avis = Avis.get(Integer.parseInt(params.id))
- avisService.modifieLibelle(avis, params.libelle)
+ avisService.modifieLibelle(securiteSession, avis, params.libelle)
}
render result as JSON
}
def modifieOrdre = {
+ notesDroitService.verifieAutorisationByEtablissement(
+ securiteSession,
+ etablissementCourant(),
+ ActionEnum.MODIFICATION,
+ (List) (NotesFonction.DIRECTION_ASSIMILE)
+ )
+
Map result = tryCatch {
Integer avisId = Integer.parseInt(params.nodeId)
Integer nouvellePositionGui = Integer.parseInt(params.position)
Avis avis = Avis.get(avisId)
- List<Avis> avisAIncrementerListe = []
- List<Avis> avisADecrementerListe = []
- Integer nouvellePosition
- if (nouvellePositionGui < avis.ordre){
- nouvellePosition = nouvellePositionGui + 1
- // Récupère les avis du même type dont l'ordre doit être incrémenté
- avisAIncrementerListe = avisService.findAvisAIncrementer(avis, nouvellePosition)
- } else if (nouvellePositionGui > avis.ordre){
- nouvellePosition = nouvellePositionGui
- // Récupère les avis du même type dont l'ordre doit être décrémenté
- avisADecrementerListe = avisService.findAvisADecrementer(avis, nouvellePosition)
- }
- avisService.modifieOrdre(avis,
- nouvellePosition,
- avisAIncrementerListe,
- avisADecrementerListe)
+ avisService.modifieOrdre(
+ securiteSession,
+ avis,
+ nouvellePositionGui)
}
render result as JSON
import org.lilie.services.eliot.notes.NotesDroitService
import org.lilie.services.eliot.notes.Avis
import org.lilie.services.eliot.notes.TypeAvis
+import org.hibernate.SessionFactory
class AvisService {
NotesDroitService notesDroitService
TypeAvisService typeAvisService
+ SessionFactory sessionFactory
- Avis cree (Integer typeAvisId, String libelleAvis) {
- // Cherche le type
- TypeAvis typeAvis = typeAvisService.findById(typeAvisId)
+ Avis cree (SecuriteSession securiteSession,
+ TypeAvis typeAvis,
+ String libelleAvis) {
- if (typeAvis) {
- // Définit son emplacement dans la liste des avis de son type
- Integer ordreSuivant = this.findOrdreSuivant(typeAvis)
-
- // Crée l'avis
- Avis avis = new Avis(
- libelle: libelleAvis,
- type: typeAvis,
- ordre: ordreSuivant
+ if (!typeAvis) {
+ throw new IllegalArgumentException(
+ "Le type de l'avis, dont l'id est null. L'avis n'a pas pu être créé"
)
+ }
- if (!avis.save(flush:true)){
- throw new IllegalStateException(
- "L'avis ayant pour libellé $libelleAvis " +
- "n'a pas pu être créé : $avis.errors"
- )
- }
+ notesDroitService.verifieAutorisationByEtablissement(
+ securiteSession,
+ typeAvis.etablissement,
+ ActionEnum.CREATION,
+ (List) (NotesFonction.DIRECTION_ASSIMILE)
+ )
+ // Définit son emplacement dans la liste des avis de son type
+ Integer ordreSuivant = this.findOrdreSuivant(typeAvis)
+
+ // Crée l'avis
+ Avis avis = new Avis(
+ libelle: libelleAvis,
+ type: typeAvis,
+ ordre: ordreSuivant
+ )
- return avis
- } else {
- throw new IllegalStateException(
- "Le type de l'avis, dont l'id est $typeAvisId, n'existe pas. " +
- "L'avis n'a pas pu être créé"
- )
- }
+ avis.save(flush: true, failOnError: true)
+
+ return avis
}
- void supprime (Avis avis) {
- if (avis){
- avis.delete(flush: true)
- } else {
- throw new IllegalStateException("L'avis d'id $avisId n'existe pas")
+ void supprime (SecuriteSession securiteSession, Avis avis) {
+
+ if (!avis) {
+ throw new IllegalArgumentException("L'avis est null")
}
+
+ notesDroitService.verifieAutorisationByEtablissement(
+ securiteSession,
+ avis.type.etablissement,
+ ActionEnum.SUPPRESSION,
+ (List) (NotesFonction.DIRECTION_ASSIMILE)
+ )
+
+ avis.delete(flush: true)
}
- void modifieLibelle (Avis avis, String nouveauLibelleAvis) {
+ void modifieLibelle (SecuriteSession securiteSession,
+ Avis avis,
+ String nouveauLibelleAvis) {
+
if (!avis){
- throw new IllegalStateException("L'avis d'id $avis.id n'existe pas")
+ throw new IllegalArgumentException("L'avis est null")
}
+
+ notesDroitService.verifieAutorisationByEtablissement(
+ securiteSession,
+ avis.type.etablissement,
+ ActionEnum.MODIFICATION,
+ (List) (NotesFonction.DIRECTION_ASSIMILE)
+ )
+
if (!nouveauLibelleAvis) {
throw new IllegalStateException("La modification de l'avis d'id " +
- "$avis.id a échoué; le libelle ne peut pas être vide")
+ "${avis.id} a échoué; le libelle ne peut pas être vide")
}
avis.libelle = nouveauLibelleAvis
- if (!avis.save(flush:true)){
- throw new IllegalStateException(
- "L'avis d'id $avis.id n'a pas pu être modifié"
- )
- }
+
+ avis.save(flush: true, failOnError: true)
}
- void modifieOrdre (Avis avis,
- Integer nouvellePosition,
- List<Avis> avisAIncrementerListe,
- List<Avis> avisADecrementerListe) {
- if (!avis){
- throw new IllegalStateException("L'avis d'id $avis.id n'existe pas")
+ void modifieOrdre (SecuriteSession securiteSession,
+ Avis avis,
+ Integer nouvellePositionGui) {
+
+ if (!avis) {
+ throw new IllegalArgumentException("L'avis est null")
}
- if (!nouvellePosition) {
+ if (nouvellePositionGui == null) {
throw new IllegalStateException("La modification de l'avis d'id " +
- "$avis.id a échoué; la nouvelle position n'est pas définie")
+ "${avis.id} a échoué; la nouvelle position n'est pas définie")
}
- avis.ordre = nouvellePosition
- if (!avis.save(flush:true)){
- throw new IllegalStateException(
- "L'avis d'id $avis.id n'a pas pu être modifié"
- )
+ notesDroitService.verifieAutorisationByEtablissement(
+ securiteSession,
+ avis.type.etablissement,
+ ActionEnum.MODIFICATION,
+ (List) (NotesFonction.DIRECTION_ASSIMILE)
+ )
+
+ List<Avis> avisAIncrementerListe = []
+ List<Avis> avisADecrementerListe = []
+ Integer nouvellePosition
+ if (nouvellePositionGui < avis.ordre){
+ nouvellePosition = nouvellePositionGui + 1
+ // Récupère les avis du même type dont l'ordre doit être incrémenté
+ avisAIncrementerListe = findAvisAIncrementer(avis, nouvellePosition)
+ } else if (nouvellePositionGui > avis.ordre){
+ nouvellePosition = nouvellePositionGui
+ // Récupère les avis du même type dont l'ordre doit être décrémenté
+ avisADecrementerListe = findAvisADecrementer(avis, nouvellePosition)
}
- if (avisAIncrementerListe != null){
- if (avisAIncrementerListe.size() > 0){
- avisAIncrementerListe.each {
- it.ordre++
- if (!it.save(flush:true)){
- throw new IllegalStateException(
- "L'ordre de l'avis d'id $avis.id n'a pas pu être incrémenté"
- )
- }
- }
+ avis.ordre = nouvellePosition
+
+ avis.save(failOnError: true)
+
+ if (avisAIncrementerListe.size() > 0){
+ avisAIncrementerListe.each {
+ it.ordre++
+ it.save(failOnError: true)
}
- } else {
- throw new IllegalStateException(
- "La liste de l'avis dont l'ordre est à incrémenter est nulle"
- )
}
- if (avisADecrementerListe != null){
- if (avisADecrementerListe.size() > 0){
- avisADecrementerListe.each {
- it.ordre--
- if (!it.save(flush:true)){
- throw new IllegalStateException(
- "L'ordre de l'avis d'id $avis.id n'a pas pu être décrémenté"
- )
- }
- }
+
+ if (avisADecrementerListe.size() > 0){
+ avisADecrementerListe.each {
+ it.ordre--
+ it.save(failOnError: true)
}
- } else {
- throw new IllegalStateException(
- "La liste de l'avis dont l'ordre est à décrémenter est nulle"
- )
}
+
+ sessionFactory.currentSession.flush()
}
Integer findOrdreSuivant(TypeAvis typeAvis) {
}
- /**
- * Retourne tous les avis existant pour un établissement.
- * @author bper
- */
+/**
+ * Retourne tous les avis existant pour un établissement.
+ * @author bper
+ */
List<Avis> findAllForEtablissement(SecuriteSession securiteSession,
Etablissement etablissement) {