1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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.AddressCityException;
26  import com.liferay.portal.AddressStreetException;
27  import com.liferay.portal.AddressZipException;
28  import com.liferay.portal.DuplicateOrganizationException;
29  import com.liferay.portal.EmailAddressException;
30  import com.liferay.portal.NoSuchCountryException;
31  import com.liferay.portal.NoSuchListTypeException;
32  import com.liferay.portal.NoSuchOrganizationException;
33  import com.liferay.portal.NoSuchRegionException;
34  import com.liferay.portal.OrganizationNameException;
35  import com.liferay.portal.OrganizationParentException;
36  import com.liferay.portal.PhoneNumberException;
37  import com.liferay.portal.RequiredOrganizationException;
38  import com.liferay.portal.WebsiteURLException;
39  import com.liferay.portal.kernel.servlet.SessionErrors;
40  import com.liferay.portal.kernel.util.Constants;
41  import com.liferay.portal.kernel.util.HttpUtil;
42  import com.liferay.portal.kernel.util.ParamUtil;
43  import com.liferay.portal.kernel.util.StringUtil;
44  import com.liferay.portal.model.Address;
45  import com.liferay.portal.model.EmailAddress;
46  import com.liferay.portal.model.OrgLabor;
47  import com.liferay.portal.model.Organization;
48  import com.liferay.portal.model.OrganizationConstants;
49  import com.liferay.portal.model.Phone;
50  import com.liferay.portal.model.Website;
51  import com.liferay.portal.security.auth.PrincipalException;
52  import com.liferay.portal.service.OrganizationServiceUtil;
53  import com.liferay.portal.service.ServiceContext;
54  import com.liferay.portal.service.ServiceContextFactory;
55  import com.liferay.portal.struts.PortletAction;
56  import com.liferay.portlet.enterpriseadmin.util.EnterpriseAdminUtil;
57  import com.liferay.util.LocalizationUtil;
58  
59  import java.util.List;
60  
61  import javax.portlet.ActionRequest;
62  import javax.portlet.ActionResponse;
63  import javax.portlet.PortletConfig;
64  import javax.portlet.PortletPreferences;
65  import javax.portlet.RenderRequest;
66  import javax.portlet.RenderResponse;
67  
68  import org.apache.struts.action.ActionForm;
69  import org.apache.struts.action.ActionForward;
70  import org.apache.struts.action.ActionMapping;
71  
72  /**
73   * <a href="EditOrganizationAction.java.html"><b><i>View Source</i></b></a>
74   *
75   * @author Brian Wing Shun Chan
76   * @author Julio Camarero
77   * @author Jorge Ferrer
78   */
79  public class EditOrganizationAction extends PortletAction {
80  
81      public void processAction(
82              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
83              ActionRequest actionRequest, ActionResponse actionResponse)
84          throws Exception {
85  
86          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
87  
88          try {
89              Organization organization = null;
90  
91              if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
92                  organization = updateOrganization(actionRequest);
93              }
94              else if (cmd.equals(Constants.DELETE)) {
95                  deleteOrganizations(actionRequest);
96              }
97  
98              String redirect = ParamUtil.getString(actionRequest, "redirect");
99  
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().getName());
113 
114                 setForward(actionRequest, "portlet.enterprise_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().getName());
139                 }
140 
141                 if (e instanceof RequiredOrganizationException) {
142                     actionResponse.sendRedirect(
143                         ParamUtil.getString(actionRequest, "redirect"));
144                 }
145             }
146             else {
147                 throw e;
148             }
149         }
150     }
151 
152     public ActionForward render(
153             ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
154             RenderRequest renderRequest, RenderResponse renderResponse)
155         throws Exception {
156 
157         try {
158             ActionUtil.getOrganization(renderRequest);
159         }
160         catch (Exception e) {
161             if (e instanceof NoSuchOrganizationException ||
162                 e instanceof PrincipalException) {
163 
164                 SessionErrors.add(renderRequest, e.getClass().getName());
165 
166                 return mapping.findForward("portlet.enterprise_admin.error");
167             }
168             else {
169                 throw e;
170             }
171         }
172 
173         return mapping.findForward(getForward(
174             renderRequest, "portlet.enterprise_admin.edit_organization"));
175     }
176 
177     protected void deleteOrganizations(ActionRequest actionRequest)
178         throws Exception {
179 
180         long[] deleteOrganizationIds = StringUtil.split(
181             ParamUtil.getString(actionRequest, "deleteOrganizationIds"), 0L);
182 
183         for (int i = 0; i < deleteOrganizationIds.length; i++) {
184             OrganizationServiceUtil.deleteOrganization(
185                 deleteOrganizationIds[i]);
186         }
187     }
188 
189     protected Organization updateOrganization(ActionRequest actionRequest)
190         throws Exception {
191 
192         long organizationId = ParamUtil.getLong(
193             actionRequest, "organizationId");
194 
195         long parentOrganizationId = ParamUtil.getLong(
196             actionRequest, "parentOrganizationSearchContainerPrimaryKeys",
197             OrganizationConstants.DEFAULT_PARENT_ORGANIZATION_ID);
198         String name = ParamUtil.getString(actionRequest, "name");
199         boolean recursable = ParamUtil.getBoolean(actionRequest, "recursable");
200         int statusId = ParamUtil.getInteger(actionRequest, "statusId");
201         String type = ParamUtil.getString(actionRequest, "type");
202         long regionId = ParamUtil.getLong(actionRequest, "regionId");
203         long countryId = ParamUtil.getLong(actionRequest, "countryId");
204         String comments = ParamUtil.getString(actionRequest, "comments");
205         List<Address> addresses = EnterpriseAdminUtil.getAddresses(
206             actionRequest);
207         List<EmailAddress> emailAddresses =
208             EnterpriseAdminUtil.getEmailAddresses(actionRequest);
209         List<OrgLabor> orgLabors = EnterpriseAdminUtil.getOrgLabors(
210             actionRequest);
211         List<Phone> phones = EnterpriseAdminUtil.getPhones(actionRequest);
212         List<Website> websites = EnterpriseAdminUtil.getWebsites(actionRequest);
213 
214         ServiceContext serviceContext = ServiceContextFactory.getInstance(
215             Organization.class.getName(), actionRequest);
216 
217         Organization organization = null;
218 
219         if (organizationId <= 0) {
220 
221             // Add organization
222 
223             organization = OrganizationServiceUtil.addOrganization(
224                 parentOrganizationId, name, type, recursable, regionId,
225                 countryId, statusId, comments, addresses, emailAddresses,
226                 orgLabors, phones, websites, serviceContext);
227         }
228         else {
229 
230             // Update organization
231 
232             organization = OrganizationServiceUtil.updateOrganization(
233                 organizationId, parentOrganizationId, name, type,
234                 recursable, regionId, countryId, statusId, comments, addresses,
235                 emailAddresses, orgLabors, phones, websites, serviceContext);
236 
237             boolean deleteLogo = ParamUtil.getBoolean(
238                 actionRequest, "deleteLogo");
239 
240             if (deleteLogo) {
241                 OrganizationServiceUtil.deleteLogo(
242                     organization.getOrganizationId());
243             }
244         }
245 
246         String reminderQueries = actionRequest.getParameter("reminderQueries");
247 
248         PortletPreferences preferences = organization.getPreferences();
249 
250         preferences.setValue("reminderQueries", reminderQueries);
251 
252         LocalizationUtil.setLocalizedPreferencesValues(
253             actionRequest, preferences, "reminderQueries");
254 
255         preferences.store();
256 
257         return organization;
258     }
259 
260 }