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                    int statusId = ParamUtil.getInteger(actionRequest, "statusId");
218                    String type = ParamUtil.getString(actionRequest, "type");
219                    long regionId = ParamUtil.getLong(actionRequest, "regionId");
220                    long countryId = ParamUtil.getLong(actionRequest, "countryId");
221                    String comments = ParamUtil.getString(actionRequest, "comments");
222                    boolean site = ParamUtil.getBoolean(actionRequest, "site");
223                    List<Address> addresses = UsersAdminUtil.getAddresses(actionRequest);
224                    List<EmailAddress> emailAddresses = UsersAdminUtil.getEmailAddresses(
225                            actionRequest);
226                    List<OrgLabor> orgLabors = UsersAdminUtil.getOrgLabors(actionRequest);
227                    List<Phone> phones = UsersAdminUtil.getPhones(actionRequest);
228                    List<Website> websites = UsersAdminUtil.getWebsites(actionRequest);
229    
230                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
231                            Organization.class.getName(), actionRequest);
232    
233                    Organization organization = null;
234    
235                    if (organizationId <= 0) {
236    
237                            // Add organization
238    
239                            organization = OrganizationServiceUtil.addOrganization(
240                                    parentOrganizationId, name, type, regionId, countryId, statusId,
241                                    comments, site, addresses, emailAddresses, orgLabors, phones,
242                                    websites, serviceContext);
243                    }
244                    else {
245    
246                            // Update organization
247    
248                            organization = OrganizationServiceUtil.updateOrganization(
249                                    organizationId, parentOrganizationId, name, type, regionId,
250                                    countryId, statusId, comments, site, addresses, emailAddresses,
251                                    orgLabors, phones, websites, serviceContext);
252    
253                            boolean deleteLogo = ParamUtil.getBoolean(
254                                    actionRequest, "deleteLogo");
255    
256                            if (deleteLogo) {
257                                    OrganizationServiceUtil.deleteLogo(
258                                            organization.getOrganizationId());
259                            }
260                    }
261    
262                    // Layout set prototypes
263    
264                    long publicLayoutSetPrototypeId = ParamUtil.getLong(
265                            actionRequest, "publicLayoutSetPrototypeId");
266                    long privateLayoutSetPrototypeId = ParamUtil.getLong(
267                            actionRequest, "privateLayoutSetPrototypeId");
268                    boolean publicLayoutSetPrototypeLinkEnabled = ParamUtil.getBoolean(
269                            actionRequest, "publicLayoutSetPrototypeLinkEnabled",
270                            (publicLayoutSetPrototypeId > 0));
271                    boolean privateLayoutSetPrototypeLinkEnabled = ParamUtil.getBoolean(
272                            actionRequest, "privateLayoutSetPrototypeLinkEnabled",
273                            (privateLayoutSetPrototypeId > 0));
274    
275                    Group organizationGroup = organization.getGroup();
276    
277                    if (GroupPermissionUtil.contains(
278                                    themeDisplay.getPermissionChecker(),
279                                    organizationGroup.getGroupId(), ActionKeys.UPDATE)) {
280    
281                            SitesUtil.updateLayoutSetPrototypesLinks(
282                                    organizationGroup, publicLayoutSetPrototypeId,
283                                    privateLayoutSetPrototypeId,
284                                    publicLayoutSetPrototypeLinkEnabled,
285                                    privateLayoutSetPrototypeLinkEnabled);
286                    }
287    
288                    // Reminder queries
289    
290                    String reminderQueries = actionRequest.getParameter("reminderQueries");
291    
292                    PortletPreferences portletPreferences = organization.getPreferences();
293    
294                    LocalizationUtil.setLocalizedPreferencesValues(
295                            actionRequest, portletPreferences, "reminderQueries");
296    
297                    portletPreferences.setValue("reminderQueries", reminderQueries);
298    
299                    portletPreferences.store();
300    
301                    return organization;
302            }
303    
304    }