001    /**
002     * Copyright (c) 2000-2013 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.usersadmin.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.kernel.util.Validator;
038    import com.liferay.portal.model.Address;
039    import com.liferay.portal.model.EmailAddress;
040    import com.liferay.portal.model.Group;
041    import com.liferay.portal.model.OrgLabor;
042    import com.liferay.portal.model.Organization;
043    import com.liferay.portal.model.OrganizationConstants;
044    import com.liferay.portal.model.Phone;
045    import com.liferay.portal.model.Website;
046    import com.liferay.portal.security.auth.PrincipalException;
047    import com.liferay.portal.security.permission.ActionKeys;
048    import com.liferay.portal.service.OrganizationServiceUtil;
049    import com.liferay.portal.service.ServiceContext;
050    import com.liferay.portal.service.ServiceContextFactory;
051    import com.liferay.portal.service.permission.GroupPermissionUtil;
052    import com.liferay.portal.struts.PortletAction;
053    import com.liferay.portal.theme.ThemeDisplay;
054    import com.liferay.portal.util.PortalUtil;
055    import com.liferay.portal.util.WebKeys;
056    import com.liferay.portlet.sites.util.SitesUtil;
057    import com.liferay.portlet.usersadmin.util.UsersAdminUtil;
058    
059    import java.util.List;
060    
061    import javax.portlet.ActionRequest;
062    import javax.portlet.ActionResponse;
063    import javax.portlet.PortletConfig;
064    import javax.portlet.PortletPreferences;
065    import javax.portlet.RenderRequest;
066    import javax.portlet.RenderResponse;
067    
068    import org.apache.struts.action.ActionForm;
069    import org.apache.struts.action.ActionForward;
070    import org.apache.struts.action.ActionMapping;
071    
072    /**
073     * @author Brian Wing Shun Chan
074     * @author Julio Camarero
075     * @author Jorge Ferrer
076     */
077    public class EditOrganizationAction extends PortletAction {
078    
079            @Override
080            public void processAction(
081                            ActionMapping actionMapping, ActionForm actionForm,
082                            PortletConfig portletConfig, ActionRequest actionRequest,
083                            ActionResponse actionResponse)
084                    throws Exception {
085    
086                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
087    
088                    try {
089                            Organization organization = null;
090    
091                            if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
092                                    organization = updateOrganization(actionRequest);
093                            }
094                            else if (cmd.equals(Constants.DELETE)) {
095                                    deleteOrganizations(actionRequest);
096                            }
097    
098                            String redirect = ParamUtil.getString(actionRequest, "redirect");
099    
100                            if (organization != null) {
101                                    redirect = HttpUtil.setParameter(
102                                            redirect, actionResponse.getNamespace() + "organizationId",
103                                            organization.getOrganizationId());
104                            }
105    
106                            sendRedirect(actionRequest, actionResponse, redirect);
107                    }
108                    catch (Exception e) {
109                            if (e instanceof NoSuchOrganizationException ||
110                                    e instanceof PrincipalException) {
111    
112                                    SessionErrors.add(actionRequest, e.getClass());
113    
114                                    setForward(actionRequest, "portlet.users_admin.error");
115                            }
116                            else if (e instanceof AddressCityException ||
117                                             e instanceof AddressStreetException ||
118                                             e instanceof AddressZipException ||
119                                             e instanceof DuplicateOrganizationException ||
120                                             e instanceof EmailAddressException ||
121                                             e instanceof NoSuchCountryException ||
122                                             e instanceof NoSuchListTypeException ||
123                                             e instanceof NoSuchRegionException ||
124                                             e instanceof OrganizationNameException ||
125                                             e instanceof OrganizationParentException ||
126                                             e instanceof PhoneNumberException ||
127                                             e instanceof RequiredOrganizationException ||
128                                             e instanceof WebsiteURLException) {
129    
130                                    if (e instanceof NoSuchListTypeException) {
131                                            NoSuchListTypeException nslte = (NoSuchListTypeException)e;
132    
133                                            SessionErrors.add(
134                                                    actionRequest,
135                                                    e.getClass().getName() + nslte.getType());
136                                    }
137                                    else {
138                                            SessionErrors.add(actionRequest, e.getClass());
139                                    }
140    
141                                    if (e instanceof RequiredOrganizationException) {
142                                            String redirect = PortalUtil.escapeRedirect(
143                                                    ParamUtil.getString(actionRequest, "redirect"));
144    
145                                            long organizationId = ParamUtil.getLong(
146                                                    actionRequest, "organizationId");
147    
148                                            if (organizationId > 0) {
149                                                    redirect = HttpUtil.setParameter(
150                                                            redirect,
151                                                            actionResponse.getNamespace() + "organizationId",
152                                                            organizationId);
153                                            }
154    
155                                            if (Validator.isNotNull(redirect)) {
156                                                    actionResponse.sendRedirect(redirect);
157                                            }
158                                    }
159                            }
160                            else {
161                                    throw e;
162                            }
163                    }
164            }
165    
166            @Override
167            public ActionForward render(
168                            ActionMapping actionMapping, ActionForm actionForm,
169                            PortletConfig portletConfig, RenderRequest renderRequest,
170                            RenderResponse renderResponse)
171                    throws Exception {
172    
173                    try {
174                            ActionUtil.getOrganization(renderRequest);
175                    }
176                    catch (Exception e) {
177                            if (e instanceof NoSuchOrganizationException ||
178                                    e instanceof PrincipalException) {
179    
180                                    SessionErrors.add(renderRequest, e.getClass());
181    
182                                    return actionMapping.findForward("portlet.users_admin.error");
183                            }
184                            else {
185                                    throw e;
186                            }
187                    }
188    
189                    return actionMapping.findForward(
190                            getForward(renderRequest, "portlet.users_admin.edit_organization"));
191            }
192    
193            protected void deleteOrganizations(ActionRequest actionRequest)
194                    throws Exception {
195    
196                    long[] deleteOrganizationIds = StringUtil.split(
197                            ParamUtil.getString(actionRequest, "deleteOrganizationIds"), 0L);
198    
199                    for (long deleteOrganizationId : deleteOrganizationIds) {
200                            OrganizationServiceUtil.deleteOrganization(deleteOrganizationId);
201                    }
202            }
203    
204            protected Organization updateOrganization(ActionRequest actionRequest)
205                    throws Exception {
206    
207                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
208                            WebKeys.THEME_DISPLAY);
209    
210                    long organizationId = ParamUtil.getLong(
211                            actionRequest, "organizationId");
212    
213                    long parentOrganizationId = ParamUtil.getLong(
214                            actionRequest, "parentOrganizationSearchContainerPrimaryKeys",
215                            OrganizationConstants.DEFAULT_PARENT_ORGANIZATION_ID);
216                    String name = ParamUtil.getString(actionRequest, "name");
217                    boolean recursable = ParamUtil.getBoolean(actionRequest, "recursable");
218                    int statusId = ParamUtil.getInteger(actionRequest, "statusId");
219                    String type = ParamUtil.getString(actionRequest, "type");
220                    long regionId = ParamUtil.getLong(actionRequest, "regionId");
221                    long countryId = ParamUtil.getLong(actionRequest, "countryId");
222                    String comments = ParamUtil.getString(actionRequest, "comments");
223                    boolean site = ParamUtil.getBoolean(actionRequest, "site");
224                    List<Address> addresses = UsersAdminUtil.getAddresses(actionRequest);
225                    List<EmailAddress> emailAddresses = UsersAdminUtil.getEmailAddresses(
226                            actionRequest);
227                    List<OrgLabor> orgLabors = UsersAdminUtil.getOrgLabors(actionRequest);
228                    List<Phone> phones = UsersAdminUtil.getPhones(actionRequest);
229                    List<Website> websites = UsersAdminUtil.getWebsites(actionRequest);
230    
231                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
232                            Organization.class.getName(), actionRequest);
233    
234                    Organization organization = null;
235    
236                    if (organizationId <= 0) {
237    
238                            // Add organization
239    
240                            organization = OrganizationServiceUtil.addOrganization(
241                                    parentOrganizationId, name, type, recursable, regionId,
242                                    countryId, statusId, comments, site, addresses, emailAddresses,
243                                    orgLabors, phones, websites, serviceContext);
244                    }
245                    else {
246    
247                            // Update organization
248    
249                            organization = OrganizationServiceUtil.updateOrganization(
250                                    organizationId, parentOrganizationId, name, type, recursable,
251                                    regionId, countryId, statusId, comments, site, addresses,
252                                    emailAddresses, orgLabors, phones, websites, serviceContext);
253    
254                            boolean deleteLogo = ParamUtil.getBoolean(
255                                    actionRequest, "deleteLogo");
256    
257                            if (deleteLogo) {
258                                    OrganizationServiceUtil.deleteLogo(
259                                            organization.getOrganizationId());
260                            }
261                    }
262    
263                    // Layout set prototypes
264    
265                    long publicLayoutSetPrototypeId = ParamUtil.getLong(
266                            actionRequest, "publicLayoutSetPrototypeId");
267                    long privateLayoutSetPrototypeId = ParamUtil.getLong(
268                            actionRequest, "privateLayoutSetPrototypeId");
269                    boolean publicLayoutSetPrototypeLinkEnabled = ParamUtil.getBoolean(
270                            actionRequest, "publicLayoutSetPrototypeLinkEnabled",
271                            (publicLayoutSetPrototypeId > 0));
272                    boolean privateLayoutSetPrototypeLinkEnabled = ParamUtil.getBoolean(
273                            actionRequest, "privateLayoutSetPrototypeLinkEnabled",
274                            (privateLayoutSetPrototypeId > 0));
275    
276                    Group organizationGroup = organization.getGroup();
277    
278                    if (GroupPermissionUtil.contains(
279                                    themeDisplay.getPermissionChecker(),
280                                    organizationGroup.getGroupId(), ActionKeys.UPDATE)) {
281    
282                            SitesUtil.updateLayoutSetPrototypesLinks(
283                                    organizationGroup, publicLayoutSetPrototypeId,
284                                    privateLayoutSetPrototypeId,
285                                    publicLayoutSetPrototypeLinkEnabled,
286                                    privateLayoutSetPrototypeLinkEnabled);
287                    }
288    
289                    // Reminder queries
290    
291                    String reminderQueries = actionRequest.getParameter("reminderQueries");
292    
293                    PortletPreferences preferences = organization.getPreferences();
294    
295                    LocalizationUtil.setLocalizedPreferencesValues(
296                            actionRequest, preferences, "reminderQueries");
297    
298                    preferences.setValue("reminderQueries", reminderQueries);
299    
300                    preferences.store();
301    
302                    return organization;
303            }
304    
305    }