KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > nextime > ion > admin > action > user > EditUserAction


1 package org.nextime.ion.admin.action.user;
2
3 import java.io.IOException JavaDoc;
4 import java.util.Enumeration JavaDoc;
5
6 import javax.servlet.ServletException JavaDoc;
7 import javax.servlet.http.HttpServletRequest JavaDoc;
8 import javax.servlet.http.HttpServletResponse JavaDoc;
9 import org.apache.struts.action.Action;
10 import org.apache.struts.action.ActionForm;
11 import org.apache.struts.action.ActionForward;
12 import org.apache.struts.action.ActionMapping;
13
14 import org.nextime.ion.admin.form.UserForm;
15 import org.nextime.ion.framework.business.Group;
16 import org.nextime.ion.framework.business.User;
17 import org.nextime.ion.framework.business.impl.UserImpl;
18 import org.nextime.ion.framework.mapping.Mapping;
19
20 public class EditUserAction extends Action {
21
22     public ActionForward perform(
23         ActionMapping mapping,
24         ActionForm form,
25         HttpServletRequest JavaDoc request,
26         HttpServletResponse JavaDoc response)
27         throws IOException JavaDoc, ServletException JavaDoc {
28
29         // pour situer la vue
30
request.setAttribute("view", "user");
31
32         if( request.getParameter("id") != null )
33             request.setAttribute("id", request.getParameter("id"));
34         String JavaDoc id = request.getAttribute("id")+"";
35
36         try {
37
38             // pour remplir les listes select
39
Mapping.begin();
40             request.setAttribute("groupList", Group.listAll());
41             Mapping.commit();
42
43             if (request.getParameter("editSubmit") == null) {
44                 Mapping.begin();
45
46                 // initialisation du formulaire
47
User u = User.getInstance(id);
48                 UserForm f = (UserForm) form;
49                 f.setLogin(u.getLogin());
50                 f.setPassword(u.getPassword());
51                 f.setGroups(u.getGroupsIds());
52                 request.setAttribute("metaData", ((UserImpl) u).getMetaData());
53
54                 Mapping.commit();
55                 return new ActionForward( mapping.getInput() );
56             }
57
58             // effectu les modifications
59
Mapping.begin();
60             UserForm f = (UserForm) form;
61             User u = User.getInstance(id);
62             u.setPassword(f.getPassword());
63             String JavaDoc[] g = f.getGroups();
64             u.resetGroups();
65             if (g != null) {
66                 for (int i = 0; i < g.length; i++) {
67                     u.addGroup(Group.getInstance(g[i]));
68                 }
69             }
70
71             // *********************************************************
72
// les metaData sont gérées separement car je n'ai
73
// aucune idée du nombre et du nom des champs qu'il va
74
// y avoir.
75
// cette partie est un peu bancale mais en général dans un
76
// veritable backoffice le nombre de champs composant le
77
// formulaire est connu et on peut les gérer comme ce que j'ai
78
// fait plus haut ( login, password ... )
79
// CETTE PARTIE N'EST DONC PAS A REPRENDRE.
80

81             // sauve les metaData
82
Enumeration JavaDoc ps = request.getParameterNames();
83             while (ps.hasMoreElements()) {
84                 String JavaDoc name = ps.nextElement() + "";
85                 if (name.startsWith("META_")) {
86                     name = name.substring(5);
87                     u.setMetaData(name, request.getParameter("META_" + name));
88                 }
89             }
90             // efface la metaData si il y a besoin
91
String JavaDoc mtd = request.getParameter("metaToDelete");
92             if ((mtd + "").trim().equals(""))
93                 mtd = null;
94             if (mtd != null) {
95                 u.removeMetaData(mtd);
96                 request.setAttribute("metaData", ((UserImpl) u).getMetaData());
97             }
98             // ajoute la metaData si il y a besoin
99
String JavaDoc mtd2 = request.getParameter("newMETA");
100             if ((mtd2 + "").trim().equals(""))
101                 mtd2 = null;
102             if (mtd2 != null) {
103                 u.setMetaData(mtd2, "");
104                 request.setAttribute("metaData", ((UserImpl) u).getMetaData());
105             }
106             Mapping.commit();
107
108             // si on a simplement effacé ou ajouté une metadonnée, on retourne au fromulaire
109
if (mtd != null || mtd2 != null) {
110                 return new ActionForward( mapping.getInput() );
111             }
112
113             // ***********************************************************
114

115         } catch (Exception JavaDoc e) {
116             Mapping.rollback();
117             request.setAttribute("error", e.getMessage());
118             return new ActionForward( mapping.getInput() );
119         }
120
121         return mapping.findForward("success");
122     }
123
124 }
Popular Tags