KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > blandware > atleap > webapp > action > testimonials > CreateTestimonialAction


1 /*
2  * Copyright 2004 Blandware (http://www.blandware.com)
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package com.blandware.atleap.webapp.action.testimonials;
17
18 import com.blandware.atleap.common.Constants;
19 import com.blandware.atleap.common.TestimonialModuleConstants;
20 import com.blandware.atleap.common.parsers.html.HTMLPlainTextExtractor;
21 import com.blandware.atleap.common.util.ConvertUtil;
22 import com.blandware.atleap.model.testimonials.Testimonial;
23 import com.blandware.atleap.search.SearchManager;
24 import com.blandware.atleap.service.exception.BeanAlreadyExistsException;
25 import com.blandware.atleap.service.testimonials.TestimonialManager;
26 import com.blandware.atleap.webapp.action.core.BaseAction;
27 import com.blandware.atleap.webapp.form.TestimonialForm;
28 import com.blandware.atleap.webapp.util.core.WebappUtil;
29 import com.blandware.atleap.webapp.util.core.WebappConstants;
30 import com.blandware.atleap.webapp.util.core.LocaleUtil;
31 import com.blandware.atleap.webapp.util.testimonials.TestimonialModuleWebConstants;
32 import org.apache.struts.action.ActionForm;
33 import org.apache.struts.action.ActionForward;
34 import org.apache.struts.action.ActionMapping;
35 import org.apache.struts.action.ActionMessage;
36 import org.apache.struts.action.ActionMessages;
37
38 import javax.servlet.http.HttpServletRequest JavaDoc;
39 import javax.servlet.http.HttpServletResponse JavaDoc;
40 import java.io.ByteArrayInputStream JavaDoc;
41 import java.io.InputStream JavaDoc;
42 import java.util.*;
43
44 /**
45  * <p>Creates new testimonial
46  * </p>
47  * <p><a HREF="CreateTestimonialAction.java.htm"><i>View Source</i></a></p>
48  * <p/>
49  *
50  * @author Andrey Grebnev <a HREF="mailto:andrey.grebnev@blandware.com">&lt;andrey.grebnev@blandware.com&gt;</a>
51  * @version $Revision: 1.15 $ $Date: 2006/03/10 17:10:32 $
52  * @struts.action path="/testimonials/create"
53  * name="testimonialForm"
54  * scope="request"
55  * input="inputForward"
56  * validate="true"
57  * roles="testimonials-item-create"
58  * @struts.action-forward name="inputForward"
59  * path=".testimonials.createTestimonial"
60  * @struts.action-forward name="listTestimonials"
61  * path="/testimonials/list.do"
62  * redirect="true"
63  * @struts.action-forward name="callCreateTestimonial"
64  * path=".testimonials.createTestimonial"
65  */

