KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > nextime > ion > admin > action > section > EditSectionAction


1 package org.nextime.ion.admin.action.section;
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.*;
15 import org.nextime.ion.framework.business.Group;
16 import org.nextime.ion.framework.business.*;
17 import org.nextime.ion.framework.business.impl.*;
18 import org.nextime.ion.framework.mapping.Mapping;
19
20 public class EditSectionAction 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", "section");
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("sectionList", Section.listAll());
41             Mapping.commit();
42
43             if (request.getParameter("editSubmit") == null) {
44                 Mapping.begin();
45
46                 // initialisation du formulaire
47
Section u = Section.getInstance(id);
48                 SectionForm f = (SectionForm) form;
49                 f.setId(u.getId());
50                 f.setParent(u.getParent().getId());
51                 request.setAttribute("metaData", ((SectionImpl) u).getMetaData());
52
53                 Mapping.commit();
54                 return new ActionForward( mapping.getInput() );
55             }
56
57             // effectu les modifications
58
Mapping.begin();
59             SectionForm f = (SectionForm) form;
60             Section u = Section.getInstance(id);
61             Section parent = null;
62             if( !f.getParent().equals("_NULL_") ) {
63                 parent = Section.getInstance(f.getParent());
64             }
65             if( parent != u.getParent() ) {
66                 u.changeParent(parent);
67             }
68
69             // *********************************************************
70
// les metaData sont gérées separement car je n'ai
71
// aucune idée du nombre et du nom des champs qu'il va
72
// y avoir.
73
// cette partie est un peu bancale mais en général dans un
74
// veritable backoffice le nombre de champs composant le
75
// formulaire est connu et on peut les gérer comme ce que j'ai
76
// fait plus haut ( login, password ... )
77
// CETTE PARTIE N'EST DONC PAS A REPRENDRE.
78

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

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