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.enterpriseadmin.action;
21  
22  import com.liferay.portal.AccountNameException;
23  import com.liferay.portal.AddressCityException;
24  import com.liferay.portal.AddressStreetException;
25  import com.liferay.portal.AddressZipException;
26  import com.liferay.portal.CompanyMxException;
27  import com.liferay.portal.CompanyVirtualHostException;
28  import com.liferay.portal.CompanyWebIdException;
29  import com.liferay.portal.EmailAddressException;
30  import com.liferay.portal.NoSuchCountryException;
31  import com.liferay.portal.NoSuchListTypeException;
32  import com.liferay.portal.NoSuchRegionException;
33  import com.liferay.portal.PhoneNumberException;
34  import com.liferay.portal.WebsiteURLException;
35  import com.liferay.portal.kernel.servlet.SessionErrors;
36  import com.liferay.portal.kernel.util.Constants;
37  import com.liferay.portal.kernel.util.ParamUtil;
38  import com.liferay.portal.kernel.util.PropertiesParamUtil;
39  import com.liferay.portal.kernel.util.UnicodeProperties;
40  import com.liferay.portal.model.Address;
41  import com.liferay.portal.model.Company;
42  import com.liferay.portal.model.EmailAddress;
43  import com.liferay.portal.model.Phone;
44  import com.liferay.portal.model.Website;
45  import com.liferay.portal.security.auth.PrincipalException;
46  import com.liferay.portal.service.CompanyServiceUtil;
47  import com.liferay.portal.struts.PortletAction;
48  import com.liferay.portal.util.PortalUtil;
49  import com.liferay.portal.util.PropsKeys;
50  import com.liferay.portlet.enterpriseadmin.util.EnterpriseAdminUtil;
51  
52  import java.util.List;
53  
54  import javax.portlet.ActionRequest;
55  import javax.portlet.ActionResponse;
56  import javax.portlet.PortletConfig;
57  
58  import org.apache.struts.action.ActionForm;
59  import org.apache.struts.action.ActionMapping;
60  
61  /**
62   * <a href="EditCompanyAction.java.html"><b><i>View Source</i></b></a>
63   *
64   * @author Brian Wing Shun Chan
65   * @author Julio Camarero
66   *
67   */
68  public class EditCompanyAction extends PortletAction {
69  
70      public void processAction(
71              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
72              ActionRequest actionRequest, ActionResponse actionResponse)
73          throws Exception {
74  
75          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
76  
77          try {
78              if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
79                  updateCompany(actionRequest);
80                  updateDisplay(actionRequest);
81              }
82  
83              sendRedirect(actionRequest, actionResponse);
84          }
85          catch (Exception e) {
86              if (e instanceof PrincipalException) {
87                  SessionErrors.add(actionRequest, e.getClass().getName());
88  
89                  setForward(actionRequest, "portlet.enterprise_admin.error");
90              }
91              else if (e instanceof AddressCityException ||
92                       e instanceof AccountNameException ||
93                       e instanceof AddressStreetException ||
94                       e instanceof AddressZipException ||
95                       e instanceof CompanyMxException ||
96                       e instanceof CompanyVirtualHostException ||
97                       e instanceof CompanyWebIdException ||
98                       e instanceof EmailAddressException ||
99                       e instanceof NoSuchCountryException ||
100                      e instanceof NoSuchListTypeException ||
101                      e instanceof NoSuchRegionException ||
102                      e instanceof PhoneNumberException ||
103                      e instanceof WebsiteURLException) {
104 
105                 if (e instanceof NoSuchListTypeException) {
106                     NoSuchListTypeException nslte = (NoSuchListTypeException)e;
107 
108                     SessionErrors.add(
109                         actionRequest,
110                         e.getClass().getName() + nslte.getType());
111                 }
112                 else {
113                     SessionErrors.add(actionRequest, e.getClass().getName(), e);
114                 }
115 
116                 setForward(actionRequest, "portlet.enterprise_admin.view");
117             }
118             else {
119                 throw e;
120             }
121         }
122     }
123 
124     protected void updateCompany(ActionRequest actionRequest) throws Exception {
125         long companyId = PortalUtil.getCompanyId(actionRequest);
126 
127         String virtualHost = ParamUtil.getString(actionRequest, "virtualHost");
128         String mx = ParamUtil.getString(actionRequest, "mx");
129         String homeURL = ParamUtil.getString(actionRequest, "homeURL");
130         String name = ParamUtil.getString(actionRequest, "name");
131         String legalName = ParamUtil.getString(actionRequest, "legalName");
132         String legalId = ParamUtil.getString(actionRequest, "legalId");
133         String legalType = ParamUtil.getString(actionRequest, "legalType");
134         String sicCode = ParamUtil.getString(actionRequest, "sicCode");
135         String tickerSymbol = ParamUtil.getString(
136             actionRequest, "tickerSymbol");
137         String industry = ParamUtil.getString(actionRequest, "industry");
138         String type = ParamUtil.getString(actionRequest, "type");
139         String size = ParamUtil.getString(actionRequest, "size");
140         String languageId = ParamUtil.getString(actionRequest, "languageId");
141         String timeZoneId = ParamUtil.getString(actionRequest, "timeZoneId");
142         List<Address> addresses = EnterpriseAdminUtil.getAddresses(
143             actionRequest);
144         List<EmailAddress> emailAddresses =
145             EnterpriseAdminUtil.getEmailAddresses(actionRequest);
146         List<Phone> phones = EnterpriseAdminUtil.getPhones(actionRequest);
147         List<Website> websites = EnterpriseAdminUtil.getWebsites(actionRequest);
148         UnicodeProperties properties = PropertiesParamUtil.getProperties(
149             actionRequest, "settings(");
150 
151         CompanyServiceUtil.updateCompany(
152             companyId, virtualHost, mx, homeURL, name, legalName, legalId,
153             legalType, sicCode, tickerSymbol, industry, type, size, languageId,
154             timeZoneId, addresses, emailAddresses, phones, websites,
155             properties);
156     }
157 
158     protected void updateDisplay(ActionRequest actionRequest) throws Exception {
159         Company company = PortalUtil.getCompany(actionRequest);
160 
161         String languageId = ParamUtil.getString(actionRequest, "languageId");
162         String timeZoneId = ParamUtil.getString(actionRequest, "timeZoneId");
163         boolean communityLogo = ParamUtil.getBoolean(
164             actionRequest,
165             "settings(" + PropsKeys.COMPANY_SECURITY_COMMUNITY_LOGO + ")");
166 
167         CompanyServiceUtil.updateDisplay(
168             company.getCompanyId(), languageId, timeZoneId);
169 
170         CompanyServiceUtil.updateSecurity(
171             company.getCompanyId(), company.getAuthType(),
172             company.isAutoLogin(), company.isSendPassword(),
173             company.isStrangers(), company.isStrangersWithMx(),
174             company.isStrangersVerify(), communityLogo);
175     }
176 
177 }