001    /**
002     * Copyright (c) 2000-2010 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.exception.PortalException;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.model.User;
020    import com.liferay.portlet.asset.TagPropertyKeyException;
021    import com.liferay.portlet.asset.TagPropertyValueException;
022    import com.liferay.portlet.asset.model.AssetTagProperty;
023    import com.liferay.portlet.asset.service.base.AssetTagPropertyLocalServiceBaseImpl;
024    import com.liferay.portlet.asset.util.AssetUtil;
025    
026    import java.util.Date;
027    import java.util.List;
028    
029    /**
030     * @author Brian Wing Shun Chan
031     */
032    public class AssetTagPropertyLocalServiceImpl
033            extends AssetTagPropertyLocalServiceBaseImpl {
034    
035            public AssetTagProperty addTagProperty(
036                            long userId, long tagId, String key, String value)
037                    throws PortalException, SystemException {
038    
039                    User user = userPersistence.findByPrimaryKey(userId);
040                    Date now = new Date();
041    
042                    validate(key, value);
043    
044                    long tagPropertyId = counterLocalService.increment();
045    
046                    AssetTagProperty tagProperty = assetTagPropertyPersistence.create(
047                            tagPropertyId);
048    
049                    tagProperty.setCompanyId(user.getCompanyId());
050                    tagProperty.setUserId(user.getUserId());
051                    tagProperty.setUserName(user.getFullName());
052                    tagProperty.setCreateDate(now);
053                    tagProperty.setModifiedDate(now);
054                    tagProperty.setTagId(tagId);
055                    tagProperty.setKey(key);
056                    tagProperty.setValue(value);
057    
058                    assetTagPropertyPersistence.update(tagProperty, false);
059    
060                    return tagProperty;
061            }
062    
063            public void deleteTagProperties(long tagId) throws SystemException {
064                    List<AssetTagProperty> tagProperties =
065                            assetTagPropertyPersistence.findByTagId(tagId);
066    
067                    for (AssetTagProperty tagProperty : tagProperties) {
068                            deleteTagProperty(tagProperty);
069                    }
070            }
071    
072            public void deleteTagProperty(AssetTagProperty tagProperty)
073                    throws SystemException {
074    
075                    assetTagPropertyPersistence.remove(tagProperty);
076            }
077    
078            public void deleteTagProperty(long tagPropertyId)
079                    throws PortalException, SystemException {
080    
081                    AssetTagProperty tagProperty =
082                            assetTagPropertyPersistence.findByPrimaryKey(tagPropertyId);
083    
084                    deleteTagProperty(tagProperty);
085            }
086    
087            public List<AssetTagProperty> getTagProperties() throws SystemException {
088                    return assetTagPropertyPersistence.findAll();
089            }
090    
091            public List<AssetTagProperty> getTagProperties(long tagId)
092                    throws SystemException {
093    
094                    return assetTagPropertyPersistence.findByTagId(tagId);
095            }
096    
097            public AssetTagProperty getTagProperty(long tagPropertyId)
098                    throws PortalException, SystemException {
099    
100                    return assetTagPropertyPersistence.findByPrimaryKey(tagPropertyId);
101            }
102    
103            public AssetTagProperty getTagProperty(long tagId, String key)
104                    throws PortalException, SystemException {
105    
106                    return assetTagPropertyPersistence.findByT_K(tagId, key);
107            }
108    
109            public String[] getTagPropertyKeys(long groupId) throws SystemException {
110                    return assetTagPropertyKeyFinder.findByGroupId(groupId);
111            }
112    
113            public List<AssetTagProperty> getTagPropertyValues(long groupId, String key)
114                    throws SystemException {
115    
116                    return assetTagPropertyFinder.findByG_K(groupId, key);
117            }
118    
119            public AssetTagProperty updateTagProperty(
120                            long tagPropertyId, String key, String value)
121                    throws PortalException, SystemException {
122    
123                    validate(key, value);
124    
125                    AssetTagProperty tagProperty =
126                            assetTagPropertyPersistence.findByPrimaryKey(tagPropertyId);
127    
128                    tagProperty.setModifiedDate(new Date());
129                    tagProperty.setKey(key);
130                    tagProperty.setValue(value);
131    
132                    assetTagPropertyPersistence.update(tagProperty, false);
133    
134                    return tagProperty;
135            }
136    
137            protected void validate(String key, String value) throws PortalException {
138                    if (!AssetUtil.isValidWord(key)) {
139                            throw new TagPropertyKeyException();
140                    }
141    
142                    if (!AssetUtil.isValidWord(value)) {
143                            throw new TagPropertyValueException();
144                    }
145            }
146    
147    }