001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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            @Override
042            public void processAction(
043                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
044                            ActionRequest actionRequest, ActionResponse actionResponse)
045                    throws Exception {
046    
047                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
048    
049                    try {
050                            if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
051                                    updateLicense(actionRequest);
052                            }
053                            else if (cmd.equals(Constants.DELETE)) {
054                                    deleteLicense(actionRequest);
055                            }
056    
057                            sendRedirect(actionRequest, actionResponse);
058                    }
059                    catch (Exception e) {
060                            if (e instanceof NoSuchLicenseException ||
061                                    e instanceof PrincipalException) {
062    
063                                    SessionErrors.add(actionRequest, e.getClass());
064    
065                                    setForward(actionRequest, "portlet.software_catalog.error");
066                            }
067                            else if (e instanceof LicenseNameException) {
068                                    SessionErrors.add(actionRequest, e.getClass());
069                            }
070                            else {
071                                    throw e;
072                            }
073                    }
074            }
075    
076            @Override
077            public ActionForward render(
078                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
079                            RenderRequest renderRequest, RenderResponse renderResponse)
080                    throws Exception {
081    
082                    try {
083                            ActionUtil.getLicense(renderRequest);
084                    }
085                    catch (Exception e) {
086                            if (e instanceof NoSuchLicenseException ||
087                                    e instanceof PrincipalException) {
088    
089                                    SessionErrors.add(renderRequest, e.getClass());
090    
091                                    return mapping.findForward("portlet.software_catalog.error");
092                            }
093                            else {
094                                    throw e;
095                            }
096                    }
097    
098                    return mapping.findForward(
099                            getForward(renderRequest, "portlet.software_catalog.edit_license"));
100            }
101    
102            protected void deleteLicense(ActionRequest actionRequest) throws Exception {
103                    long licenseId = ParamUtil.getLong(actionRequest, "licenseId");
104    
105                    SCLicenseServiceUtil.deleteLicense(licenseId);
106            }
107    
108            protected void updateLicense(ActionRequest actionRequest) throws Exception {
109                    long licenseId = ParamUtil.getLong(actionRequest, "licenseId");
110    
111                    String name = ParamUtil.getString(actionRequest, "name");
112                    String url = ParamUtil.getString(actionRequest, "url");
113                    boolean openSource = ParamUtil.getBoolean(actionRequest, "openSource");
114                    boolean active = ParamUtil.getBoolean(actionRequest, "active");
115                    boolean recommended = ParamUtil.getBoolean(
116                            actionRequest, "recommended");
117    
118                    if (licenseId <= 0) {
119    
120                            // Add license
121    
122                            SCLicenseServiceUtil.addLicense(
123                                    name, url, openSource, active, recommended);
124                    }
125                    else {
126    
127                            // Update license
128    
129                            SCLicenseServiceUtil.updateLicense(
130                                    licenseId, name, url, openSource, active, recommended);
131                    }
132            }
133    
134    }