IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

JSF Java Discussion :

Comment gérer le cas d'une suppression par un autre utilisateur


Sujet :

JSF Java

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre éclairé
    Profil pro
    100
    Inscrit en
    Juillet 2007
    Messages
    585
    Détails du profil
    Informations personnelles :
    Localisation : Suisse

    Informations professionnelles :
    Activité : 100

    Informations forums :
    Inscription : Juillet 2007
    Messages : 585
    Par défaut Comment gérer le cas d'une suppression par un autre utilisateur
    Bonjour,

    J'ai un tableau qui affiche des éléments. Je peux supprimer / modifier ces éléments. J'utilise le Framework JPA pour la persistance.
    Lorsque je veux modifier un élément, j'initialise certains champs dans une méthode @PostConstruct, en l'occurrence :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    @PostConstruct
    	public void init() {
    		FacesContext context = FacesContext.getCurrentInstance();
    		ExternalContext externalContext = context.getExternalContext();
    
    		Map<String, String> params = externalContext.getRequestParameterMap();
    		String idIdentite = params.get("idIdentite");
    
    		if (idIdentite != null) {
    			currentIdentite = m_identiteService.getIdentite(Integer
    					.valueOf(idIdentite));
    			
    			// Test au cas ou un autre utilisateur aurait supprimé entre temps l'identité
    			if (currentIdentite == null) {
    				// TODO
    			}		} else {
    			currentIdentite = new Identite();
    			currentIdentite.setTypeIdentiteCi(TypeIdentiteCi.PHYSIQUE);
    		}
    
    		Adresse adresseIdentite = null;
    		if (currentIdentite.getId() > 0) {
    			adresseIdentite = currentIdentite.getAdressePrincipale();
    			canton = adresseIdentite.getLocalite() != null ? adresseIdentite
    					.getLocalite().getCanton() : m_adresseService
    					.getCanton("1");
    		} else {
    			adresseIdentite = new Adresse();
    			adresseIdentite.setTypeAdresse(TypeAdresseCi.PRINCIPALE);
    			adresseIdentite.setPays(m_adresseService.getPays("CH"));
    			canton = m_adresseService.getCanton("1");
    			currentIdentite.addAdresse(adresseIdentite);
    		}
    
    	}
    Le problème est que si un autre utilisateur supprime l'élément que je veux modifier entre le moment ou j'affiche le tableau et le moment ou je clique pour modifier, j'aurai une erreur.
    J'ai ajouté un test dans la méthode mais je ne sais pas si c'est le bon endroit pour ça et surtout qu'est-ce que je dois faire si le test est positif.

    J'aimerais rester sur la même page et avoir un message qui indique que l'élément ne se trouve plus en base de données.

    Une idée de comment faire ?

    Merci d'avance pour votre aide

  2. #2
    Membre Expert Avatar de jeffray03
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Juillet 2008
    Messages
    1 501
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Allemagne

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Juillet 2008
    Messages : 1 501
    Par défaut
    Salut,
    voici une documentation qui pourra t´aider ici

    eric

  3. #3
    Membre éclairé
    Profil pro
    100
    Inscrit en
    Juillet 2007
    Messages
    585
    Détails du profil
    Informations personnelles :
    Localisation : Suisse

    Informations professionnelles :
    Activité : 100

    Informations forums :
    Inscription : Juillet 2007
    Messages : 585
    Par défaut
    Salut,

    Merci, j'ai lu l'article. C'est très intéressant et me sera très utile pour la suite.

    Par contre, ça ne résoud pas mon problème.
    J'aimerais savoir quoi faire dans mon managedBean si l'élément que je veux accéder à été supprimé
    Voici mon bean. Après avoir cliqué sur le lien dans ma table pour modifier l'élément, je me retrouve sur une nouvelle page (qui correspond à ce bean)
    Dans la méthode init(), je me rend compte que l'élément n'est plus en base..... J'aimerais simplement revenir sur la page de ma table et avertir l'utilisateur comme quoi l'élément n'existe plus en base

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
     
    @ManagedBean(name = "saisirIdentiteForm")
    @ViewScoped
    public class SaisirIdentiteForm implements Serializable {
     
    	private static final long serialVersionUID = 1L;
     
    	private Identite currentIdentite;
    	private Canton canton;
     
    	// Listes
    	private List<TypeIdentiteCi> listTypeIdentites;
    	private List<Pays> listPays;
    	private List<Canton> listCantons;
     
    	// Services
    	@EJB
    	private IdentiteService m_identiteService;
    	@EJB
    	private AdresseService m_adresseService;
     
    	/**
             * Default Constructor
             */
    	public SaisirIdentiteForm() {
    	}
     
    	@PostConstruct
    	public void init() {
    		FacesContext context = FacesContext.getCurrentInstance();
    		ExternalContext externalContext = context.getExternalContext();
     
    		Map<String, String> params = externalContext.getRequestParameterMap();
    		String idIdentite = params.get("idIdentite");
     
    		if (idIdentite != null) {
    			currentIdentite = m_identiteService.getIdentite(Integer
    					.valueOf(idIdentite));
     
    			// Test au cas ou un autre utilisateur aurait supprimé entre temps l'identité
    			if (currentIdentite == null) {
    				// TODO
    			}
    		} else {
    			currentIdentite = new Identite();
    			currentIdentite.setTypeIdentiteCi(TypeIdentiteCi.PHYSIQUE);
    		}
     
    		Adresse adresseIdentite = null;
    		if (currentIdentite.getId() > 0) {
    			adresseIdentite = currentIdentite.getAdressePrincipale();
    			canton = adresseIdentite.getLocalite() != null ? adresseIdentite
    					.getLocalite().getCanton() : m_adresseService
    					.getCanton("JU");
    		} else {
    			adresseIdentite = new Adresse();
    			adresseIdentite.setTypeAdresse(TypeAdresseCi.PRINCIPALE);
    			adresseIdentite.setPays(m_adresseService.getPays("CH"));
    			canton = m_adresseService.getCanton("JU");
    			currentIdentite.addAdresse(adresseIdentite);
    		}
     
    	}
     
    	private Adresse getAdresseIdentite() {
    		return currentIdentite.getAdressePrincipale();
    	}
     
    	private boolean isCreateIdentite() {
    		return currentIdentite.getId() <= 0;
    	}
     
    	public List<Localite> completeLocalite(String query) {
            List<Localite> lstLocalites = getListLocalites();
            List<Localite> lstLocalitesFiltrees = new ArrayList<Localite>();
     
            if ("*".equals(query)) {
            	lstLocalitesFiltrees.addAll(lstLocalites);
            } else {
            	for (int i = 0; i < lstLocalites.size(); i++) {
            		Localite localite = lstLocalites.get(i);
            		if(localite.getNom().toLowerCase().startsWith(query.toLowerCase()) || Integer.valueOf(localite.getCodePostal()).toString().toLowerCase().startsWith(query.toLowerCase())) {
            			lstLocalitesFiltrees.add(localite);
            		}
            	}
            }
     
            return lstLocalitesFiltrees;
        }
     
    	public List<Canton> completeCanton(String query) {
    		FacesContext context = FacesContext.getCurrentInstance();
     
    		CantonManagedBean cantonMB = context.getApplication()
    				.evaluateExpressionGet(context, "#{cantonManagedBean}",
    						CantonManagedBean.class);
     
            List<Canton> lstCantons = cantonMB.getLstCanton();
            List<Canton> lstCantonsFiltres = new ArrayList<Canton>();
     
            if ("*".equals(query)) {
            	lstCantonsFiltres.addAll(lstCantons);
            } else {
            	for (int i = 0; i < lstCantons.size(); i++) {
            		Canton canton = lstCantons.get(i);
            		if(canton.getNom().toLowerCase().startsWith(query.toLowerCase())) {
            			lstCantonsFiltres.add(canton);
            		}
            	}
            }
     
            return lstCantonsFiltres;
        }
     
    	public List<TypeIdentiteCi> getListTypeIdentites() {
    		if (listTypeIdentites == null) {
    			listTypeIdentites = new ArrayList<>();
    			listTypeIdentites.add(TypeIdentiteCi.PHYSIQUE);
    			listTypeIdentites.add(TypeIdentiteCi.MORALE);
    		}
     
    		return listTypeIdentites;
    	}
     
    	public List<Pays> getListPays() {
    		if (listPays == null) {
    			listPays = m_adresseService.getPaysList();
    		}
     
    		return listPays;
    	}
     
    	public List<Canton> getListCantons() {
    		if (listCantons == null) {
    			listCantons = m_adresseService.getCantonList();
    		}
     
    		return listCantons;
    	}
     
    	public List<Localite> getListLocalites() {
    		return m_adresseService.getLocaliteListByCanton(canton);
    	}
     
    	public void reinitialiserIdentite() {
    		if (TypeIdentiteCi.PHYSIQUE == currentIdentite.getTypeIdentiteCi()) {
    			currentIdentite.setRaisonSociale(null);
    		} else {
    			currentIdentite.setNom(null);
    			currentIdentite.setPrenom(null);
    		}
    	}
     
    	/**
             * Enregistre l'identité dans la base de données
             */
    	public String enregistrerIdentite() {
    		try {
     
    			if (currentIdentite.getId() > 0) {
    				m_identiteService.updateIdentite(currentIdentite);
    			} else {
    				m_identiteService.createIdentite(currentIdentite);
    			}
     
    			FacesContext
    					.getCurrentInstance()
    					.addMessage(
    							null,
    							Messages.getMessage(
    									isCreateIdentite() ? "LIdentiteAEteAjouteeAvecSucces"
    											: "LIdentiteAEteModifieeAvecSucces",
    									FacesMessage.SEVERITY_INFO, null));
    		} catch (Throwable e) {
    			System.out.println(e);
    			FacesContext.getCurrentInstance().addMessage(null,
    					new FacesMessage(e.getMessage()));
    			return "failure";
    		}
     
    		return "identite";
    	}
     
    	/**
             * Redirige vers la page de gestion des identités
             */
    	public String cancel() {
    		return "identite";
    	}
     
    	public Canton getCanton() {
    		return canton;
    	}
     
    	public void setCanton(Canton canton) {
    		this.canton = canton;
    	}
     
    	public TypeIdentiteCi getTypeIdentite() {
    		return currentIdentite.getTypeIdentiteCi();
    	}
     
    	public void setTypeIdentite(TypeIdentiteCi typePersonne) {
    		currentIdentite.setTypeIdentiteCi(typePersonne);
    	}
     
    	public String getNom() {
    		return currentIdentite.getNom();
    	}
     
    	public void setNom(String nom) {
    		currentIdentite.setNom(nom);
    	}
     
    	public String getPrenom() {
    		return currentIdentite.getPrenom();
    	}
     
    	public void setPrenom(String prenom) {
    		currentIdentite.setPrenom(prenom);
    	}
     
    	public String getRaisonSociale() {
    		return currentIdentite.getRaisonSociale();
    	}
     
    	public void setRaisonSociale(String raisonSociale) {
    		currentIdentite.setRaisonSociale(raisonSociale);
    	}
     
    	public Pays getPays() {
    		return getAdresseIdentite().getPays();
    	}
     
    	public void setPays(Pays pays) {
    		getAdresseIdentite().setPays(pays);
    	}
     
    	public String getRueNumero() {
    		return getAdresseIdentite().getRueNumero();
    	}
     
    	public void setRueNumero(String rueNumero) {
    		getAdresseIdentite().setRueNumero(rueNumero);
    	}
     
    	public String getCasePostale() {
    		return getAdresseIdentite().getCasePostale();
    	}
     
    	public void setCasePostale(String casePostale) {
    		getAdresseIdentite().setCasePostale(casePostale);
    	}
     
    	public Localite getLocalite() {
    		return getAdresseIdentite().getLocalite();
    	}
     
    	public void setLocalite(Localite localite) {
    		getAdresseIdentite().setLocalite(localite);
    	}
     
    	public String getCodePostalLibre() {
    		return getAdresseIdentite().getCodePostalLibre();
    	}
     
    	public void setCodePostalLibre(String codePostalLibre) {
    		getAdresseIdentite().setCodePostalLibre(codePostalLibre);
    	}
     
    	public String getLocaliteLibre() {
    		return getAdresseIdentite().getLocaliteLibre();
    	}
     
    	public void setLocaliteLibre(String localiteLibre) {
    		getAdresseIdentite().setLocaliteLibre(localiteLibre);
    	}
     
    	public String getNoTelProf() {
    		return getAdresseIdentite().getNoTelProf();
    	}
     
    	public void setNoTelProf(String noTelProf) {
    		getAdresseIdentite().setNoTelProf(noTelProf);
    	}
     
    	public String getNoTelMobile() {
    		return getAdresseIdentite().getNoTelMobile();
    	}
     
    	public void setNoTelMobile(String noTelMobile) {
    		getAdresseIdentite().setNoTelMobile(noTelMobile);
    	}
     
    	public String getNoTelPrive() {
    		return getAdresseIdentite().getNoTelPrive();
    	}
     
    	public void setNoTelPrive(String noTelPrive) {
    		getAdresseIdentite().setNoTelPrive(noTelPrive);
    	}
     
    	public String getEmail() {
    		return getAdresseIdentite().getEmail();
    	}
     
    	public void setEmail(String email) {
    		getAdresseIdentite().setEmail(email);
    	}
     
    	public String getNoFax() {
    		return getAdresseIdentite().getNoFax();
    	}
     
    	public void setNoFax(String noFax) {
    		getAdresseIdentite().setNoFax(noFax);
    	}
     
    }

  4. #4
    Membre éclairé
    Profil pro
    100
    Inscrit en
    Juillet 2007
    Messages
    585
    Détails du profil
    Informations personnelles :
    Localisation : Suisse

    Informations professionnelles :
    Activité : 100

    Informations forums :
    Inscription : Juillet 2007
    Messages : 585
    Par défaut
    La solution se trouve dans ce post :

    http://www.developpez.net/forums/d15...ne-fonctionne/

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Réponses: 2
    Dernier message: 23/06/2009, 14h51
  2. Réponses: 1
    Dernier message: 23/03/2009, 13h48
  3. comment gérer plusieurs locks sur une table?
    Par charluber dans le forum Oracle
    Réponses: 4
    Dernier message: 18/04/2006, 21h28
  4. [MFC] Comment gérer la priorité d'une application
    Par Philippe320 dans le forum MFC
    Réponses: 4
    Dernier message: 04/04/2006, 13h56
  5. [C#/SQL] Comment gérer les exceptions d'une Procédure stockée ?
    Par thomas_strass dans le forum Accès aux données
    Réponses: 10
    Dernier message: 06/07/2005, 10h40

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo