001    /**
002     * Copyright (c) 2000-2012 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.Date;
045    import java.util.List;
046    
047    /**
048     * @author Brian Wing Shun Chan
049     * @author Alvaro del Castillo
050     * @author Jorge Ferrer
051     * @author Bruno Farache
052     */
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                    // Tag
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                    // Resources
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                    // Properties
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                    // Entries
198    
199                    List<AssetEntry> entries = assetTagPersistence.getAssetEntries(
200                            tag.getTagId());
201    
202                    // Tag
203    
204                    assetTagPersistence.remove(tag);
205    
206                    // Resources
207    
208                    resourceLocalService.deleteResource(
209                            tag.getCompanyId(), AssetTag.class.getName(),
210                            ResourceConstants.SCOPE_INDIVIDUAL, tag.getTagId());
211    
212                    // Properties
213    
214                    assetTagPropertyLocalService.deleteTagProperties(tag.getTagId());
215    
216                    // Indexer
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 assetTagFinder.findByG_N_P(
465                            groupId, name, tagProperties, start, end, null);
466            }
467    
468            public AssetTag updateTag(
469                            long userId, long tagId, String name, String[] tagProperties,
470                            ServiceContext serviceContext)
471                    throws PortalException, SystemException {
472    
473                    // Tag
474    
475                    AssetTag tag = assetTagPersistence.findByPrimaryKey(tagId);
476    
477                    String oldName = tag.getName();
478    
479                    tag.setModifiedDate(new Date());
480    
481                    name = name.trim();
482                    name = name.toLowerCase();
483    
484                    if (tagProperties == null) {
485                            tagProperties = new String[0];
486                    }
487    
488                    if (!name.equals(tag.getName()) && hasTag(tag.getGroupId(), name)) {
489                            throw new DuplicateTagException(
490                                    "A tag with the name " + name + " already exists");
491                    }
492    
493                    if (!tag.getName().equals(name)) {
494                            try {
495                                    AssetTag existingAssetTag = getTag(tag.getGroupId(), name);
496    
497                                    if (existingAssetTag.getTagId() != tagId) {
498                                            throw new DuplicateTagException(
499                                                    "A tag with the name " + name + " already exists");
500                                    }
501                            }
502                            catch (NoSuchTagException nste) {
503                            }
504                    }
505    
506                    validate(name);
507    
508                    tag.setName(name);
509    
510                    assetTagPersistence.update(tag);
511    
512                    // Properties
513    
514                    List<AssetTagProperty> oldTagProperties =
515                            assetTagPropertyPersistence.findByTagId(tagId);
516    
517                    for (AssetTagProperty tagProperty : oldTagProperties) {
518                            assetTagPropertyLocalService.deleteTagProperty(tagProperty);
519                    }
520    
521                    for (int i = 0; i < tagProperties.length; i++) {
522                            String[] tagProperty = StringUtil.split(
523                                    tagProperties[i], CharPool.COLON);
524    
525                            String key = StringPool.BLANK;
526    
527                            if (tagProperty.length > 0) {
528                                    key = GetterUtil.getString(tagProperty[0]);
529                            }
530    
531                            String value = StringPool.BLANK;
532    
533                            if (tagProperty.length > 1) {
534                                    value = GetterUtil.getString(tagProperty[1]);
535                            }
536    
537                            if (Validator.isNotNull(key)) {
538                                    assetTagPropertyLocalService.addTagProperty(
539                                            userId, tagId, key, value);
540                            }
541                    }
542    
543                    // Indexer
544    
545                    if (!oldName.equals(name)) {
546                            List<AssetEntry> entries = assetTagPersistence.getAssetEntries(
547                                    tag.getTagId());
548    
549                            assetEntryLocalService.reindex(entries);
550                    }
551    
552                    return tag;
553            }
554    
555            protected String[] getTagNames(List<AssetTag>tags) {
556                    return StringUtil.split(
557                            ListUtil.toString(tags, AssetTag.NAME_ACCESSOR));
558            }
559    
560            protected void validate(String name) throws PortalException {
561                    if (!AssetUtil.isValidWord(name)) {
562                            throw new AssetTagException(
563                                    StringUtil.merge(
564                                            AssetUtil.INVALID_CHARACTERS, StringPool.SPACE),
565                                    AssetTagException.INVALID_CHARACTER);
566                    }
567            }
568    
569    }