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.AddressCityException;
018    import com.liferay.portal.AddressStreetException;
019    import com.liferay.portal.AddressZipException;
020    import com.liferay.portal.DuplicateOrganizationException;
021    import com.liferay.portal.EmailAddressException;
022    import com.liferay.portal.NoSuchCountryException;
023    import com.liferay.portal.NoSuchListTypeException;
024    import com.liferay.portal.NoSuchOrganizationException;
025    import com.liferay.portal.NoSuchRegionException;
026    import com.liferay.portal.OrganizationNameException;
027    import com.liferay.portal.OrganizationParentException;
028    import com.liferay.portal.PhoneNumberException;
029    import com.liferay.portal.RequiredOrganizationException;
030    import com.liferay.portal.WebsiteURLException;
031    import com.liferay.portal.kernel.servlet.SessionErrors;
032    import com.liferay.portal.kernel.util.Constants;
033    import com.liferay.portal.kernel.util.HttpUtil;
034    import com.liferay.portal.kernel.util.LocalizationUtil;
035    import com.liferay.portal.kernel.util.ParamUtil;
036    import com.liferay.portal.kernel.util.StringUtil;
037    import com.liferay.portal.model.Address;
038    import com.liferay.portal.model.EmailAddress;
039    import com.liferay.portal.model.OrgLabor;
040    import com.liferay.portal.model.Organization;
041    import com.liferay.portal.model.OrganizationConstants;
042    import com.liferay.portal.model.Phone;
043    import com.liferay.portal.model.Website;
044    import com.liferay.portal.security.auth.PrincipalException;
045    import com.liferay.portal.service.OrganizationServiceUtil;
046    import com.liferay.portal.service.ServiceContext;
047    import com.liferay.portal.service.ServiceContextFactory;
048    import com.liferay.portal.struts.PortletAction;
049    import com.liferay.portlet.communities.util.CommunitiesUtil;
050    import com.liferay.portlet.enterpriseadmin.util.EnterpriseAdminUtil;
051    
052    import java.util.List;
053    
054    import javax.portlet.ActionRequest;
055    import javax.portlet.ActionResponse;
056    import javax.portlet.PortletConfig;
057    import javax.portlet.PortletPreferences;
058    import javax.portlet.RenderRequest;
059    import javax.portlet.RenderResponse;
060    
061    import org.apache.struts.action.ActionForm;
062    import org.apache.struts.action.ActionForward;
063    import org.apache.struts.action.ActionMapping;
064    
065    /**
066     * @author Brian Wing Shun Chan
067     * @author Julio Camarero
068     * @author Jorge Ferrer
069     */
070    public class EditOrganizationAction extends PortletAction {
071    
072            public void processAction(
073                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
074                            ActionRequest actionRequest, ActionResponse actionResponse)
075                    throws Exception {
076    
077                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
078    
079                    try {
080                            Organization organization = null;
081    
082                            if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
083                                    organization = updateOrganization(actionRequest);
084                            }
085                            else if (cmd.equals(Constants.DELETE)) {
086                                    deleteOrganizations(actionRequest);
087                            }
088    
089                            String redirect = ParamUtil.getString(actionRequest, "redirect");
090    
091                            if (organization != null) {
092                                    redirect = HttpUtil.setParameter(
093                                            redirect, actionResponse.getNamespace() + "organizationId",
094                                            organization.getOrganizationId());
095                            }
096    
097                            sendRedirect(actionRequest, actionResponse, redirect);
098                    }
099                    catch (Exception e) {
100                            if (e instanceof NoSuchOrganizationException ||
101                                    e instanceof PrincipalException) {
102    
103                                    SessionErrors.add(actionRequest, e.getClass().getName());
104    
105                                    setForward(actionRequest, "portlet.enterprise_admin.error");
106                            }
107                            else if (e instanceof AddressCityException ||
108                                             e instanceof AddressStreetException ||
109                                             e instanceof AddressZipException ||
110                                             e instanceof DuplicateOrganizationException ||
111                                             e instanceof EmailAddressException ||
112                                             e instanceof NoSuchCountryException ||
113                                             e instanceof NoSuchListTypeException ||
114                                             e instanceof NoSuchRegionException ||
115                                             e instanceof OrganizationNameException ||
116                                             e instanceof OrganizationParentException ||
117                                             e instanceof PhoneNumberException ||
118                                             e instanceof RequiredOrganizationException ||
119                                             e instanceof WebsiteURLException) {
120    
121                                    if (e instanceof NoSuchListTypeException) {
122                                            NoSuchListTypeException nslte = (NoSuchListTypeException)e;
123    
124                                            SessionErrors.add(
125                                                    actionRequest,
126                                                    e.getClass().getName() + nslte.getType());
127                                    }
128                                    else {
129                                            SessionErrors.add(actionRequest, e.getClass().getName());
130                                    }
131    
132                                    if (e instanceof RequiredOrganizationException) {
133                                            actionResponse.sendRedirect(
134                                                    ParamUtil.getString(actionRequest, "redirect"));
135                                    }
136                            }
137                            else {
138                                    throw e;
139                            }
140                    }
141            }
142    
143            public ActionForward render(
144                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
145                            RenderRequest renderRequest, RenderResponse renderResponse)
146                    throws Exception {
147    
148                    try {
149                            ActionUtil.getOrganization(renderRequest);
150                    }
151                    catch (Exception e) {
152                            if (e instanceof NoSuchOrganizationException ||
153                                    e instanceof PrincipalException) {
154    
155                                    SessionErrors.add(renderRequest, e.getClass().getName());
156    
157                                    return mapping.findForward("portlet.enterprise_admin.error");
158                            }
159                            else {
160                                    throw e;
161                            }
162                    }
163    
164                    return mapping.findForward(getForward(
165                            renderRequest, "portlet.enterprise_admin.edit_organization"));
166            }
167    
168            protected void deleteOrganizations(ActionRequest actionRequest)
169                    throws Exception {
170    
171                    long[] deleteOrganizationIds = StringUtil.split(
172                            ParamUtil.getString(actionRequest, "deleteOrganizationIds"), 0L);
173    
174                    for (int i = 0; i < deleteOrganizationIds.length; i++) {
175                            OrganizationServiceUtil.deleteOrganization(
176                                    deleteOrganizationIds[i]);
177                    }
178            }
179    
180            protected Organization updateOrganization(ActionRequest actionRequest)
181                    throws Exception {
182    
183                    long organizationId = ParamUtil.getLong(
184                            actionRequest, "organizationId");
185    
186                    long parentOrganizationId = ParamUtil.getLong(
187                            actionRequest, "parentOrganizationSearchContainerPrimaryKeys",
188                            OrganizationConstants.DEFAULT_PARENT_ORGANIZATION_ID);
189                    String name = ParamUtil.getString(actionRequest, "name");
190                    boolean recursable = ParamUtil.getBoolean(actionRequest, "recursable");
191                    int statusId = ParamUtil.getInteger(actionRequest, "statusId");
192                    String type = ParamUtil.getString(actionRequest, "type");
193                    long regionId = ParamUtil.getLong(actionRequest, "regionId");
194                    long countryId = ParamUtil.getLong(actionRequest, "countryId");
195                    String comments = ParamUtil.getString(actionRequest, "comments");
196                    List<Address> addresses = EnterpriseAdminUtil.getAddresses(
197                            actionRequest);
198                    List<EmailAddress> emailAddresses =
199                            EnterpriseAdminUtil.getEmailAddresses(actionRequest);
200                    List<OrgLabor> orgLabors = EnterpriseAdminUtil.getOrgLabors(
201                            actionRequest);
202                    List<Phone> phones = EnterpriseAdminUtil.getPhones(actionRequest);
203                    List<Website> websites = EnterpriseAdminUtil.getWebsites(actionRequest);
204    
205                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
206                            Organization.class.getName(), actionRequest);
207    
208                    Organization organization = null;
209    
210                    if (organizationId <= 0) {
211    
212                            // Add organization
213    
214                            organization = OrganizationServiceUtil.addOrganization(
215                                    parentOrganizationId, name, type, recursable, regionId,
216                                    countryId, statusId, comments, addresses, emailAddresses,
217                                    orgLabors, phones, websites, serviceContext);
218                    }
219                    else {
220    
221                            // Update organization
222    
223                            organization = OrganizationServiceUtil.updateOrganization(
224                                    organizationId, parentOrganizationId, name, type,
225                                    recursable, regionId, countryId, statusId, comments, addresses,
226                                    emailAddresses, orgLabors, phones, websites, serviceContext);
227    
228                            boolean deleteLogo = ParamUtil.getBoolean(
229                                    actionRequest, "deleteLogo");
230    
231                            if (deleteLogo) {
232                                    OrganizationServiceUtil.deleteLogo(
233                                            organization.getOrganizationId());
234                            }
235                    }
236    
237                    // Layout set prototypes
238    
239                    long publicLayoutSetPrototypeId = ParamUtil.getLong(
240                            actionRequest, "publicLayoutSetPrototypeId");
241                    long privateLayoutSetPrototypeId = ParamUtil.getLong(
242                            actionRequest, "privateLayoutSetPrototypeId");
243    
244                    CommunitiesUtil.applyLayoutSetPrototypes(
245                            organization.getGroup(), publicLayoutSetPrototypeId,
246                            privateLayoutSetPrototypeId);
247    
248                    // Reminder queries
249    
250                    String reminderQueries = actionRequest.getParameter("reminderQueries");
251    
252                    PortletPreferences preferences = organization.getPreferences();
253    
254                    preferences.setValue("reminderQueries", reminderQueries);
255    
256                    LocalizationUtil.setLocalizedPreferencesValues(
257                            actionRequest, preferences, "reminderQueries");
258    
259                    preferences.store();
260    
261                    return organization;
262            }
263    
264    }