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.EmailAddressException;
26  import com.liferay.portal.NoSuchEmailAddressException;
27  import com.liferay.portal.NoSuchListTypeException;
28  import com.liferay.portal.kernel.servlet.SessionErrors;
29  import com.liferay.portal.kernel.util.Constants;
30  import com.liferay.portal.kernel.util.ParamUtil;
31  import com.liferay.portal.security.auth.PrincipalException;
32  import com.liferay.portal.service.EmailAddressServiceUtil;
33  import com.liferay.portal.struts.PortletAction;
34  
35  import javax.portlet.ActionRequest;
36  import javax.portlet.ActionResponse;
37  import javax.portlet.PortletConfig;
38  import javax.portlet.RenderRequest;
39  import javax.portlet.RenderResponse;
40  
41  import org.apache.struts.action.ActionForm;
42  import org.apache.struts.action.ActionForward;
43  import org.apache.struts.action.ActionMapping;
44  
45  /**
46   * <a href="EditEmailAddressAction.java.html"><b><i>View Source</i></b></a>
47   *
48   * @author Brian Wing Shun Chan
49   * @author Alexander Chow
50   */
51  public class EditEmailAddressAction extends PortletAction {
52  
53      public void processAction(
54              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
55              ActionRequest actionRequest, ActionResponse actionResponse)
56          throws Exception {
57  
58          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
59  
60          try {
61              if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
62                  updateEmailAddress(actionRequest);
63              }
64              else if (cmd.equals(Constants.DELETE)) {
65                  deleteEmailAddress(actionRequest);
66              }
67  
68              sendRedirect(actionRequest, actionResponse);
69          }
70          catch (Exception e) {
71              if (e instanceof NoSuchEmailAddressException ||
72                  e instanceof PrincipalException) {
73  
74                  SessionErrors.add(actionRequest, e.getClass().getName());
75  
76                  setForward(actionRequest, "portlet.enterprise_admin.error");
77              }
78              else if (e instanceof EmailAddressException ||
79                       e instanceof NoSuchListTypeException) {
80  
81                  SessionErrors.add(actionRequest, e.getClass().getName());
82              }
83              else {
84                  throw e;
85              }
86          }
87      }
88  
89      public ActionForward render(
90              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
91              RenderRequest renderRequest, RenderResponse renderResponse)
92          throws Exception {
93  
94          try {
95              ActionUtil.getEmailAddress(renderRequest);
96          }
97          catch (Exception e) {
98              if (e instanceof NoSuchEmailAddressException ||
99                  e instanceof PrincipalException) {
100 
101                 SessionErrors.add(renderRequest, e.getClass().getName());
102 
103                 return mapping.findForward("portlet.enterprise_admin.error");
104             }
105             else {
106                 throw e;
107             }
108         }
109 
110         return mapping.findForward(getForward(
111             renderRequest, "portlet.enterprise_admin.edit_email_address"));
112     }
113 
114     protected void deleteEmailAddress(ActionRequest actionRequest)
115         throws Exception {
116 
117         long emailAddressId = ParamUtil.getLong(
118             actionRequest, "emailAddressId");
119 
120         EmailAddressServiceUtil.deleteEmailAddress(emailAddressId);
121     }
122 
123     protected void updateEmailAddress(ActionRequest actionRequest)
124         throws Exception {
125 
126         long emailAddressId = ParamUtil.getLong(
127             actionRequest, "emailAddressId");
128 
129         String className = ParamUtil.getString(actionRequest, "className");
130         long classPK = ParamUtil.getLong(actionRequest, "classPK");
131 
132         String address = ParamUtil.getString(actionRequest, "address");
133         int typeId = ParamUtil.getInteger(actionRequest, "typeId");
134         boolean primary = ParamUtil.getBoolean(actionRequest, "primary");
135 
136         if (emailAddressId <= 0) {
137 
138             // Add email address
139 
140             EmailAddressServiceUtil.addEmailAddress(
141                 className, classPK, address, typeId, primary);
142         }
143         else {
144 
145             // Update email address
146 
147             EmailAddressServiceUtil.updateEmailAddress(
148                 emailAddressId, address, typeId, primary);
149         }
150     }
151 
152 }