001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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.exception.PortalException;
019    import com.liferay.portal.kernel.exception.SystemException;
020    import com.liferay.portal.kernel.search.Indexer;
021    import com.liferay.portal.kernel.search.IndexerRegistryUtil;
022    import com.liferay.portal.kernel.util.ArrayUtil;
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    
042    import java.util.ArrayList;
043    import java.util.Date;
044    import java.util.List;
045    
046    /**
047     * @author Brian Wing Shun Chan
048     * @author Alvaro del Castillo
049     * @author Jorge Ferrer
050     * @author Bruno Farache
051     */
052    public class AssetTagLocalServiceImpl extends AssetTagLocalServiceBaseImpl {
053    
054            public AssetTag addTag(
055                            long userId, String name, String[] tagProperties,
056                            ServiceContext serviceContext)
057                    throws PortalException, SystemException {
058    
059                    // Tag
060    
061                    User user = userPersistence.findByPrimaryKey(userId);
062                    long groupId = serviceContext.getScopeGroupId();
063    
064                    if (tagProperties == null) {
065                            tagProperties = new String[0];
066                    }
067    
068                    Date now = new Date();
069    
070                    long tagId = counterLocalService.increment();
071    
072                    AssetTag tag = assetTagPersistence.create(tagId);
073    
074                    tag.setGroupId(groupId);
075                    tag.setCompanyId(user.getCompanyId());
076                    tag.setUserId(user.getUserId());
077                    tag.setUserName(user.getFullName());
078                    tag.setCreateDate(now);
079                    tag.setModifiedDate(now);
080    
081                    name = name.trim();
082                    name = name.toLowerCase();
083    
084                    if (hasTag(groupId, name)) {
085                            throw new DuplicateTagException(
086                                    "A tag with the name " + name + " already exists");
087                    }
088    
089                    validate(name);
090    
091                    tag.setName(name);
092    
093                    assetTagPersistence.update(tag, false);
094    
095                    // Resources
096    
097                    if (serviceContext.getAddCommunityPermissions() ||
098                            serviceContext.getAddGuestPermissions()) {
099    
100                            addTagResources(
101                                    tag, serviceContext.getAddCommunityPermissions(),
102                                    serviceContext.getAddGuestPermissions());
103                    }
104                    else {
105                            addTagResources(
106                                    tag, serviceContext.getCommunityPermissions(),
107                                    serviceContext.getGuestPermissions());
108                    }
109    
110                    // Properties
111    
112                    for (int i = 0; i < tagProperties.length; i++) {
113                            String[] tagProperty = StringUtil.split(
114                                    tagProperties[i], StringPool.COLON);
115    
116                            String key = StringPool.BLANK;
117    
118                            if (tagProperty.length > 1) {
119                                    key = GetterUtil.getString(tagProperty[1]);
120                            }
121    
122                            String value = StringPool.BLANK;
123    
124                            if (tagProperty.length > 2) {
125                                    value = GetterUtil.getString(tagProperty[2]);
126                            }
127    
128                            if (Validator.isNotNull(key)) {
129                                    assetTagPropertyLocalService.addTagProperty(
130                                            userId, tagId, key, value);
131                            }
132                    }
133    
134                    return tag;
135            }
136    
137            public void addTagResources(
138                            AssetTag tag, boolean addCommunityPermissions,
139                            boolean addGuestPermissions)
140                    throws PortalException, SystemException {
141    
142                    resourceLocalService.addResources(
143                            tag.getCompanyId(), tag.getGroupId(), tag.getUserId(),
144                            AssetTag.class.getName(), tag.getTagId(), false,
145                            addCommunityPermissions, addGuestPermissions);
146            }
147    
148            public void addTagResources(
149                            AssetTag tag, String[] communityPermissions,
150                            String[] guestPermissions)
151                    throws PortalException, SystemException {
152    
153                    resourceLocalService.addModelResources(
154                            tag.getCompanyId(), tag.getGroupId(), tag.getUserId(),
155                            AssetTag.class.getName(), tag.getTagId(), communityPermissions,
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.setAddCommunityPermissions(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                    // 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                    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> getGroupTags(long groupId) throws SystemException {
232                    return assetTagPersistence.findByGroupId(groupId);
233            }
234    
235            public AssetTag getTag(long tagId) throws PortalException, SystemException {
236                    return assetTagPersistence.findByPrimaryKey(tagId);
237            }
238    
239            public AssetTag getTag(long groupId, String name)
240                    throws PortalException, SystemException {
241    
242                    return assetTagFinder.findByG_N(groupId, name);
243            }
244    
245            public long[] getTagIds(long groupId, String[] names)
246                    throws PortalException, SystemException {
247    
248                    List<Long> tagIds = new ArrayList<Long>(names.length);
249    
250                    for (String name : names) {
251                            try {
252                                    AssetTag tag = getTag(groupId, name);
253    
254                                    tagIds.add(tag.getTagId());
255                            }
256                            catch (NoSuchTagException nste) {
257                            }
258                    }
259    
260                    return ArrayUtil.toArray(tagIds.toArray(new Long[tagIds.size()]));
261            }
262    
263            public long[] getTagIds(long[] groupIds, String[] names)
264                    throws PortalException, SystemException {
265    
266                    long[] tagsIds = new long[0];
267    
268                    for (long groupId : groupIds) {
269                            tagsIds = ArrayUtil.append(tagsIds, getTagIds(groupId, names));
270                    }
271    
272                    return tagsIds;
273            }
274    
275            public String[] getTagNames() throws SystemException {
276                    return getTagNames(getTags());
277            }
278    
279            public String[] getTagNames(long classNameId, long classPK)
280                    throws SystemException {
281    
282                    return getTagNames(getTags(classNameId, classPK));
283            }
284    
285            public String[] getTagNames(String className, long classPK)
286                    throws SystemException {
287    
288                    return getTagNames(getTags(className, classPK));
289            }
290    
291            public List<AssetTag> getTags() throws SystemException {
292                    return assetTagPersistence.findAll();
293            }
294    
295            public List<AssetTag> getTags(long classNameId, long classPK)
296                    throws SystemException {
297    
298                    return assetTagFinder.findByC_C(classNameId, classPK);
299            }
300    
301            public List<AssetTag> getTags(long groupId, long classNameId, String name)
302                    throws SystemException {
303    
304                    return assetTagFinder.findByG_C_N(groupId, classNameId, name);
305            }
306    
307            public List<AssetTag> getTags(
308                            long groupId, long classNameId, String name, int start, int end)
309                    throws SystemException {
310    
311                    return assetTagFinder.findByG_C_N(
312                            groupId, classNameId, name, start, end);
313            }
314    
315            @ThreadLocalCachable
316            public List<AssetTag> getTags(String className, long classPK)
317                    throws SystemException {
318    
319                    long classNameId = PortalUtil.getClassNameId(className);
320    
321                    return getTags(classNameId, classPK);
322            }
323    
324            public int getTagsSize(long groupId, long classNameId, String name)
325                    throws SystemException {
326    
327                    return assetTagFinder.countByG_C_N(groupId, classNameId, name);
328            }
329    
330            public boolean hasTag(long groupId, String name)
331                    throws PortalException, SystemException {
332    
333                    try {
334                            getTag(groupId, name);
335    
336                            return true;
337                    }
338                    catch (NoSuchTagException nste) {
339                            return false;
340                    }
341            }
342    
343            public AssetTag incrementAssetCount(long tagId, long classNameId)
344                    throws PortalException, SystemException {
345    
346                    AssetTag tag = assetTagPersistence.findByPrimaryKey(tagId);
347    
348                    tag.setAssetCount(tag.getAssetCount() + 1);
349    
350                    assetTagPersistence.update(tag, false);
351    
352                    assetTagStatsLocalService.updateTagStats(tagId, classNameId);
353    
354                    return tag;
355            }
356    
357            public void mergeTags(long fromTagId, long toTagId)
358                    throws PortalException, SystemException {
359    
360                    List<AssetEntry> entries = assetTagPersistence.getAssetEntries(
361                            fromTagId);
362    
363                    assetTagPersistence.addAssetEntries(toTagId, entries);
364    
365                    List<AssetTagProperty> tagProperties =
366                            assetTagPropertyPersistence.findByTagId(fromTagId);
367    
368                    for (AssetTagProperty fromTagProperty : tagProperties) {
369                            AssetTagProperty toTagProperty =
370                                    assetTagPropertyPersistence.fetchByT_K(
371                                            toTagId, fromTagProperty.getKey());
372    
373                            if (toTagProperty == null) {
374                                    fromTagProperty.setTagId(toTagId);
375    
376                                    assetTagPropertyPersistence.update(fromTagProperty, false);
377                            }
378                    }
379    
380                    deleteTag(fromTagId);
381            }
382    
383            public List<AssetTag> search(
384                            long groupId, String name, String[] tagProperties, int start,
385                            int end)
386                    throws SystemException {
387    
388                    return assetTagFinder.findByG_N_P(
389                            groupId, name, tagProperties, start, end);
390            }
391    
392            public AssetTag updateTag(
393                            long userId, long tagId, String name, String[] tagProperties,
394                            ServiceContext serviceContext)
395                    throws PortalException, SystemException {
396    
397                    // Tag
398    
399                    AssetTag tag = assetTagPersistence.findByPrimaryKey(tagId);
400    
401                    String oldName = tag.getName();
402    
403                    tag.setModifiedDate(new Date());
404    
405                    name = name.trim();
406                    name = name.toLowerCase();
407    
408                    if (tagProperties == null) {
409                            tagProperties = new String[0];
410                    }
411    
412                    if (!tag.getName().equals(name) &&
413                            hasTag(tag.getGroupId(), name)) {
414    
415                            throw new DuplicateTagException(
416                                    "A tag with the name " + name + " already exists");
417                    }
418    
419                    if (!tag.getName().equals(name)) {
420                            try {
421                                    AssetTag existingAssetTag = getTag(tag.getGroupId(), name);
422    
423                                    if (existingAssetTag.getTagId() != tagId) {
424                                            throw new DuplicateTagException(
425                                                    "A tag with the name " + name + " already exists");
426                                    }
427                            }
428                            catch (NoSuchTagException nste) {
429                            }
430                    }
431    
432                    validate(name);
433    
434                    tag.setName(name);
435    
436                    assetTagPersistence.update(tag, false);
437    
438                    // Properties
439    
440                    List<AssetTagProperty> oldTagProperties =
441                            assetTagPropertyPersistence.findByTagId(tagId);
442    
443                    for (AssetTagProperty tagProperty : oldTagProperties) {
444                            assetTagPropertyLocalService.deleteTagProperty(tagProperty);
445                    }
446    
447                    for (int i = 0; i < tagProperties.length; i++) {
448                            String[] tagProperty = StringUtil.split(
449                                    tagProperties[i], StringPool.COLON);
450    
451                            String key = StringPool.BLANK;
452    
453                            if (tagProperty.length > 0) {
454                                    key = GetterUtil.getString(tagProperty[0]);
455                            }
456    
457                            String value = StringPool.BLANK;
458    
459                            if (tagProperty.length > 1) {
460                                    value = GetterUtil.getString(tagProperty[1]);
461                            }
462    
463                            if (Validator.isNotNull(key)) {
464                                    assetTagPropertyLocalService.addTagProperty(
465                                            userId, tagId, key, value);
466                            }
467                    }
468    
469                    // Indexer
470    
471                    if (!oldName.equals(name)) {
472                            List<AssetEntry> entries = assetTagPersistence.getAssetEntries(
473                                    tag.getTagId());
474    
475                            reindex(entries);
476                    }
477    
478                    return tag;
479            }
480    
481            protected String[] getTagNames(List <AssetTag>tags) {
482                    return StringUtil.split(ListUtil.toString(tags, "name"));
483            }
484    
485            protected void reindex(List<AssetEntry> entries) throws PortalException {
486                    for (AssetEntry entry : entries) {
487                            String className = PortalUtil.getClassName(entry.getClassNameId());
488    
489                            Indexer indexer = IndexerRegistryUtil.getIndexer(className);
490    
491                            indexer.reindex(className, entry.getClassPK());
492                    }
493            }
494    
495            protected void validate(String name) throws PortalException {
496                    if (!AssetUtil.isValidWord(name)) {
497                            throw new AssetTagException(AssetTagException.INVALID_CHARACTER);
498                    }
499            }
500    
501    }