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