001    /**
002     * Copyright (c) 2000-present 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.model.User;
019    import com.liferay.portlet.asset.TagPropertyKeyException;
020    import com.liferay.portlet.asset.TagPropertyValueException;
021    import com.liferay.portlet.asset.model.AssetTagProperty;
022    import com.liferay.portlet.asset.service.base.AssetTagPropertyLocalServiceBaseImpl;
023    import com.liferay.portlet.asset.util.AssetUtil;
024    
025    import java.util.Date;
026    import java.util.List;
027    
028    /**
029     * Provides the local service for accessing, adding, deleting, and updating
030     * asset tag properties.
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             */
048            @Override
049            public AssetTagProperty addTagProperty(
050                            long userId, long tagId, String key, String value)
051                    throws PortalException {
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);
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             */
082            @Override
083            public void deleteTagProperties(long tagId) {
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             */
097            @Override
098            public void deleteTagProperty(AssetTagProperty tagProperty) {
099                    assetTagPropertyPersistence.remove(tagProperty);
100            }
101    
102            /**
103             * Deletes the asset tag property with the specified ID.
104             *
105             * @param  tagPropertyId the primary key of the asset tag property instance
106             * @throws PortalException if an asset tag property with the primary key
107             *         could not be found
108             */
109            @Override
110            public void deleteTagProperty(long tagPropertyId) throws PortalException {
111                    AssetTagProperty tagProperty =
112                            assetTagPropertyPersistence.findByPrimaryKey(tagPropertyId);
113    
114                    deleteTagProperty(tagProperty);
115            }
116    
117            /**
118             * Returns all the asset tag property instances.
119             *
120             * @return the asset tag property instances
121             */
122            @Override
123            public List<AssetTagProperty> getTagProperties() {
124                    return assetTagPropertyPersistence.findAll();
125            }
126    
127            /**
128             * Returns all the asset tag property instances with the specified tag ID.
129             *
130             * @param  tagId the primary key of the tag
131             * @return the matching asset tag properties
132             */
133            @Override
134            public List<AssetTagProperty> getTagProperties(long tagId) {
135                    return assetTagPropertyPersistence.findByTagId(tagId);
136            }
137    
138            /**
139             * Returns the asset tag property with the specified ID.
140             *
141             * @param  tagPropertyId the primary key of the asset tag property
142             * @return the matching asset tag property
143             * @throws PortalException if an asset tag property with the primary key
144             *         could not be found
145             */
146            @Override
147            public AssetTagProperty getTagProperty(long tagPropertyId)
148                    throws PortalException {
149    
150                    return assetTagPropertyPersistence.findByPrimaryKey(tagPropertyId);
151            }
152    
153            /**
154             * Returns the asset tag property with the specified tag ID and key.
155             *
156             * @param  tagId the primary key of the tag
157             * @param  key the key that refers to some value
158             * @return the matching asset tag property
159             * @throws PortalException if an asset tag property with the tag ID and key
160             *         could not be found
161             */
162            @Override
163            public AssetTagProperty getTagProperty(long tagId, String key)
164                    throws PortalException {
165    
166                    return assetTagPropertyPersistence.findByT_K(tagId, key);
167            }
168    
169            /**
170             * Returns asset tag property keys with the specified group
171             *
172             * @param  groupId the primary key of the group
173             * @return the matching asset tag property keys
174             */
175            @Override
176            public String[] getTagPropertyKeys(long groupId) {
177                    return assetTagPropertyKeyFinder.findByGroupId(groupId);
178            }
179    
180            /**
181             * Returns asset tag properties with the specified group and key.
182             *
183             * @param  groupId the primary key of the group
184             * @param  key the key that refers to some value
185             * @return the matching asset tag properties
186             */
187            @Override
188            public List<AssetTagProperty> getTagPropertyValues(
189                    long groupId, String key) {
190    
191                    return assetTagPropertyFinder.findByG_K(groupId, key);
192            }
193    
194            /**
195             * Updates the asset tag property.
196             *
197             * @param  tagPropertyId the primary key of the asset tag property
198             * @param  key the new key to be associated to the value
199             * @param  value the new value to which the key will refer
200             * @return the updated asset tag property
201             * @throws PortalException if an asset tag property with the primary key
202             *         could not be found, or if the key or value were invalid
203             */
204            @Override
205            public AssetTagProperty updateTagProperty(
206                            long tagPropertyId, String key, String value)
207                    throws PortalException {
208    
209                    validate(key, value);
210    
211                    AssetTagProperty tagProperty =
212                            assetTagPropertyPersistence.findByPrimaryKey(tagPropertyId);
213    
214                    tagProperty.setModifiedDate(new Date());
215                    tagProperty.setKey(key);
216                    tagProperty.setValue(value);
217    
218                    assetTagPropertyPersistence.update(tagProperty);
219    
220                    return tagProperty;
221            }
222    
223            protected void validate(String key, String value) throws PortalException {
224                    if (!AssetUtil.isValidWord(key)) {
225                            throw new TagPropertyKeyException();
226                    }
227    
228                    if (!AssetUtil.isValidWord(value)) {
229                            throw new TagPropertyValueException();
230                    }
231            }
232    
233    }