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.admin.action;
21  
22  import com.liferay.portal.CompanyMxException;
23  import com.liferay.portal.CompanyVirtualHostException;
24  import com.liferay.portal.CompanyWebIdException;
25  import com.liferay.portal.NoSuchCompanyException;
26  import com.liferay.portal.kernel.servlet.SessionErrors;
27  import com.liferay.portal.kernel.util.ParamUtil;
28  import com.liferay.portal.model.Company;
29  import com.liferay.portal.security.auth.PrincipalException;
30  import com.liferay.portal.service.CompanyServiceUtil;
31  import com.liferay.portal.struts.PortletAction;
32  import com.liferay.portal.util.PortalInstances;
33  import com.liferay.portal.util.WebKeys;
34  
35  import javax.portlet.ActionRequest;
36  import javax.portlet.ActionResponse;
37  import javax.portlet.PortletConfig;
38  import javax.portlet.RenderRequest;
39  import javax.portlet.RenderResponse;
40  
41  import javax.servlet.ServletContext;
42  
43  import org.apache.struts.action.ActionForm;
44  import org.apache.struts.action.ActionForward;
45  import org.apache.struts.action.ActionMapping;
46  
47  /**
48   * <a href="EditInstanceAction.java.html"><b><i>View Source</i></b></a>
49   *
50   * @author Brian Wing Shun Chan
51   *
52   */
53  public class EditInstanceAction extends PortletAction {
54  
55      public void processAction(
56              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
57              ActionRequest actionRequest, ActionResponse actionResponse)
58          throws Exception {
59  
60          try {
61              updateInstance(actionRequest);
62  
63              sendRedirect(actionRequest, actionResponse);
64          }
65          catch (Exception e) {
66              if (e instanceof NoSuchCompanyException ||
67                  e instanceof PrincipalException) {
68  
69                  SessionErrors.add(actionRequest, e.getClass().getName());
70  
71                  setForward(actionRequest, "portlet.admin.error");
72              }
73              else if (e instanceof CompanyMxException ||
74                       e instanceof CompanyVirtualHostException ||
75                       e instanceof CompanyWebIdException) {
76  
77                  SessionErrors.add(actionRequest, e.getClass().getName());
78              }
79              else {
80                  throw e;
81              }
82          }
83      }
84  
85      public ActionForward render(
86              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
87              RenderRequest renderRequest, RenderResponse renderResponse)
88          throws Exception {
89  
90          try {
91              ActionUtil.getInstance(renderRequest);
92          }
93          catch (Exception e) {
94              if (e instanceof NoSuchCompanyException ||
95                  e instanceof PrincipalException) {
96  
97                  SessionErrors.add(renderRequest, e.getClass().getName());
98  
99                  return mapping.findForward("portlet.admin.error");
100             }
101             else {
102                 throw e;
103             }
104         }
105 
106         return mapping.findForward(
107             getForward(renderRequest, "portlet.admin.edit_instance"));
108     }
109 
110     protected void updateInstance(ActionRequest actionRequest)
111         throws Exception {
112 
113         long companyId = ParamUtil.getLong(actionRequest, "companyId");
114 
115         String webId = ParamUtil.getString(actionRequest, "webId");
116         String virtualHost = ParamUtil.getString(actionRequest, "virtualHost");
117         String mx = ParamUtil.getString(actionRequest, "mx");
118 
119         if (companyId <= 0) {
120 
121             // Add instance
122 
123             Company company = CompanyServiceUtil.addCompany(
124                 webId, virtualHost, mx);
125 
126             ServletContext servletContext =
127                 (ServletContext)actionRequest.getAttribute(WebKeys.CTX);
128 
129             PortalInstances.initCompany(servletContext, company.getWebId());
130         }
131         else {
132 
133             // Update instance
134 
135             CompanyServiceUtil.updateCompany(companyId, virtualHost, mx);
136         }
137     }
138 
139 }