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