001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.enterpriseadmin.action;
016    
017    import com.liferay.portal.NoSuchListTypeException;
018    import com.liferay.portal.NoSuchOrgLaborException;
019    import com.liferay.portal.kernel.servlet.SessionErrors;
020    import com.liferay.portal.kernel.util.Constants;
021    import com.liferay.portal.kernel.util.ParamUtil;
022    import com.liferay.portal.security.auth.PrincipalException;
023    import com.liferay.portal.service.OrgLaborServiceUtil;
024    import com.liferay.portal.struts.PortletAction;
025    
026    import javax.portlet.ActionRequest;
027    import javax.portlet.ActionResponse;
028    import javax.portlet.PortletConfig;
029    import javax.portlet.RenderRequest;
030    import javax.portlet.RenderResponse;
031    
032    import org.apache.struts.action.ActionForm;
033    import org.apache.struts.action.ActionForward;
034    import org.apache.struts.action.ActionMapping;
035    
036    /**
037     * @author Brian Wing Shun Chan
038     */
039    public class EditOrgLaborAction extends PortletAction {
040    
041            public void processAction(
042                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
043                            ActionRequest actionRequest, ActionResponse actionResponse)
044                    throws Exception {
045    
046                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
047    
048                    try {
049                            if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
050                                    updateOrgLabor(actionRequest);
051                            }
052                            else if (cmd.equals(Constants.DELETE)) {
053                                    deleteOrgLabor(actionRequest);
054                            }
055    
056                            sendRedirect(actionRequest, actionResponse);
057                    }
058                    catch (Exception e) {
059                            if (e instanceof NoSuchOrgLaborException ||
060                                    e instanceof PrincipalException) {
061    
062                                    SessionErrors.add(actionRequest, e.getClass().getName());
063    
064                                    setForward(actionRequest, "portlet.enterprise_admin.error");
065                            }
066                            else if (e instanceof NoSuchListTypeException) {
067                                    SessionErrors.add(actionRequest, e.getClass().getName());
068                            }
069                            else {
070                                    throw e;
071                            }
072                    }
073            }
074    
075            public ActionForward render(
076                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
077                            RenderRequest renderRequest, RenderResponse renderResponse)
078                    throws Exception {
079    
080                    try {
081                            ActionUtil.getOrgLabor(renderRequest);
082                    }
083                    catch (Exception e) {
084                            if (e instanceof NoSuchOrgLaborException ||
085                                    e instanceof PrincipalException) {
086    
087                                    SessionErrors.add(renderRequest, e.getClass().getName());
088    
089                                    return mapping.findForward("portlet.enterprise_admin.error");
090                            }
091                            else {
092                                    throw e;
093                            }
094                    }
095    
096                    return mapping.findForward(getForward(
097                            renderRequest, "portlet.enterprise_admin.edit_org_labor"));
098            }
099    
100            protected void deleteOrgLabor(ActionRequest actionRequest)
101                    throws Exception {
102    
103                    long orgLaborId = ParamUtil.getLong(actionRequest, "orgLaborId");
104    
105                    OrgLaborServiceUtil.deleteOrgLabor(orgLaborId);
106            }
107    
108            protected void updateOrgLabor(ActionRequest actionRequest)
109                    throws Exception {
110    
111                    long orgLaborId = ParamUtil.getLong(actionRequest, "orgLaborId");
112    
113                    long organizationId = ParamUtil.getLong(
114                            actionRequest, "organizationId");
115                    int typeId = ParamUtil.getInteger(actionRequest, "typeId");
116    
117                    int sunOpen = ParamUtil.getInteger(actionRequest, "sunOpen");
118                    int sunClose = ParamUtil.getInteger(actionRequest, "sunClose");
119    
120                    int monOpen = ParamUtil.getInteger(actionRequest, "monOpen");
121                    int monClose = ParamUtil.getInteger(actionRequest, "monClose");
122    
123                    int tueOpen = ParamUtil.getInteger(actionRequest, "tueOpen");
124                    int tueClose = ParamUtil.getInteger(actionRequest, "tueClose");
125    
126                    int wedOpen = ParamUtil.getInteger(actionRequest, "wedOpen");
127                    int wedClose = ParamUtil.getInteger(actionRequest, "wedClose");
128    
129                    int thuOpen = ParamUtil.getInteger(actionRequest, "thuOpen");
130                    int thuClose = ParamUtil.getInteger(actionRequest, "thuClose");
131    
132                    int friOpen = ParamUtil.getInteger(actionRequest, "friOpen");
133                    int friClose = ParamUtil.getInteger(actionRequest, "friClose");
134    
135                    int satOpen = ParamUtil.getInteger(actionRequest, "satOpen");
136                    int satClose = ParamUtil.getInteger(actionRequest, "satClose");
137    
138                    if (orgLaborId <= 0) {
139    
140                            // Add organization labor
141    
142                            OrgLaborServiceUtil.addOrgLabor(
143                                    organizationId, typeId, sunOpen, sunClose, monOpen, monClose,
144                                    tueOpen, tueClose, wedOpen, wedClose, thuOpen, thuClose,
145                                    friOpen, friClose, satOpen, satClose);
146                    }
147                    else {
148    
149                            // Update organization labor
150    
151                            OrgLaborServiceUtil.updateOrgLabor(
152                                    orgLaborId, typeId, sunOpen, sunClose, monOpen, monClose,
153                                    tueOpen, tueClose, wedOpen, wedClose, thuOpen, thuClose,
154                                    friOpen, friClose, satOpen, satClose);
155                    }
156            }
157    
158    }