001    /**
002     * Copyright (c) 2000-2013 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.usersadmin.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            @Override
042            public void processAction(
043                            ActionMapping actionMapping, ActionForm actionForm,
044                            PortletConfig portletConfig, ActionRequest actionRequest,
045                            ActionResponse actionResponse)
046                    throws Exception {
047    
048                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
049    
050                    try {
051                            if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
052                                    updateOrgLabor(actionRequest);
053                            }
054                            else if (cmd.equals(Constants.DELETE)) {
055                                    deleteOrgLabor(actionRequest);
056                            }
057    
058                            sendRedirect(actionRequest, actionResponse);
059                    }
060                    catch (Exception e) {
061                            if (e instanceof NoSuchOrgLaborException ||
062                                    e instanceof PrincipalException) {
063    
064                                    SessionErrors.add(actionRequest, e.getClass());
065    
066                                    setForward(actionRequest, "portlet.users_admin.error");
067                            }
068                            else if (e instanceof NoSuchListTypeException) {
069                                    SessionErrors.add(actionRequest, e.getClass());
070                            }
071                            else {
072                                    throw e;
073                            }
074                    }
075            }
076    
077            @Override
078            public ActionForward render(
079                            ActionMapping actionMapping, ActionForm actionForm,
080                            PortletConfig portletConfig, RenderRequest renderRequest,
081                            RenderResponse renderResponse)
082                    throws Exception {
083    
084                    try {
085                            ActionUtil.getOrgLabor(renderRequest);
086                    }
087                    catch (Exception e) {
088                            if (e instanceof NoSuchOrgLaborException ||
089                                    e instanceof PrincipalException) {
090    
091                                    SessionErrors.add(renderRequest, e.getClass());
092    
093                                    return actionMapping.findForward("portlet.users_admin.error");
094                            }
095                            else {
096                                    throw e;
097                            }
098                    }
099    
100                    return actionMapping.findForward(
101                            getForward(renderRequest, "portlet.users_admin.edit_org_labor"));
102            }
103    
104            protected void deleteOrgLabor(ActionRequest actionRequest)
105                    throws Exception {
106    
107                    long orgLaborId = ParamUtil.getLong(actionRequest, "orgLaborId");
108    
109                    OrgLaborServiceUtil.deleteOrgLabor(orgLaborId);
110            }
111    
112            protected void updateOrgLabor(ActionRequest actionRequest)
113                    throws Exception {
114    
115                    long orgLaborId = ParamUtil.getLong(actionRequest, "orgLaborId");
116    
117                    long organizationId = ParamUtil.getLong(
118                            actionRequest, "organizationId");
119                    int typeId = ParamUtil.getInteger(actionRequest, "typeId");
120    
121                    int sunOpen = ParamUtil.getInteger(actionRequest, "sunOpen");
122                    int sunClose = ParamUtil.getInteger(actionRequest, "sunClose");
123    
124                    int monOpen = ParamUtil.getInteger(actionRequest, "monOpen");
125                    int monClose = ParamUtil.getInteger(actionRequest, "monClose");
126    
127                    int tueOpen = ParamUtil.getInteger(actionRequest, "tueOpen");
128                    int tueClose = ParamUtil.getInteger(actionRequest, "tueClose");
129    
130                    int wedOpen = ParamUtil.getInteger(actionRequest, "wedOpen");
131                    int wedClose = ParamUtil.getInteger(actionRequest, "wedClose");
132    
133                    int thuOpen = ParamUtil.getInteger(actionRequest, "thuOpen");
134                    int thuClose = ParamUtil.getInteger(actionRequest, "thuClose");
135    
136                    int friOpen = ParamUtil.getInteger(actionRequest, "friOpen");
137                    int friClose = ParamUtil.getInteger(actionRequest, "friClose");
138    
139                    int satOpen = ParamUtil.getInteger(actionRequest, "satOpen");
140                    int satClose = ParamUtil.getInteger(actionRequest, "satClose");
141    
142                    if (orgLaborId <= 0) {
143    
144                            // Add organization labor
145    
146                            OrgLaborServiceUtil.addOrgLabor(
147                                    organizationId, typeId, sunOpen, sunClose, monOpen, monClose,
148                                    tueOpen, tueClose, wedOpen, wedClose, thuOpen, thuClose,
149                                    friOpen, friClose, satOpen, satClose);
150                    }
151                    else {
152    
153                            // Update organization labor
154    
155                            OrgLaborServiceUtil.updateOrgLabor(
156                                    orgLaborId, typeId, sunOpen, sunClose, monOpen, monClose,
157                                    tueOpen, tueClose, wedOpen, wedClose, thuOpen, thuClose,
158                                    friOpen, friClose, satOpen, satClose);
159                    }
160            }
161    
162    }