001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.journal.service.impl;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.kernel.log.Log;
020    import com.liferay.portal.kernel.log.LogFactoryUtil;
021    import com.liferay.portal.kernel.util.CharPool;
022    import com.liferay.portal.kernel.util.HtmlUtil;
023    import com.liferay.portal.kernel.util.OrderByComparator;
024    import com.liferay.portal.kernel.util.Validator;
025    import com.liferay.portal.kernel.xml.Document;
026    import com.liferay.portal.kernel.xml.Node;
027    import com.liferay.portal.kernel.xml.SAXReaderUtil;
028    import com.liferay.portal.kernel.xml.XPath;
029    import com.liferay.portal.model.ResourceConstants;
030    import com.liferay.portal.model.User;
031    import com.liferay.portal.service.ServiceContext;
032    import com.liferay.portal.util.PortalUtil;
033    import com.liferay.portlet.expando.model.ExpandoBridge;
034    import com.liferay.portlet.journal.DuplicateFeedIdException;
035    import com.liferay.portlet.journal.FeedContentFieldException;
036    import com.liferay.portlet.journal.FeedIdException;
037    import com.liferay.portlet.journal.FeedNameException;
038    import com.liferay.portlet.journal.FeedTargetLayoutFriendlyUrlException;
039    import com.liferay.portlet.journal.model.JournalFeed;
040    import com.liferay.portlet.journal.model.JournalFeedConstants;
041    import com.liferay.portlet.journal.model.JournalStructure;
042    import com.liferay.portlet.journal.service.base.JournalFeedLocalServiceBaseImpl;
043    import com.liferay.util.RSSUtil;
044    
045    import java.util.Date;
046    import java.util.List;
047    
048    /**
049     * @author Raymond Aug??
050     */
051    public class JournalFeedLocalServiceImpl
052            extends JournalFeedLocalServiceBaseImpl {
053    
054            @Override
055            public JournalFeed addFeed(
056                            long userId, long groupId, String feedId, boolean autoFeedId,
057                            String name, String description, String type, String structureId,
058                            String templateId, String rendererTemplateId, int delta,
059                            String orderByCol, String orderByType,
060                            String targetLayoutFriendlyUrl, String targetPortletId,
061                            String contentField, String feedType, double feedVersion,
062                            ServiceContext serviceContext)
063                    throws PortalException, SystemException {
064    
065                    // Feed
066    
067                    User user = userPersistence.findByPrimaryKey(userId);
068                    feedId = feedId.trim().toUpperCase();
069                    Date now = new Date();
070    
071                    validate(
072                            user.getCompanyId(), groupId, feedId, autoFeedId, name, structureId,
073                            targetLayoutFriendlyUrl, contentField);
074    
075                    if (autoFeedId) {
076                            feedId = String.valueOf(counterLocalService.increment());
077                    }
078    
079                    long id = counterLocalService.increment();
080    
081                    JournalFeed feed = journalFeedPersistence.create(id);
082    
083                    feed.setUuid(serviceContext.getUuid());
084                    feed.setGroupId(groupId);
085                    feed.setCompanyId(user.getCompanyId());
086                    feed.setUserId(user.getUserId());
087                    feed.setUserName(user.getFullName());
088                    feed.setCreateDate(serviceContext.getCreateDate(now));
089                    feed.setModifiedDate(serviceContext.getModifiedDate(now));
090                    feed.setFeedId(feedId);
091                    feed.setName(name);
092                    feed.setDescription(description);
093                    feed.setType(type);
094                    feed.setStructureId(structureId);
095                    feed.setTemplateId(templateId);
096                    feed.setRendererTemplateId(rendererTemplateId);
097                    feed.setDelta(delta);
098                    feed.setOrderByCol(orderByCol);
099                    feed.setOrderByType(orderByType);
100                    feed.setTargetLayoutFriendlyUrl(targetLayoutFriendlyUrl);
101                    feed.setTargetPortletId(targetPortletId);
102                    feed.setContentField(contentField);
103    
104                    if (Validator.isNull(feedType)) {
105                            feed.setFeedType(RSSUtil.TYPE_DEFAULT);
106                            feed.setFeedVersion(RSSUtil.VERSION_DEFAULT);
107                    }
108                    else {
109                            feed.setFeedType(feedType);
110                            feed.setFeedVersion(feedVersion);
111                    }
112    
113                    journalFeedPersistence.update(feed, false);
114    
115                    // Resources
116    
117                    if (serviceContext.isAddGroupPermissions() ||
118                            serviceContext.isAddGuestPermissions()) {
119    
120                            addFeedResources(
121                                    feed, serviceContext.isAddGroupPermissions(),
122                                    serviceContext.isAddGuestPermissions());
123                    }
124                    else {
125                            addFeedResources(
126                                    feed, serviceContext.getGroupPermissions(),
127                                    serviceContext.getGuestPermissions());
128                    }
129    
130                    // Expando
131    
132                    ExpandoBridge expandoBridge = feed.getExpandoBridge();
133    
134                    expandoBridge.setAttributes(serviceContext);
135    
136                    return feed;
137            }
138    
139            @Override
140            public void addFeedResources(
141                            JournalFeed feed, boolean addGroupPermissions,
142                            boolean addGuestPermissions)
143                    throws PortalException, SystemException {
144    
145                    resourceLocalService.addResources(
146                            feed.getCompanyId(), feed.getGroupId(), feed.getUserId(),
147                            JournalFeed.class.getName(), feed.getId(), false,
148                            addGroupPermissions, addGuestPermissions);
149            }
150    
151            @Override
152            public void addFeedResources(
153                            JournalFeed feed, String[] groupPermissions,
154                            String[] guestPermissions)
155                    throws PortalException, SystemException {
156    
157                    resourceLocalService.addModelResources(
158                            feed.getCompanyId(), feed.getGroupId(), feed.getUserId(),
159                            JournalFeed.class.getName(), feed.getId(), groupPermissions,
160                            guestPermissions);
161            }
162    
163            @Override
164            public void addFeedResources(
165                            long feedId, boolean addGroupPermissions,
166                            boolean addGuestPermissions)
167                    throws PortalException, SystemException {
168    
169                    JournalFeed feed = journalFeedPersistence.findByPrimaryKey(feedId);
170    
171                    addFeedResources(feed, addGroupPermissions, addGuestPermissions);
172            }
173    
174            @Override
175            public void addFeedResources(
176                            long feedId, String[] groupPermissions, String[] guestPermissions)
177                    throws PortalException, SystemException {
178    
179                    JournalFeed feed = journalFeedPersistence.findByPrimaryKey(feedId);
180    
181                    addFeedResources(feed, groupPermissions, guestPermissions);
182            }
183    
184            @Override
185            public void deleteFeed(JournalFeed feed)
186                    throws PortalException, SystemException {
187    
188                    // Expando
189    
190                    expandoValueLocalService.deleteValues(
191                            JournalFeed.class.getName(), feed.getId());
192    
193                    // Resources
194    
195                    resourceLocalService.deleteResource(
196                            feed.getCompanyId(), JournalFeed.class.getName(),
197                            ResourceConstants.SCOPE_INDIVIDUAL, feed.getId());
198    
199                    // Feed
200    
201                    journalFeedPersistence.remove(feed);
202            }
203    
204            @Override
205            public void deleteFeed(long feedId)
206                    throws PortalException, SystemException {
207    
208                    JournalFeed feed = journalFeedPersistence.findByPrimaryKey(feedId);
209    
210                    deleteFeed(feed);
211            }
212    
213            @Override
214            public void deleteFeed(long groupId, String feedId)
215                    throws PortalException, SystemException {
216    
217                    JournalFeed feed = journalFeedPersistence.findByG_F(groupId, feedId);
218    
219                    deleteFeed(feed);
220            }
221    
222            @Override
223            public JournalFeed getFeed(long feedId)
224                    throws PortalException, SystemException {
225    
226                    return journalFeedPersistence.findByPrimaryKey(feedId);
227            }
228    
229            @Override
230            public JournalFeed getFeed(long groupId, String feedId)
231                    throws PortalException, SystemException {
232    
233                    return journalFeedPersistence.findByG_F(groupId, feedId);
234            }
235    
236            @Override
237            public List<JournalFeed> getFeeds() throws SystemException {
238                    return journalFeedPersistence.findAll();
239            }
240    
241            @Override
242            public List<JournalFeed> getFeeds(long groupId) throws SystemException {
243                    return journalFeedPersistence.findByGroupId(groupId);
244            }
245    
246            @Override
247            public List<JournalFeed> getFeeds(long groupId, int start, int end)
248                    throws SystemException {
249    
250                    return journalFeedPersistence.findByGroupId(groupId, start, end);
251            }
252    
253            @Override
254            public int getFeedsCount(long groupId) throws SystemException {
255                    return journalFeedPersistence.countByGroupId(groupId);
256            }
257    
258            @Override
259            public List<JournalFeed> search(
260                            long companyId, long groupId, String keywords, int start, int end,
261                            OrderByComparator obc)
262                    throws SystemException {
263    
264                    return journalFeedFinder.findByKeywords(
265                            companyId, groupId, keywords, start, end, obc);
266            }
267    
268            @Override
269            public List<JournalFeed> search(
270                            long companyId, long groupId, String feedId, String name,
271                            String description, boolean andOperator, int start, int end,
272                            OrderByComparator obc)
273                    throws SystemException {
274    
275                    return journalFeedFinder.findByC_G_F_N_D(
276                            companyId, groupId, feedId, name, description, andOperator, start,
277                            end, obc);
278            }
279    
280            @Override
281            public int searchCount(long companyId, long groupId, String keywords)
282                    throws SystemException {
283    
284                    return journalFeedFinder.countByKeywords(companyId, groupId, keywords);
285            }
286    
287            @Override
288            public int searchCount(
289                            long companyId, long groupId, String feedId, String name,
290                            String description, boolean andOperator)
291                    throws SystemException {
292    
293                    return journalFeedFinder.countByC_G_F_N_D(
294                            companyId, groupId, feedId, name, description, andOperator);
295            }
296    
297            @Override
298            public JournalFeed updateFeed(
299                            long groupId, String feedId, String name, String description,
300                            String type, String structureId, String templateId,
301                            String rendererTemplateId, int delta, String orderByCol,
302                            String orderByType, String targetLayoutFriendlyUrl,
303                            String targetPortletId, String contentField, String feedType,
304                            double feedVersion, ServiceContext serviceContext)
305                    throws PortalException, SystemException {
306    
307                    // Feed
308    
309                    JournalFeed feed = journalFeedPersistence.findByG_F(groupId, feedId);
310    
311                    validate(
312                            feed.getCompanyId(), groupId, name, structureId,
313                            targetLayoutFriendlyUrl, contentField);
314    
315                    feed.setModifiedDate(serviceContext.getModifiedDate(null));
316                    feed.setName(name);
317                    feed.setDescription(description);
318                    feed.setType(type);
319                    feed.setStructureId(structureId);
320                    feed.setTemplateId(templateId);
321                    feed.setRendererTemplateId(rendererTemplateId);
322                    feed.setDelta(delta);
323                    feed.setOrderByCol(orderByCol);
324                    feed.setOrderByType(orderByType);
325                    feed.setTargetLayoutFriendlyUrl(targetLayoutFriendlyUrl);
326                    feed.setTargetPortletId(targetPortletId);
327                    feed.setContentField(contentField);
328    
329                    if (Validator.isNull(feedType)) {
330                            feed.setFeedType(RSSUtil.TYPE_DEFAULT);
331                            feed.setFeedVersion(RSSUtil.VERSION_DEFAULT);
332                    }
333                    else {
334                            feed.setFeedType(feedType);
335                            feed.setFeedVersion(feedVersion);
336                    }
337    
338                    journalFeedPersistence.update(feed, false);
339    
340                    // Expando
341    
342                    ExpandoBridge expandoBridge = feed.getExpandoBridge();
343    
344                    expandoBridge.setAttributes(serviceContext);
345    
346                    return feed;
347            }
348    
349            protected boolean isValidStructureField(
350                    long groupId, String structureId, String contentField) {
351    
352                    if (contentField.equals(JournalFeedConstants.WEB_CONTENT_DESCRIPTION) ||
353                            contentField.equals(JournalFeedConstants.RENDERED_WEB_CONTENT)) {
354    
355                            return true;
356                    }
357                    else {
358                            try {
359                                    JournalStructure structure =
360                                            journalStructurePersistence.findByG_S(groupId, structureId);
361    
362                                    Document document = SAXReaderUtil.read(structure.getXsd());
363    
364                                    contentField = HtmlUtil.escapeXPathAttribute(contentField);
365    
366                                    XPath xPathSelector = SAXReaderUtil.createXPath(
367                                            "//dynamic-element[@name="+ contentField + "]");
368    
369                                    Node node = xPathSelector.selectSingleNode(document);
370    
371                                    if (node != null) {
372                                            return true;
373                                    }
374                            }
375                            catch (Exception e) {
376                                    _log.error(e, e);
377                            }
378                    }
379    
380                    return false;
381            }
382    
383            protected void validate(
384                            long companyId, long groupId, String feedId, boolean autoFeedId,
385                            String name, String structureId, String targetLayoutFriendlyUrl,
386                            String contentField)
387                    throws PortalException, SystemException {
388    
389                    if (!autoFeedId) {
390                            if (Validator.isNull(feedId) || Validator.isNumber(feedId) ||
391                                    (feedId.indexOf(CharPool.SPACE) != -1)) {
392    
393                                    throw new FeedIdException();
394                            }
395    
396                            JournalFeed feed = journalFeedPersistence.fetchByG_F(
397                                    groupId, feedId);
398    
399                            if (feed != null) {
400                                    throw new DuplicateFeedIdException();
401                            }
402                    }
403    
404                    validate(
405                            companyId, groupId, name, structureId, targetLayoutFriendlyUrl,
406                            contentField);
407            }
408    
409            protected void validate(
410                            long companyId, long groupId, String name, String structureId,
411                            String targetLayoutFriendlyUrl, String contentField)
412                    throws PortalException {
413    
414                    if (Validator.isNull(name)) {
415                            throw new FeedNameException();
416                    }
417    
418                    long plid = PortalUtil.getPlidFromFriendlyURL(
419                            companyId, targetLayoutFriendlyUrl);
420    
421                    if (plid <= 0) {
422                            throw new FeedTargetLayoutFriendlyUrlException();
423                    }
424    
425                    if (!isValidStructureField(groupId, structureId, contentField)) {
426                            throw new FeedContentFieldException();
427                    }
428            }
429    
430            private static Log _log = LogFactoryUtil.getLog(
431                    JournalFeedLocalServiceImpl.class);
432    
433    }