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.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     * Provides the local service for accessing, adding, deleting, and updating
031     * asset tag properties.
032     *
033     * @author Brian Wing Shun Chan
034     */
035    public class AssetTagPropertyLocalServiceImpl
036            extends AssetTagPropertyLocalServiceBaseImpl {
037    
038            /**
039             * Adds an asset tag property.
040             *
041             * @param  userId the primary key of the user
042             * @param  tagId the primary key of the tag
043             * @param  key the key to be associated to the value
044             * @param  value the value to which the key will refer
045             * @return the created asset tag property
046             * @throws PortalException if a user with the primary key could not be
047             *         found, or if the key or value were invalid
048             * @throws SystemException if a system exception occurred
049             */
050            public AssetTagProperty addTagProperty(
051                            long userId, long tagId, String key, String value)
052                    throws PortalException, SystemException {
053    
054                    User user = userPersistence.findByPrimaryKey(userId);
055                    Date now = new Date();
056    
057                    validate(key, value);
058    
059                    long tagPropertyId = counterLocalService.increment();
060    
061                    AssetTagProperty tagProperty = assetTagPropertyPersistence.create(
062                            tagPropertyId);
063    
064                    tagProperty.setCompanyId(user.getCompanyId());
065                    tagProperty.setUserId(user.getUserId());
066                    tagProperty.setUserName(user.getFullName());
067                    tagProperty.setCreateDate(now);
068                    tagProperty.setModifiedDate(now);
069                    tagProperty.setTagId(tagId);
070                    tagProperty.setKey(key);
071                    tagProperty.setValue(value);
072    
073                    assetTagPropertyPersistence.update(tagProperty);
074    
075                    return tagProperty;
076            }
077    
078            /**
079             * Deletes the asset tag property with the specified tag ID.
080             *
081             * @param  tagId the primary key of the tag
082             * @throws SystemException if a system exception occurred
083             */
084            public void deleteTagProperties(long tagId) throws SystemException {
085                    List<AssetTagProperty> tagProperties =
086                            assetTagPropertyPersistence.findByTagId(tagId);
087    
088                    for (AssetTagProperty tagProperty : tagProperties) {
089                            deleteTagProperty(tagProperty);
090                    }
091            }
092    
093            /**
094             * Deletes the asset tag property instance.
095             *
096             * @param  tagProperty the asset tag property instance
097             * @throws SystemException if a system exception occurred
098             */
099            public void deleteTagProperty(AssetTagProperty tagProperty)
100                    throws SystemException {
101    
102                    assetTagPropertyPersistence.remove(tagProperty);
103            }
104    
105            /**
106             * Deletes the asset tag property with the specified ID.
107             *
108             * @param  tagPropertyId the primary key of the asset tag property instance
109             * @throws PortalException if an asset tag property with the primary key
110             *         could not be found
111             * @throws SystemException if a system exception occurred
112             */
113            public void deleteTagProperty(long tagPropertyId)
114                    throws PortalException, SystemException {
115    
116                    AssetTagProperty tagProperty =
117                            assetTagPropertyPersistence.findByPrimaryKey(tagPropertyId);
118    
119                    deleteTagProperty(tagProperty);
120            }
121    
122            /**
123             * Returns all the asset tag property instances.
124             *
125             * @return the asset tag property instances
126             * @throws SystemException if a system exception occurred
127             */
128            public List<AssetTagProperty> getTagProperties() throws SystemException {
129                    return assetTagPropertyPersistence.findAll();
130            }
131    
132            /**
133             * Returns all the asset tag property instances with the specified tag ID.
134             *
135             * @param  tagId the primary key of the tag
136             * @return the matching asset tag properties
137             * @throws SystemException if a system exception occurred
138             */
139            public List<AssetTagProperty> getTagProperties(long tagId)
140                    throws SystemException {
141    
142                    return assetTagPropertyPersistence.findByTagId(tagId);
143            }
144    
145            /**
146             * Returns the asset tag property with the specified ID.
147             *
148             * @param  tagPropertyId the primary key of the asset tag property
149             * @return the matching asset tag property
150             * @throws PortalException if an asset tag property with the primary key
151             *         could not be found
152             * @throws SystemException if a system exception occurred
153             */
154            public AssetTagProperty getTagProperty(long tagPropertyId)
155                    throws PortalException, SystemException {
156    
157                    return assetTagPropertyPersistence.findByPrimaryKey(tagPropertyId);
158            }
159    
160            /**
161             * Returns the asset tag property with the specified tag ID and key.
162             *
163             * @param  tagId the primary key of the tag
164             * @param  key the key that refers to some value
165             * @return the matching asset tag property
166             * @throws PortalException if an asset tag property with the tag ID and key
167             *         could not be found
168             * @throws SystemException if a system exception occurred
169             */
170            public AssetTagProperty getTagProperty(long tagId, String key)
171                    throws PortalException, SystemException {
172    
173                    return assetTagPropertyPersistence.findByT_K(tagId, key);
174            }
175    
176            /**
177             * Returns asset tag property keys with the specified group
178             *
179             * @param  groupId the primary key of the group
180             * @return the matching asset tag property keys
181             * @throws SystemException if a system exception occurred
182             */
183            public String[] getTagPropertyKeys(long groupId) throws SystemException {
184                    return assetTagPropertyKeyFinder.findByGroupId(groupId);
185            }
186    
187            /**
188             * Returns asset tag properties with the specified group and key.
189             *
190             * @param  groupId the primary key of the group
191             * @param  key the key that refers to some value
192             * @return the matching asset tag properties
193             * @throws SystemException if a system exception occurred
194             */
195            public List<AssetTagProperty> getTagPropertyValues(long groupId, String key)
196                    throws SystemException {
197    
198                    return assetTagPropertyFinder.findByG_K(groupId, key);
199            }
200    
201            /**
202             * Updates the asset tag property.
203             *
204             * @param  tagPropertyId the primary key of the asset tag property
205             * @param  key the new key to be associated to the value
206             * @param  value the new value to which the key will refer
207             * @return the updated asset tag property
208             * @throws PortalException if an asset tag property with the primary key
209             *         could not be found, or if the key or value were invalid
210             * @throws SystemException if a system exception occurred
211             */
212            public AssetTagProperty updateTagProperty(
213                            long tagPropertyId, String key, String value)
214                    throws PortalException, SystemException {
215    
216                    validate(key, value);
217    
218                    AssetTagProperty tagProperty =
219                            assetTagPropertyPersistence.findByPrimaryKey(tagPropertyId);
220    
221                    tagProperty.setModifiedDate(new Date());
222                    tagProperty.setKey(key);
223                    tagProperty.setValue(value);
224    
225                    assetTagPropertyPersistence.update(tagProperty);
226    
227                    return tagProperty;
228            }
229    
230            protected void validate(String key, String value) throws PortalException {
231                    if (!AssetUtil.isValidWord(key)) {
232                            throw new TagPropertyKeyException();
233                    }
234    
235                    if (!AssetUtil.isValidWord(value)) {
236                            throw new TagPropertyValueException();
237                    }
238            }
239    
240    }