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