1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions 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.admin.action;
24  
25  import com.liferay.portal.CompanyMxException;
26  import com.liferay.portal.CompanyVirtualHostException;
27  import com.liferay.portal.CompanyWebIdException;
28  import com.liferay.portal.NoSuchCompanyException;
29  import com.liferay.portal.kernel.servlet.SessionErrors;
30  import com.liferay.portal.kernel.util.ParamUtil;
31  import com.liferay.portal.model.Company;
32  import com.liferay.portal.security.auth.PrincipalException;
33  import com.liferay.portal.service.CompanyServiceUtil;
34  import com.liferay.portal.struts.PortletAction;
35  import com.liferay.portal.util.PortalInstances;
36  import com.liferay.portal.util.PropsValues;
37  import com.liferay.portal.util.WebKeys;
38  
39  import javax.portlet.ActionRequest;
40  import javax.portlet.ActionResponse;
41  import javax.portlet.PortletConfig;
42  import javax.portlet.RenderRequest;
43  import javax.portlet.RenderResponse;
44  
45  import javax.servlet.ServletContext;
46  
47  import org.apache.struts.action.ActionForm;
48  import org.apache.struts.action.ActionForward;
49  import org.apache.struts.action.ActionMapping;
50  
51  /**
52   * <a href="EditInstanceAction.java.html"><b><i>View Source</i></b></a>
53   *
54   * @author Brian Wing Shun Chan
55   *
56   */
57  public class EditInstanceAction extends PortletAction {
58  
59      public void processAction(
60              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
61              ActionRequest actionRequest, ActionResponse actionResponse)
62          throws Exception {
63  
64          try {
65              updateInstance(actionRequest);
66  
67              sendRedirect(actionRequest, actionResponse);
68          }
69          catch (Exception e) {
70              if (e instanceof NoSuchCompanyException ||
71                  e instanceof PrincipalException) {
72  
73                  SessionErrors.add(actionRequest, e.getClass().getName());
74  
75                  setForward(actionRequest, "portlet.admin.error");
76              }
77              else if (e instanceof CompanyMxException ||
78                       e instanceof CompanyVirtualHostException ||
79                       e instanceof CompanyWebIdException) {
80  
81                  SessionErrors.add(actionRequest, e.getClass().getName());
82              }
83              else {
84                  throw e;
85              }
86          }
87      }
88  
89      public ActionForward render(
90              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
91              RenderRequest renderRequest, RenderResponse renderResponse)
92          throws Exception {
93  
94          try {
95              ActionUtil.getInstance(renderRequest);
96          }
97          catch (Exception e) {
98              if (e instanceof NoSuchCompanyException ||
99                  e instanceof PrincipalException) {
100 
101                 SessionErrors.add(renderRequest, e.getClass().getName());
102 
103                 return mapping.findForward("portlet.admin.error");
104             }
105             else {
106                 throw e;
107             }
108         }
109 
110         return mapping.findForward(
111             getForward(renderRequest, "portlet.admin.edit_instance"));
112     }
113 
114     protected void updateInstance(ActionRequest actionRequest)
115         throws Exception {
116 
117         long companyId = ParamUtil.getLong(actionRequest, "companyId");
118 
119         String webId = ParamUtil.getString(actionRequest, "webId");
120         String virtualHost = ParamUtil.getString(actionRequest, "virtualHost");
121         String mx = ParamUtil.getString(actionRequest, "mx");
122         String shardName = ParamUtil.getString(
123             actionRequest, "shardName", PropsValues.SHARD_DEFAULT_NAME);
124         boolean system = false;
125 
126         if (companyId <= 0) {
127 
128             // Add instance
129 
130             Company company = CompanyServiceUtil.addCompany(
131                 webId, virtualHost, mx, shardName, system);
132 
133             ServletContext servletContext =
134                 (ServletContext)actionRequest.getAttribute(WebKeys.CTX);
135 
136             PortalInstances.initCompany(servletContext, company.getWebId());
137         }
138         else {
139 
140             // Update instance
141 
142             CompanyServiceUtil.updateCompany(companyId, virtualHost, mx);
143         }
144     }
145 
146 }