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