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     * 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            @Override
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, false);
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            @Override
085            public void deleteTagProperties(long tagId) throws SystemException {
086                    List<AssetTagProperty> tagProperties =
087                            assetTagPropertyPersistence.findByTagId(tagId);
088    
089                    for (AssetTagProperty tagProperty : tagProperties) {
090                            deleteTagProperty(tagProperty);
091                    }
092            }
093    
094            /**
095             * Deletes the asset tag property instance.
096             *
097             * @param  tagProperty the asset tag property instance
098             * @throws SystemException if a system exception occurred
099             */
100            @Override
101            public void deleteTagProperty(AssetTagProperty tagProperty)
102                    throws SystemException {
103    
104                    assetTagPropertyPersistence.remove(tagProperty);
105            }
106    
107            /**
108             * Deletes the asset tag property with the specified ID.
109             *
110             * @param  tagPropertyId the primary key of the asset tag property instance
111             * @throws PortalException if an asset tag property with the primary key
112             *         could not be found
113             * @throws SystemException if a system exception occurred
114             */
115            @Override
116            public void deleteTagProperty(long tagPropertyId)
117                    throws PortalException, SystemException {
118    
119                    AssetTagProperty tagProperty =
120                            assetTagPropertyPersistence.findByPrimaryKey(tagPropertyId);
121    
122                    deleteTagProperty(tagProperty);
123            }
124    
125            /**
126             * Returns all the asset tag property instances.
127             *
128             * @return the asset tag property instances
129             * @throws SystemException if a system exception occurred
130             */
131            @Override
132            public List<AssetTagProperty> getTagProperties() throws SystemException {
133                    return assetTagPropertyPersistence.findAll();
134            }
135    
136            /**
137             * Returns all the asset tag property instances with the specified tag ID.
138             *
139             * @param  tagId the primary key of the tag
140             * @return the matching asset tag properties
141             * @throws SystemException if a system exception occurred
142             */
143            @Override
144            public List<AssetTagProperty> getTagProperties(long tagId)
145                    throws SystemException {
146    
147                    return assetTagPropertyPersistence.findByTagId(tagId);
148            }
149    
150            /**
151             * Returns the asset tag property with the specified ID.
152             *
153             * @param  tagPropertyId the primary key of the asset tag property
154             * @return the matching asset tag property
155             * @throws PortalException if an asset tag property with the primary key
156             *         could not be found
157             * @throws SystemException if a system exception occurred
158             */
159            @Override
160            public AssetTagProperty getTagProperty(long tagPropertyId)
161                    throws PortalException, SystemException {
162    
163                    return assetTagPropertyPersistence.findByPrimaryKey(tagPropertyId);
164            }
165    
166            /**
167             * Returns the asset tag property with the specified tag ID and key.
168             *
169             * @param  tagId the primary key of the tag
170             * @param  key the key that refers to some value
171             * @return the matching asset tag property
172             * @throws PortalException if an asset tag property with the tag ID and key
173             *         could not be found
174             * @throws SystemException if a system exception occurred
175             */
176            @Override
177            public AssetTagProperty getTagProperty(long tagId, String key)
178                    throws PortalException, SystemException {
179    
180                    return assetTagPropertyPersistence.findByT_K(tagId, key);
181            }
182    
183            /**
184             * Returns asset tag property keys with the specified group
185             *
186             * @param  groupId the primary key of the group
187             * @return the matching asset tag property keys
188             * @throws SystemException if a system exception occurred
189             */
190            @Override
191            public String[] getTagPropertyKeys(long groupId) throws SystemException {
192                    return assetTagPropertyKeyFinder.findByGroupId(groupId);
193            }
194    
195            /**
196             * Returns asset tag properties with the specified group and key.
197             *
198             * @param  groupId the primary key of the group
199             * @param  key the key that refers to some value
200             * @return the matching asset tag properties
201             * @throws SystemException if a system exception occurred
202             */
203            @Override
204            public List<AssetTagProperty> getTagPropertyValues(long groupId, String key)
205                    throws SystemException {
206    
207                    return assetTagPropertyFinder.findByG_K(groupId, key);
208            }
209    
210            /**
211             * Updates the asset tag property.
212             *
213             * @param  tagPropertyId the primary key of the asset tag property
214             * @param  key the new key to be associated to the value
215             * @param  value the new value to which the key will refer
216             * @return the updated asset tag property
217             * @throws PortalException if an asset tag property with the primary key
218             *         could not be found, or if the key or value were invalid
219             * @throws SystemException if a system exception occurred
220             */
221            @Override
222            public AssetTagProperty updateTagProperty(
223                            long tagPropertyId, String key, String value)
224                    throws PortalException, SystemException {
225    
226                    validate(key, value);
227    
228                    AssetTagProperty tagProperty =
229                            assetTagPropertyPersistence.findByPrimaryKey(tagPropertyId);
230    
231                    tagProperty.setModifiedDate(new Date());
232                    tagProperty.setKey(key);
233                    tagProperty.setValue(value);
234    
235                    assetTagPropertyPersistence.update(tagProperty, false);
236    
237                    return tagProperty;
238            }
239    
240            protected void validate(String key, String value) throws PortalException {
241                    if (!AssetUtil.isValidWord(key)) {
242                            throw new TagPropertyKeyException();
243                    }
244    
245                    if (!AssetUtil.isValidWord(value)) {
246                            throw new TagPropertyValueException();
247                    }
248            }
249    
250    }