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.softwarecatalog.action;
016    
017    import com.liferay.portal.kernel.servlet.SessionErrors;
018    import com.liferay.portal.kernel.util.Constants;
019    import com.liferay.portal.kernel.util.ParamUtil;
020    import com.liferay.portal.security.auth.PrincipalException;
021    import com.liferay.portal.struts.PortletAction;
022    import com.liferay.portlet.softwarecatalog.LicenseNameException;
023    import com.liferay.portlet.softwarecatalog.NoSuchLicenseException;
024    import com.liferay.portlet.softwarecatalog.RequiredLicenseException;
025    import com.liferay.portlet.softwarecatalog.service.SCLicenseServiceUtil;
026    
027    import javax.portlet.ActionRequest;
028    import javax.portlet.ActionResponse;
029    import javax.portlet.PortletConfig;
030    import javax.portlet.RenderRequest;
031    import javax.portlet.RenderResponse;
032    
033    import org.apache.struts.action.ActionForm;
034    import org.apache.struts.action.ActionForward;
035    import org.apache.struts.action.ActionMapping;
036    
037    /**
038     * @author Jorge Ferrer
039     */
040    public class EditLicenseAction extends PortletAction {
041    
042            @Override
043            public void processAction(
044                            ActionMapping actionMapping, ActionForm actionForm,
045                            PortletConfig portletConfig, ActionRequest actionRequest,
046                            ActionResponse actionResponse)
047                    throws Exception {
048    
049                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
050    
051                    try {
052                            if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
053                                    updateLicense(actionRequest);
054                            }
055                            else if (cmd.equals(Constants.DELETE)) {
056                                    deleteLicense(actionRequest);
057                            }
058    
059                            sendRedirect(actionRequest, actionResponse);
060                    }
061                    catch (Exception e) {
062                            if (e instanceof NoSuchLicenseException ||
063                                    e instanceof PrincipalException) {
064    
065                                    SessionErrors.add(actionRequest, e.getClass());
066    
067                                    setForward(actionRequest, "portlet.software_catalog.error");
068                            }
069                            else if (e instanceof LicenseNameException ||
070                                             e instanceof RequiredLicenseException) {
071    
072                                    SessionErrors.add(actionRequest, e.getClass());
073                            }
074                            else {
075                                    throw e;
076                            }
077                    }
078            }
079    
080            @Override
081            public ActionForward render(
082                            ActionMapping actionMapping, ActionForm actionForm,
083                            PortletConfig portletConfig, RenderRequest renderRequest,
084                            RenderResponse renderResponse)
085                    throws Exception {
086    
087                    try {
088                            ActionUtil.getLicense(renderRequest);
089                    }
090                    catch (Exception e) {
091                            if (e instanceof NoSuchLicenseException ||
092                                    e instanceof PrincipalException) {
093    
094                                    SessionErrors.add(renderRequest, e.getClass());
095    
096                                    return actionMapping.findForward(
097                                            "portlet.software_catalog.error");
098                            }
099                            else {
100                                    throw e;
101                            }
102                    }
103    
104                    return actionMapping.findForward(
105                            getForward(renderRequest, "portlet.software_catalog.edit_license"));
106            }
107    
108            protected void deleteLicense(ActionRequest actionRequest) throws Exception {
109                    long licenseId = ParamUtil.getLong(actionRequest, "licenseId");
110    
111                    SCLicenseServiceUtil.deleteLicense(licenseId);
112            }
113    
114            protected void updateLicense(ActionRequest actionRequest) throws Exception {
115                    long licenseId = ParamUtil.getLong(actionRequest, "licenseId");
116    
117                    String name = ParamUtil.getString(actionRequest, "name");
118                    String url = ParamUtil.getString(actionRequest, "url");
119                    boolean openSource = ParamUtil.getBoolean(actionRequest, "openSource");
120                    boolean active = ParamUtil.getBoolean(actionRequest, "active");
121                    boolean recommended = ParamUtil.getBoolean(
122                            actionRequest, "recommended");
123    
124                    if (licenseId <= 0) {
125    
126                            // Add license
127    
128                            SCLicenseServiceUtil.addLicense(
129                                    name, url, openSource, active, recommended);
130                    }
131                    else {
132    
133                            // Update license
134    
135                            SCLicenseServiceUtil.updateLicense(
136                                    licenseId, name, url, openSource, active, recommended);
137                    }
138            }
139    
140    }