001    /**
002     * Copyright (c) 2000-2012 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 mapping, ActionForm form, PortletConfig portletConfig,
045                            ActionRequest actionRequest, 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                                    updateLicense(actionRequest);
053                            }
054                            else if (cmd.equals(Constants.DELETE)) {
055                                    deleteLicense(actionRequest);
056                            }
057    
058                            sendRedirect(actionRequest, actionResponse);
059                    }
060                    catch (Exception e) {
061                            if (e instanceof NoSuchLicenseException ||
062                                    e instanceof PrincipalException) {
063    
064                                    SessionErrors.add(actionRequest, e.getClass());
065    
066                                    setForward(actionRequest, "portlet.software_catalog.error");
067                            }
068                            else if (e instanceof LicenseNameException ||
069                                             e instanceof RequiredLicenseException) {
070    
071                                    SessionErrors.add(actionRequest, e.getClass());
072                            }
073                            else {
074                                    throw e;
075                            }
076                    }
077            }
078    
079            @Override
080            public ActionForward render(
081                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
082                            RenderRequest renderRequest, RenderResponse renderResponse)
083                    throws Exception {
084    
085                    try {
086                            ActionUtil.getLicense(renderRequest);
087                    }
088                    catch (Exception e) {
089                            if (e instanceof NoSuchLicenseException ||
090                                    e instanceof PrincipalException) {
091    
092                                    SessionErrors.add(renderRequest, e.getClass());
093    
094                                    return mapping.findForward("portlet.software_catalog.error");
095                            }
096                            else {
097                                    throw e;
098                            }
099                    }
100    
101                    return mapping.findForward(
102                            getForward(renderRequest, "portlet.software_catalog.edit_license"));
103            }
104    
105            protected void deleteLicense(ActionRequest actionRequest) throws Exception {
106                    long licenseId = ParamUtil.getLong(actionRequest, "licenseId");
107    
108                    SCLicenseServiceUtil.deleteLicense(licenseId);
109            }
110    
111            protected void updateLicense(ActionRequest actionRequest) throws Exception {
112                    long licenseId = ParamUtil.getLong(actionRequest, "licenseId");
113    
114                    String name = ParamUtil.getString(actionRequest, "name");
115                    String url = ParamUtil.getString(actionRequest, "url");
116                    boolean openSource = ParamUtil.getBoolean(actionRequest, "openSource");
117                    boolean active = ParamUtil.getBoolean(actionRequest, "active");
118                    boolean recommended = ParamUtil.getBoolean(
119                            actionRequest, "recommended");
120    
121                    if (licenseId <= 0) {
122    
123                            // Add license
124    
125                            SCLicenseServiceUtil.addLicense(
126                                    name, url, openSource, active, recommended);
127                    }
128                    else {
129    
130                            // Update license
131    
132                            SCLicenseServiceUtil.updateLicense(
133                                    licenseId, name, url, openSource, active, recommended);
134                    }
135            }
136    
137    }