1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet.softwarecatalog.action;
16  
17  import com.liferay.portal.kernel.servlet.SessionErrors;
18  import com.liferay.portal.kernel.util.Constants;
19  import com.liferay.portal.kernel.util.ParamUtil;
20  import com.liferay.portal.security.auth.PrincipalException;
21  import com.liferay.portal.struts.PortletAction;
22  import com.liferay.portal.util.PortalUtil;
23  import com.liferay.portlet.softwarecatalog.DuplicateProductVersionDirectDownloadURLException;
24  import com.liferay.portlet.softwarecatalog.NoSuchProductVersionException;
25  import com.liferay.portlet.softwarecatalog.ProductVersionChangeLogException;
26  import com.liferay.portlet.softwarecatalog.ProductVersionDownloadURLException;
27  import com.liferay.portlet.softwarecatalog.ProductVersionFrameworkVersionException;
28  import com.liferay.portlet.softwarecatalog.ProductVersionNameException;
29  import com.liferay.portlet.softwarecatalog.UnavailableProductVersionDirectDownloadURLException;
30  import com.liferay.portlet.softwarecatalog.service.SCProductVersionServiceUtil;
31  
32  import javax.portlet.ActionRequest;
33  import javax.portlet.ActionResponse;
34  import javax.portlet.PortletConfig;
35  import javax.portlet.RenderRequest;
36  import javax.portlet.RenderResponse;
37  
38  import org.apache.struts.action.ActionForm;
39  import org.apache.struts.action.ActionForward;
40  import org.apache.struts.action.ActionMapping;
41  
42  /**
43   * <a href="EditProductVersionAction.java.html"><b><i>View Source</i></b></a>
44   *
45   * @author Jorge Ferrer
46   */
47  public class EditProductVersionAction extends PortletAction {
48  
49      public void processAction(
50              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
51              ActionRequest actionRequest, ActionResponse actionResponse)
52          throws Exception {
53  
54          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
55  
56          try {
57              if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
58                  updateProductVersion(actionRequest);
59              }
60              else if (cmd.equals(Constants.DELETE)) {
61                  deleteProductVersion(actionRequest);
62              }
63  
64              sendRedirect(actionRequest, actionResponse);
65          }
66          catch (Exception e) {
67              if (e instanceof NoSuchProductVersionException ||
68                  e instanceof PrincipalException) {
69  
70                  SessionErrors.add(actionRequest, e.getClass().getName());
71  
72                  setForward(actionRequest, "portlet.software_catalog.error");
73              }
74              else if (e instanceof
75                          DuplicateProductVersionDirectDownloadURLException ||
76                       e instanceof ProductVersionChangeLogException ||
77                       e instanceof ProductVersionDownloadURLException ||
78                       e instanceof ProductVersionFrameworkVersionException ||
79                       e instanceof ProductVersionNameException ||
80                       e instanceof
81                          UnavailableProductVersionDirectDownloadURLException) {
82  
83                  SessionErrors.add(actionRequest, e.getClass().getName());
84              }
85              else {
86                  throw e;
87              }
88          }
89      }
90  
91      public ActionForward render(
92              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
93              RenderRequest renderRequest, RenderResponse renderResponse)
94          throws Exception {
95  
96          try {
97              ActionUtil.getProductVersion(renderRequest);
98          }
99          catch (Exception e) {
100             if (e instanceof NoSuchProductVersionException ||
101                 e instanceof PrincipalException) {
102 
103                 SessionErrors.add(renderRequest, e.getClass().getName());
104 
105                 return mapping.findForward("portlet.software_catalog.error");
106             }
107             else {
108                 throw e;
109             }
110         }
111 
112         return mapping.findForward(getForward(
113             renderRequest, "portlet.software_catalog.edit_product_version"));
114     }
115 
116     protected void deleteProductVersion(ActionRequest actionRequest)
117         throws Exception {
118 
119         long productVersionId = ParamUtil.getLong(
120             actionRequest, "productVersionId");
121 
122         SCProductVersionServiceUtil.deleteProductVersion(productVersionId);
123     }
124 
125     protected void updateProductVersion(ActionRequest actionRequest)
126         throws Exception {
127 
128         long productVersionId = ParamUtil.getLong(
129             actionRequest, "productVersionId");
130 
131         long productEntryId = ParamUtil.getLong(
132             actionRequest, "productEntryId");
133         String version = ParamUtil.getString(actionRequest, "version");
134         String changeLog = ParamUtil.getString(actionRequest, "changeLog");
135         String downloadPageURL = ParamUtil.getString(
136             actionRequest, "downloadPageURL");
137         String directDownloadURL = ParamUtil.getString(
138             actionRequest, "directDownloadURL");
139         boolean testDirectDownloadURL = ParamUtil.getBoolean(
140             actionRequest, "testDirectDownloadURL");
141         boolean repoStoreArtifact = ParamUtil.getBoolean(
142             actionRequest, "repoStoreArtifact");
143 
144         long[] frameworkVersionIds = ParamUtil.getLongValues(
145             actionRequest, "frameworkVersions");
146 
147         String[] communityPermissions = PortalUtil.getCommunityPermissions(
148             actionRequest);
149         String[] guestPermissions = PortalUtil.getGuestPermissions(
150             actionRequest);
151 
152         if (productVersionId <= 0) {
153 
154             // Add product version
155 
156             SCProductVersionServiceUtil.addProductVersion(
157                 productEntryId, version, changeLog, downloadPageURL,
158                 directDownloadURL, testDirectDownloadURL, repoStoreArtifact,
159                 frameworkVersionIds, communityPermissions, guestPermissions);
160         }
161         else {
162 
163             // Update product version
164 
165             SCProductVersionServiceUtil.updateProductVersion(
166                 productVersionId, version, changeLog, downloadPageURL,
167                 directDownloadURL, testDirectDownloadURL, repoStoreArtifact,
168                 frameworkVersionIds);
169         }
170     }
171 
172 }