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.service.ServiceContext;
022    import com.liferay.portal.service.ServiceContextFactory;
023    import com.liferay.portal.struts.PortletAction;
024    import com.liferay.portlet.softwarecatalog.FrameworkVersionNameException;
025    import com.liferay.portlet.softwarecatalog.NoSuchFrameworkVersionException;
026    import com.liferay.portlet.softwarecatalog.model.SCFrameworkVersion;
027    import com.liferay.portlet.softwarecatalog.service.SCFrameworkVersionServiceUtil;
028    
029    import javax.portlet.ActionRequest;
030    import javax.portlet.ActionResponse;
031    import javax.portlet.PortletConfig;
032    import javax.portlet.RenderRequest;
033    import javax.portlet.RenderResponse;
034    
035    import org.apache.struts.action.ActionForm;
036    import org.apache.struts.action.ActionForward;
037    import org.apache.struts.action.ActionMapping;
038    
039    /**
040     * @author Jorge Ferrer
041     */
042    public class EditFrameworkVersionAction extends PortletAction {
043    
044            public void processAction(
045                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
046                            ActionRequest actionRequest, 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                                    updateFrameworkVersion(actionRequest);
054                            }
055                            else if (cmd.equals(Constants.DELETE)) {
056                                    deleteFrameworkVersion(actionRequest);
057                            }
058    
059                            sendRedirect(actionRequest, actionResponse);
060                    }
061                    catch (Exception e) {
062                            if (e instanceof NoSuchFrameworkVersionException ||
063                                    e instanceof PrincipalException) {
064    
065                                    SessionErrors.add(actionRequest, e.getClass().getName());
066    
067                                    setForward(actionRequest, "portlet.software_catalog.error");
068                            }
069                            else if (e instanceof FrameworkVersionNameException) {
070    
071                                    SessionErrors.add(actionRequest, e.getClass().getName());
072                            }
073                            else {
074                                    throw e;
075                            }
076                    }
077            }
078    
079            public ActionForward render(
080                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
081                            RenderRequest renderRequest, RenderResponse renderResponse)
082                    throws Exception {
083    
084                    try {
085                            ActionUtil.getFrameworkVersion(renderRequest);
086                    }
087                    catch (Exception e) {
088                            if (e instanceof NoSuchFrameworkVersionException ||
089                                    e instanceof PrincipalException) {
090    
091                                    SessionErrors.add(renderRequest, e.getClass().getName());
092    
093                                    return mapping.findForward("portlet.software_catalog.error");
094                            }
095                            else {
096                                    throw e;
097                            }
098                    }
099    
100                    return mapping.findForward(getForward(
101                            renderRequest, "portlet.software_catalog.edit_framework_version"));
102            }
103    
104            protected void deleteFrameworkVersion(ActionRequest actionRequest)
105                    throws Exception {
106    
107                    long frameworkVersionId = ParamUtil.getLong(
108                            actionRequest, "frameworkVersionId");
109    
110                    SCFrameworkVersionServiceUtil.deleteFrameworkVersion(
111                            frameworkVersionId);
112            }
113    
114            protected void updateFrameworkVersion(ActionRequest actionRequest)
115                    throws Exception {
116    
117                    long frameworkVersionId = ParamUtil.getLong(
118                            actionRequest, "frameworkVersionId");
119    
120                    String name = ParamUtil.getString(actionRequest, "name");
121                    String url = ParamUtil.getString(actionRequest, "url");
122                    boolean active = ParamUtil.getBoolean(actionRequest, "active");
123                    int priority = ParamUtil.getInteger(actionRequest, "priority");
124    
125                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
126                            SCFrameworkVersion.class.getName(), actionRequest);
127    
128                    if (frameworkVersionId <= 0) {
129    
130                            // Add framework version
131    
132                            SCFrameworkVersionServiceUtil.addFrameworkVersion(
133                                    name, url, active, priority, serviceContext);
134                    }
135                    else {
136    
137                            // Update framework version
138    
139                            SCFrameworkVersionServiceUtil.updateFrameworkVersion(
140                                    frameworkVersionId, name, url, active, priority);
141                    }
142            }
143    
144    }