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