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