1   /**
2    * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.softwarecatalog.action;
24  
25  import com.liferay.portal.kernel.servlet.SessionErrors;
26  import com.liferay.portal.kernel.util.Constants;
27  import com.liferay.portal.kernel.util.ParamUtil;
28  import com.liferay.portal.security.auth.PrincipalException;
29  import com.liferay.portal.struts.PortletAction;
30  import com.liferay.portlet.softwarecatalog.DuplicateProductVersionDirectDownloadURLException;
31  import com.liferay.portlet.softwarecatalog.NoSuchProductVersionException;
32  import com.liferay.portlet.softwarecatalog.ProductVersionChangeLogException;
33  import com.liferay.portlet.softwarecatalog.ProductVersionDownloadURLException;
34  import com.liferay.portlet.softwarecatalog.ProductVersionFrameworkVersionException;
35  import com.liferay.portlet.softwarecatalog.ProductVersionNameException;
36  import com.liferay.portlet.softwarecatalog.service.SCProductVersionServiceUtil;
37  
38  import javax.portlet.ActionRequest;
39  import javax.portlet.ActionResponse;
40  import javax.portlet.PortletConfig;
41  import javax.portlet.RenderRequest;
42  import javax.portlet.RenderResponse;
43  
44  import org.apache.struts.action.ActionForm;
45  import org.apache.struts.action.ActionForward;
46  import org.apache.struts.action.ActionMapping;
47  
48  /**
49   * <a href="EditProductVersionAction.java.html"><b><i>View Source</i></b></a>
50   *
51   * @author Jorge Ferrer
52   *
53   */
54  public class EditProductVersionAction extends PortletAction {
55  
56      public void processAction(
57              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
58              ActionRequest actionRequest, ActionResponse actionResponse)
59          throws Exception {
60  
61          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
62  
63          try {
64              if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
65                  updateProductVersion(actionRequest);
66              }
67              else if (cmd.equals(Constants.DELETE)) {
68                  deleteProductVersion(actionRequest);
69              }
70  
71              sendRedirect(actionRequest, actionResponse);
72          }
73          catch (Exception e) {
74              if (e instanceof NoSuchProductVersionException ||
75                  e instanceof PrincipalException) {
76  
77                  SessionErrors.add(actionRequest, e.getClass().getName());
78  
79                  setForward(actionRequest, "portlet.software_catalog.error");
80              }
81              else if (e instanceof
82                          DuplicateProductVersionDirectDownloadURLException ||
83                       e instanceof ProductVersionChangeLogException ||
84                       e instanceof ProductVersionDownloadURLException ||
85                       e instanceof ProductVersionFrameworkVersionException ||
86                       e instanceof ProductVersionNameException) {
87  
88                  SessionErrors.add(actionRequest, e.getClass().getName());
89              }
90              else {
91                  throw e;
92              }
93          }
94      }
95  
96      public ActionForward render(
97              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
98              RenderRequest renderRequest, RenderResponse renderResponse)
99          throws Exception {
100 
101         try {
102             ActionUtil.getProductVersion(renderRequest);
103         }
104         catch (Exception e) {
105             if (e instanceof NoSuchProductVersionException ||
106                 e instanceof PrincipalException) {
107 
108                 SessionErrors.add(renderRequest, e.getClass().getName());
109 
110                 return mapping.findForward("portlet.software_catalog.error");
111             }
112             else {
113                 throw e;
114             }
115         }
116 
117         return mapping.findForward(getForward(
118             renderRequest, "portlet.software_catalog.edit_product_version"));
119     }
120 
121     protected void deleteProductVersion(ActionRequest actionRequest)
122         throws Exception {
123 
124         long productVersionId = ParamUtil.getLong(
125             actionRequest, "productVersionId");
126 
127         SCProductVersionServiceUtil.deleteProductVersion(productVersionId);
128     }
129 
130     protected void updateProductVersion(ActionRequest actionRequest)
131         throws Exception {
132 
133         long productVersionId = ParamUtil.getLong(
134             actionRequest, "productVersionId");
135 
136         long productEntryId = ParamUtil.getLong(
137             actionRequest, "productEntryId");
138         String version = ParamUtil.getString(actionRequest, "version");
139         String changeLog = ParamUtil.getString(actionRequest, "changeLog");
140         String downloadPageURL = ParamUtil.getString(
141             actionRequest, "downloadPageURL");
142         String directDownloadURL = ParamUtil.getString(
143             actionRequest, "directDownloadURL");
144         boolean repoStoreArtifact = ParamUtil.getBoolean(
145             actionRequest, "repoStoreArtifact");
146 
147         long[] frameworkVersionIds = ParamUtil.getLongValues(
148             actionRequest, "frameworkVersions");
149 
150         String[] communityPermissions = actionRequest.getParameterValues(
151             "communityPermissions");
152         String[] guestPermissions = actionRequest.getParameterValues(
153             "guestPermissions");
154 
155         if (productVersionId <= 0) {
156 
157             // Add product version
158 
159             SCProductVersionServiceUtil.addProductVersion(
160                 productEntryId, version, changeLog, downloadPageURL,
161                 directDownloadURL, repoStoreArtifact, frameworkVersionIds,
162                 communityPermissions, guestPermissions);
163         }
164         else {
165 
166             // Update product version
167 
168             SCProductVersionServiceUtil.updateProductVersion(
169                 productVersionId, version, changeLog, downloadPageURL,
170                 directDownloadURL, repoStoreArtifact, frameworkVersionIds);
171         }
172     }
173 
174 }