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