001
014
015 package com.liferay.portlet.asset.service.impl;
016
017 import com.liferay.portal.kernel.cache.ThreadLocalCachable;
018 import com.liferay.portal.kernel.dao.orm.QueryUtil;
019 import com.liferay.portal.kernel.exception.PortalException;
020 import com.liferay.portal.kernel.exception.SystemException;
021 import com.liferay.portal.kernel.util.ArrayUtil;
022 import com.liferay.portal.kernel.util.CharPool;
023 import com.liferay.portal.kernel.util.GetterUtil;
024 import com.liferay.portal.kernel.util.ListUtil;
025 import com.liferay.portal.kernel.util.StringPool;
026 import com.liferay.portal.kernel.util.StringUtil;
027 import com.liferay.portal.kernel.util.Validator;
028 import com.liferay.portal.model.ResourceConstants;
029 import com.liferay.portal.model.User;
030 import com.liferay.portal.service.ServiceContext;
031 import com.liferay.portal.util.PortalUtil;
032 import com.liferay.portal.util.PropsValues;
033 import com.liferay.portlet.asset.AssetTagException;
034 import com.liferay.portlet.asset.DuplicateTagException;
035 import com.liferay.portlet.asset.NoSuchTagException;
036 import com.liferay.portlet.asset.model.AssetEntry;
037 import com.liferay.portlet.asset.model.AssetTag;
038 import com.liferay.portlet.asset.model.AssetTagProperty;
039 import com.liferay.portlet.asset.service.base.AssetTagLocalServiceBaseImpl;
040 import com.liferay.portlet.asset.util.AssetUtil;
041 import com.liferay.portlet.social.util.SocialCounterPeriodUtil;
042
043 import java.util.ArrayList;
044 import java.util.Date;
045 import java.util.List;
046
047
053 public class AssetTagLocalServiceImpl extends AssetTagLocalServiceBaseImpl {
054
055 public AssetTag addTag(
056 long userId, String name, String[] tagProperties,
057 ServiceContext serviceContext)
058 throws PortalException, SystemException {
059
060
061
062 User user = userPersistence.findByPrimaryKey(userId);
063 long groupId = serviceContext.getScopeGroupId();
064
065 if (tagProperties == null) {
066 tagProperties = new String[0];
067 }
068
069 Date now = new Date();
070
071 long tagId = counterLocalService.increment();
072
073 AssetTag tag = assetTagPersistence.create(tagId);
074
075 tag.setGroupId(groupId);
076 tag.setCompanyId(user.getCompanyId());
077 tag.setUserId(user.getUserId());
078 tag.setUserName(user.getFullName());
079 tag.setCreateDate(now);
080 tag.setModifiedDate(now);
081
082 name = name.trim();
083 name = name.toLowerCase();
084
085 if (hasTag(groupId, name)) {
086 throw new DuplicateTagException(
087 "A tag with the name " + name + " already exists");
088 }
089
090 validate(name);
091
092 tag.setName(name);
093
094 assetTagPersistence.update(tag);
095
096
097
098 if (serviceContext.isAddGroupPermissions() ||
099 serviceContext.isAddGuestPermissions()) {
100
101 addTagResources(
102 tag, serviceContext.isAddGroupPermissions(),
103 serviceContext.isAddGuestPermissions());
104 }
105 else {
106 addTagResources(
107 tag, serviceContext.getGroupPermissions(),
108 serviceContext.getGuestPermissions());
109 }
110
111
112
113 for (int i = 0; i < tagProperties.length; i++) {
114 String[] tagProperty = StringUtil.split(
115 tagProperties[i], CharPool.COLON);
116
117 String key = StringPool.BLANK;
118
119 if (tagProperty.length > 0) {
120 key = GetterUtil.getString(tagProperty[0]);
121 }
122
123 String value = StringPool.BLANK;
124
125 if (tagProperty.length > 1) {
126 value = GetterUtil.getString(tagProperty[1]);
127 }
128
129 if (Validator.isNotNull(key)) {
130 assetTagPropertyLocalService.addTagProperty(
131 userId, tagId, key, value);
132 }
133 }
134
135 return tag;
136 }
137
138 public void addTagResources(
139 AssetTag tag, boolean addGroupPermissions,
140 boolean addGuestPermissions)
141 throws PortalException, SystemException {
142
143 resourceLocalService.addResources(
144 tag.getCompanyId(), tag.getGroupId(), tag.getUserId(),
145 AssetTag.class.getName(), tag.getTagId(), false,
146 addGroupPermissions, addGuestPermissions);
147 }
148
149 public void addTagResources(
150 AssetTag tag, String[] groupPermissions, String[] guestPermissions)
151 throws PortalException, SystemException {
152
153 resourceLocalService.addModelResources(
154 tag.getCompanyId(), tag.getGroupId(), tag.getUserId(),
155 AssetTag.class.getName(), tag.getTagId(), groupPermissions,
156 guestPermissions);
157 }
158
159 public void checkTags(long userId, long groupId, String[] names)
160 throws PortalException, SystemException {
161
162 for (String name : names) {
163 try {
164 getTag(groupId, name);
165 }
166 catch (NoSuchTagException nste) {
167 ServiceContext serviceContext = new ServiceContext();
168
169 serviceContext.setAddGroupPermissions(true);
170 serviceContext.setAddGuestPermissions(true);
171 serviceContext.setScopeGroupId(groupId);
172
173 addTag(
174 userId, name, PropsValues.ASSET_TAG_PROPERTIES_DEFAULT,
175 serviceContext);
176 }
177 }
178 }
179
180 public AssetTag decrementAssetCount(long tagId, long classNameId)
181 throws PortalException, SystemException {
182
183 AssetTag tag = assetTagPersistence.findByPrimaryKey(tagId);
184
185 tag.setAssetCount(Math.max(0, tag.getAssetCount() - 1));
186
187 assetTagPersistence.update(tag);
188
189 assetTagStatsLocalService.updateTagStats(tagId, classNameId);
190
191 return tag;
192 }
193
194 public void deleteTag(AssetTag tag)
195 throws PortalException, SystemException {
196
197
198
199 List<AssetEntry> entries = assetTagPersistence.getAssetEntries(
200 tag.getTagId());
201
202
203
204 assetTagPersistence.remove(tag);
205
206
207
208 resourceLocalService.deleteResource(
209 tag.getCompanyId(), AssetTag.class.getName(),
210 ResourceConstants.SCOPE_INDIVIDUAL, tag.getTagId());
211
212
213
214 assetTagPropertyLocalService.deleteTagProperties(tag.getTagId());
215
216
217
218 assetEntryLocalService.reindex(entries);
219 }
220
221 public void deleteTag(long tagId) throws PortalException, SystemException {
222 AssetTag tag = assetTagPersistence.findByPrimaryKey(tagId);
223
224 deleteTag(tag);
225 }
226
227 public List<AssetTag> getEntryTags(long entryId) throws SystemException {
228 return assetTagFinder.findByEntryId(entryId);
229 }
230
231 public List<AssetTag> getGroupsTags(long[] groupIds)
232 throws SystemException {
233
234 List<AssetTag> groupsTags = new ArrayList<AssetTag>();
235
236 for (long groupId : groupIds) {
237 List<AssetTag> groupTags = getGroupTags(groupId);
238
239 groupsTags.addAll(groupTags);
240 }
241
242 return groupsTags;
243 }
244
245 public List<AssetTag> getGroupTags(long groupId) throws SystemException {
246 return assetTagPersistence.findByGroupId(groupId);
247 }
248
249 public List<AssetTag> getGroupTags(long groupId, int start, int end)
250 throws SystemException {
251
252 return assetTagPersistence.findByGroupId(groupId, start, end);
253 }
254
255 public int getGroupTagsCount(long groupId) throws SystemException {
256 return assetTagPersistence.countByGroupId(groupId);
257 }
258
259 public List<AssetTag> getSocialActivityCounterOffsetTags(
260 long groupId, String socialActivityCounterName, int startOffset,
261 int endOffset)
262 throws SystemException {
263
264 int startPeriod = SocialCounterPeriodUtil.getStartPeriod(startOffset);
265 int endPeriod = SocialCounterPeriodUtil.getEndPeriod(endOffset);
266
267 return getSocialActivityCounterPeriodTags(
268 groupId, socialActivityCounterName, startPeriod, endPeriod);
269 }
270
271 public List<AssetTag> getSocialActivityCounterPeriodTags(
272 long groupId, String socialActivityCounterName, int startPeriod,
273 int endPeriod)
274 throws SystemException {
275
276 int offset = SocialCounterPeriodUtil.getOffset(endPeriod);
277
278 int periodLength = SocialCounterPeriodUtil.getPeriodLength(offset);
279
280 return assetTagFinder.findByG_N_S_E(
281 groupId, socialActivityCounterName, startPeriod, endPeriod,
282 periodLength);
283 }
284
285 public AssetTag getTag(long tagId) throws PortalException, SystemException {
286 return assetTagPersistence.findByPrimaryKey(tagId);
287 }
288
289 public AssetTag getTag(long groupId, String name)
290 throws PortalException, SystemException {
291
292 return assetTagFinder.findByG_N(groupId, name);
293 }
294
295 public long[] getTagIds(long groupId, String[] names)
296 throws PortalException, SystemException {
297
298 List<Long> tagIds = new ArrayList<Long>(names.length);
299
300 for (String name : names) {
301 try {
302 AssetTag tag = getTag(groupId, name);
303
304 tagIds.add(tag.getTagId());
305 }
306 catch (NoSuchTagException nste) {
307 }
308 }
309
310 return ArrayUtil.toArray(tagIds.toArray(new Long[tagIds.size()]));
311 }
312
313 public long[] getTagIds(long[] groupIds, String name)
314 throws PortalException, SystemException {
315
316 List<Long> tagIds = new ArrayList<Long>(groupIds.length);
317
318 for (long groupId : groupIds) {
319 try {
320 AssetTag tag = getTag(groupId, name);
321
322 tagIds.add(tag.getTagId());
323 }
324 catch (NoSuchTagException nste) {
325 }
326 }
327
328 return ArrayUtil.toArray(tagIds.toArray(new Long[tagIds.size()]));
329 }
330
331 public long[] getTagIds(long[] groupIds, String[] names)
332 throws PortalException, SystemException {
333
334 long[] tagsIds = new long[0];
335
336 for (long groupId : groupIds) {
337 tagsIds = ArrayUtil.append(tagsIds, getTagIds(groupId, names));
338 }
339
340 return tagsIds;
341 }
342
343 public String[] getTagNames() throws SystemException {
344 return getTagNames(getTags());
345 }
346
347 public String[] getTagNames(long classNameId, long classPK)
348 throws SystemException {
349
350 return getTagNames(getTags(classNameId, classPK));
351 }
352
353 public String[] getTagNames(String className, long classPK)
354 throws SystemException {
355
356 return getTagNames(getTags(className, classPK));
357 }
358
359 public List<AssetTag> getTags() throws SystemException {
360 return assetTagPersistence.findAll();
361 }
362
363 public List<AssetTag> getTags(long classNameId, long classPK)
364 throws SystemException {
365
366 return assetTagFinder.findByC_C(classNameId, classPK);
367 }
368
369 public List<AssetTag> getTags(long groupId, long classNameId, String name)
370 throws SystemException {
371
372 return assetTagFinder.findByG_C_N(
373 groupId, classNameId, name, QueryUtil.ALL_POS, QueryUtil.ALL_POS,
374 null);
375 }
376
377 public List<AssetTag> getTags(
378 long groupId, long classNameId, String name, int start, int end)
379 throws SystemException {
380
381 return assetTagFinder.findByG_C_N(
382 groupId, classNameId, name, start, end, null);
383 }
384
385 @ThreadLocalCachable
386 public List<AssetTag> getTags(String className, long classPK)
387 throws SystemException {
388
389 long classNameId = PortalUtil.getClassNameId(className);
390
391 return getTags(classNameId, classPK);
392 }
393
394 public int getTagsSize(long groupId, long classNameId, String name)
395 throws SystemException {
396
397 return assetTagFinder.countByG_C_N(groupId, classNameId, name);
398 }
399
400 public boolean hasTag(long groupId, String name)
401 throws PortalException, SystemException {
402
403 try {
404 getTag(groupId, name);
405
406 return true;
407 }
408 catch (NoSuchTagException nste) {
409 return false;
410 }
411 }
412
413 public AssetTag incrementAssetCount(long tagId, long classNameId)
414 throws PortalException, SystemException {
415
416 AssetTag tag = assetTagPersistence.findByPrimaryKey(tagId);
417
418 tag.setAssetCount(tag.getAssetCount() + 1);
419
420 assetTagPersistence.update(tag);
421
422 assetTagStatsLocalService.updateTagStats(tagId, classNameId);
423
424 return tag;
425 }
426
427 public void mergeTags(
428 long fromTagId, long toTagId, boolean overrideProperties)
429 throws PortalException, SystemException {
430
431 List<AssetEntry> entries = assetTagPersistence.getAssetEntries(
432 fromTagId);
433
434 assetTagPersistence.addAssetEntries(toTagId, entries);
435
436 List<AssetTagProperty> tagProperties =
437 assetTagPropertyPersistence.findByTagId(fromTagId);
438
439 for (AssetTagProperty fromTagProperty : tagProperties) {
440 AssetTagProperty toTagProperty =
441 assetTagPropertyPersistence.fetchByT_K(
442 toTagId, fromTagProperty.getKey());
443
444 if (overrideProperties && (toTagProperty != null)) {
445 toTagProperty.setValue(fromTagProperty.getValue());
446
447 assetTagPropertyPersistence.update(toTagProperty);
448 }
449 else if (toTagProperty == null) {
450 fromTagProperty.setTagId(toTagId);
451
452 assetTagPropertyPersistence.update(fromTagProperty);
453 }
454 }
455
456 deleteTag(fromTagId);
457 }
458
459 public List<AssetTag> search(
460 long groupId, String name, String[] tagProperties, int start,
461 int end)
462 throws SystemException {
463
464 return search(new long[] {groupId}, name, tagProperties, start, end);
465 }
466
467 public List<AssetTag> search(
468 long[] groupIds, String name, String[] tagProperties, int start,
469 int end)
470 throws SystemException {
471
472 return assetTagFinder.findByG_N_P(
473 groupIds, name, tagProperties, start, end, null);
474 }
475
476 public AssetTag updateTag(
477 long userId, long tagId, String name, String[] tagProperties,
478 ServiceContext serviceContext)
479 throws PortalException, SystemException {
480
481
482
483 AssetTag tag = assetTagPersistence.findByPrimaryKey(tagId);
484
485 String oldName = tag.getName();
486
487 tag.setModifiedDate(new Date());
488
489 name = name.trim();
490 name = name.toLowerCase();
491
492 if (tagProperties == null) {
493 tagProperties = new String[0];
494 }
495
496 if (!name.equals(tag.getName()) && hasTag(tag.getGroupId(), name)) {
497 throw new DuplicateTagException(
498 "A tag with the name " + name + " already exists");
499 }
500
501 if (!tag.getName().equals(name)) {
502 try {
503 AssetTag existingAssetTag = getTag(tag.getGroupId(), name);
504
505 if (existingAssetTag.getTagId() != tagId) {
506 throw new DuplicateTagException(
507 "A tag with the name " + name + " already exists");
508 }
509 }
510 catch (NoSuchTagException nste) {
511 }
512 }
513
514 validate(name);
515
516 tag.setName(name);
517
518 assetTagPersistence.update(tag);
519
520
521
522 List<AssetTagProperty> oldTagProperties =
523 assetTagPropertyPersistence.findByTagId(tagId);
524
525 for (AssetTagProperty tagProperty : oldTagProperties) {
526 assetTagPropertyLocalService.deleteTagProperty(tagProperty);
527 }
528
529 for (int i = 0; i < tagProperties.length; i++) {
530 String[] tagProperty = StringUtil.split(
531 tagProperties[i], CharPool.COLON);
532
533 String key = StringPool.BLANK;
534
535 if (tagProperty.length > 0) {
536 key = GetterUtil.getString(tagProperty[0]);
537 }
538
539 String value = StringPool.BLANK;
540
541 if (tagProperty.length > 1) {
542 value = GetterUtil.getString(tagProperty[1]);
543 }
544
545 if (Validator.isNotNull(key)) {
546 assetTagPropertyLocalService.addTagProperty(
547 userId, tagId, key, value);
548 }
549 }
550
551
552
553 if (!oldName.equals(name)) {
554 List<AssetEntry> entries = assetTagPersistence.getAssetEntries(
555 tag.getTagId());
556
557 assetEntryLocalService.reindex(entries);
558 }
559
560 return tag;
561 }
562
563 protected String[] getTagNames(List<AssetTag>tags) {
564 return StringUtil.split(
565 ListUtil.toString(tags, AssetTag.NAME_ACCESSOR));
566 }
567
568 protected void validate(String name) throws PortalException {
569 if (!AssetUtil.isValidWord(name)) {
570 throw new AssetTagException(
571 StringUtil.merge(
572 AssetUtil.INVALID_CHARACTERS, StringPool.SPACE),
573 AssetTagException.INVALID_CHARACTER);
574 }
575 }
576
577 }