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