1
22
23 package com.liferay.portlet.tags.service.impl;
24
25 import com.liferay.counter.service.CounterLocalServiceUtil;
26 import com.liferay.portal.PortalException;
27 import com.liferay.portal.SystemException;
28 import com.liferay.portal.kernel.search.Document;
29 import com.liferay.portal.kernel.search.Hits;
30 import com.liferay.portal.kernel.util.GetterUtil;
31 import com.liferay.portal.kernel.util.InstancePool;
32 import com.liferay.portal.kernel.util.StringPool;
33 import com.liferay.portal.kernel.util.StringUtil;
34 import com.liferay.portal.kernel.util.Validator;
35 import com.liferay.portal.lucene.LuceneFields;
36 import com.liferay.portal.lucene.LuceneUtil;
37 import com.liferay.portal.model.User;
38 import com.liferay.portal.service.persistence.UserUtil;
39 import com.liferay.portal.util.PortalInstances;
40 import com.liferay.portal.util.PortalUtil;
41 import com.liferay.portal.util.PortletKeys;
42 import com.liferay.portal.util.PropsUtil;
43 import com.liferay.portlet.blogs.model.BlogsEntry;
44 import com.liferay.portlet.documentlibrary.model.DLFileEntry;
45 import com.liferay.portlet.documentlibrary.service.DLFileEntryLocalServiceUtil;
46 import com.liferay.portlet.journal.model.JournalArticle;
47 import com.liferay.portlet.journal.service.JournalArticleResourceLocalServiceUtil;
48 import com.liferay.portlet.messageboards.model.MBMessage;
49 import com.liferay.portlet.tags.model.TagsAsset;
50 import com.liferay.portlet.tags.model.TagsAssetDisplay;
51 import com.liferay.portlet.tags.model.TagsAssetType;
52 import com.liferay.portlet.tags.model.TagsEntry;
53 import com.liferay.portlet.tags.service.TagsEntryLocalServiceUtil;
54 import com.liferay.portlet.tags.service.base.TagsAssetLocalServiceBaseImpl;
55 import com.liferay.portlet.tags.service.persistence.TagsAssetFinder;
56 import com.liferay.portlet.tags.service.persistence.TagsAssetUtil;
57 import com.liferay.portlet.tags.service.persistence.TagsEntryUtil;
58 import com.liferay.portlet.tags.util.TagsAssetValidator;
59 import com.liferay.portlet.tags.util.TagsUtil;
60 import com.liferay.portlet.wiki.model.WikiPage;
61 import com.liferay.portlet.wiki.service.WikiPageResourceLocalServiceUtil;
62 import com.liferay.util.ListUtil;
63 import com.liferay.util.lucene.HitsImpl;
64
65 import java.util.ArrayList;
66 import java.util.Date;
67 import java.util.List;
68
69 import org.apache.commons.logging.Log;
70 import org.apache.commons.logging.LogFactory;
71 import org.apache.lucene.index.Term;
72 import org.apache.lucene.search.BooleanClause;
73 import org.apache.lucene.search.BooleanQuery;
74 import org.apache.lucene.search.Searcher;
75 import org.apache.lucene.search.TermQuery;
76
77
83 public class TagsAssetLocalServiceImpl extends TagsAssetLocalServiceBaseImpl {
84
85 public void deleteAsset(long assetId)
86 throws PortalException, SystemException {
87
88 TagsAsset asset = TagsAssetUtil.findByPrimaryKey(assetId);
89
90 deleteAsset(asset);
91 }
92
93 public void deleteAsset(String className, long classPK)
94 throws PortalException, SystemException {
95
96 long classNameId = PortalUtil.getClassNameId(className);
97
98 TagsAsset asset = TagsAssetUtil.fetchByC_C(classNameId, classPK);
99
100 if (asset != null) {
101 deleteAsset(asset);
102 }
103 }
104
105 public void deleteAsset(TagsAsset asset)
106 throws PortalException, SystemException {
107
108 TagsAssetUtil.remove(asset.getAssetId());
109 }
110
111 public TagsAsset getAsset(long assetId)
112 throws PortalException, SystemException {
113
114 return TagsAssetUtil.findByPrimaryKey(assetId);
115 }
116
117 public TagsAsset getAsset(String className, long classPK)
118 throws PortalException, SystemException {
119
120 long classNameId = PortalUtil.getClassNameId(className);
121
122 return TagsAssetUtil.findByC_C(classNameId, classPK);
123 }
124
125 public TagsAssetType[] getAssetTypes(String languageId) {
126 TagsAssetType[] assetTypes =
127 new TagsAssetType[TagsUtil.ASSET_TYPE_CLASS_NAMES.length];
128
129 for (int i = 0; i < TagsUtil.ASSET_TYPE_CLASS_NAMES.length; i++) {
130 assetTypes[i] = getAssetType(
131 TagsUtil.ASSET_TYPE_CLASS_NAMES[i], languageId);
132 }
133
134 return assetTypes;
135 }
136
137 public List getAssets(
138 long[] entryIds, long[] notEntryIds, boolean andOperator, int begin,
139 int end)
140 throws SystemException {
141
142 return getAssets(
143 entryIds, notEntryIds, andOperator, null, null, begin, end);
144 }
145
146 public List getAssets(
147 long[] entryIds, long[] notEntryIds, boolean andOperator,
148 Date publishDate, Date expirationDate, int begin, int end)
149 throws SystemException {
150
151 if (andOperator) {
152 return TagsAssetFinder.findByAndEntryIds(
153 entryIds, notEntryIds, publishDate, expirationDate, begin, end);
154 }
155 else {
156 return TagsAssetFinder.findByOrEntryIds(
157 entryIds, notEntryIds, publishDate, expirationDate, begin, end);
158 }
159 }
160
161 public int getAssetsCount(
162 long[] entryIds, long[] notEntryIds, boolean andOperator)
163 throws SystemException {
164
165 return getAssetsCount(entryIds, notEntryIds, andOperator, null, null);
166 }
167
168 public int getAssetsCount(
169 long[] entryIds, long[] notEntryIds, boolean andOperator,
170 Date publishDate, Date expirationDate)
171 throws SystemException {
172
173 if (andOperator) {
174 return TagsAssetFinder.countByAndEntryIds(
175 entryIds, notEntryIds, publishDate, expirationDate);
176 }
177 else {
178 return TagsAssetFinder.countByOrEntryIds(
179 entryIds, notEntryIds, publishDate, expirationDate);
180 }
181 }
182
183 public TagsAssetDisplay[] getCompanyAssetDisplays(
184 long companyId, int begin, int end, String languageId)
185 throws PortalException, SystemException {
186
187 return getAssetDisplays(
188 getCompanyAssets(companyId, begin, end), languageId);
189 }
190
191 public List getCompanyAssets(long companyId, int begin, int end)
192 throws SystemException {
193
194 return TagsAssetUtil.findByCompanyId(companyId, begin, end);
195 }
196
197 public int getCompanyAssetsCount(long companyId) throws SystemException {
198 return TagsAssetUtil.countByCompanyId(companyId);
199 }
200
201 public Hits search(long companyId, String portletId, String keywords)
202 throws SystemException {
203
204 Searcher searcher = null;
205
206 try {
207 HitsImpl hits = new HitsImpl();
208
209 BooleanQuery contextQuery = new BooleanQuery();
210
211 if (Validator.isNotNull(portletId)) {
212 LuceneUtil.addRequiredTerm(
213 contextQuery, LuceneFields.PORTLET_ID, portletId);
214 }
215 else {
216 BooleanQuery portletIdsQuery = new BooleanQuery();
217
218 for (int i = 0; i < TagsUtil.ASSET_TYPE_PORTLET_IDS.length;
219 i++) {
220
221 Term term = new Term(
222 LuceneFields.PORTLET_ID,
223 TagsUtil.ASSET_TYPE_PORTLET_IDS[i]);
224 TermQuery termQuery = new TermQuery(term);
225
226 portletIdsQuery.add(termQuery, BooleanClause.Occur.SHOULD);
227 }
228
229 contextQuery.add(portletIdsQuery, BooleanClause.Occur.MUST);
230 }
231
232 BooleanQuery searchQuery = new BooleanQuery();
233
234 if (Validator.isNotNull(keywords)) {
235 LuceneUtil.addTerm(searchQuery, LuceneFields.CONTENT, keywords);
236 }
237
238 BooleanQuery fullQuery = new BooleanQuery();
239
240 fullQuery.add(contextQuery, BooleanClause.Occur.MUST);
241
242 if (searchQuery.clauses().size() > 0) {
243 fullQuery.add(searchQuery, BooleanClause.Occur.MUST);
244 }
245
246 searcher = LuceneUtil.getSearcher(companyId);
247
248 hits.recordHits(searcher.search(fullQuery), searcher);
249
250 return hits;
251 }
252 catch (Exception e) {
253 return LuceneUtil.closeSearcher(searcher, keywords, e);
254 }
255 }
256
257 public TagsAssetDisplay[] searchAssetDisplays(
258 long companyId, String portletId, String keywords,
259 String languageId, int begin, int end)
260 throws PortalException, SystemException {
261
262 List assets = new ArrayList();
263
264 Hits hits = search(companyId, portletId, keywords);
265
266 hits = hits.subset(begin, end);
267
268 List hitsList = hits.toList();
269
270 for (int i = 0; i < hitsList.size(); i++) {
271 Document doc = (Document)hitsList.get(i);
272
273 try {
274 TagsAsset asset = getAsset(doc);
275
276 if (asset != null) {
277 assets.add(asset);
278 }
279 }
280 catch (Exception e) {
281 if (_log.isWarnEnabled()) {
282 _log.warn(e);
283 }
284 }
285 }
286
287 return getAssetDisplays(assets, languageId);
288 }
289
290 public int searchAssetDisplaysCount(
291 long companyId, String portletId, String keywords,
292 String languageId)
293 throws SystemException {
294
295 Hits hits = search(companyId, portletId, keywords);
296
297 return hits.getLength();
298 }
299
300 public TagsAsset updateAsset(
301 long userId, String className, long classPK, String[] entryNames)
302 throws PortalException, SystemException {
303
304 return updateAsset(
305 userId, className, classPK, entryNames, null, null, null, null,
306 null, null, null, null, null, 0, 0);
307 }
308
309 public TagsAsset updateAsset(
310 long userId, String className, long classPK, String[] entryNames,
311 Date startDate, Date endDate, Date publishDate, Date expirationDate,
312 String mimeType, String title, String description, String summary,
313 String url, int height, int width)
314 throws PortalException, SystemException {
315
316
318 User user = UserUtil.findByPrimaryKey(userId);
319 long classNameId = PortalUtil.getClassNameId(className);
320
321 if (entryNames == null) {
322 entryNames = new String[0];
323 }
324
325 title = StringUtil.shorten(title, 300, StringPool.BLANK);
326 Date now = new Date();
327
328 validate(className, entryNames);
329
330 TagsAsset asset = TagsAssetUtil.fetchByC_C(classNameId, classPK);
331
332 if (asset == null) {
333 long assetId = CounterLocalServiceUtil.increment();
334
335 asset = TagsAssetUtil.create(assetId);
336
337 asset.setCompanyId(user.getCompanyId());
338 asset.setUserId(user.getUserId());
339 asset.setUserName(user.getFullName());
340 asset.setCreateDate(now);
341 asset.setClassNameId(classNameId);
342 asset.setClassPK(classPK);
343 asset.setPublishDate(publishDate);
344 asset.setExpirationDate(expirationDate);
345 }
346
347 asset.setModifiedDate(now);
348 asset.setStartDate(startDate);
349 asset.setEndDate(endDate);
350 asset.setPublishDate(publishDate);
351 asset.setExpirationDate(expirationDate);
352 asset.setMimeType(mimeType);
353 asset.setTitle(title);
354 asset.setDescription(description);
355 asset.setSummary(summary);
356 asset.setUrl(url);
357 asset.setHeight(height);
358 asset.setWidth(width);
359
360 TagsAssetUtil.update(asset);
361
362
364 List entries = new ArrayList(entryNames.length);
365
366 for (int i = 0; i < entryNames.length; i++) {
367 String name = entryNames[i].trim().toLowerCase();
368
369 TagsEntry entry = TagsEntryUtil.fetchByC_N(
370 user.getCompanyId(), name);
371
372 if (entry == null) {
373 String defaultProperties = "0:category:no category";
374
375 TagsEntry newTagsEntry = TagsEntryLocalServiceUtil.addEntry(
376 user.getUserId(), entryNames[i],
377 new String[] {defaultProperties});
378
379 entries.add(newTagsEntry);
380 }
381 else {
382 entries.add(entry);
383 }
384 }
385
386 TagsAssetUtil.setTagsEntries(asset.getAssetId(), entries);
387
388 return asset;
389 }
390
391 public void validate(String className, String[] entryNames)
392 throws PortalException {
393
394 TagsAssetValidator validator = (TagsAssetValidator)InstancePool.get(
395 PropsUtil.get(PropsUtil.TAGS_ASSET_VALIDATOR));
396
397 validator.validate(className, entryNames);
398 }
399
400 protected TagsAsset getAsset(Document doc)
401 throws PortalException, SystemException {
402
403 String portletId = GetterUtil.getString(doc.get(LuceneFields.PORTLET_ID));
404
405 if (portletId.equals(PortletKeys.BLOGS)) {
406 long entryId = GetterUtil.getLong(doc.get("entryId"));
407
408 long classNameId = PortalUtil.getClassNameId(
409 BlogsEntry.class.getName());
410 long classPK = entryId;
411
412 return TagsAssetUtil.findByC_C(classNameId, classPK);
413 }
414 else if (portletId.equals(PortletKeys.BOOKMARKS)) {
415 }
416 else if (portletId.equals(PortletKeys.DOCUMENT_LIBRARY)) {
417 long folderId = GetterUtil.getLong(doc.get("repositoryId"));
418 String name = doc.get("path");
419
420 DLFileEntry fileEntry = DLFileEntryLocalServiceUtil.getFileEntry(
421 folderId, name);
422
423 long classNameId = PortalUtil.getClassNameId(
424 DLFileEntry.class.getName());
425 long classPK = fileEntry.getFileEntryId();
426
427 return TagsAssetUtil.findByC_C(classNameId, classPK);
428 }
429 else if (portletId.equals(PortletKeys.IMAGE_GALLERY)) {
430 }
431 else if (portletId.equals(PortletKeys.JOURNAL)) {
432 long groupId = GetterUtil.getLong(
433 doc.get(LuceneFields.GROUP_ID));
434 String articleId = doc.get("articleId");
435 double version = GetterUtil.getDouble(doc.get("version"));
436
437 long articleResourcePrimKey =
438 JournalArticleResourceLocalServiceUtil.
439 getArticleResourcePrimKey(groupId, articleId);
440
441 long classNameId = PortalUtil.getClassNameId(
442 JournalArticle.class.getName());
443 long classPK = articleResourcePrimKey;
444
445 return TagsAssetUtil.findByC_C(classNameId, classPK);
446 }
447 else if (portletId.equals(PortletKeys.MESSAGE_BOARDS)) {
448 long messageId = GetterUtil.getLong(doc.get("messageId"));
449
450 long classNameId = PortalUtil.getClassNameId(
451 MBMessage.class.getName());
452 long classPK = messageId;
453
454 return TagsAssetUtil.findByC_C(classNameId, classPK);
455 }
456 else if (portletId.equals(PortletKeys.WIKI)) {
457 long nodeId = GetterUtil.getLong(doc.get("nodeId"));
458 String title = doc.get(LuceneFields.TITLE);
459
460 long pageResourcePrimKey =
461 WikiPageResourceLocalServiceUtil.getPageResourcePrimKey(
462 nodeId, title);
463
464 long classNameId = PortalUtil.getClassNameId(
465 WikiPage.class.getName());
466 long classPK = pageResourcePrimKey;
467
468 return TagsAssetUtil.findByC_C(classNameId, classPK);
469 }
470
471 return null;
472 }
473
474 protected TagsAssetDisplay[] getAssetDisplays(
475 List assets, String languageId)
476 throws PortalException, SystemException {
477
478 TagsAssetDisplay[] assetDisplays = new TagsAssetDisplay[assets.size()];
479
480 for (int i = 0; i < assets.size(); i++) {
481 TagsAsset asset = (TagsAsset)assets.get(i);
482
483 String className = PortalUtil.getClassName(asset.getClassNameId());
484 String portletId = PortalUtil.getClassNamePortletId(className);
485 String portletTitle = PortalUtil.getPortletTitle(
486 portletId, asset.getCompanyId(), languageId);
487
488 List tagsEntriesList = TagsAssetUtil.getTagsEntries(
489 asset.getAssetId());
490
491 String tagsEntries = ListUtil.toString(
492 tagsEntriesList, "name", ", ");
493
494 TagsAssetDisplay assetDisplay = new TagsAssetDisplay();
495
496 assetDisplay.setAssetId(asset.getAssetId());
497 assetDisplay.setCompanyId(asset.getCompanyId());
498 assetDisplay.setUserId(asset.getUserId());
499 assetDisplay.setUserName(asset.getUserName());
500 assetDisplay.setCreateDate(asset.getCreateDate());
501 assetDisplay.setModifiedDate(asset.getModifiedDate());
502 assetDisplay.setClassNameId(asset.getClassNameId());
503 assetDisplay.setClassName(className);
504 assetDisplay.setClassPK(asset.getClassPK());
505 assetDisplay.setPortletId(portletId);
506 assetDisplay.setPortletTitle(portletTitle);
507 assetDisplay.setStartDate(asset.getStartDate());
508 assetDisplay.setEndDate(asset.getEndDate());
509 assetDisplay.setPublishDate(asset.getPublishDate());
510 assetDisplay.setExpirationDate(asset.getExpirationDate());
511 assetDisplay.setMimeType(asset.getMimeType());
512 assetDisplay.setTitle(asset.getTitle());
513 assetDisplay.setDescription(asset.getDescription());
514 assetDisplay.setSummary(asset.getSummary());
515 assetDisplay.setUrl(asset.getUrl());
516 assetDisplay.setHeight(asset.getHeight());
517 assetDisplay.setWidth(asset.getWidth());
518 assetDisplay.setTagsEntries(tagsEntries);
519
520 assetDisplays[i] = assetDisplay;
521 }
522
523 return assetDisplays;
524 }
525
526 protected TagsAssetType getAssetType(String className, String languageId) {
527 long companyId = PortalInstances.getDefaultCompanyId();
528
529 long classNameId = PortalUtil.getClassNameId(className);
530
531 String portletId = PortalUtil.getClassNamePortletId(className);
532 String portletTitle = PortalUtil.getPortletTitle(
533 portletId, companyId, languageId);
534
535 TagsAssetType assetType = new TagsAssetType();
536
537 assetType.setClassNameId(classNameId);
538 assetType.setClassName(className);
539 assetType.setPortletId(portletId);
540 assetType.setPortletTitle(portletTitle);
541
542 return assetType;
543 }
544
545 private static Log _log =
546 LogFactory.getLog(TagsAssetLocalServiceImpl.class);
547
548 }