1   /**
2    * Copyright (c) 2000-2009 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.portal.util.PortalUtil;
31  import com.liferay.portlet.softwarecatalog.DuplicateProductVersionDirectDownloadURLException;
32  import com.liferay.portlet.softwarecatalog.NoSuchProductVersionException;
33  import com.liferay.portlet.softwarecatalog.ProductVersionChangeLogException;
34  import com.liferay.portlet.softwarecatalog.ProductVersionDownloadURLException;
35  import com.liferay.portlet.softwarecatalog.ProductVersionFrameworkVersionException;
36  import com.liferay.portlet.softwarecatalog.ProductVersionNameException;
37  import com.liferay.portlet.softwarecatalog.UnavailableProductVersionDirectDownloadURLException;
38  import com.liferay.portlet.softwarecatalog.service.SCProductVersionServiceUtil;
39  
40  import javax.portlet.ActionRequest;
41  import javax.portlet.ActionResponse;
42  import javax.portlet.PortletConfig;
43  import javax.portlet.RenderRequest;
44  import javax.portlet.RenderResponse;
45  
46  import org.apache.struts.action.ActionForm;
47  import org.apache.struts.action.ActionForward;
48  import org.apache.struts.action.ActionMapping;
49  
50  /**
51   * <a href="EditProductVersionAction.java.html"><b><i>View Source</i></b></a>
52   *
53   * @author Jorge Ferrer
54   */
55  public class EditProductVersionAction extends PortletAction {
56  
57      public void processAction(
58              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
59              ActionRequest actionRequest, ActionResponse actionResponse)
60          throws Exception {
61  
62          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
63  
64          try {
65              if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
66                  updateProductVersion(actionRequest);
67              }
68              else if (cmd.equals(Constants.DELETE)) {
69                  deleteProductVersion(actionRequest);
70              }
71  
72              sendRedirect(actionRequest, actionResponse);
73          }
74          catch (Exception e) {
75              if (e instanceof NoSuchProductVersionException ||
76                  e instanceof PrincipalException) {
77  
78                  SessionErrors.add(actionRequest, e.getClass().getName());
79  
80                  setForward(actionRequest, "portlet.software_catalog.error");
81              }
82              else if (e instanceof
83                          DuplicateProductVersionDirectDownloadURLException ||
84                       e instanceof ProductVersionChangeLogException ||
85                       e instanceof ProductVersionDownloadURLException ||
86                       e instanceof ProductVersionFrameworkVersionException ||
87                       e instanceof ProductVersionNameException ||
88                       e instanceof
89                          UnavailableProductVersionDirectDownloadURLException) {
90  
91                  SessionErrors.add(actionRequest, e.getClass().getName());
92              }
93              else {
94                  throw e;
95              }
96          }
97      }
98  
99      public ActionForward render(
100             ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
101             RenderRequest renderRequest, RenderResponse renderResponse)
102         throws Exception {
103 
104         try {
105             ActionUtil.getProductVersion(renderRequest);
106         }
107         catch (Exception e) {
108             if (e instanceof NoSuchProductVersionException ||
109                 e instanceof PrincipalException) {
110 
111                 SessionErrors.add(renderRequest, e.getClass().getName());
112 
113                 return mapping.findForward("portlet.software_catalog.error");
114             }
115             else {
116                 throw e;
117             }
118         }
119 
120         return mapping.findForward(getForward(
121             renderRequest, "portlet.software_catalog.edit_product_version"));
122     }
123 
124     protected void deleteProductVersion(ActionRequest actionRequest)
125         throws Exception {
126 
127         long productVersionId = ParamUtil.getLong(
128             actionRequest, "productVersionId");
129 
130         SCProductVersionServiceUtil.deleteProductVersion(productVersionId);
131     }
132 
133     protected void updateProductVersion(ActionRequest actionRequest)
134         throws Exception {
135 
136         long productVersionId = ParamUtil.getLong(
137             actionRequest, "productVersionId");
138 
139         long productEntryId = ParamUtil.getLong(
140             actionRequest, "productEntryId");
141         String version = ParamUtil.getString(actionRequest, "version");
142         String changeLog = ParamUtil.getString(actionRequest, "changeLog");
143         String downloadPageURL = ParamUtil.getString(
144             actionRequest, "downloadPageURL");
145         String directDownloadURL = ParamUtil.getString(
146             actionRequest, "directDownloadURL");
147         boolean testDirectDownloadURL = ParamUtil.getBoolean(
148             actionRequest, "testDirectDownloadURL");
149         boolean repoStoreArtifact = ParamUtil.getBoolean(
150             actionRequest, "repoStoreArtifact");
151 
152         long[] frameworkVersionIds = ParamUtil.getLongValues(
153             actionRequest, "frameworkVersions");
154 
155         String[] communityPermissions = PortalUtil.getCommunityPermissions(
156             actionRequest);
157         String[] guestPermissions = PortalUtil.getGuestPermissions(
158             actionRequest);
159 
160         if (productVersionId <= 0) {
161 
162             // Add product version
163 
164             SCProductVersionServiceUtil.addProductVersion(
165                 productEntryId, version, changeLog, downloadPageURL,
166                 directDownloadURL, testDirectDownloadURL, repoStoreArtifact,
167                 frameworkVersionIds, communityPermissions, guestPermissions);
168         }
169         else {
170 
171             // Update product version
172 
173             SCProductVersionServiceUtil.updateProductVersion(
174                 productVersionId, version, changeLog, downloadPageURL,
175                 directDownloadURL, testDirectDownloadURL, repoStoreArtifact,
176                 frameworkVersionIds);
177         }
178     }
179 
180 }