KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.nextime.ion.backoffice.action.content;
2
3 import java.io.IOException JavaDoc;
4 import java.util.Vector 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.ActionErrors;
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.backoffice.action.BaseAction;
15 import org.nextime.ion.backoffice.bean.SectionTypes;
16 import org.nextime.ion.backoffice.bean.TypeBean;
17 import org.nextime.ion.backoffice.exception.BackofficeSecurityException;
18 import org.nextime.ion.backoffice.form.CreatePublicationForm;
19 import org.nextime.ion.backoffice.security.SecurityManagerImpl;
20 import org.nextime.ion.commons.PublicationSorter;
21 import org.nextime.ion.framework.business.Publication;
22 import org.nextime.ion.framework.business.Section;
23 import org.nextime.ion.framework.business.TypePublication;
24 import org.nextime.ion.framework.business.User;
25 import org.nextime.ion.framework.helper.IdGenerator;
26 import org.nextime.ion.framework.mapping.Mapping;
27 import org.nextime.ion.framework.workflow.Workflow;
28
29 public class CreatePublicationAction extends BaseAction {
30
31     public ActionForward perform(
32         ActionMapping mapping,
33         ActionForm form,
34         HttpServletRequest JavaDoc request,
35         HttpServletResponse JavaDoc response)
36         throws IOException JavaDoc, ServletException JavaDoc {
37
38         // check if user is correctly logged
39
checkUser(request);
40
41         // check if this action is allowed
42
try {
43             Mapping.begin();
44             if (!new SecurityManagerImpl()
45                 .canCreatePublication(
46                     Section.getInstance(request.getParameter("id").toString()),
47                     User.getInstance(
48                         request.getSession().getAttribute("userLogin")
49                             + ""))) {
50                 throw new Exception JavaDoc();
51             }
52         } catch (Exception JavaDoc e) {
53             throw new BackofficeSecurityException();
54         } finally {
55             Mapping.rollback();
56         }
57
58         // get the form
59
CreatePublicationForm sform = (CreatePublicationForm) form;
60         ActionErrors errors = sform.myValidate(request);
61
62         // user need cancel
63
if (request.getParameter("cancel") != null) {
64             // Forward to the next page
65
return (mapping.findForward("cancel"));
66         }
67
68         // retrieve section id
69
String JavaDoc id = request.getParameter("id").toString();
70
71         // fill data | first time
72
if (sform.getName() == null) {
73             try {
74                 Mapping.begin();
75
76                 Section section = Section.getInstance(id);
77                 String JavaDoc template = section.getMetaData("template") + "";
78                 TypeBean type = SectionTypes.getSectionBean(servlet, template, request);
79
80                 Vector JavaDoc types = new Vector JavaDoc();
81                 for (int t = 0; t < type.getPublicationTypes().size(); t++) {
82                     try {
83                         types.add(
84                             TypePublication.getInstance(
85                                 type.getPublicationTypes().get(t) + ""));
86                     } catch (Exception JavaDoc e) {
87                         e.printStackTrace();
88                     }
89                 }
90
91                 request.setAttribute("types", types);
92                 request.setAttribute("workflows", Workflow.listTypes());
93
94                 Mapping.rollback();
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
109                 Section section = Section.getInstance(id);
110                 String JavaDoc template = section.getMetaData("template") + "";
111                 TypeBean type = SectionTypes.getSectionBean(servlet, template, request);
112
113                 Vector JavaDoc types = new Vector JavaDoc();
114                 for (int t = 0; t < type.getPublicationTypes().size(); t++) {
115                     try {
116                         types.add(
117                             TypePublication.getInstance(
118                                 type.getPublicationTypes().get(t) + ""));
119                     } catch (Exception JavaDoc e) {
120                         e.printStackTrace();
121                     }
122                 }
123
124                 request.setAttribute("types", types);
125
126                 request.setAttribute(ERROR_KEY, errors);
127
128                 Mapping.rollback();
129             } catch (Exception JavaDoc e) {
130                 Mapping.rollback();
131                 throw new ServletException JavaDoc(e);
132             }
133
134             // Forward to the view page
135
return (mapping.findForward("view"));
136         }
137
138         // all it's ok : create publication
139
try {
140             Mapping.begin();
141
142             Section section = Section.getInstance(id);
143             String JavaDoc newId = IdGenerator.nextPublicationId();
144             TypePublication type = TypePublication.getInstance(sform.getType());
145             Publication publi =
146                 Publication.create(
147                     User.getInstance(
148                         request.getSession().getAttribute("userLogin") + ""),
149                     newId,
150                     type,
151                     section.getMetaData("workflow") + "");
152             section.addPublication(publi);
153             publi.setMetaData("name", sform.getName());
154             PublicationSorter.initPublication(publi, section);
155             request.setAttribute("id", newId);
156             request.setAttribute(
157                 "version",
158                 publi.getLastVersion().getVersion() + "");
159
160             Mapping.commit();
161         } catch (Exception JavaDoc e) {
162             Mapping.rollback();
163             throw new ServletException JavaDoc(e);
164         }
165
166         // Forward to the next page
167
return (mapping.findForward("ok"));
168     }
169
170 }
171
Popular Tags