KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > nextime > ion > admin > action > publi > EditPubliAction


1 package org.nextime.ion.admin.action.publi;
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
10 import org.apache.struts.action.Action;
11 import org.apache.struts.action.ActionForm;
12 import org.apache.struts.action.ActionForward;
13 import org.apache.struts.action.ActionMapping;
14 import org.nextime.ion.admin.form.PubliForm;
15 import org.nextime.ion.framework.business.Category;
16 import org.nextime.ion.framework.business.Publication;
17 import org.nextime.ion.framework.business.Section;
18 import org.nextime.ion.framework.business.TypePublication;
19 import org.nextime.ion.framework.business.User;
20 import org.nextime.ion.framework.business.impl.PublicationImpl;
21 import org.nextime.ion.framework.mapping.Mapping;
22 import org.nextime.ion.framework.workflow.Workflow;
23 import org.nextime.ion.framework.workflow.WorkflowStep;
24
25 public class EditPubliAction extends Action {
26
27     public ActionForward perform(
28         ActionMapping mapping,
29         ActionForm form,
30         HttpServletRequest JavaDoc request,
31         HttpServletResponse JavaDoc response)
32         throws IOException JavaDoc, ServletException JavaDoc {
33
34         // pour situer la vue
35
request.setAttribute("view", "publi");
36
37         if (request.getParameter("id") != null)
38             request.setAttribute("id", request.getParameter("id"));
39         String JavaDoc id = request.getAttribute("id") + "";
40         int version = 1;
41         if( request.getParameter("version") != null ) {
42             version = Integer.parseInt(request.getParameter("version"));
43         }
44         request.setAttribute("version",version+"");
45
46         try {
47
48             // pour remplir les listes select
49
Mapping.begin();
50             request.setAttribute("sectionList", Section.listAll());
51             request.setAttribute("categoryList", Category.listAll());
52             request.setAttribute("userList", User.listAll());
53             request.setAttribute("typeList", TypePublication.listAll());
54             request.setAttribute("workflowList", Workflow.listTypes());
55             request.setAttribute("versions", Publication.getInstance(id).getVersions());
56             Mapping.rollback();
57
58             if (request.getParameter("editSubmit") == null || "changeVersion".equals(request.getParameter("editSubmit"))) {
59                 Mapping.begin();
60
61                 // initialisation du formulaire
62
Publication u = Publication.getInstance(id);
63                 PubliForm f = (PubliForm) form;
64                 f.setVersion(version+"");
65                 f.setId(u.getId());
66                 if (u.getVersion(version).getAuthor() != null) {
67                     f.setAuthor(u.getVersion(version).getAuthor().getLogin());
68                 }
69                 f.setType(u.getType().getId());
70                 f.setSections(u.getSectionsIds());
71                 f.setCategories(u.getCategoriesIds());
72                 request.setAttribute(
73                     "metaData",
74                     ((PublicationImpl) u).getMetaData());
75                 request.setAttribute("data", ((PublicationImpl) u).getVersion(version).getDataAsString());
76                 // workflow management
77
String JavaDoc workflowUser = ((User)User.listAll().firstElement()).getLogin();
78                 f.setWorkflowUser(workflowUser);
79                 f.setWorkflow(u.getVersion(version).getWorkflow(User.getInstance(workflowUser)).getId()+" ("+u.getVersion(version).getWorkflow(User.getInstance(workflowUser)).getWorkflowType()+")");
80                 request.setAttribute("actions", u.getVersion(version).getWorkflow(User.getInstance(workflowUser)).getAvailableActions());
81                 request.setAttribute("permissions", u.getVersion(version).getWorkflow(User.getInstance(workflowUser)).getPermissions());
82                 request.setAttribute("steps", u.getVersion(version).getWorkflow(User.getInstance(workflowUser)).getCurrentSteps() );
83                 
84                 try {
85                     System.out.println(((WorkflowStep)u.getVersion(version).getWorkflow(User.getInstance(f.getWorkflowUser())).getCurrentSteps().firstElement()).getName());
86                 } catch( Exception JavaDoc e ) {
87                     e.printStackTrace();
88                 }
89                 
90                 Mapping.commit();
91                 return new ActionForward(mapping.getInput());
92             }
93
94             // effectue les modifications
95
Mapping.begin();
96             PubliForm f = (PubliForm) form;
97             version = Integer.parseInt(f.getVersion());
98             Publication u = Publication.getInstance(id);
99 // if (!f.getAuthor().equals("_NULL_")) {
100
// u.setAuthor(User.getInstance(f.getAuthor()));
101
// } else {
102
// u.setAuthor(null);
103
// }
104
if (!f.getType().equals(u.getType().getId())) {
105                 u.getVersion(version).resetData();
106             }
107             u.changeType(TypePublication.getInstance(f.getType()));
108             String JavaDoc[] g = f.getSections();
109             u.resetSections();
110             if (g != null) {
111                 for (int i = 0; i < g.length; i++) {
112                     u.addSection(Section.getInstance(g[i]));
113                 }
114             }
115             g = f.getCategories();
116             if (g != null) {
117                 for (int i = 0; i < g.length; i++) {
118                     u.addCategory(Category.getInstance(g[i]));
119                 }
120             }
121             // workflow management
122
if( !request.getParameter("workflowAction").equals("NULL") ) {
123                 u.getVersion(version).getWorkflow(User.getInstance(f.getWorkflowUser())).getAction(Integer.parseInt(request.getParameter("workflowAction"))).execute();
124             }
125             request.setAttribute("actions", u.getVersion(version).getWorkflow(User.getInstance(f.getWorkflowUser())).getAvailableActions());
126             request.setAttribute("permissions", u.getVersion(version).getWorkflow(User.getInstance(f.getWorkflowUser())).getPermissions());
127             request.setAttribute("steps", u.getVersion(version).getWorkflow(User.getInstance(f.getWorkflowUser())).getCurrentSteps() );
128
129             
130             // *********************************************************
131
// Gestion des données
132

133             String JavaDoc data = request.getParameter("data");
134             u.getVersion(version).setDataAsString(data);
135
136             request.setAttribute("data", ((PublicationImpl) u).getVersion(version).getDataAsString());
137
138             // *********************************************************
139

140             // *********************************************************
141
// les metaData sont gérées separement car je n'ai
142
// aucune idée du nombre et du nom des champs qu'il va
143
// y avoir.
144
// cette partie est un peu bancale mais en général dans un
145
// veritable backoffice le nombre de champs composant le
146
// formulaire est connu et on peut les gérer comme ce que j'ai
147
// fait plus haut ( login, password ... )
148
// CETTE PARTIE N'EST DONC PAS A REPRENDRE.
149

150             // sauve les metaData
151
Enumeration JavaDoc ps = request.getParameterNames();
152             while (ps.hasMoreElements()) {
153                 String JavaDoc name = ps.nextElement() + "";
154                 if (name.startsWith("META_")) {
155                     name = name.substring(5);
156                     u.setMetaData(name, request.getParameter("META_" + name));
157                 }
158             }
159             // efface la metaData si il y a besoin
160
String JavaDoc mtd = request.getParameter("metaToDelete");
161             if ((mtd + "").trim().equals(""))
162                 mtd = null;
163             if (mtd != null) {
164                 u.removeMetaData(mtd);
165                 request.setAttribute(
166                     "metaData",
167                     ((PublicationImpl) u).getMetaData());
168             }
169             // ajoute la metaData si il y a besoin
170
String JavaDoc mtd2 = request.getParameter("newMETA");
171             if ((mtd2 + "").trim().equals(""))
172                 mtd2 = null;
173             if (mtd2 != null) {
174                 u.setMetaData(mtd2, "");
175                 request.setAttribute(
176                     "metaData",
177                     ((PublicationImpl) u).getMetaData());
178             }
179             Mapping.commit();
180
181             // si on a simplement effacé ou ajouté une metadonnée, on retourne au fromulaire
182
if (mtd != null || mtd2 != null) {
183                 return new ActionForward(mapping.getInput());
184             }
185
186             // si on a simplement changé le type, on retourne au fromulaire
187
if (request.getParameter("editSubmit").equals("temp")) {
188                 return new ActionForward(mapping.getInput());
189             }
190
191             // ***********************************************************
192

193         } catch (Exception JavaDoc e) {
194             Mapping.rollback();
195             request.setAttribute("error", e.getMessage());
196             return new ActionForward(mapping.getInput());
197         }
198
199         return mapping.findForward("success");
200     }
201
202 }
Popular Tags