001    /**
002     * Copyright (c) 2000-2010 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                                                    "article-id", 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                    journalContentSearchPersistence.removeByG_P_L_P_A(
110                            groupId, privateLayout, layoutId, portletId, articleId);
111            }
112    
113            public void deleteArticleContentSearches(long groupId, String articleId)
114                    throws SystemException {
115    
116                    journalContentSearchPersistence.removeByG_A(groupId, articleId);
117            }
118    
119            public void deleteLayoutContentSearches(
120                            long groupId, boolean privateLayout, long layoutId)
121                    throws SystemException {
122    
123                    journalContentSearchPersistence.removeByG_P_L(
124                            groupId, privateLayout, layoutId);
125            }
126    
127            public void deleteOwnerContentSearches(long groupId, boolean privateLayout)
128                    throws SystemException {
129    
130                    journalContentSearchPersistence.removeByG_P(groupId, privateLayout);
131            }
132    
133            public List<JournalContentSearch> getArticleContentSearches()
134                    throws SystemException {
135    
136                    return journalContentSearchPersistence.findAll();
137            }
138    
139            public List<JournalContentSearch> getArticleContentSearches(
140                            long groupId, String articleId)
141                    throws SystemException {
142    
143                    return journalContentSearchPersistence.findByG_A(groupId, articleId);
144            }
145    
146            public List<JournalContentSearch> getArticleContentSearches(
147                            String articleId)
148                    throws SystemException {
149    
150                    return journalContentSearchPersistence.findByArticleId(articleId);
151            }
152    
153            public List<Long> getLayoutIds(
154                            long groupId, boolean privateLayout, String articleId)
155                    throws SystemException {
156    
157                    List<Long> layoutIds = new ArrayList<Long>();
158    
159                    List<JournalContentSearch> contentSearches =
160                            journalContentSearchPersistence.findByG_P_A(
161                                    groupId, privateLayout, articleId);
162    
163                    for (JournalContentSearch contentSearch : contentSearches) {
164                            layoutIds.add(contentSearch.getLayoutId());
165                    }
166    
167                    return layoutIds;
168            }
169    
170            public int getLayoutIdsCount(
171                            long groupId, boolean privateLayout, String articleId)
172                    throws SystemException {
173    
174                    return journalContentSearchPersistence.countByG_P_A(
175                            groupId, privateLayout, articleId);
176            }
177    
178            public int getLayoutIdsCount(String articleId) throws SystemException {
179                    return journalContentSearchPersistence.countByArticleId(articleId);
180            }
181    
182            public JournalContentSearch updateContentSearch(
183                            long groupId, boolean privateLayout, long layoutId,
184                            String portletId, String articleId)
185                    throws PortalException, SystemException {
186    
187                    return updateContentSearch(
188                            groupId, privateLayout, layoutId, portletId, articleId, false);
189            }
190    
191            public JournalContentSearch updateContentSearch(
192                            long groupId, boolean privateLayout, long layoutId,
193                            String portletId, String articleId, boolean purge)
194                    throws PortalException, SystemException {
195    
196                    if (purge) {
197                            journalContentSearchPersistence.removeByG_P_L_P(
198                                    groupId, privateLayout, layoutId, portletId);
199                    }
200    
201                    Group group = groupPersistence.findByPrimaryKey(groupId);
202    
203                    JournalContentSearch contentSearch =
204                            journalContentSearchPersistence.fetchByG_P_L_P_A(
205                                    groupId, privateLayout, layoutId, portletId, articleId);
206    
207                    if (contentSearch == null) {
208                            long contentSearchId = counterLocalService.increment();
209    
210                            contentSearch = journalContentSearchPersistence.create(
211                                    contentSearchId);
212    
213                            contentSearch.setGroupId(groupId);
214                            contentSearch.setCompanyId(group.getCompanyId());
215                            contentSearch.setPrivateLayout(privateLayout);
216                            contentSearch.setLayoutId(layoutId);
217                            contentSearch.setPortletId(portletId);
218                            contentSearch.setArticleId(articleId);
219                    }
220    
221                    journalContentSearchPersistence.update(contentSearch, false);
222    
223                    return contentSearch;
224            }
225    
226            public List<JournalContentSearch> updateContentSearch(
227                            long groupId, boolean privateLayout, long layoutId,
228                            String portletId, String[] articleIds)
229                    throws PortalException, SystemException {
230    
231                    journalContentSearchPersistence.removeByG_P_L_P(
232                            groupId, privateLayout, layoutId, portletId);
233    
234                    List<JournalContentSearch> contentSearches =
235                            new ArrayList<JournalContentSearch>();
236    
237                    for (String articleId : articleIds) {
238                            JournalContentSearch contentSearch = updateContentSearch(
239                                    groupId, privateLayout, layoutId, portletId, articleId, false);
240    
241                            contentSearches.add(contentSearch);
242                    }
243    
244                    return contentSearches;
245            }
246    
247            private static Log _log = LogFactoryUtil.getLog(
248                    JournalContentSearchLocalServiceImpl.class);
249    
250    }