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.enterpriseadmin.action;
21  
22  import com.liferay.portal.NoSuchListTypeException;
23  import com.liferay.portal.NoSuchWebsiteException;
24  import com.liferay.portal.WebsiteURLException;
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.security.auth.PrincipalException;
29  import com.liferay.portal.service.WebsiteServiceUtil;
30  import com.liferay.portal.struts.PortletAction;
31  
32  import javax.portlet.ActionRequest;
33  import javax.portlet.ActionResponse;
34  import javax.portlet.PortletConfig;
35  import javax.portlet.RenderRequest;
36  import javax.portlet.RenderResponse;
37  
38  import org.apache.struts.action.ActionForm;
39  import org.apache.struts.action.ActionForward;
40  import org.apache.struts.action.ActionMapping;
41  
42  /**
43   * <a href="EditWebsiteAction.java.html"><b><i>View Source</i></b></a>
44   *
45   * @author Brian Wing Shun Chan
46   *
47   */
48  public class EditWebsiteAction extends PortletAction {
49  
50      public void processAction(
51              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
52              ActionRequest actionRequest, ActionResponse actionResponse)
53          throws Exception {
54  
55          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
56  
57          try {
58              if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
59                  updateWebsite(actionRequest);
60              }
61              else if (cmd.equals(Constants.DELETE)) {
62                  deleteWebsite(actionRequest);
63              }
64  
65              sendRedirect(actionRequest, actionResponse);
66          }
67          catch (Exception e) {
68              if (e instanceof NoSuchWebsiteException ||
69                  e instanceof PrincipalException) {
70  
71                  SessionErrors.add(actionRequest, e.getClass().getName());
72  
73                  setForward(actionRequest, "portlet.enterprise_admin.error");
74              }
75              else if (e instanceof NoSuchListTypeException ||
76                       e instanceof WebsiteURLException) {
77  
78                  SessionErrors.add(actionRequest, e.getClass().getName());
79              }
80              else {
81                  throw e;
82              }
83          }
84      }
85  
86      public ActionForward render(
87              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
88              RenderRequest renderRequest, RenderResponse renderResponse)
89          throws Exception {
90  
91          try {
92              ActionUtil.getWebsite(renderRequest);
93          }
94          catch (Exception e) {
95              if (e instanceof NoSuchWebsiteException ||
96                  e instanceof PrincipalException) {
97  
98                  SessionErrors.add(renderRequest, e.getClass().getName());
99  
100                 return mapping.findForward("portlet.enterprise_admin.error");
101             }
102             else {
103                 throw e;
104             }
105         }
106 
107         return mapping.findForward(
108             getForward(renderRequest, "portlet.enterprise_admin.edit_website"));
109     }
110 
111     protected void deleteWebsite(ActionRequest actionRequest) throws Exception {
112         long websiteId = ParamUtil.getLong(actionRequest, "websiteId");
113 
114         WebsiteServiceUtil.deleteWebsite(websiteId);
115     }
116 
117     protected void updateWebsite(ActionRequest actionRequest) throws Exception {
118         long websiteId = ParamUtil.getLong(actionRequest, "websiteId");
119 
120         String className = ParamUtil.getString(actionRequest, "className");
121         long classPK = ParamUtil.getLong(actionRequest, "classPK");
122 
123         String url = ParamUtil.getString(actionRequest, "url");
124         int typeId = ParamUtil.getInteger(actionRequest, "typeId");
125         boolean primary = ParamUtil.getBoolean(actionRequest, "primary");
126 
127         if (websiteId <= 0) {
128 
129             // Add website
130 
131             WebsiteServiceUtil.addWebsite(
132                 className, classPK, url, typeId, primary);
133         }
134         else {
135 
136             // Update website
137 
138             WebsiteServiceUtil.updateWebsite(websiteId, url, typeId, primary);
139         }
140     }
141 
142 }