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.NoSuchListTypeException;
18  import com.liferay.portal.NoSuchPhoneException;
19  import com.liferay.portal.PhoneNumberException;
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.PhoneServiceUtil;
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="EditPhoneAction.java.html"><b><i>View Source</i></b></a>
39   *
40   * @author Brian Wing Shun Chan
41   */
42  public class EditPhoneAction extends PortletAction {
43  
44      public void processAction(
45              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
46              ActionRequest actionRequest, ActionResponse actionResponse)
47          throws Exception {
48  
49          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
50  
51          try {
52              if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
53                  updatePhone(actionRequest);
54              }
55              else if (cmd.equals(Constants.DELETE)) {
56                  deletePhone(actionRequest);
57              }
58  
59              sendRedirect(actionRequest, actionResponse);
60          }
61          catch (Exception e) {
62              if (e instanceof NoSuchPhoneException ||
63                  e instanceof PrincipalException) {
64  
65                  SessionErrors.add(actionRequest, e.getClass().getName());
66  
67                  setForward(actionRequest, "portlet.enterprise_admin.error");
68              }
69              else if (e instanceof NoSuchListTypeException ||
70                       e instanceof PhoneNumberException) {
71  
72                  SessionErrors.add(actionRequest, e.getClass().getName());
73              }
74              else {
75                  throw e;
76              }
77          }
78      }
79  
80      public ActionForward render(
81              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
82              RenderRequest renderRequest, RenderResponse renderResponse)
83          throws Exception {
84  
85          try {
86              ActionUtil.getPhone(renderRequest);
87          }
88          catch (Exception e) {
89              if (e instanceof NoSuchPhoneException ||
90                  e instanceof PrincipalException) {
91  
92                  SessionErrors.add(renderRequest, e.getClass().getName());
93  
94                  return mapping.findForward("portlet.enterprise_admin.error");
95              }
96              else {
97                  throw e;
98              }
99          }
100 
101         return mapping.findForward(
102             getForward(renderRequest, "portlet.enterprise_admin.edit_phone"));
103     }
104 
105     protected void deletePhone(ActionRequest actionRequest) throws Exception {
106         long phoneId = ParamUtil.getLong(actionRequest, "phoneId");
107 
108         PhoneServiceUtil.deletePhone(phoneId);
109     }
110 
111     protected void updatePhone(ActionRequest actionRequest) throws Exception {
112         long phoneId = ParamUtil.getLong(actionRequest, "phoneId");
113 
114         String className = ParamUtil.getString(actionRequest, "className");
115         long classPK = ParamUtil.getLong(actionRequest, "classPK");
116 
117         String number = ParamUtil.getString(actionRequest, "number");
118         String extension = ParamUtil.getString(actionRequest, "extension");
119         int typeId = ParamUtil.getInteger(actionRequest, "typeId");
120         boolean primary = ParamUtil.getBoolean(actionRequest, "primary");
121 
122         if (phoneId <= 0) {
123 
124             // Add phone
125 
126             PhoneServiceUtil.addPhone(
127                 className, classPK, number, extension, typeId, primary);
128         }
129         else {
130 
131             // Update phone
132 
133             PhoneServiceUtil.updatePhone(
134                 phoneId, number, extension, typeId, primary);
135         }
136     }
137 
138 }