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