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 SystemException {
108    
109                    JournalContentSearch contentSearch =
110                            journalContentSearchPersistence.fetchByG_P_L_P_A(
111                                    groupId, privateLayout, layoutId, portletId, articleId);
112    
113                    if (contentSearch != null) {
114                            deleteJournalContentSearch(contentSearch);
115                    }
116            }
117    
118            public void deleteArticleContentSearches(long groupId, String articleId)
119                    throws SystemException {
120    
121                    List<JournalContentSearch> contentSearches =
122                            journalContentSearchPersistence.findByG_A(groupId, articleId);
123    
124                    for (JournalContentSearch contentSearch : contentSearches) {
125                            deleteJournalContentSearch(contentSearch);
126                    }
127            }
128    
129            public void deleteLayoutContentSearches(
130                            long groupId, boolean privateLayout, long layoutId)
131                    throws SystemException {
132    
133                    List<JournalContentSearch> contentSearches =
134                            journalContentSearchPersistence.findByG_P_L(
135                                    groupId, privateLayout, layoutId);
136    
137                    for (JournalContentSearch contentSearch : contentSearches) {
138                            deleteJournalContentSearch(contentSearch);
139                    }
140            }
141    
142            public void deleteOwnerContentSearches(long groupId, boolean privateLayout)
143                    throws SystemException {
144    
145                    List<JournalContentSearch> contentSearches =
146                            journalContentSearchPersistence.findByG_P(groupId, privateLayout);
147    
148                    for (JournalContentSearch contentSearch : contentSearches) {
149                            deleteJournalContentSearch(contentSearch);
150                    }
151            }
152    
153            public List<JournalContentSearch> getArticleContentSearches()
154                    throws SystemException {
155    
156                    return journalContentSearchPersistence.findAll();
157            }
158    
159            public List<JournalContentSearch> getArticleContentSearches(
160                            long groupId, String articleId)
161                    throws SystemException {
162    
163                    return journalContentSearchPersistence.findByG_A(groupId, articleId);
164            }
165    
166            public List<JournalContentSearch> getArticleContentSearches(
167                            String articleId)
168                    throws SystemException {
169    
170                    return journalContentSearchPersistence.findByArticleId(articleId);
171            }
172    
173            public List<Long> getLayoutIds(
174                            long groupId, boolean privateLayout, String articleId)
175                    throws SystemException {
176    
177                    List<Long> layoutIds = new ArrayList<Long>();
178    
179                    List<JournalContentSearch> contentSearches =
180                            journalContentSearchPersistence.findByG_P_A(
181                                    groupId, privateLayout, articleId);
182    
183                    for (JournalContentSearch contentSearch : contentSearches) {
184                            layoutIds.add(contentSearch.getLayoutId());
185                    }
186    
187                    return layoutIds;
188            }
189    
190            public int getLayoutIdsCount(
191                            long groupId, boolean privateLayout, String articleId)
192                    throws SystemException {
193    
194                    return journalContentSearchPersistence.countByG_P_A(
195                            groupId, privateLayout, articleId);
196            }
197    
198            public int getLayoutIdsCount(String articleId) throws SystemException {
199                    return journalContentSearchPersistence.countByArticleId(articleId);
200            }
201    
202            public List<JournalContentSearch> getPortletContentSearches(
203                            String portletId)
204                    throws SystemException {
205    
206                    return journalContentSearchPersistence.findByPortletId(portletId);
207            }
208    
209            public JournalContentSearch updateContentSearch(
210                            long groupId, boolean privateLayout, long layoutId,
211                            String portletId, String articleId)
212                    throws PortalException, SystemException {
213    
214                    return updateContentSearch(
215                            groupId, privateLayout, layoutId, portletId, articleId, false);
216            }
217    
218            public JournalContentSearch updateContentSearch(
219                            long groupId, boolean privateLayout, long layoutId,
220                            String portletId, String articleId, boolean purge)
221                    throws PortalException, SystemException {
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            public List<JournalContentSearch> updateContentSearch(
254                            long groupId, boolean privateLayout, long layoutId,
255                            String portletId, String[] articleIds)
256                    throws PortalException, SystemException {
257    
258                    journalContentSearchPersistence.removeByG_P_L_P(
259                            groupId, privateLayout, layoutId, portletId);
260    
261                    List<JournalContentSearch> contentSearches =
262                            new ArrayList<JournalContentSearch>();
263    
264                    for (String articleId : articleIds) {
265                            JournalContentSearch contentSearch = updateContentSearch(
266                                    groupId, privateLayout, layoutId, portletId, articleId, false);
267    
268                            contentSearches.add(contentSearch);
269                    }
270    
271                    return contentSearches;
272            }
273    
274            private static Log _log = LogFactoryUtil.getLog(
275                    JournalContentSearchLocalServiceImpl.class);
276    
277    }