1   /**
2    * Copyright (c) 2000-2009 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   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portlet.enterpriseadmin.action;
21  
22  import com.liferay.portal.EmailAddressException;
23  import com.liferay.portal.NoSuchEmailAddressException;
24  import com.liferay.portal.NoSuchListTypeException;
25  import com.liferay.portal.kernel.servlet.SessionErrors;
26  import com.liferay.portal.kernel.util.Constants;
27  import com.liferay.portal.kernel.util.ParamUtil;
28  import com.liferay.portal.security.auth.PrincipalException;
29  import com.liferay.portal.service.EmailAddressServiceUtil;
30  import com.liferay.portal.struts.PortletAction;
31  
32  import javax.portlet.ActionRequest;
33  import javax.portlet.ActionResponse;
34  import javax.portlet.PortletConfig;
35  import javax.portlet.RenderRequest;
36  import javax.portlet.RenderResponse;
37  
38  import org.apache.struts.action.ActionForm;
39  import org.apache.struts.action.ActionForward;
40  import org.apache.struts.action.ActionMapping;
41  
42  /**
43   * <a href="EditEmailAddressAction.java.html"><b><i>View Source</i></b></a>
44   *
45   * @author Brian Wing Shun Chan
46   * @author Alexander Chow
47   *
48   */
49  public class EditEmailAddressAction extends PortletAction {
50  
51      public void processAction(
52              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
53              ActionRequest actionRequest, ActionResponse actionResponse)
54          throws Exception {
55  
56          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
57  
58          try {
59              if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
60                  updateEmailAddress(actionRequest);
61              }
62              else if (cmd.equals(Constants.DELETE)) {
63                  deleteEmailAddress(actionRequest);
64              }
65  
66              sendRedirect(actionRequest, actionResponse);
67          }
68          catch (Exception e) {
69              if (e instanceof NoSuchEmailAddressException ||
70                  e instanceof PrincipalException) {
71  
72                  SessionErrors.add(actionRequest, e.getClass().getName());
73  
74                  setForward(actionRequest, "portlet.enterprise_admin.error");
75              }
76              else if (e instanceof EmailAddressException ||
77                       e instanceof NoSuchListTypeException) {
78  
79                  SessionErrors.add(actionRequest, e.getClass().getName());
80              }
81              else {
82                  throw e;
83              }
84          }
85      }
86  
87      public ActionForward render(
88              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
89              RenderRequest renderRequest, RenderResponse renderResponse)
90          throws Exception {
91  
92          try {
93              ActionUtil.getEmailAddress(renderRequest);
94          }
95          catch (Exception e) {
96              if (e instanceof NoSuchEmailAddressException ||
97                  e instanceof PrincipalException) {
98  
99                  SessionErrors.add(renderRequest, e.getClass().getName());
100 
101                 return mapping.findForward("portlet.enterprise_admin.error");
102             }
103             else {
104                 throw e;
105             }
106         }
107 
108         return mapping.findForward(getForward(
109             renderRequest, "portlet.enterprise_admin.edit_email_address"));
110     }
111 
112     protected void deleteEmailAddress(ActionRequest actionRequest)
113         throws Exception {
114 
115         long emailAddressId = ParamUtil.getLong(
116             actionRequest, "emailAddressId");
117 
118         EmailAddressServiceUtil.deleteEmailAddress(emailAddressId);
119     }
120 
121     protected void updateEmailAddress(ActionRequest actionRequest)
122         throws Exception {
123 
124         long emailAddressId = ParamUtil.getLong(
125             actionRequest, "emailAddressId");
126 
127         String className = ParamUtil.getString(actionRequest, "className");
128         long classPK = ParamUtil.getLong(actionRequest, "classPK");
129 
130         String address = ParamUtil.getString(actionRequest, "address");
131         int typeId = ParamUtil.getInteger(actionRequest, "typeId");
132         boolean primary = ParamUtil.getBoolean(actionRequest, "primary");
133 
134         if (emailAddressId <= 0) {
135 
136             // Add email address
137 
138             EmailAddressServiceUtil.addEmailAddress(
139                 className, classPK, address, typeId, primary);
140         }
141         else {
142 
143             // Update email address
144 
145             EmailAddressServiceUtil.updateEmailAddress(
146                 emailAddressId, address, typeId, primary);
147         }
148     }
149 
150 }