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