001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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            public void deleteLayoutContentSearches(
128                            long groupId, boolean privateLayout, long layoutId)
129                    throws SystemException {
130    
131                    List<JournalContentSearch> contentSearches =
132                            journalContentSearchPersistence.findByG_P_L(
133                                    groupId, privateLayout, layoutId);
134    
135                    for (JournalContentSearch contentSearch : contentSearches) {
136                            deleteJournalContentSearch(contentSearch);
137                    }
138            }
139    
140            public void deleteOwnerContentSearches(long groupId, boolean privateLayout)
141                    throws SystemException {
142    
143                    List<JournalContentSearch> contentSearches =
144                            journalContentSearchPersistence.findByG_P(groupId, privateLayout);
145    
146                    for (JournalContentSearch contentSearch : contentSearches) {
147                            deleteJournalContentSearch(contentSearch);
148                    }
149            }
150    
151            public List<JournalContentSearch> getArticleContentSearches()
152                    throws SystemException {
153    
154                    return journalContentSearchPersistence.findAll();
155            }
156    
157            public List<JournalContentSearch> getArticleContentSearches(
158                            long groupId, String articleId)
159                    throws SystemException {
160    
161                    return journalContentSearchPersistence.findByG_A(groupId, articleId);
162            }
163    
164            public List<JournalContentSearch> getArticleContentSearches(
165                            String articleId)
166                    throws SystemException {
167    
168                    return journalContentSearchPersistence.findByArticleId(articleId);
169            }
170    
171            public List<Long> getLayoutIds(
172                            long groupId, boolean privateLayout, String articleId)
173                    throws SystemException {
174    
175                    List<Long> layoutIds = new ArrayList<Long>();
176    
177                    List<JournalContentSearch> contentSearches =
178                            journalContentSearchPersistence.findByG_P_A(
179                                    groupId, privateLayout, articleId);
180    
181                    for (JournalContentSearch contentSearch : contentSearches) {
182                            layoutIds.add(contentSearch.getLayoutId());
183                    }
184    
185                    return layoutIds;
186            }
187    
188            public int getLayoutIdsCount(
189                            long groupId, boolean privateLayout, String articleId)
190                    throws SystemException {
191    
192                    return journalContentSearchPersistence.countByG_P_A(
193                            groupId, privateLayout, articleId);
194            }
195    
196            public int getLayoutIdsCount(String articleId) throws SystemException {
197                    return journalContentSearchPersistence.countByArticleId(articleId);
198            }
199    
200            public JournalContentSearch updateContentSearch(
201                            long groupId, boolean privateLayout, long layoutId,
202                            String portletId, String articleId)
203                    throws PortalException, SystemException {
204    
205                    return updateContentSearch(
206                            groupId, privateLayout, layoutId, portletId, articleId, false);
207            }
208    
209            public JournalContentSearch updateContentSearch(
210                            long groupId, boolean privateLayout, long layoutId,
211                            String portletId, String articleId, boolean purge)
212                    throws PortalException, SystemException {
213    
214                    if (purge) {
215                            journalContentSearchPersistence.removeByG_P_L_P(
216                                    groupId, privateLayout, layoutId, portletId);
217                    }
218    
219                    Group group = groupPersistence.findByPrimaryKey(groupId);
220    
221                    JournalContentSearch contentSearch =
222                            journalContentSearchPersistence.fetchByG_P_L_P_A(
223                                    groupId, privateLayout, layoutId, portletId, articleId);
224    
225                    if (contentSearch == null) {
226                            long contentSearchId = counterLocalService.increment();
227    
228                            contentSearch = journalContentSearchPersistence.create(
229                                    contentSearchId);
230    
231                            contentSearch.setGroupId(groupId);
232                            contentSearch.setCompanyId(group.getCompanyId());
233                            contentSearch.setPrivateLayout(privateLayout);
234                            contentSearch.setLayoutId(layoutId);
235                            contentSearch.setPortletId(portletId);
236                            contentSearch.setArticleId(articleId);
237                    }
238    
239                    journalContentSearchPersistence.update(contentSearch, false);
240    
241                    return contentSearch;
242            }
243    
244            public List<JournalContentSearch> updateContentSearch(
245                            long groupId, boolean privateLayout, long layoutId,
246                            String portletId, String[] articleIds)
247                    throws PortalException, SystemException {
248    
249                    journalContentSearchPersistence.removeByG_P_L_P(
250                            groupId, privateLayout, layoutId, portletId);
251    
252                    List<JournalContentSearch> contentSearches =
253                            new ArrayList<JournalContentSearch>();
254    
255                    for (String articleId : articleIds) {
256                            JournalContentSearch contentSearch = updateContentSearch(
257                                    groupId, privateLayout, layoutId, portletId, articleId, false);
258    
259                            contentSearches.add(contentSearch);
260                    }
261    
262                    return contentSearches;
263            }
264    
265            private static Log _log = LogFactoryUtil.getLog(
266                    JournalContentSearchLocalServiceImpl.class);
267    
268    }