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 JournalContentSearch updateContentSearch(
215                            long groupId, boolean privateLayout, long layoutId,
216                            String portletId, String articleId)
217                    throws PortalException, SystemException {
218    
219                    return updateContentSearch(
220                            groupId, privateLayout, layoutId, portletId, articleId, false);
221            }
222    
223            @Override
224            public JournalContentSearch updateContentSearch(
225                            long groupId, boolean privateLayout, long layoutId,
226                            String portletId, String articleId, boolean purge)
227                    throws PortalException, SystemException {
228    
229                    if (purge) {
230                            journalContentSearchPersistence.removeByG_P_L_P(
231                                    groupId, privateLayout, layoutId, portletId);
232                    }
233    
234                    Group group = groupPersistence.findByPrimaryKey(groupId);
235    
236                    JournalContentSearch contentSearch =
237                            journalContentSearchPersistence.fetchByG_P_L_P_A(
238                                    groupId, privateLayout, layoutId, portletId, articleId);
239    
240                    if (contentSearch == null) {
241                            long contentSearchId = counterLocalService.increment();
242    
243                            contentSearch = journalContentSearchPersistence.create(
244                                    contentSearchId);
245    
246                            contentSearch.setGroupId(groupId);
247                            contentSearch.setCompanyId(group.getCompanyId());
248                            contentSearch.setPrivateLayout(privateLayout);
249                            contentSearch.setLayoutId(layoutId);
250                            contentSearch.setPortletId(portletId);
251                            contentSearch.setArticleId(articleId);
252                    }
253    
254                    journalContentSearchPersistence.update(contentSearch, false);
255    
256                    return contentSearch;
257            }
258    
259            @Override
260            public List<JournalContentSearch> updateContentSearch(
261                            long groupId, boolean privateLayout, long layoutId,
262                            String portletId, String[] articleIds)
263                    throws PortalException, SystemException {
264    
265                    journalContentSearchPersistence.removeByG_P_L_P(
266                            groupId, privateLayout, layoutId, portletId);
267    
268                    List<JournalContentSearch> contentSearches =
269                            new ArrayList<JournalContentSearch>();
270    
271                    for (String articleId : articleIds) {
272                            JournalContentSearch contentSearch = updateContentSearch(
273                                    groupId, privateLayout, layoutId, portletId, articleId, false);
274    
275                            contentSearches.add(contentSearch);
276                    }
277    
278                    return contentSearches;
279            }
280    
281            private static Log _log = LogFactoryUtil.getLog(
282                    JournalContentSearchLocalServiceImpl.class);
283    
284    }