66 public final class CreateTestimonialAction extends BaseAction {
67     /**
68      * @param mapping The ActionMapping used to select this instance
69      * @param form The optional ActionForm bean for this request (if any)
70      * @param request The HTTP request we are proceeding
71      * @param response The HTTP response we are creating
72      * @return an ActionForward instance describing where and how
73      * control should be forwarded, or null if response
74      * has already been completed
75      */

76     public ActionForward execute(ActionMapping mapping, ActionForm form,
77                                  HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws Exception JavaDoc {
78
79         TestimonialForm testimonialForm = (TestimonialForm) form;
80         String JavaDoc localesMode = testimonialForm.getLocalesMode();
81
82         if ( isBackPressed(request) ) {
83             // First check, may be session has been invalidated and we need to
84
// fill content locales again
85
if (request.getSession().getAttribute(WebappConstants.CONTENT_LOCALES_COLLECTION_KEY) == null) {
86                 List contentLocales = LocaleUtil.getInstance(servlet.getServletContext()).getAvailableLocales();
87                 request.getSession().setAttribute(WebappConstants.CONTENT_LOCALES_COLLECTION_KEY, contentLocales);
88             }
89
90             // check 'check all' ckeckbox if needed
91
testimonialForm.setAllCheckedFlagForBodyCheckboxes(request, localesMode);
92
93             return mapping.findForward("inputForward");
94         }
95
96         if ( isCancelled(request) ) {
97             return mapping.findForward("listTestimonials");
98         }
99
100         if ( request.getSession().getAttribute(TestimonialModuleWebConstants.TESTIMONIAL_SUBMITTED_ACTION_KEY) != null ) {
101             return mapping.findForward("listTestimonials");
102         }
103         request.getSession().setAttribute(TestimonialModuleWebConstants.TESTIMONIAL_SUBMITTED_ACTION_KEY, "create");
104
105         Testimonial testimonial = new Testimonial();
106         WebappUtil.copyProperties(testimonial, testimonialForm, request);
107         testimonial.setActive(Boolean.TRUE);
108
109         Map bodyMap = testimonialForm.getBodyMap();
110         Map bodyCheckedBoxes = testimonialForm.getBodyCheckedBoxes();
111         for (Iterator i = bodyMap.entrySet().iterator(); i.hasNext();) {
112             Map.Entry entry = (Map.Entry) i.next();
113             String JavaDoc localeIdentifier = (String JavaDoc) entry.getKey();
114             if (bodyCheckedBoxes.get(localeIdentifier) == null) {
115                 // if this locale is not checked, remove body
116
bodyMap.put(localeIdentifier, "");
117             }
118         }
119
120         testimonial.setTitle(testimonialForm.getTitleMap());
121         testimonial.setBody(bodyMap);
122         TestimonialManager testimonialManager = (TestimonialManager) getBean(TestimonialModuleConstants.TESTIMONIAL_MANAGER_BEAN);
123
124         // get all refs
125
HTMLPlainTextExtractor extractor = new HTMLPlainTextExtractor();
126         Set refs = new HashSet();
127         for ( Iterator i = bodyMap.entrySet().iterator(); i.hasNext(); ) {
128             Map.Entry entry = (Map.Entry) i.next();
129             String JavaDoc body = (String JavaDoc) entry.getValue();
130             InputStream JavaDoc bodyIS = new ByteArrayInputStream JavaDoc(ConvertUtil.convertToByteArray(body));
131             refs.addAll(extractor.extractAllRefs(bodyIS, Constants.DEFAULT_ENCODING));
132         }
133
134         Map linkedObjects = WebappUtil.mapObjectsToRefs(refs, request.getSession().getServletContext(), request.getContextPath());
135
136         Long JavaDoc testimonialId = null;
137         String JavaDoc uri = null;
138
139         try {
140             // we need an ID to create URI of testimonial, so we must first create testimonial, then set URI and update testimonial
141
testimonialId = testimonialManager.createTestimonial(testimonial, linkedObjects);
142
143             uri = TestimonialModuleWebConstants.TESTIMONIAL_URI_PREFIX + "/item" + testimonialId;
144             testimonial.setUri(uri);
145             testimonialManager.updateTestimonial(testimonial, linkedObjects);
146
147             if ( testimonial.getActive().booleanValue() ) {
148                 // index testimonial item
149
SearchManager searchManager = SearchManager.getInstance(request.getSession().getServletContext());
150                 searchManager.reIndexPage(testimonial, request);
151             }
152
153         } catch ( BeanAlreadyExistsException e ) {
154             // testimonial already exists
155

156             // check 'check all' ckeckbox if needed
157
testimonialForm.setAllCheckedFlagForBodyCheckboxes(request, localesMode);
158
159             ActionMessages errors = new ActionMessages();
160             errors.add("testimonialAlreadyExists", new ActionMessage("testimonials.errors.alreadyExists", uri));
161             saveErrors(request, errors);
162             saveToken(request);
163             return mapping.getInputForward();
164         }
165         return mapping.findForward("listTestimonials");
166     }
167 }
Popular Tags