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.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            @Override
045            public void processAction(
046                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
047                            ActionRequest actionRequest, ActionResponse actionResponse)
048                    throws Exception {
049    
050                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
051    
052                    try {
053                            if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
054                                    updateFrameworkVersion(actionRequest);
055                            }
056                            else if (cmd.equals(Constants.DELETE)) {
057                                    deleteFrameworkVersion(actionRequest);
058                            }
059    
060                            sendRedirect(actionRequest, actionResponse);
061                    }
062                    catch (Exception e) {
063                            if (e instanceof NoSuchFrameworkVersionException ||
064                                    e instanceof PrincipalException) {
065    
066                                    SessionErrors.add(actionRequest, e.getClass().getName());
067    
068                                    setForward(actionRequest, "portlet.software_catalog.error");
069                            }
070                            else if (e instanceof FrameworkVersionNameException) {
071    
072                                    SessionErrors.add(actionRequest, e.getClass().getName());
073                            }
074                            else {
075                                    throw e;
076                            }
077                    }
078            }
079    
080            @Override
081            public ActionForward render(
082                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
083                            RenderRequest renderRequest, RenderResponse renderResponse)
084                    throws Exception {
085    
086                    try {
087                            ActionUtil.getFrameworkVersion(renderRequest);
088                    }
089                    catch (Exception e) {
090                            if (e instanceof NoSuchFrameworkVersionException ||
091                                    e instanceof PrincipalException) {
092    
093                                    SessionErrors.add(renderRequest, e.getClass().getName());
094    
095                                    return mapping.findForward("portlet.software_catalog.error");
096                            }
097                            else {
098                                    throw e;
099                            }
100                    }
101    
102                    return mapping.findForward(getForward(
103                            renderRequest, "portlet.software_catalog.edit_framework_version"));
104            }
105    
106            protected void deleteFrameworkVersion(ActionRequest actionRequest)
107                    throws Exception {
108    
109                    long frameworkVersionId = ParamUtil.getLong(
110                            actionRequest, "frameworkVersionId");
111    
112                    SCFrameworkVersionServiceUtil.deleteFrameworkVersion(
113                            frameworkVersionId);
114            }
115    
116            protected void updateFrameworkVersion(ActionRequest actionRequest)
117                    throws Exception {
118    
119                    long frameworkVersionId = ParamUtil.getLong(
120                            actionRequest, "frameworkVersionId");
121    
122                    String name = ParamUtil.getString(actionRequest, "name");
123                    String url = ParamUtil.getString(actionRequest, "url");
124                    boolean active = ParamUtil.getBoolean(actionRequest, "active");
125                    int priority = ParamUtil.getInteger(actionRequest, "priority");
126    
127                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
128                            SCFrameworkVersion.class.getName(), actionRequest);
129    
130                    if (frameworkVersionId <= 0) {
131    
132                            // Add framework version
133    
134                            SCFrameworkVersionServiceUtil.addFrameworkVersion(
135                                    name, url, active, priority, serviceContext);
136                    }
137                    else {
138    
139                            // Update framework version
140    
141                            SCFrameworkVersionServiceUtil.updateFrameworkVersion(
142                                    frameworkVersionId, name, url, active, priority);
143                    }
144            }
145    
146    }