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.NoSuchListTypeException;
23  import com.liferay.portal.NoSuchOrgLaborException;
24  import com.liferay.portal.kernel.servlet.SessionErrors;
25  import com.liferay.portal.kernel.util.Constants;
26  import com.liferay.portal.kernel.util.ParamUtil;
27  import com.liferay.portal.security.auth.PrincipalException;
28  import com.liferay.portal.service.OrgLaborServiceUtil;
29  import com.liferay.portal.struts.PortletAction;
30  
31  import javax.portlet.ActionRequest;
32  import javax.portlet.ActionResponse;
33  import javax.portlet.PortletConfig;
34  import javax.portlet.RenderRequest;
35  import javax.portlet.RenderResponse;
36  
37  import org.apache.struts.action.ActionForm;
38  import org.apache.struts.action.ActionForward;
39  import org.apache.struts.action.ActionMapping;
40  
41  /**
42   * <a href="EditOrgLaborAction.java.html"><b><i>View Source</i></b></a>
43   *
44   * @author Brian Wing Shun Chan
45   *
46   */
47  public class EditOrgLaborAction extends PortletAction {
48  
49      public void processAction(
50              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
51              ActionRequest actionRequest, ActionResponse actionResponse)
52          throws Exception {
53  
54          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
55  
56          try {
57              if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
58                  updateOrgLabor(actionRequest);
59              }
60              else if (cmd.equals(Constants.DELETE)) {
61                  deleteOrgLabor(actionRequest);
62              }
63  
64              sendRedirect(actionRequest, actionResponse);
65          }
66          catch (Exception e) {
67              if (e instanceof NoSuchOrgLaborException ||
68                  e instanceof PrincipalException) {
69  
70                  SessionErrors.add(actionRequest, e.getClass().getName());
71  
72                  setForward(actionRequest, "portlet.enterprise_admin.error");
73              }
74              else if (e instanceof NoSuchListTypeException) {
75                  SessionErrors.add(actionRequest, e.getClass().getName());
76              }
77              else {
78                  throw e;
79              }
80          }
81      }
82  
83      public ActionForward render(
84              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
85              RenderRequest renderRequest, RenderResponse renderResponse)
86          throws Exception {
87  
88          try {
89              ActionUtil.getOrgLabor(renderRequest);
90          }
91          catch (Exception e) {
92              if (e instanceof NoSuchOrgLaborException ||
93                  e instanceof PrincipalException) {
94  
95                  SessionErrors.add(renderRequest, e.getClass().getName());
96  
97                  return mapping.findForward("portlet.enterprise_admin.error");
98              }
99              else {
100                 throw e;
101             }
102         }
103 
104         return mapping.findForward(getForward(
105             renderRequest, "portlet.enterprise_admin.edit_org_labor"));
106     }
107 
108     protected void deleteOrgLabor(ActionRequest actionRequest)
109         throws Exception {
110 
111         long orgLaborId = ParamUtil.getLong(actionRequest, "orgLaborId");
112 
113         OrgLaborServiceUtil.deleteOrgLabor(orgLaborId);
114     }
115 
116     protected void updateOrgLabor(ActionRequest actionRequest)
117         throws Exception {
118 
119         long orgLaborId = ParamUtil.getLong(actionRequest, "orgLaborId");
120 
121         long organizationId = ParamUtil.getLong(
122             actionRequest, "organizationId");
123         int typeId = ParamUtil.getInteger(actionRequest, "typeId");
124 
125         int sunOpen = ParamUtil.getInteger(actionRequest, "sunOpen");
126         int sunClose = ParamUtil.getInteger(actionRequest, "sunClose");
127 
128         int monOpen = ParamUtil.getInteger(actionRequest, "monOpen");
129         int monClose = ParamUtil.getInteger(actionRequest, "monClose");
130 
131         int tueOpen = ParamUtil.getInteger(actionRequest, "tueOpen");
132         int tueClose = ParamUtil.getInteger(actionRequest, "tueClose");
133 
134         int wedOpen = ParamUtil.getInteger(actionRequest, "wedOpen");
135         int wedClose = ParamUtil.getInteger(actionRequest, "wedClose");
136 
137         int thuOpen = ParamUtil.getInteger(actionRequest, "thuOpen");
138         int thuClose = ParamUtil.getInteger(actionRequest, "thuClose");
139 
140         int friOpen = ParamUtil.getInteger(actionRequest, "friOpen");
141         int friClose = ParamUtil.getInteger(actionRequest, "friClose");
142 
143         int satOpen = ParamUtil.getInteger(actionRequest, "satOpen");
144         int satClose = ParamUtil.getInteger(actionRequest, "satClose");
145 
146         if (orgLaborId <= 0) {
147 
148             // Add organization labor
149 
150             OrgLaborServiceUtil.addOrgLabor(
151                 organizationId, typeId, sunOpen, sunClose, monOpen, monClose,
152                 tueOpen, tueClose, wedOpen, wedClose, thuOpen, thuClose,
153                 friOpen, friClose, satOpen, satClose);
154         }
155         else {
156 
157             // Update organization labor
158 
159             OrgLaborServiceUtil.updateOrgLabor(
160                 orgLaborId, typeId, sunOpen, sunClose, monOpen, monClose,
161                 tueOpen, tueClose, wedOpen, wedClose, thuOpen, thuClose,
162                 friOpen, friClose, satOpen, satClose);
163         }
164     }
165 
166 }