1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   * 
13   */
14  
15  package com.liferay.portlet.enterpriseadmin.action;
16  
17  import com.liferay.portal.EmailAddressException;
18  import com.liferay.portal.NoSuchEmailAddressException;
19  import com.liferay.portal.NoSuchListTypeException;
20  import com.liferay.portal.kernel.servlet.SessionErrors;
21  import com.liferay.portal.kernel.util.Constants;
22  import com.liferay.portal.kernel.util.ParamUtil;
23  import com.liferay.portal.security.auth.PrincipalException;
24  import com.liferay.portal.service.EmailAddressServiceUtil;
25  import com.liferay.portal.struts.PortletAction;
26  
27  import javax.portlet.ActionRequest;
28  import javax.portlet.ActionResponse;
29  import javax.portlet.PortletConfig;
30  import javax.portlet.RenderRequest;
31  import javax.portlet.RenderResponse;
32  
33  import org.apache.struts.action.ActionForm;
34  import org.apache.struts.action.ActionForward;
35  import org.apache.struts.action.ActionMapping;
36  
37  /**
38   * <a href="EditEmailAddressAction.java.html"><b><i>View Source</i></b></a>
39   *
40   * @author Brian Wing Shun Chan
41   * @author Alexander Chow
42   */
43  public class EditEmailAddressAction extends PortletAction {
44  
45      public void processAction(
46              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
47              ActionRequest actionRequest, ActionResponse actionResponse)
48          throws Exception {
49  
50          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
51  
52          try {
53              if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
54                  updateEmailAddress(actionRequest);
55              }
56              else if (cmd.equals(Constants.DELETE)) {
57                  deleteEmailAddress(actionRequest);
58              }
59  
60              sendRedirect(actionRequest, actionResponse);
61          }
62          catch (Exception e) {
63              if (e instanceof NoSuchEmailAddressException ||
64                  e instanceof PrincipalException) {
65  
66                  SessionErrors.add(actionRequest, e.getClass().getName());
67  
68                  setForward(actionRequest, "portlet.enterprise_admin.error");
69              }
70              else if (e instanceof EmailAddressException ||
71                       e instanceof NoSuchListTypeException) {
72  
73                  SessionErrors.add(actionRequest, e.getClass().getName());
74              }
75              else {
76                  throw e;
77              }
78          }
79      }
80  
81      public ActionForward render(
82              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
83              RenderRequest renderRequest, RenderResponse renderResponse)
84          throws Exception {
85  
86          try {
87              ActionUtil.getEmailAddress(renderRequest);
88          }
89          catch (Exception e) {
90              if (e instanceof NoSuchEmailAddressException ||
91                  e instanceof PrincipalException) {
92  
93                  SessionErrors.add(renderRequest, e.getClass().getName());
94  
95                  return mapping.findForward("portlet.enterprise_admin.error");
96              }
97              else {
98                  throw e;
99              }
100         }
101 
102         return mapping.findForward(getForward(
103             renderRequest, "portlet.enterprise_admin.edit_email_address"));
104     }
105 
106     protected void deleteEmailAddress(ActionRequest actionRequest)
107         throws Exception {
108 
109         long emailAddressId = ParamUtil.getLong(
110             actionRequest, "emailAddressId");
111 
112         EmailAddressServiceUtil.deleteEmailAddress(emailAddressId);
113     }
114 
115     protected void updateEmailAddress(ActionRequest actionRequest)
116         throws Exception {
117 
118         long emailAddressId = ParamUtil.getLong(
119             actionRequest, "emailAddressId");
120 
121         String className = ParamUtil.getString(actionRequest, "className");
122         long classPK = ParamUtil.getLong(actionRequest, "classPK");
123 
124         String address = ParamUtil.getString(actionRequest, "address");
125         int typeId = ParamUtil.getInteger(actionRequest, "typeId");
126         boolean primary = ParamUtil.getBoolean(actionRequest, "primary");
127 
128         if (emailAddressId <= 0) {
129 
130             // Add email address
131 
132             EmailAddressServiceUtil.addEmailAddress(
133                 className, classPK, address, typeId, primary);
134         }
135         else {
136 
137             // Update email address
138 
139             EmailAddressServiceUtil.updateEmailAddress(
140                 emailAddressId, address, typeId, primary);
141         }
142     }
143 
144 }