1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions 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.NoSuchListTypeException;
26  import com.liferay.portal.NoSuchOrgLaborException;
27  import com.liferay.portal.kernel.servlet.SessionErrors;
28  import com.liferay.portal.kernel.util.Constants;
29  import com.liferay.portal.kernel.util.ParamUtil;
30  import com.liferay.portal.security.auth.PrincipalException;
31  import com.liferay.portal.service.OrgLaborServiceUtil;
32  import com.liferay.portal.struts.PortletAction;
33  
34  import javax.portlet.ActionRequest;
35  import javax.portlet.ActionResponse;
36  import javax.portlet.PortletConfig;
37  import javax.portlet.RenderRequest;
38  import javax.portlet.RenderResponse;
39  
40  import org.apache.struts.action.ActionForm;
41  import org.apache.struts.action.ActionForward;
42  import org.apache.struts.action.ActionMapping;
43  
44  /**
45   * <a href="EditOrgLaborAction.java.html"><b><i>View Source</i></b></a>
46   *
47   * @author Brian Wing Shun Chan
48   *
49   */
50  public class EditOrgLaborAction extends PortletAction {
51  
52      public void processAction(
53              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
54              ActionRequest actionRequest, ActionResponse actionResponse)
55          throws Exception {
56  
57          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
58  
59          try {
60              if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
61                  updateOrgLabor(actionRequest);
62              }
63              else if (cmd.equals(Constants.DELETE)) {
64                  deleteOrgLabor(actionRequest);
65              }
66  
67              sendRedirect(actionRequest, actionResponse);
68          }
69          catch (Exception e) {
70              if (e instanceof NoSuchOrgLaborException ||
71                  e instanceof PrincipalException) {
72  
73                  SessionErrors.add(actionRequest, e.getClass().getName());
74  
75                  setForward(actionRequest, "portlet.enterprise_admin.error");
76              }
77              else if (e instanceof NoSuchListTypeException) {
78                  SessionErrors.add(actionRequest, e.getClass().getName());
79              }
80              else {
81                  throw e;
82              }
83          }
84      }
85  
86      public ActionForward render(
87              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
88              RenderRequest renderRequest, RenderResponse renderResponse)
89          throws Exception {
90  
91          try {
92              ActionUtil.getOrgLabor(renderRequest);
93          }
94          catch (Exception e) {
95              if (e instanceof NoSuchOrgLaborException ||
96                  e instanceof PrincipalException) {
97  
98                  SessionErrors.add(renderRequest, e.getClass().getName());
99  
100                 return mapping.findForward("portlet.enterprise_admin.error");
101             }
102             else {
103                 throw e;
104             }
105         }
106 
107         return mapping.findForward(getForward(
108             renderRequest, "portlet.enterprise_admin.edit_org_labor"));
109     }
110 
111     protected void deleteOrgLabor(ActionRequest actionRequest)
112         throws Exception {
113 
114         long orgLaborId = ParamUtil.getLong(actionRequest, "orgLaborId");
115 
116         OrgLaborServiceUtil.deleteOrgLabor(orgLaborId);
117     }
118 
119     protected void updateOrgLabor(ActionRequest actionRequest)
120         throws Exception {
121 
122         long orgLaborId = ParamUtil.getLong(actionRequest, "orgLaborId");
123 
124         long organizationId = ParamUtil.getLong(
125             actionRequest, "organizationId");
126         int typeId = ParamUtil.getInteger(actionRequest, "typeId");
127 
128         int sunOpen = ParamUtil.getInteger(actionRequest, "sunOpen");
129         int sunClose = ParamUtil.getInteger(actionRequest, "sunClose");
130 
131         int monOpen = ParamUtil.getInteger(actionRequest, "monOpen");
132         int monClose = ParamUtil.getInteger(actionRequest, "monClose");
133 
134         int tueOpen = ParamUtil.getInteger(actionRequest, "tueOpen");
135         int tueClose = ParamUtil.getInteger(actionRequest, "tueClose");
136 
137         int wedOpen = ParamUtil.getInteger(actionRequest, "wedOpen");
138         int wedClose = ParamUtil.getInteger(actionRequest, "wedClose");
139 
140         int thuOpen = ParamUtil.getInteger(actionRequest, "thuOpen");
141         int thuClose = ParamUtil.getInteger(actionRequest, "thuClose");
142 
143         int friOpen = ParamUtil.getInteger(actionRequest, "friOpen");
144         int friClose = ParamUtil.getInteger(actionRequest, "friClose");
145 
146         int satOpen = ParamUtil.getInteger(actionRequest, "satOpen");
147         int satClose = ParamUtil.getInteger(actionRequest, "satClose");
148 
149         if (orgLaborId <= 0) {
150 
151             // Add organization labor
152 
153             OrgLaborServiceUtil.addOrgLabor(
154                 organizationId, typeId, sunOpen, sunClose, monOpen, monClose,
155                 tueOpen, tueClose, wedOpen, wedClose, thuOpen, thuClose,
156                 friOpen, friClose, satOpen, satClose);
157         }
158         else {
159 
160             // Update organization labor
161 
162             OrgLaborServiceUtil.updateOrgLabor(
163                 orgLaborId, typeId, sunOpen, sunClose, monOpen, monClose,
164                 tueOpen, tueClose, wedOpen, wedClose, thuOpen, thuClose,
165                 friOpen, friClose, satOpen, satClose);
166         }
167     }
168 
169 }