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, false);
095
096
097
098 if (serviceContext.getAddGroupPermissions() ||
099 serviceContext.getAddGuestPermissions()) {
100
101 addTagResources(
102 tag, serviceContext.getAddGroupPermissions(),
103 serviceContext.getAddGuestPermissions());
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, false);
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 AssetTag getTag(long tagId) throws PortalException, SystemException {
260 return assetTagPersistence.findByPrimaryKey(tagId);
261 }
262
263 public AssetTag getTag(long groupId, String name)
264 throws PortalException, SystemException {
265
266 return assetTagFinder.findByG_N(groupId, name);
267 }
268
269 public long[] getTagIds(long groupId, String[] names)
270 throws PortalException, SystemException {
271
272 List<Long> tagIds = new ArrayList<Long>(names.length);
273
274 for (String name : names) {
275 try {
276 AssetTag tag = getTag(groupId, name);
277
278 tagIds.add(tag.getTagId());
279 }
280 catch (NoSuchTagException nste) {
281 }
282 }
283
284 return ArrayUtil.toArray(tagIds.toArray(new Long[tagIds.size()]));
285 }
286
287 public long[] getTagIds(long[] groupIds, String[] names)
288 throws PortalException, SystemException {
289
290 long[] tagsIds = new long[0];
291
292 for (long groupId : groupIds) {
293 tagsIds = ArrayUtil.append(tagsIds, getTagIds(groupId, names));
294 }
295
296 return tagsIds;
297 }
298
299 public String[] getTagNames() throws SystemException {
300 return getTagNames(getTags());
301 }
302
303 public String[] getTagNames(long classNameId, long classPK)
304 throws SystemException {
305
306 return getTagNames(getTags(classNameId, classPK));
307 }
308
309 public String[] getTagNames(String className, long classPK)
310 throws SystemException {
311
312 return getTagNames(getTags(className, classPK));
313 }
314
315 public List<AssetTag> getTags() throws SystemException {
316 return assetTagPersistence.findAll();
317 }
318
319 public List<AssetTag> getTags(long classNameId, long classPK)
320 throws SystemException {
321
322 return assetTagFinder.findByC_C(classNameId, classPK);
323 }
324
325 public List<AssetTag> getTags(long groupId, long classNameId, String name)
326 throws SystemException {
327
328 return assetTagFinder.findByG_C_N(
329 groupId, classNameId, name, QueryUtil.ALL_POS, QueryUtil.ALL_POS,
330 null);
331 }
332
333 public List<AssetTag> getTags(
334 long groupId, long classNameId, String name, int start, int end)
335 throws SystemException {
336
337 return assetTagFinder.findByG_C_N(
338 groupId, classNameId, name, start, end, null);
339 }
340
341 public List<AssetTag> getTags(
342 long groupId, String socialActivityCounterName, int offset,
343 boolean includeCurrentPeriod)
344 throws SystemException {
345
346 int startPeriod = SocialCounterPeriodUtil.getStartPeriod(-offset);
347 int endPeriod = -1;
348
349 if (!includeCurrentPeriod) {
350 endPeriod = SocialCounterPeriodUtil.getStartPeriod() - 1;
351 }
352
353 return getTags(
354 groupId, socialActivityCounterName, startPeriod, endPeriod);
355 }
356
357 public List<AssetTag> getTags(
358 long groupId, String socialActivityCounterName, int startPeriod,
359 int endPeriod)
360 throws SystemException {
361
362 return assetTagFinder.findByG_N_S_E(
363 groupId, socialActivityCounterName, startPeriod, endPeriod);
364 }
365
366 @ThreadLocalCachable
367 public List<AssetTag> getTags(String className, long classPK)
368 throws SystemException {
369
370 long classNameId = PortalUtil.getClassNameId(className);
371
372 return getTags(classNameId, classPK);
373 }
374
375 public int getTagsSize(long groupId, long classNameId, String name)
376 throws SystemException {
377
378 return assetTagFinder.countByG_C_N(groupId, classNameId, name);
379 }
380
381 public boolean hasTag(long groupId, String name)
382 throws PortalException, SystemException {
383
384 try {
385 getTag(groupId, name);
386
387 return true;
388 }
389 catch (NoSuchTagException nste) {
390 return false;
391 }
392 }
393
394 public AssetTag incrementAssetCount(long tagId, long classNameId)
395 throws PortalException, SystemException {
396
397 AssetTag tag = assetTagPersistence.findByPrimaryKey(tagId);
398
399 tag.setAssetCount(tag.getAssetCount() + 1);
400
401 assetTagPersistence.update(tag, false);
402
403 assetTagStatsLocalService.updateTagStats(tagId, classNameId);
404
405 return tag;
406 }
407
408 public void mergeTags(
409 long fromTagId, long toTagId, boolean overrideProperties)
410 throws PortalException, SystemException {
411
412 List<AssetEntry> entries = assetTagPersistence.getAssetEntries(
413 fromTagId);
414
415 assetTagPersistence.addAssetEntries(toTagId, entries);
416
417 List<AssetTagProperty> tagProperties =
418 assetTagPropertyPersistence.findByTagId(fromTagId);
419
420 for (AssetTagProperty fromTagProperty : tagProperties) {
421 AssetTagProperty toTagProperty =
422 assetTagPropertyPersistence.fetchByT_K(
423 toTagId, fromTagProperty.getKey());
424
425 if (overrideProperties && (toTagProperty != null)) {
426 toTagProperty.setValue(fromTagProperty.getValue());
427
428 assetTagPropertyPersistence.update(toTagProperty, false);
429 }
430 else if (toTagProperty == null) {
431 fromTagProperty.setTagId(toTagId);
432
433 assetTagPropertyPersistence.update(fromTagProperty, false);
434 }
435 }
436
437 deleteTag(fromTagId);
438 }
439
440 public List<AssetTag> search(
441 long groupId, String name, String[] tagProperties, int start,
442 int end)
443 throws SystemException {
444
445 return assetTagFinder.findByG_N_P(
446 groupId, name, tagProperties, start, end, null);
447 }
448
449 public AssetTag updateTag(
450 long userId, long tagId, String name, String[] tagProperties,
451 ServiceContext serviceContext)
452 throws PortalException, SystemException {
453
454
455
456 AssetTag tag = assetTagPersistence.findByPrimaryKey(tagId);
457
458 String oldName = tag.getName();
459
460 tag.setModifiedDate(new Date());
461
462 name = name.trim();
463 name = name.toLowerCase();
464
465 if (tagProperties == null) {
466 tagProperties = new String[0];
467 }
468
469 if (!name.equals(tag.getName()) && hasTag(tag.getGroupId(), name)) {
470 throw new DuplicateTagException(
471 "A tag with the name " + name + " already exists");
472 }
473
474 if (!tag.getName().equals(name)) {
475 try {
476 AssetTag existingAssetTag = getTag(tag.getGroupId(), name);
477
478 if (existingAssetTag.getTagId() != tagId) {
479 throw new DuplicateTagException(
480 "A tag with the name " + name + " already exists");
481 }
482 }
483 catch (NoSuchTagException nste) {
484 }
485 }
486
487 validate(name);
488
489 tag.setName(name);
490
491 assetTagPersistence.update(tag, false);
492
493
494
495 List<AssetTagProperty> oldTagProperties =
496 assetTagPropertyPersistence.findByTagId(tagId);
497
498 for (AssetTagProperty tagProperty : oldTagProperties) {
499 assetTagPropertyLocalService.deleteTagProperty(tagProperty);
500 }
501
502 for (int i = 0; i < tagProperties.length; i++) {
503 String[] tagProperty = StringUtil.split(
504 tagProperties[i], CharPool.COLON);
505
506 String key = StringPool.BLANK;
507
508 if (tagProperty.length > 0) {
509 key = GetterUtil.getString(tagProperty[0]);
510 }
511
512 String value = StringPool.BLANK;
513
514 if (tagProperty.length > 1) {
515 value = GetterUtil.getString(tagProperty[1]);
516 }
517
518 if (Validator.isNotNull(key)) {
519 assetTagPropertyLocalService.addTagProperty(
520 userId, tagId, key, value);
521 }
522 }
523
524
525
526 if (!oldName.equals(name)) {
527 List<AssetEntry> entries = assetTagPersistence.getAssetEntries(
528 tag.getTagId());
529
530 assetEntryLocalService.reindex(entries);
531 }
532
533 return tag;
534 }
535
536 protected String[] getTagNames(List <AssetTag>tags) {
537 return StringUtil.split(
538 ListUtil.toString(tags, AssetTag.NAME_ACCESSOR));
539 }
540
541 protected void validate(String name) throws PortalException {
542 if (!AssetUtil.isValidWord(name)) {
543 throw new AssetTagException(
544 StringUtil.merge(
545 AssetUtil.INVALID_CHARACTERS, StringPool.SPACE),
546 AssetTagException.INVALID_CHARACTER);
547 }
548 }
549
550 }