1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portlet.journal.action;
21  
22  import com.liferay.portal.kernel.servlet.SessionErrors;
23  import com.liferay.portal.kernel.util.Constants;
24  import com.liferay.portal.kernel.util.ParamUtil;
25  import com.liferay.portal.kernel.util.StringUtil;
26  import com.liferay.portal.kernel.util.Validator;
27  import com.liferay.portal.security.auth.PrincipalException;
28  import com.liferay.portal.service.ServiceContext;
29  import com.liferay.portal.service.ServiceContextFactory;
30  import com.liferay.portal.struts.PortletAction;
31  import com.liferay.portal.theme.ThemeDisplay;
32  import com.liferay.portal.util.WebKeys;
33  import com.liferay.portlet.ActionRequestImpl;
34  import com.liferay.portlet.PortletURLImpl;
35  import com.liferay.portlet.journal.DuplicateStructureIdException;
36  import com.liferay.portlet.journal.NoSuchStructureException;
37  import com.liferay.portlet.journal.RequiredStructureException;
38  import com.liferay.portlet.journal.StructureDescriptionException;
39  import com.liferay.portlet.journal.StructureIdException;
40  import com.liferay.portlet.journal.StructureInheritanceException;
41  import com.liferay.portlet.journal.StructureNameException;
42  import com.liferay.portlet.journal.StructureXsdException;
43  import com.liferay.portlet.journal.model.JournalStructure;
44  import com.liferay.portlet.journal.service.JournalStructureServiceUtil;
45  import com.liferay.portlet.journal.util.JournalUtil;
46  
47  import javax.portlet.ActionRequest;
48  import javax.portlet.ActionResponse;
49  import javax.portlet.PortletConfig;
50  import javax.portlet.PortletRequest;
51  import javax.portlet.RenderRequest;
52  import javax.portlet.RenderResponse;
53  import javax.portlet.WindowState;
54  
55  import org.apache.struts.action.ActionForm;
56  import org.apache.struts.action.ActionForward;
57  import org.apache.struts.action.ActionMapping;
58  
59  /**
60   * <a href="EditStructureAction.java.html"><b><i>View Source</i></b></a>
61   *
62   * @author Brian Wing Shun Chan
63   * @author Raymond Augé
64   *
65   */
66  public class EditStructureAction extends PortletAction {
67  
68      public void processAction(
69              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
70              ActionRequest actionRequest, ActionResponse actionResponse)
71          throws Exception {
72  
73          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
74  
75          JournalStructure structure = null;
76  
77          try {
78              if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
79                  structure = updateStructure(actionRequest);
80              }
81              else if (cmd.equals(Constants.DELETE)) {
82                  deleteStructures(actionRequest);
83              }
84  
85              if (Validator.isNotNull(cmd)) {
86                  String redirect = ParamUtil.getString(
87                      actionRequest, "redirect");
88  
89                  if (structure != null) {
90                      boolean saveAndContinue = ParamUtil.getBoolean(
91                          actionRequest, "saveAndContinue");
92  
93                      if (saveAndContinue) {
94                          redirect = getSaveAndContinueRedirect(
95                              portletConfig, actionRequest, structure, redirect);
96                      }
97                  }
98  
99                  sendRedirect(actionRequest, actionResponse, redirect);
100             }
101         }
102         catch (Exception e) {
103             if (e instanceof NoSuchStructureException ||
104                 e instanceof PrincipalException) {
105 
106                 SessionErrors.add(actionRequest, e.getClass().getName());
107 
108                 setForward(actionRequest, "portlet.journal.error");
109             }
110             else if (e instanceof DuplicateStructureIdException ||
111                      e instanceof RequiredStructureException ||
112                      e instanceof StructureDescriptionException ||
113                      e instanceof StructureIdException ||
114                      e instanceof StructureInheritanceException ||
115                      e instanceof StructureNameException ||
116                      e instanceof StructureXsdException) {
117 
118                 SessionErrors.add(actionRequest, e.getClass().getName());
119 
120                 if (e instanceof RequiredStructureException) {
121                     actionResponse.sendRedirect(
122                         ParamUtil.getString(actionRequest, "redirect"));
123                 }
124             }
125             else {
126                 throw e;
127             }
128         }
129     }
130 
131     public ActionForward render(
132             ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
133             RenderRequest renderRequest, RenderResponse renderResponse)
134         throws Exception {
135 
136         try {
137             String cmd = ParamUtil.getString(renderRequest, Constants.CMD);
138 
139             if (!cmd.equals(Constants.ADD)) {
140                 ActionUtil.getStructure(renderRequest);
141             }
142         }
143         catch (NoSuchStructureException nsse) {
144 
145             // Let this slide because the user can manually input a structure id
146             // for a new structure that does not yet exist.
147 
148         }
149         catch (Exception e) {
150             if (//e instanceof NoSuchStructureException ||
151                 e instanceof PrincipalException) {
152 
153                 SessionErrors.add(renderRequest, e.getClass().getName());
154 
155                 return mapping.findForward("portlet.journal.error");
156             }
157             else {
158                 throw e;
159             }
160         }
161 
162         return mapping.findForward(
163             getForward(renderRequest, "portlet.journal.edit_structure"));
164     }
165 
166     protected void deleteStructures(ActionRequest actionRequest)
167         throws Exception {
168 
169         long groupId = ParamUtil.getLong(actionRequest, "groupId");
170 
171         String[] deleteStructureIds = StringUtil.split(
172             ParamUtil.getString(actionRequest, "deleteStructureIds"));
173 
174         for (int i = 0; i < deleteStructureIds.length; i++) {
175             JournalStructureServiceUtil.deleteStructure(
176                 groupId, deleteStructureIds[i]);
177 
178             JournalUtil.removeRecentStructure(
179                 actionRequest, deleteStructureIds[i]);
180         }
181     }
182 
183     protected String getSaveAndContinueRedirect(
184             PortletConfig portletConfig, ActionRequest actionRequest,
185             JournalStructure structure, String redirect)
186         throws Exception {
187 
188         ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
189             WebKeys.THEME_DISPLAY);
190 
191         String originalRedirect = ParamUtil.getString(
192             actionRequest, "originalRedirect");
193 
194         PortletURLImpl portletURL = new PortletURLImpl(
195             (ActionRequestImpl)actionRequest, portletConfig.getPortletName(),
196             themeDisplay.getPlid(), PortletRequest.RENDER_PHASE);
197 
198         portletURL.setWindowState(WindowState.MAXIMIZED);
199 
200         portletURL.setParameter("struts_action", "/journal/edit_structure");
201         portletURL.setParameter(Constants.CMD, Constants.UPDATE, false);
202         portletURL.setParameter("redirect", redirect, false);
203         portletURL.setParameter("originalRedirect", originalRedirect, false);
204         portletURL.setParameter(
205             "groupId", String.valueOf(structure.getGroupId()), false);
206         portletURL.setParameter(
207             "structureId", structure.getStructureId(), false);
208 
209         return portletURL.toString();
210     }
211 
212     protected JournalStructure updateStructure(ActionRequest actionRequest)
213         throws Exception {
214 
215         String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
216 
217         long groupId = ParamUtil.getLong(actionRequest, "groupId");
218 
219         String structureId = ParamUtil.getString(actionRequest, "structureId");
220         boolean autoStructureId = ParamUtil.getBoolean(
221             actionRequest, "autoStructureId");
222 
223         String parentStructureId = ParamUtil.getString(
224             actionRequest, "parentStructureId");
225         String name = ParamUtil.getString(actionRequest, "name");
226         String description = ParamUtil.getString(actionRequest, "description");
227         String xsd = ParamUtil.getString(actionRequest, "xsd");
228 
229         ServiceContext serviceContext = ServiceContextFactory.getInstance(
230             JournalStructure.class.getName(), actionRequest);
231 
232         JournalStructure structure = null;
233 
234         if (cmd.equals(Constants.ADD)) {
235 
236             // Add structure
237 
238             structure = JournalStructureServiceUtil.addStructure(
239                 groupId, structureId, autoStructureId, parentStructureId, name,
240                 description, xsd, serviceContext);
241         }
242         else {
243 
244             // Update structure
245 
246             structure = JournalStructureServiceUtil.updateStructure(
247                 groupId, structureId, parentStructureId, name, description,
248                 xsd, serviceContext);
249         }
250 
251         // Recent structures
252 
253         JournalUtil.addRecentStructure(actionRequest, structure);
254 
255         return structure;
256     }
257 
258 }