1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet.admin.action;
16  
17  import com.liferay.portal.CompanyMxException;
18  import com.liferay.portal.CompanyVirtualHostException;
19  import com.liferay.portal.CompanyWebIdException;
20  import com.liferay.portal.NoSuchCompanyException;
21  import com.liferay.portal.kernel.servlet.SessionErrors;
22  import com.liferay.portal.kernel.util.ParamUtil;
23  import com.liferay.portal.model.Company;
24  import com.liferay.portal.security.auth.PrincipalException;
25  import com.liferay.portal.service.CompanyServiceUtil;
26  import com.liferay.portal.struts.PortletAction;
27  import com.liferay.portal.util.PortalInstances;
28  import com.liferay.portal.util.PropsValues;
29  import com.liferay.portal.util.WebKeys;
30  
31  import javax.portlet.ActionRequest;
32  import javax.portlet.ActionResponse;
33  import javax.portlet.PortletConfig;
34  import javax.portlet.RenderRequest;
35  import javax.portlet.RenderResponse;
36  
37  import javax.servlet.ServletContext;
38  
39  import org.apache.struts.action.ActionForm;
40  import org.apache.struts.action.ActionForward;
41  import org.apache.struts.action.ActionMapping;
42  
43  /**
44   * <a href="EditInstanceAction.java.html"><b><i>View Source</i></b></a>
45   *
46   * @author Brian Wing Shun Chan
47   */
48  public class EditInstanceAction extends PortletAction {
49  
50      public void processAction(
51              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
52              ActionRequest actionRequest, ActionResponse actionResponse)
53          throws Exception {
54  
55          try {
56              updateInstance(actionRequest);
57  
58              sendRedirect(actionRequest, actionResponse);
59          }
60          catch (Exception e) {
61              if (e instanceof NoSuchCompanyException ||
62                  e instanceof PrincipalException) {
63  
64                  SessionErrors.add(actionRequest, e.getClass().getName());
65  
66                  setForward(actionRequest, "portlet.admin.error");
67              }
68              else if (e instanceof CompanyMxException ||
69                       e instanceof CompanyVirtualHostException ||
70                       e instanceof CompanyWebIdException) {
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.getInstance(renderRequest);
87          }
88          catch (Exception e) {
89              if (e instanceof NoSuchCompanyException ||
90                  e instanceof PrincipalException) {
91  
92                  SessionErrors.add(renderRequest, e.getClass().getName());
93  
94                  return mapping.findForward("portlet.admin.error");
95              }
96              else {
97                  throw e;
98              }
99          }
100 
101         return mapping.findForward(
102             getForward(renderRequest, "portlet.admin.edit_instance"));
103     }
104 
105     protected void updateInstance(ActionRequest actionRequest)
106         throws Exception {
107 
108         long companyId = ParamUtil.getLong(actionRequest, "companyId");
109 
110         String webId = ParamUtil.getString(actionRequest, "webId");
111         String virtualHost = ParamUtil.getString(actionRequest, "virtualHost");
112         String mx = ParamUtil.getString(actionRequest, "mx");
113         String shardName = ParamUtil.getString(
114             actionRequest, "shardName", PropsValues.SHARD_DEFAULT_NAME);
115         boolean system = false;
116 
117         if (companyId <= 0) {
118 
119             // Add instance
120 
121             Company company = CompanyServiceUtil.addCompany(
122                 webId, virtualHost, mx, shardName, system);
123 
124             ServletContext servletContext =
125                 (ServletContext)actionRequest.getAttribute(WebKeys.CTX);
126 
127             PortalInstances.initCompany(servletContext, company.getWebId());
128         }
129         else {
130 
131             // Update instance
132 
133             CompanyServiceUtil.updateCompany(companyId, virtualHost, mx);
134         }
135     }
136 
137 }