001    /**
002     * Copyright (c) 2000-present 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.dao.orm.QueryUtil;
018    import com.liferay.portal.kernel.exception.PortalException;
019    import com.liferay.portal.kernel.log.Log;
020    import com.liferay.portal.kernel.log.LogFactoryUtil;
021    import com.liferay.portal.model.Group;
022    import com.liferay.portal.model.Layout;
023    import com.liferay.portal.model.LayoutTypePortlet;
024    import com.liferay.portal.model.PortletConstants;
025    import com.liferay.portal.util.PortletKeys;
026    import com.liferay.portlet.asset.provider.DisplayInformationProvider;
027    import com.liferay.portlet.journal.model.JournalContentSearch;
028    import com.liferay.portlet.journal.service.base.JournalContentSearchLocalServiceBaseImpl;
029    import com.liferay.registry.collections.ServiceTrackerCollections;
030    import com.liferay.registry.collections.ServiceTrackerMap;
031    
032    import java.util.ArrayList;
033    import java.util.List;
034    
035    import javax.portlet.PortletPreferences;
036    
037    /**
038     * @author Brian Wing Shun Chan
039     * @author Wesley Gong
040     */
041    public class JournalContentSearchLocalServiceImpl
042            extends JournalContentSearchLocalServiceBaseImpl {
043    
044            @Override
045            public void checkContentSearches(long companyId) throws PortalException {
046                    if (_log.isInfoEnabled()) {
047                            _log.info("Checking journal content search for " + companyId);
048                    }
049    
050                    List<Layout> layouts = new ArrayList<Layout>();
051    
052                    List<Group> groups = groupLocalService.search(
053                            companyId, null, null, null, QueryUtil.ALL_POS, QueryUtil.ALL_POS);
054    
055                    for (Group group : groups) {
056    
057                            // Private layouts
058    
059                            deleteOwnerContentSearches(group.getGroupId(), true);
060    
061                            layouts.addAll(
062                                    layoutLocalService.getLayouts(group.getGroupId(), true));
063    
064                            // Public layouts
065    
066                            deleteOwnerContentSearches(group.getGroupId(), false);
067    
068                            layouts.addAll(
069                                    layoutLocalService.getLayouts(group.getGroupId(), false));
070                    }
071    
072                    for (Layout layout : layouts) {
073                            LayoutTypePortlet layoutTypePortlet =
074                                    (LayoutTypePortlet)layout.getLayoutType();
075    
076                            List<String> portletIds = layoutTypePortlet.getPortletIds();
077    
078                            for (String portletId : portletIds) {
079                                    String rootPortletId = PortletConstants.getRootPortletId(
080                                            portletId);
081    
082                                    DisplayInformationProvider displayInformationProvider =
083                                            _serviceTrackerMap.getService(rootPortletId);
084    
085                                    if (displayInformationProvider == null) {
086                                            continue;
087                                    }
088    
089                                    PortletPreferences portletPreferences =
090                                            portletPreferencesLocalService.getPreferences(
091                                                    layout.getCompanyId(),
092                                                    PortletKeys.PREFS_OWNER_ID_DEFAULT,
093                                                    PortletKeys.PREFS_OWNER_TYPE_LAYOUT, layout.getPlid(),
094                                                    portletId);
095    
096                                    String classPK = displayInformationProvider.getClassPK(
097                                            portletPreferences);
098    
099                                    updateContentSearch(
100                                            layout.getGroupId(), layout.isPrivateLayout(),
101                                            layout.getLayoutId(), portletId, classPK);
102                            }
103                    }
104            }
105    
106            @Override
107            public void deleteArticleContentSearch(
108                    long groupId, boolean privateLayout, long layoutId, String portletId) {
109    
110                    journalContentSearchPersistence.removeByG_P_L_P(
111                            groupId, privateLayout, layoutId, portletId);
112            }
113    
114            @Override
115            public void deleteArticleContentSearch(
116                    long groupId, boolean privateLayout, long layoutId, String portletId,
117                    String articleId) {
118    
119                    JournalContentSearch contentSearch =
120                            journalContentSearchPersistence.fetchByG_P_L_P_A(
121                                    groupId, privateLayout, layoutId, portletId, articleId);
122    
123                    if (contentSearch != null) {
124                            deleteJournalContentSearch(contentSearch);
125                    }
126            }
127    
128            @Override
129            public void deleteArticleContentSearches(long groupId, String articleId) {
130                    List<JournalContentSearch> contentSearches =
131                            journalContentSearchPersistence.findByG_A(groupId, articleId);
132    
133                    for (JournalContentSearch contentSearch : contentSearches) {
134                            deleteJournalContentSearch(contentSearch);
135                    }
136            }
137    
138            @Override
139            public void deleteLayoutContentSearches(
140                    long groupId, boolean privateLayout, long layoutId) {
141    
142                    List<JournalContentSearch> contentSearches =
143                            journalContentSearchPersistence.findByG_P_L(
144                                    groupId, privateLayout, layoutId);
145    
146                    for (JournalContentSearch contentSearch : contentSearches) {
147                            deleteJournalContentSearch(contentSearch);
148                    }
149            }
150    
151            @Override
152            public void deleteOwnerContentSearches(
153                    long groupId, boolean privateLayout) {
154    
155                    List<JournalContentSearch> contentSearches =
156                            journalContentSearchPersistence.findByG_P(groupId, privateLayout);
157    
158                    for (JournalContentSearch contentSearch : contentSearches) {
159                            deleteJournalContentSearch(contentSearch);
160                    }
161            }
162    
163            @Override
164            public List<JournalContentSearch> getArticleContentSearches() {
165                    return journalContentSearchPersistence.findAll();
166            }
167    
168            @Override
169            public List<JournalContentSearch> getArticleContentSearches(
170                    long groupId, String articleId) {
171    
172                    return journalContentSearchPersistence.findByG_A(groupId, articleId);
173            }
174    
175            @Override
176            public List<JournalContentSearch> getArticleContentSearches(
177                    String articleId) {
178    
179                    return journalContentSearchPersistence.findByArticleId(articleId);
180            }
181    
182            @Override
183            public List<Long> getLayoutIds(
184                    long groupId, boolean privateLayout, String articleId) {
185    
186                    List<Long> layoutIds = new ArrayList<Long>();
187    
188                    List<JournalContentSearch> contentSearches =
189                            journalContentSearchPersistence.findByG_P_A(
190                                    groupId, privateLayout, articleId);
191    
192                    for (JournalContentSearch contentSearch : contentSearches) {
193                            layoutIds.add(contentSearch.getLayoutId());
194                    }
195    
196                    return layoutIds;
197            }
198    
199            @Override
200            public int getLayoutIdsCount(
201                    long groupId, boolean privateLayout, String articleId) {
202    
203                    return journalContentSearchPersistence.countByG_P_A(
204                            groupId, privateLayout, articleId);
205            }
206    
207            @Override
208            public int getLayoutIdsCount(String articleId) {
209                    return journalContentSearchPersistence.countByArticleId(articleId);
210            }
211    
212            @Override
213            public List<JournalContentSearch> getPortletContentSearches(
214                    String portletId) {
215    
216                    return journalContentSearchPersistence.findByPortletId(portletId);
217            }
218    
219            @Override
220            public JournalContentSearch updateContentSearch(
221                            long groupId, boolean privateLayout, long layoutId,
222                            String portletId, String articleId)
223                    throws PortalException {
224    
225                    return updateContentSearch(
226                            groupId, privateLayout, layoutId, portletId, articleId, false);
227            }
228    
229            @Override
230            public JournalContentSearch updateContentSearch(
231                            long groupId, boolean privateLayout, long layoutId,
232                            String portletId, String articleId, boolean purge)
233                    throws PortalException {
234    
235                    if (purge) {
236                            journalContentSearchPersistence.removeByG_P_L_P(
237                                    groupId, privateLayout, layoutId, portletId);
238                    }
239    
240                    Group group = groupPersistence.findByPrimaryKey(groupId);
241    
242                    JournalContentSearch contentSearch =
243                            journalContentSearchPersistence.fetchByG_P_L_P_A(
244                                    groupId, privateLayout, layoutId, portletId, articleId);
245    
246                    if (contentSearch == null) {
247                            long contentSearchId = counterLocalService.increment();
248    
249                            contentSearch = journalContentSearchPersistence.create(
250                                    contentSearchId);
251    
252                            contentSearch.setGroupId(groupId);
253                            contentSearch.setCompanyId(group.getCompanyId());
254                            contentSearch.setPrivateLayout(privateLayout);
255                            contentSearch.setLayoutId(layoutId);
256                            contentSearch.setPortletId(portletId);
257                            contentSearch.setArticleId(articleId);
258                    }
259    
260                    journalContentSearchPersistence.update(contentSearch);
261    
262                    return contentSearch;
263            }
264    
265            @Override
266            public List<JournalContentSearch> updateContentSearch(
267                            long groupId, boolean privateLayout, long layoutId,
268                            String portletId, String[] articleIds)
269                    throws PortalException {
270    
271                    journalContentSearchPersistence.removeByG_P_L_P(
272                            groupId, privateLayout, layoutId, portletId);
273    
274                    List<JournalContentSearch> contentSearches =
275                            new ArrayList<JournalContentSearch>();
276    
277                    for (String articleId : articleIds) {
278                            JournalContentSearch contentSearch = updateContentSearch(
279                                    groupId, privateLayout, layoutId, portletId, articleId, false);
280    
281                            contentSearches.add(contentSearch);
282                    }
283    
284                    return contentSearches;
285            }
286    
287            private static final Log _log = LogFactoryUtil.getLog(
288                    JournalContentSearchLocalServiceImpl.class);
289    
290            private static final ServiceTrackerMap<String, DisplayInformationProvider>
291                    _serviceTrackerMap = ServiceTrackerCollections.singleValueMap(
292                            DisplayInformationProvider.class, "javax.portlet.name");
293    
294            static {
295                    _serviceTrackerMap.open();
296            }
297    
298    }