1   /**
2    * Copyright (c) 2000-2008 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.util.Constants;
28  import com.liferay.portal.kernel.util.ParamUtil;
29  import com.liferay.portal.security.auth.PrincipalException;
30  import com.liferay.portal.service.OrgLaborServiceUtil;
31  import com.liferay.portal.struts.PortletAction;
32  import com.liferay.util.servlet.SessionErrors;
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 config,
54              ActionRequest req, ActionResponse res)
55          throws Exception {
56  
57          String cmd = ParamUtil.getString(req, Constants.CMD);
58  
59          try {
60              if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
61                  updateOrgLabor(req);
62              }
63              else if (cmd.equals(Constants.DELETE)) {
64                  deleteOrgLabor(req);
65              }
66  
67              sendRedirect(req, res);
68          }
69          catch (Exception e) {
70              if (e instanceof NoSuchOrgLaborException ||
71                  e instanceof PrincipalException) {
72  
73                  SessionErrors.add(req, e.getClass().getName());
74  
75                  setForward(req, "portlet.enterprise_admin.error");
76              }
77              else if (e instanceof NoSuchListTypeException) {
78                  SessionErrors.add(req, e.getClass().getName());
79              }
80              else {
81                  throw e;
82              }
83          }
84      }
85  
86      public ActionForward render(
87              ActionMapping mapping, ActionForm form, PortletConfig config,
88              RenderRequest req, RenderResponse res)
89          throws Exception {
90  
91          try {
92              ActionUtil.getOrgLabor(req);
93          }
94          catch (Exception e) {
95              if (e instanceof NoSuchOrgLaborException ||
96                  e instanceof PrincipalException) {
97  
98                  SessionErrors.add(req, 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(
108             getForward(req, "portlet.enterprise_admin.edit_org_labor"));
109     }
110 
111     protected void deleteOrgLabor(ActionRequest req) throws Exception {
112         long orgLaborId = ParamUtil.getLong(req, "orgLaborId");
113 
114         OrgLaborServiceUtil.deleteOrgLabor(orgLaborId);
115     }
116 
117     protected void updateOrgLabor(ActionRequest req) throws Exception {
118         long orgLaborId = ParamUtil.getLong(req, "orgLaborId");
119 
120         long organizationId = ParamUtil.getLong(req, "organizationId");
121         int typeId = ParamUtil.getInteger(req, "typeId");
122 
123         int sunOpen = ParamUtil.getInteger(req, "sunOpen");
124         int sunClose = ParamUtil.getInteger(req, "sunClose");
125 
126         int monOpen = ParamUtil.getInteger(req, "monOpen");
127         int monClose = ParamUtil.getInteger(req, "monClose");
128 
129         int tueOpen = ParamUtil.getInteger(req, "tueOpen");
130         int tueClose = ParamUtil.getInteger(req, "tueClose");
131 
132         int wedOpen = ParamUtil.getInteger(req, "wedOpen");
133         int wedClose = ParamUtil.getInteger(req, "wedClose");
134 
135         int thuOpen = ParamUtil.getInteger(req, "thuOpen");
136         int thuClose = ParamUtil.getInteger(req, "thuClose");
137 
138         int friOpen = ParamUtil.getInteger(req, "friOpen");
139         int friClose = ParamUtil.getInteger(req, "friClose");
140 
141         int satOpen = ParamUtil.getInteger(req, "satOpen");
142         int satClose = ParamUtil.getInteger(req, "satClose");
143 
144         if (orgLaborId <= 0) {
145 
146             // Add organization labor
147 
148             OrgLaborServiceUtil.addOrgLabor(
149                 organizationId, typeId, sunOpen, sunClose, monOpen, monClose,
150                 tueOpen, tueClose, wedOpen, wedClose, thuOpen, thuClose,
151                 friOpen, friClose, satOpen, satClose);
152         }
153         else {
154 
155             // Update organization labor
156 
157             OrgLaborServiceUtil.updateOrgLabor(
158                 orgLaborId, typeId, sunOpen, sunClose, monOpen, monClose,
159                 tueOpen, tueClose, wedOpen, wedClose, thuOpen, thuClose,
160                 friOpen, friClose, satOpen, satClose);
161         }
162     }
163 
164 }