001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.enterpriseadmin.action;
016    
017    import com.liferay.portal.AccountNameException;
018    import com.liferay.portal.AddressCityException;
019    import com.liferay.portal.AddressStreetException;
020    import com.liferay.portal.AddressZipException;
021    import com.liferay.portal.CompanyMxException;
022    import com.liferay.portal.CompanyVirtualHostException;
023    import com.liferay.portal.CompanyWebIdException;
024    import com.liferay.portal.EmailAddressException;
025    import com.liferay.portal.NoSuchCountryException;
026    import com.liferay.portal.NoSuchListTypeException;
027    import com.liferay.portal.NoSuchRegionException;
028    import com.liferay.portal.PhoneNumberException;
029    import com.liferay.portal.WebsiteURLException;
030    import com.liferay.portal.kernel.servlet.SessionErrors;
031    import com.liferay.portal.kernel.util.Constants;
032    import com.liferay.portal.kernel.util.ParamUtil;
033    import com.liferay.portal.kernel.util.PropertiesParamUtil;
034    import com.liferay.portal.kernel.util.PropsKeys;
035    import com.liferay.portal.kernel.util.UnicodeProperties;
036    import com.liferay.portal.model.Address;
037    import com.liferay.portal.model.Company;
038    import com.liferay.portal.model.EmailAddress;
039    import com.liferay.portal.model.Phone;
040    import com.liferay.portal.model.Website;
041    import com.liferay.portal.security.auth.PrincipalException;
042    import com.liferay.portal.service.CompanyServiceUtil;
043    import com.liferay.portal.struts.PortletAction;
044    import com.liferay.portal.util.PortalUtil;
045    import com.liferay.portlet.enterpriseadmin.util.EnterpriseAdminUtil;
046    
047    import java.util.List;
048    
049    import javax.portlet.ActionRequest;
050    import javax.portlet.ActionResponse;
051    import javax.portlet.PortletConfig;
052    
053    import org.apache.struts.action.ActionForm;
054    import org.apache.struts.action.ActionMapping;
055    
056    /**
057     * @author Brian Wing Shun Chan
058     * @author Julio Camarero
059     */
060    public class EditCompanyAction extends PortletAction {
061    
062            public void processAction(
063                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
064                            ActionRequest actionRequest, ActionResponse actionResponse)
065                    throws Exception {
066    
067                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
068    
069                    try {
070                            if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
071                                    updateCompany(actionRequest);
072                                    updateDisplay(actionRequest);
073                            }
074    
075                            sendRedirect(actionRequest, actionResponse);
076                    }
077                    catch (Exception e) {
078                            if (e instanceof PrincipalException) {
079                                    SessionErrors.add(actionRequest, e.getClass().getName());
080    
081                                    setForward(actionRequest, "portlet.enterprise_admin.error");
082                            }
083                            else if (e instanceof AddressCityException ||
084                                             e instanceof AccountNameException ||
085                                             e instanceof AddressStreetException ||
086                                             e instanceof AddressZipException ||
087                                             e instanceof CompanyMxException ||
088                                             e instanceof CompanyVirtualHostException ||
089                                             e instanceof CompanyWebIdException ||
090                                             e instanceof EmailAddressException ||
091                                             e instanceof NoSuchCountryException ||
092                                             e instanceof NoSuchListTypeException ||
093                                             e instanceof NoSuchRegionException ||
094                                             e instanceof PhoneNumberException ||
095                                             e instanceof WebsiteURLException) {
096    
097                                    if (e instanceof NoSuchListTypeException) {
098                                            NoSuchListTypeException nslte = (NoSuchListTypeException)e;
099    
100                                            SessionErrors.add(
101                                                    actionRequest,
102                                                    e.getClass().getName() + nslte.getType());
103                                    }
104                                    else {
105                                            SessionErrors.add(actionRequest, e.getClass().getName(), e);
106                                    }
107    
108                                    setForward(actionRequest, "portlet.enterprise_admin.view");
109                            }
110                            else {
111                                    throw e;
112                            }
113                    }
114            }
115    
116            protected void updateCompany(ActionRequest actionRequest) throws Exception {
117                    long companyId = PortalUtil.getCompanyId(actionRequest);
118    
119                    String virtualHost = ParamUtil.getString(actionRequest, "virtualHost");
120                    String mx = ParamUtil.getString(actionRequest, "mx");
121                    String homeURL = ParamUtil.getString(actionRequest, "homeURL");
122                    String name = ParamUtil.getString(actionRequest, "name");
123                    String legalName = ParamUtil.getString(actionRequest, "legalName");
124                    String legalId = ParamUtil.getString(actionRequest, "legalId");
125                    String legalType = ParamUtil.getString(actionRequest, "legalType");
126                    String sicCode = ParamUtil.getString(actionRequest, "sicCode");
127                    String tickerSymbol = ParamUtil.getString(
128                            actionRequest, "tickerSymbol");
129                    String industry = ParamUtil.getString(actionRequest, "industry");
130                    String type = ParamUtil.getString(actionRequest, "type");
131                    String size = ParamUtil.getString(actionRequest, "size");
132                    String languageId = ParamUtil.getString(actionRequest, "languageId");
133                    String timeZoneId = ParamUtil.getString(actionRequest, "timeZoneId");
134                    List<Address> addresses = EnterpriseAdminUtil.getAddresses(
135                            actionRequest);
136                    List<EmailAddress> emailAddresses =
137                            EnterpriseAdminUtil.getEmailAddresses(actionRequest);
138                    List<Phone> phones = EnterpriseAdminUtil.getPhones(actionRequest);
139                    List<Website> websites = EnterpriseAdminUtil.getWebsites(actionRequest);
140                    UnicodeProperties properties = PropertiesParamUtil.getProperties(
141                            actionRequest, "settings--");
142    
143                    CompanyServiceUtil.updateCompany(
144                            companyId, virtualHost, mx, homeURL, name, legalName, legalId,
145                            legalType, sicCode, tickerSymbol, industry, type, size, languageId,
146                            timeZoneId, addresses, emailAddresses, phones, websites,
147                            properties);
148            }
149    
150            protected void updateDisplay(ActionRequest actionRequest) throws Exception {
151                    Company company = PortalUtil.getCompany(actionRequest);
152    
153                    String languageId = ParamUtil.getString(actionRequest, "languageId");
154                    String timeZoneId = ParamUtil.getString(actionRequest, "timeZoneId");
155    
156                    CompanyServiceUtil.updateDisplay(
157                            company.getCompanyId(), languageId, timeZoneId);
158    
159                    boolean communityLogo = ParamUtil.getBoolean(
160                            actionRequest,
161                            "settings--" + PropsKeys.COMPANY_SECURITY_COMMUNITY_LOGO + "--");
162    
163                    CompanyServiceUtil.updateSecurity(
164                            company.getCompanyId(), company.getAuthType(),
165                            company.isAutoLogin(), company.isSendPassword(),
166                            company.isStrangers(), company.isStrangersWithMx(),
167                            company.isStrangersVerify(), communityLogo);
168    
169                    boolean deleteLogo = ParamUtil.getBoolean(actionRequest, "deleteLogo");
170    
171                    if (deleteLogo) {
172                            CompanyServiceUtil.deleteLogo(company.getCompanyId());
173                    }
174            }
175    
176    }