1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet.asset.service.impl;
16  
17  import com.liferay.portal.kernel.exception.PortalException;
18  import com.liferay.portal.kernel.exception.SystemException;
19  import com.liferay.portal.model.User;
20  import com.liferay.portlet.asset.TagPropertyKeyException;
21  import com.liferay.portlet.asset.TagPropertyValueException;
22  import com.liferay.portlet.asset.model.AssetTagProperty;
23  import com.liferay.portlet.asset.service.base.AssetTagPropertyLocalServiceBaseImpl;
24  import com.liferay.portlet.asset.util.AssetUtil;
25  
26  import java.util.Date;
27  import java.util.List;
28  
29  /**
30   * <a href="AssetTagPropertyLocalServiceImpl.java.html"><b><i>View Source</i>
31   * </b></a>
32   *
33   * @author Brian Wing Shun Chan
34   */
35  public class AssetTagPropertyLocalServiceImpl
36      extends AssetTagPropertyLocalServiceBaseImpl {
37  
38      public AssetTagProperty addTagProperty(
39              long userId, long tagId, String key, String value)
40          throws PortalException, SystemException {
41  
42          User user = userPersistence.findByPrimaryKey(userId);
43          Date now = new Date();
44  
45          validate(key, value);
46  
47          long tagPropertyId = counterLocalService.increment();
48  
49          AssetTagProperty tagProperty = assetTagPropertyPersistence.create(
50              tagPropertyId);
51  
52          tagProperty.setCompanyId(user.getCompanyId());
53          tagProperty.setUserId(user.getUserId());
54          tagProperty.setUserName(user.getFullName());
55          tagProperty.setCreateDate(now);
56          tagProperty.setModifiedDate(now);
57          tagProperty.setTagId(tagId);
58          tagProperty.setKey(key);
59          tagProperty.setValue(value);
60  
61          assetTagPropertyPersistence.update(tagProperty, false);
62  
63          return tagProperty;
64      }
65  
66      public void deleteTagProperties(long tagId) throws SystemException {
67          List<AssetTagProperty> tagProperties =
68              assetTagPropertyPersistence.findByTagId(tagId);
69  
70          for (AssetTagProperty tagProperty : tagProperties) {
71              deleteTagProperty(tagProperty);
72          }
73      }
74  
75      public void deleteTagProperty(AssetTagProperty tagProperty)
76          throws SystemException {
77  
78          assetTagPropertyPersistence.remove(tagProperty);
79      }
80  
81      public void deleteTagProperty(long tagPropertyId)
82          throws PortalException, SystemException {
83  
84          AssetTagProperty tagProperty =
85              assetTagPropertyPersistence.findByPrimaryKey(tagPropertyId);
86  
87          deleteTagProperty(tagProperty);
88      }
89  
90      public List<AssetTagProperty> getTagProperties() throws SystemException {
91          return assetTagPropertyPersistence.findAll();
92      }
93  
94      public List<AssetTagProperty> getTagProperties(long tagId)
95          throws SystemException {
96  
97          return assetTagPropertyPersistence.findByTagId(tagId);
98      }
99  
100     public AssetTagProperty getTagProperty(long tagPropertyId)
101         throws PortalException, SystemException {
102 
103         return assetTagPropertyPersistence.findByPrimaryKey(tagPropertyId);
104     }
105 
106     public AssetTagProperty getTagProperty(long tagId, String key)
107         throws PortalException, SystemException {
108 
109         return assetTagPropertyPersistence.findByT_K(tagId, key);
110     }
111 
112     public String[] getTagPropertyKeys(long groupId) throws SystemException {
113         return assetTagPropertyKeyFinder.findByGroupId(groupId);
114     }
115 
116     public List<AssetTagProperty> getTagPropertyValues(long groupId, String key)
117         throws SystemException {
118 
119         return assetTagPropertyFinder.findByG_K(groupId, key);
120     }
121 
122     public AssetTagProperty updateTagProperty(
123             long tagPropertyId, String key, String value)
124         throws PortalException, SystemException {
125 
126         validate(key, value);
127 
128         AssetTagProperty tagProperty =
129             assetTagPropertyPersistence.findByPrimaryKey(tagPropertyId);
130 
131         tagProperty.setModifiedDate(new Date());
132         tagProperty.setKey(key);
133         tagProperty.setValue(value);
134 
135         assetTagPropertyPersistence.update(tagProperty, false);
136 
137         return tagProperty;
138     }
139 
140     protected void validate(String key, String value) throws PortalException {
141         if (!AssetUtil.isValidWord(key)) {
142             throw new TagPropertyKeyException();
143         }
144 
145         if (!AssetUtil.isValidWord(value)) {
146             throw new TagPropertyValueException();
147         }
148     }
149 
150 }