1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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.journal.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.GetterUtil;
28  import com.liferay.portal.kernel.util.ParamUtil;
29  import com.liferay.portal.kernel.util.StringPool;
30  import com.liferay.portal.kernel.util.StringUtil;
31  import com.liferay.portal.kernel.util.Validator;
32  import com.liferay.portal.security.auth.PrincipalException;
33  import com.liferay.portal.service.ServiceContext;
34  import com.liferay.portal.service.ServiceContextFactory;
35  import com.liferay.portal.struts.PortletAction;
36  import com.liferay.portlet.journal.DuplicateFeedIdException;
37  import com.liferay.portlet.journal.FeedContentFieldException;
38  import com.liferay.portlet.journal.FeedDescriptionException;
39  import com.liferay.portlet.journal.FeedIdException;
40  import com.liferay.portlet.journal.FeedNameException;
41  import com.liferay.portlet.journal.FeedTargetLayoutFriendlyUrlException;
42  import com.liferay.portlet.journal.FeedTargetPortletIdException;
43  import com.liferay.portlet.journal.NoSuchFeedException;
44  import com.liferay.portlet.journal.model.JournalFeed;
45  import com.liferay.portlet.journal.service.JournalFeedServiceUtil;
46  import com.liferay.util.RSSUtil;
47  
48  import javax.portlet.ActionRequest;
49  import javax.portlet.ActionResponse;
50  import javax.portlet.PortletConfig;
51  import javax.portlet.RenderRequest;
52  import javax.portlet.RenderResponse;
53  
54  import org.apache.struts.action.ActionForm;
55  import org.apache.struts.action.ActionForward;
56  import org.apache.struts.action.ActionMapping;
57  
58  /**
59   * <a href="EditFeedAction.java.html"><b><i>View Source</i></b></a>
60   *
61   * @author Raymond Augé
62   */
63  public class EditFeedAction extends PortletAction {
64  
65      public void processAction(
66              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
67              ActionRequest actionRequest, ActionResponse actionResponse)
68          throws Exception {
69  
70          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
71  
72          try {
73              if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
74                  updateFeed(actionRequest);
75              }
76              else if (cmd.equals(Constants.DELETE)) {
77                  deleteFeeds(actionRequest);
78              }
79  
80              sendRedirect(actionRequest, actionResponse);
81          }
82          catch (Exception e) {
83              if (e instanceof NoSuchFeedException ||
84                  e instanceof PrincipalException) {
85  
86                  SessionErrors.add(actionRequest, e.getClass().getName());
87  
88                  setForward(actionRequest, "portlet.journal.error");
89              }
90              else if (e instanceof DuplicateFeedIdException ||
91                       e instanceof FeedContentFieldException ||
92                       e instanceof FeedDescriptionException ||
93                       e instanceof FeedIdException ||
94                       e instanceof FeedNameException ||
95                       e instanceof FeedTargetLayoutFriendlyUrlException ||
96                       e instanceof FeedTargetPortletIdException) {
97  
98                  SessionErrors.add(actionRequest, e.getClass().getName());
99              }
100             else {
101                 throw e;
102             }
103         }
104     }
105 
106     public ActionForward render(
107             ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
108             RenderRequest renderRequest, RenderResponse renderResponse)
109         throws Exception {
110 
111         try {
112             String cmd = ParamUtil.getString(renderRequest, Constants.CMD);
113 
114             if (!cmd.equals(Constants.ADD)) {
115                 ActionUtil.getFeed(renderRequest);
116             }
117         }
118         catch (NoSuchFeedException nssfe) {
119 
120             // Let this slide because the user can manually input a feed id for
121             // a new syndicated feed that does not yet exist.
122 
123         }
124         catch (Exception e) {
125             if (e instanceof PrincipalException) {
126                 SessionErrors.add(renderRequest, e.getClass().getName());
127 
128                 return mapping.findForward("portlet.journal.error");
129             }
130             else {
131                 throw e;
132             }
133         }
134 
135         return mapping.findForward(
136             getForward(renderRequest, "portlet.journal.edit_feed"));
137     }
138 
139     protected void deleteFeeds(ActionRequest actionRequest) throws Exception {
140         long groupId = ParamUtil.getLong(actionRequest, "groupId");
141 
142         String[] deleteFeedIds = StringUtil.split(
143             ParamUtil.getString(actionRequest, "deleteFeedIds"));
144 
145         for (int i = 0; i < deleteFeedIds.length; i++) {
146             JournalFeedServiceUtil.deleteFeed(groupId, deleteFeedIds[i]);
147         }
148     }
149 
150     protected void updateFeed(ActionRequest actionRequest) throws Exception {
151         String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
152 
153         long groupId = ParamUtil.getLong(actionRequest, "groupId");
154 
155         String feedId = ParamUtil.getString(actionRequest, "feedId");
156         boolean autoFeedId = ParamUtil.getBoolean(actionRequest, "autoFeedId");
157 
158         String name = ParamUtil.getString(actionRequest, "name");
159         String description = ParamUtil.getString(actionRequest, "description");
160         String type = ParamUtil.getString(actionRequest, "type");
161         String structureId = ParamUtil.getString(actionRequest, "structureId");
162         String templateId = ParamUtil.getString(actionRequest, "templateId");
163         String rendererTemplateId = ParamUtil.getString(
164             actionRequest, "rendererTemplateId");
165         int delta = ParamUtil.getInteger(actionRequest, "delta");
166         String orderByCol = ParamUtil.getString(actionRequest, "orderByCol");
167         String orderByType = ParamUtil.getString(actionRequest, "orderByType");
168         String targetLayoutFriendlyUrl = ParamUtil.getString(
169             actionRequest, "targetLayoutFriendlyUrl");
170         String targetPortletId = ParamUtil.getString(
171             actionRequest, "targetPortletId");
172         String contentField = ParamUtil.getString(
173             actionRequest, "contentField");
174 
175         String feedType = RSSUtil.DEFAULT_TYPE;
176         double feedVersion = RSSUtil.DEFAULT_VERSION;
177 
178         String feedTypeAndVersion = ParamUtil.getString(
179             actionRequest, "feedTypeAndVersion");
180 
181         if (Validator.isNotNull(feedTypeAndVersion)) {
182             String[] parts = feedTypeAndVersion.split(StringPool.COLON);
183 
184             try {
185                 feedType = parts[0];
186                 feedVersion = GetterUtil.getDouble(parts[1]);
187             }
188             catch (Exception e) {
189             }
190         }
191         else {
192             feedType = ParamUtil.getString(actionRequest, "feedType", feedType);
193             feedVersion = ParamUtil.getDouble(
194                 actionRequest, "feedVersion", feedVersion);
195         }
196 
197         ServiceContext serviceContext = ServiceContextFactory.getInstance(
198             JournalFeed.class.getName(), actionRequest);
199 
200         if (cmd.equals(Constants.ADD)) {
201 
202             // Add feed
203 
204             JournalFeedServiceUtil.addFeed(
205                 groupId, feedId, autoFeedId, name, description, type,
206                 structureId, templateId, rendererTemplateId, delta, orderByCol,
207                 orderByType, targetLayoutFriendlyUrl, targetPortletId,
208                 contentField, feedType, feedVersion, serviceContext);
209         }
210         else {
211 
212             // Update feed
213 
214             JournalFeedServiceUtil.updateFeed(
215                 groupId, feedId, name, description, type, structureId,
216                 templateId, rendererTemplateId, delta, orderByCol, orderByType,
217                 targetLayoutFriendlyUrl, targetPortletId, contentField,
218                 feedType, feedVersion, serviceContext);
219         }
220     }
221 
222 }