KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > nextime > ion > backoffice > action > content > EditPublicationAction


1 package org.nextime.ion.backoffice.action.content;
2
3 import java.io.IOException JavaDoc;
4 import java.text.SimpleDateFormat JavaDoc;
5 import javax.servlet.ServletException JavaDoc;
6 import javax.servlet.http.HttpServletRequest JavaDoc;
7 import javax.servlet.http.HttpServletResponse JavaDoc;
8
9 import org.apache.struts.action.ActionErrors;
10 import org.apache.struts.action.ActionForm;
11 import org.apache.struts.action.ActionForward;
12 import org.apache.struts.action.ActionMapping;
13 import org.nextime.ion.backoffice.action.BaseAction;
14 import org.nextime.ion.backoffice.form.EditPublicationForm;
15 import org.nextime.ion.backoffice.exception.BackofficeSecurityException;
16 import org.nextime.ion.backoffice.security.SecurityManagerImpl;
17
18 import org.nextime.ion.framework.business.Publication;
19 import org.nextime.ion.framework.business.User;
20 import org.nextime.ion.framework.locale.LocaleList;
21 import org.nextime.ion.framework.mapping.Mapping;
22
23 public class EditPublicationAction extends BaseAction {
24
25     public ActionForward perform(
26         ActionMapping mapping,
27         ActionForm form,
28         HttpServletRequest JavaDoc request,
29         HttpServletResponse JavaDoc response)
30         throws IOException JavaDoc, ServletException JavaDoc {
31
32         // check if user is correctly logged
33
checkUser(request);
34
35         // get the form
36
EditPublicationForm sform = (EditPublicationForm) form;
37         ActionErrors errors = sform.myValidate(request);
38         
39         // set languages list
40
request.setAttribute("locales", LocaleList.getInstance().getLocales() );
41         request.setAttribute("defaultLocale", LocaleList.getInstance().getLocales().iterator().next() );
42         
43         // retrieve id
44
String JavaDoc id =
45             (request.getAttribute("id") != null)
46                 ? request.getAttribute("id").toString()
47                 : request.getParameter("id").toString();
48
49         // retrieve version
50
int version =
51             Integer.parseInt(
52                 (request.getAttribute("version") != null)
53                     ? request.getAttribute("version").toString()
54                     : request.getParameter("version").toString());
55         request.setAttribute("version", version + "");
56         
57         // check if publication can be edited
58
try {
59             Mapping.begin();
60             if (!new SecurityManagerImpl()
61                 .canEditPublication(
62                     Publication.getInstance(id),
63                     version,
64                     User.getInstance(
65                         request.getSession().getAttribute("userLogin")
66                             + ""))) {
67                 throw new Exception JavaDoc();
68             }
69         } catch (Exception JavaDoc e) {
70             throw new BackofficeSecurityException();
71         } finally {
72             Mapping.rollback();
73         }
74
75         // user need cancel
76
if (request.getParameter("cancel") != null) {
77             // Forward to the next page
78
return (mapping.findForward("cancel"));
79         }
80
81         // fill data | first time
82
if (request.getParameter("itsOk") == null) {
83             try {
84                 Mapping.begin();
85                 Publication publication = Publication.getInstance(id);
86                 Mapping.rollback();
87
88                 request.setAttribute("publication", publication);
89
90                 SimpleDateFormat JavaDoc formatter = new SimpleDateFormat JavaDoc("dd/MM/yyyy");
91                 sform.setDate(formatter.format(publication.getDate()));
92                 sform.setData(
93                     publication.getVersion(version).getDataAsString());
94
95             } catch (Exception JavaDoc e) {
96                 Mapping.rollback();
97                 throw new ServletException JavaDoc(e);
98             }
99
100             // Forward to the view page
101
return (mapping.findForward("view"));
102         }
103
104         // fill data | errors
105
if (errors.size() > 0) {
106             try {
107                 Mapping.begin();
108                 Publication publication = Publication.getInstance(id);
109                 request.setAttribute("publication", publication);
110                 request.setAttribute(ERROR_KEY, errors);
111                 Mapping.rollback();
112             } catch (Exception JavaDoc e) {
113                 Mapping.rollback();
114                 throw new ServletException JavaDoc(e);
115             }
116
117             // Forward to the view page
118
return (mapping.findForward("view"));
119         }
120
121         // all it's ok : update publication
122
try {
123             Mapping.begin();
124             Publication publication = Publication.getInstance(id);
125             publication.getVersion(version).setDataAsString(sform.getData());
126             Mapping.commit();
127         } catch (Exception JavaDoc e) {
128             Mapping.rollback();
129             throw new ServletException JavaDoc(e);
130         }
131
132         // Forward to the next page
133
return (mapping.findForward("ok"));
134     }
135
136 }
137
Popular Tags