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