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