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.service.impl;
24  
25  import com.liferay.portal.PortalException;
26  import com.liferay.portal.SystemException;
27  import com.liferay.portal.kernel.util.OrderByComparator;
28  import com.liferay.portal.kernel.util.StringPool;
29  import com.liferay.portal.kernel.util.Validator;
30  import com.liferay.portal.kernel.xml.Document;
31  import com.liferay.portal.kernel.xml.Element;
32  import com.liferay.portal.kernel.xml.SAXReaderUtil;
33  import com.liferay.portal.kernel.xml.XPath;
34  import com.liferay.portal.model.ResourceConstants;
35  import com.liferay.portal.model.User;
36  import com.liferay.portal.service.ServiceContext;
37  import com.liferay.portal.util.PortalUtil;
38  import com.liferay.portlet.expando.model.ExpandoBridge;
39  import com.liferay.portlet.journal.DuplicateFeedIdException;
40  import com.liferay.portlet.journal.FeedContentFieldException;
41  import com.liferay.portlet.journal.FeedDescriptionException;
42  import com.liferay.portlet.journal.FeedIdException;
43  import com.liferay.portlet.journal.FeedNameException;
44  import com.liferay.portlet.journal.FeedTargetLayoutFriendlyUrlException;
45  import com.liferay.portlet.journal.model.JournalFeed;
46  import com.liferay.portlet.journal.model.JournalStructure;
47  import com.liferay.portlet.journal.model.impl.JournalFeedImpl;
48  import com.liferay.portlet.journal.service.base.JournalFeedLocalServiceBaseImpl;
49  import com.liferay.util.RSSUtil;
50  
51  import java.util.Date;
52  import java.util.List;
53  
54  /**
55   * <a href="JournalFeedLocalServiceImpl.java.html"><b><i>View Source</i></b></a>
56   *
57   * @author Raymond Augé
58   */
59  public class JournalFeedLocalServiceImpl
60      extends JournalFeedLocalServiceBaseImpl {
61  
62      public JournalFeed addFeed(
63              long userId, long groupId, String feedId, boolean autoFeedId,
64              String name, String description, String type, String structureId,
65              String templateId, String rendererTemplateId, int delta,
66              String orderByCol, String orderByType,
67              String targetLayoutFriendlyUrl, String targetPortletId,
68              String contentField, String feedType, double feedVersion,
69              ServiceContext serviceContext)
70          throws PortalException, SystemException {
71  
72          return addFeed(
73              null, userId, groupId, feedId, autoFeedId, name, description, type,
74              structureId, templateId, rendererTemplateId, delta, orderByCol,
75              orderByType, targetLayoutFriendlyUrl, targetPortletId, contentField,
76              feedType, feedVersion, serviceContext);
77      }
78  
79      public JournalFeed addFeed(
80              String uuid, long userId, long groupId, String feedId,
81              boolean autoFeedId, String name, String description,
82              String type, String structureId, String templateId,
83              String rendererTemplateId, int delta, String orderByCol,
84              String orderByType, String targetLayoutFriendlyUrl,
85              String targetPortletId, String contentField, String feedType,
86              double feedVersion, ServiceContext serviceContext)
87          throws PortalException, SystemException {
88  
89          // Feed
90  
91          User user = userPersistence.findByPrimaryKey(userId);
92          feedId = feedId.trim().toUpperCase();
93          Date now = new Date();
94  
95          validate(
96              user.getCompanyId(), groupId, feedId, autoFeedId, name, description,
97              structureId, targetLayoutFriendlyUrl, contentField);
98  
99          if (autoFeedId) {
100             feedId = String.valueOf(counterLocalService.increment());
101         }
102 
103         long id = counterLocalService.increment();
104 
105         JournalFeed feed = journalFeedPersistence.create(id);
106 
107         feed.setUuid(uuid);
108         feed.setGroupId(groupId);
109         feed.setCompanyId(user.getCompanyId());
110         feed.setUserId(user.getUserId());
111         feed.setUserName(user.getFullName());
112         feed.setCreateDate(now);
113         feed.setModifiedDate(now);
114         feed.setFeedId(feedId);
115         feed.setName(name);
116         feed.setDescription(description);
117         feed.setType(type);
118         feed.setStructureId(structureId);
119         feed.setTemplateId(templateId);
120         feed.setRendererTemplateId(rendererTemplateId);
121         feed.setDelta(delta);
122         feed.setOrderByCol(orderByCol);
123         feed.setOrderByType(orderByType);
124         feed.setTargetLayoutFriendlyUrl(targetLayoutFriendlyUrl);
125         feed.setTargetPortletId(targetPortletId);
126         feed.setContentField(contentField);
127 
128         if (Validator.isNull(feedType)) {
129             feed.setFeedType(RSSUtil.DEFAULT_TYPE);
130             feed.setFeedVersion(RSSUtil.DEFAULT_VERSION);
131         }
132         else {
133             feed.setFeedType(feedType);
134             feed.setFeedVersion(feedVersion);
135         }
136 
137         journalFeedPersistence.update(feed, false);
138 
139         // Resources
140 
141         if (serviceContext.getAddCommunityPermissions() ||
142             serviceContext.getAddGuestPermissions()) {
143 
144             addFeedResources(
145                 feed, serviceContext.getAddCommunityPermissions(),
146                 serviceContext.getAddGuestPermissions());
147         }
148         else {
149             addFeedResources(
150                 feed, serviceContext.getCommunityPermissions(),
151                 serviceContext.getGuestPermissions());
152         }
153 
154         // Expando
155 
156         ExpandoBridge expandoBridge = feed.getExpandoBridge();
157 
158         expandoBridge.setAttributes(serviceContext);
159 
160         return feed;
161     }
162 
163     public void addFeedResources(
164             long feedId, boolean addCommunityPermissions,
165             boolean addGuestPermissions)
166         throws PortalException, SystemException {
167 
168         JournalFeed feed = journalFeedPersistence.findByPrimaryKey(feedId);
169 
170         addFeedResources(feed, addCommunityPermissions, addGuestPermissions);
171     }
172 
173     public void addFeedResources(
174             JournalFeed feed, boolean addCommunityPermissions,
175             boolean addGuestPermissions)
176         throws PortalException, SystemException {
177 
178         resourceLocalService.addResources(
179             feed.getCompanyId(), feed.getGroupId(), feed.getUserId(),
180             JournalFeed.class.getName(), feed.getId(), false,
181             addCommunityPermissions, addGuestPermissions);
182     }
183 
184     public void addFeedResources(
185             long feedId, String[] communityPermissions,
186             String[] guestPermissions)
187         throws PortalException, SystemException {
188 
189         JournalFeed feed = journalFeedPersistence.findByPrimaryKey(feedId);
190 
191         addFeedResources(feed, communityPermissions, guestPermissions);
192     }
193 
194     public void addFeedResources(
195             JournalFeed feed, String[] communityPermissions,
196             String[] guestPermissions)
197         throws PortalException, SystemException {
198 
199         resourceLocalService.addModelResources(
200             feed.getCompanyId(), feed.getGroupId(), feed.getUserId(),
201             JournalFeed.class.getName(), feed.getId(), communityPermissions,
202             guestPermissions);
203     }
204 
205     public void deleteFeed(long feedId)
206         throws PortalException, SystemException {
207 
208         JournalFeed feed = journalFeedPersistence.findByPrimaryKey(feedId);
209 
210         deleteFeed(feed);
211     }
212 
213     public void deleteFeed(long groupId, String feedId)
214         throws PortalException, SystemException {
215 
216         JournalFeed feed = journalFeedPersistence.findByG_F(groupId, feedId);
217 
218         deleteFeed(feed);
219     }
220 
221     public void deleteFeed(JournalFeed feed)
222         throws PortalException, SystemException {
223 
224         // Expando
225 
226         expandoValueLocalService.deleteValues(
227             JournalFeed.class.getName(), feed.getId());
228 
229         // Resources
230 
231         resourceLocalService.deleteResource(
232             feed.getCompanyId(), JournalFeed.class.getName(),
233             ResourceConstants.SCOPE_INDIVIDUAL, feed.getId());
234 
235         // Feed
236 
237         journalFeedPersistence.remove(feed);
238     }
239 
240     public JournalFeed getFeed(long feedId)
241         throws PortalException, SystemException {
242 
243         return journalFeedPersistence.findByPrimaryKey(feedId);
244     }
245 
246     public JournalFeed getFeed(long groupId, String feedId)
247         throws PortalException, SystemException {
248 
249         return journalFeedPersistence.findByG_F(groupId, feedId);
250     }
251 
252     public List<JournalFeed> getFeeds() throws SystemException {
253         return journalFeedPersistence.findAll();
254     }
255 
256     public List<JournalFeed> getFeeds(long groupId) throws SystemException {
257         return journalFeedPersistence.findByGroupId(groupId);
258     }
259 
260     public List<JournalFeed> getFeeds(long groupId, int start, int end)
261         throws SystemException {
262 
263         return journalFeedPersistence.findByGroupId(groupId, start, end);
264     }
265 
266     public int getFeedsCount(long groupId) throws SystemException {
267         return journalFeedPersistence.countByGroupId(groupId);
268     }
269 
270     public List<JournalFeed> search(
271             long companyId, long groupId, String keywords, int start, int end,
272             OrderByComparator obc)
273         throws SystemException {
274 
275         return journalFeedFinder.findByKeywords(
276             companyId, groupId, keywords, start, end, obc);
277     }
278 
279     public List<JournalFeed> search(
280             long companyId, long groupId, String feedId, String name,
281             String description, boolean andOperator, int start, int end,
282             OrderByComparator obc)
283         throws SystemException {
284 
285         return journalFeedFinder.findByC_G_F_N_D(
286             companyId, groupId, feedId, name, description, andOperator, start,
287             end, obc);
288     }
289 
290     public int searchCount(long companyId, long groupId, String keywords)
291         throws SystemException {
292 
293         return journalFeedFinder.countByKeywords(
294             companyId, groupId, keywords);
295     }
296 
297     public int searchCount(
298             long companyId, long groupId, String feedId, String name,
299             String description, boolean andOperator)
300         throws SystemException {
301 
302         return journalFeedFinder.countByC_G_F_N_D(
303             companyId, groupId, feedId, name, description, andOperator);
304     }
305 
306     public JournalFeed updateFeed(
307             long groupId, String feedId, String name, String description,
308             String type, String structureId, String templateId,
309             String rendererTemplateId, int delta, String orderByCol,
310             String orderByType, String targetLayoutFriendlyUrl,
311             String targetPortletId, String contentField, String feedType,
312             double feedVersion, ServiceContext serviceContext)
313         throws PortalException, SystemException{
314 
315         // Feed
316 
317         JournalFeed feed = journalFeedPersistence.findByG_F(groupId, feedId);
318 
319         validate(
320             feed.getCompanyId(), groupId, name, description, structureId,
321             targetLayoutFriendlyUrl, contentField);
322 
323         feed.setModifiedDate(new Date());
324         feed.setName(name);
325         feed.setDescription(description);
326         feed.setType(type);
327         feed.setStructureId(structureId);
328         feed.setTemplateId(templateId);
329         feed.setRendererTemplateId(rendererTemplateId);
330         feed.setDelta(delta);
331         feed.setOrderByCol(orderByCol);
332         feed.setOrderByType(orderByType);
333         feed.setTargetLayoutFriendlyUrl(targetLayoutFriendlyUrl);
334         feed.setTargetPortletId(targetPortletId);
335         feed.setContentField(contentField);
336 
337         if (Validator.isNull(feedType)) {
338             feed.setFeedType(RSSUtil.DEFAULT_TYPE);
339             feed.setFeedVersion(RSSUtil.DEFAULT_VERSION);
340         }
341         else {
342             feed.setFeedType(feedType);
343             feed.setFeedVersion(feedVersion);
344         }
345 
346         journalFeedPersistence.update(feed, false);
347 
348         // Expando
349 
350         ExpandoBridge expandoBridge = feed.getExpandoBridge();
351 
352         expandoBridge.setAttributes(serviceContext);
353 
354         return feed;
355     }
356 
357     protected boolean isValidStructureField(
358         long groupId, String structureId, String contentField) {
359 
360         if (contentField.equals(JournalFeedImpl.WEB_CONTENT_DESCRIPTION) ||
361             contentField.equals(JournalFeedImpl.RENDERED_WEB_CONTENT)) {
362 
363             return true;
364         }
365         else {
366             try {
367                 JournalStructure structure =
368                     journalStructurePersistence.findByG_S(groupId, structureId);
369 
370                 Document doc = SAXReaderUtil.read(structure.getXsd());
371 
372                 XPath xpathSelector = SAXReaderUtil.createXPath(
373                     "//dynamic-element[@name='"+ contentField + "']");
374 
375                 Element el = (Element)xpathSelector.selectSingleNode(doc);
376 
377                 if (el != null) {
378                     return true;
379                 }
380             }
381             catch (Exception e) {
382             }
383         }
384 
385         return false;
386     }
387 
388     protected void validate(
389             long companyId, long groupId, String feedId, boolean autoFeedId,
390             String name, String description, String structureId,
391             String targetLayoutFriendlyUrl, String contentField)
392         throws PortalException, SystemException {
393 
394         if (!autoFeedId) {
395             if ((Validator.isNull(feedId)) || (Validator.isNumber(feedId)) ||
396                 (feedId.indexOf(StringPool.SPACE) != -1)) {
397 
398                 throw new FeedIdException();
399             }
400 
401             JournalFeed feed = journalFeedPersistence.fetchByG_F(
402                 groupId, feedId);
403 
404             if (feed != null) {
405                 throw new DuplicateFeedIdException();
406             }
407         }
408 
409         validate(
410             companyId, groupId, name, description, structureId,
411             targetLayoutFriendlyUrl, contentField);
412     }
413 
414     protected void validate(
415             long companyId, long groupId, String name, String description,
416             String structureId, String targetLayoutFriendlyUrl,
417             String contentField)
418         throws PortalException {
419 
420         if (Validator.isNull(name)) {
421             throw new FeedNameException();
422         }
423 
424         if (Validator.isNull(description)) {
425             throw new FeedDescriptionException();
426         }
427 
428         long plid = PortalUtil.getPlidFromFriendlyURL(
429             companyId, targetLayoutFriendlyUrl);
430 
431         if (plid <= 0) {
432             throw new FeedTargetLayoutFriendlyUrlException();
433         }
434 
435         if (!isValidStructureField(groupId, structureId, contentField)) {
436             throw new FeedContentFieldException();
437         }
438     }
439 
440 }