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