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.model.Layout;
28  import com.liferay.portal.security.auth.PrincipalException;
29  import com.liferay.portal.struts.PortletAction;
30  import com.liferay.portal.theme.ThemeDisplay;
31  import com.liferay.portal.util.WebKeys;
32  import com.liferay.portlet.ActionRequestImpl;
33  import com.liferay.portlet.PortletURLImpl;
34  import com.liferay.portlet.journal.DuplicateStructureIdException;
35  import com.liferay.portlet.journal.NoSuchStructureException;
36  import com.liferay.portlet.journal.RequiredStructureException;
37  import com.liferay.portlet.journal.StructureDescriptionException;
38  import com.liferay.portlet.journal.StructureIdException;
39  import com.liferay.portlet.journal.StructureNameException;
40  import com.liferay.portlet.journal.StructureXsdException;
41  import com.liferay.portlet.journal.model.JournalStructure;
42  import com.liferay.portlet.journal.service.JournalStructureServiceUtil;
43  import com.liferay.portlet.journal.util.JournalUtil;
44  
45  import javax.portlet.ActionRequest;
46  import javax.portlet.ActionResponse;
47  import javax.portlet.PortletConfig;
48  import javax.portlet.PortletRequest;
49  import javax.portlet.RenderRequest;
50  import javax.portlet.RenderResponse;
51  import javax.portlet.WindowState;
52  
53  import org.apache.struts.action.ActionForm;
54  import org.apache.struts.action.ActionForward;
55  import org.apache.struts.action.ActionMapping;
56  
57  /**
58   * <a href="EditStructureAction.java.html"><b><i>View Source</i></b></a>
59   *
60   * @author Brian Wing Shun Chan
61   *
62   */
63  public class EditStructureAction extends PortletAction {
64  
65      public void processAction(
66              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
67              ActionRequest actionRequest, ActionResponse actionResponse)
68          throws Exception {
69  
70          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
71  
72          JournalStructure structure = null;
73  
74          try {
75              if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
76                  structure = updateStructure(actionRequest);
77              }
78              else if (cmd.equals(Constants.DELETE)) {
79                  deleteStructures(actionRequest);
80              }
81  
82              if (Validator.isNotNull(cmd)) {
83                  String redirect = ParamUtil.getString(
84                      actionRequest, "redirect");
85  
86                  if (structure != null) {
87                      boolean saveAndContinue = ParamUtil.getBoolean(
88                          actionRequest, "saveAndContinue");
89  
90                      if (saveAndContinue) {
91                          redirect = getSaveAndContinueRedirect(
92                              portletConfig, actionRequest, structure, redirect);
93                      }
94                  }
95  
96                  sendRedirect(actionRequest, actionResponse, redirect);
97              }
98          }
99          catch (Exception e) {
100             if (e instanceof NoSuchStructureException ||
101                 e instanceof PrincipalException) {
102 
103                 SessionErrors.add(actionRequest, e.getClass().getName());
104 
105                 setForward(actionRequest, "portlet.journal.error");
106             }
107             else if (e instanceof DuplicateStructureIdException ||
108                      e instanceof RequiredStructureException ||
109                      e instanceof StructureDescriptionException ||
110                      e instanceof StructureIdException ||
111                      e instanceof StructureNameException ||
112                      e instanceof StructureXsdException) {
113 
114                 SessionErrors.add(actionRequest, e.getClass().getName());
115 
116                 if (e instanceof RequiredStructureException) {
117                     actionResponse.sendRedirect(
118                         ParamUtil.getString(actionRequest, "redirect"));
119                 }
120             }
121             else {
122                 throw e;
123             }
124         }
125     }
126 
127     public ActionForward render(
128             ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
129             RenderRequest renderRequest, RenderResponse renderResponse)
130         throws Exception {
131 
132         try {
133             String cmd = ParamUtil.getString(renderRequest, Constants.CMD);
134 
135             if (!cmd.equals(Constants.ADD)) {
136                 ActionUtil.getStructure(renderRequest);
137             }
138         }
139         catch (NoSuchStructureException nsse) {
140 
141             // Let this slide because the user can manually input a structure id
142             // for a new structure that does not yet exist.
143 
144         }
145         catch (Exception e) {
146             if (//e instanceof NoSuchStructureException ||
147                 e instanceof PrincipalException) {
148 
149                 SessionErrors.add(renderRequest, e.getClass().getName());
150 
151                 return mapping.findForward("portlet.journal.error");
152             }
153             else {
154                 throw e;
155             }
156         }
157 
158         return mapping.findForward(
159             getForward(renderRequest, "portlet.journal.edit_structure"));
160     }
161 
162     protected void deleteStructures(ActionRequest actionRequest)
163         throws Exception {
164 
165         long groupId = ParamUtil.getLong(actionRequest, "groupId");
166 
167         String[] deleteStructureIds = StringUtil.split(
168             ParamUtil.getString(actionRequest, "deleteStructureIds"));
169 
170         for (int i = 0; i < deleteStructureIds.length; i++) {
171             JournalStructureServiceUtil.deleteStructure(
172                 groupId, deleteStructureIds[i]);
173 
174             JournalUtil.removeRecentStructure(
175                 actionRequest, deleteStructureIds[i]);
176         }
177     }
178 
179     protected String getSaveAndContinueRedirect(
180             PortletConfig portletConfig, ActionRequest actionRequest,
181             JournalStructure structure, String redirect)
182         throws Exception {
183 
184         ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
185             WebKeys.THEME_DISPLAY);
186 
187         String originalRedirect = ParamUtil.getString(
188             actionRequest, "originalRedirect");
189 
190         PortletURLImpl portletURL = new PortletURLImpl(
191             (ActionRequestImpl)actionRequest, portletConfig.getPortletName(),
192             themeDisplay.getPlid(), PortletRequest.RENDER_PHASE);
193 
194         portletURL.setWindowState(WindowState.MAXIMIZED);
195 
196         portletURL.setParameter("struts_action", "/journal/edit_structure");
197         portletURL.setParameter(Constants.CMD, Constants.UPDATE, false);
198         portletURL.setParameter("redirect", redirect, false);
199         portletURL.setParameter("originalRedirect", originalRedirect, false);
200         portletURL.setParameter(
201             "groupId", String.valueOf(structure.getGroupId()), false);
202         portletURL.setParameter(
203             "structureId", structure.getStructureId(), false);
204 
205         return portletURL.toString();
206     }
207 
208     protected JournalStructure updateStructure(ActionRequest actionRequest)
209         throws Exception {
210 
211         String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
212 
213         Layout layout = (Layout)actionRequest.getAttribute(WebKeys.LAYOUT);
214 
215         long groupId = ParamUtil.getLong(actionRequest, "groupId");
216 
217         String structureId = ParamUtil.getString(actionRequest, "structureId");
218         boolean autoStructureId = ParamUtil.getBoolean(
219             actionRequest, "autoStructureId");
220 
221         String name = ParamUtil.getString(actionRequest, "name");
222         String description = ParamUtil.getString(actionRequest, "description");
223         String xsd = ParamUtil.getString(actionRequest, "xsd");
224 
225         String[] communityPermissions = actionRequest.getParameterValues(
226             "communityPermissions");
227         String[] guestPermissions = actionRequest.getParameterValues(
228             "guestPermissions");
229 
230         JournalStructure structure = null;
231 
232         if (cmd.equals(Constants.ADD)) {
233 
234             // Add structure
235 
236             structure = JournalStructureServiceUtil.addStructure(
237                 structureId, autoStructureId, layout.getPlid(), name,
238                 description, xsd, communityPermissions, guestPermissions);
239         }
240         else {
241 
242             // Update structure
243 
244             structure = JournalStructureServiceUtil.updateStructure(
245                 groupId, structureId, name, description, xsd);
246         }
247 
248         // Recent structures
249 
250         JournalUtil.addRecentStructure(actionRequest, structure);
251 
252         return structure;
253     }
254 
255 }