1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portlet.tags.service.impl;
21  
22  import com.liferay.portal.PortalException;
23  import com.liferay.portal.SystemException;
24  import com.liferay.portal.model.User;
25  import com.liferay.portlet.tags.PropertyKeyException;
26  import com.liferay.portlet.tags.PropertyValueException;
27  import com.liferay.portlet.tags.model.TagsEntry;
28  import com.liferay.portlet.tags.model.TagsProperty;
29  import com.liferay.portlet.tags.service.base.TagsPropertyLocalServiceBaseImpl;
30  import com.liferay.portlet.tags.util.TagsUtil;
31  
32  import java.util.Date;
33  import java.util.List;
34  
35  /**
36   * <a href="TagsPropertyLocalServiceImpl.java.html"><b><i>View Source</i></b>
37   * </a>
38   *
39   * @author Brian Wing Shun Chan
40   *
41   */
42  public class TagsPropertyLocalServiceImpl
43      extends TagsPropertyLocalServiceBaseImpl {
44  
45      public TagsProperty addProperty(
46              long userId, long entryId, String key, String value)
47          throws PortalException, SystemException {
48  
49          User user = userPersistence.findByPrimaryKey(userId);
50          Date now = new Date();
51  
52          validate(key, value);
53  
54          long propertyId = counterLocalService.increment();
55  
56          TagsProperty property = tagsPropertyPersistence.create(propertyId);
57  
58          property.setCompanyId(user.getCompanyId());
59          property.setUserId(user.getUserId());
60          property.setUserName(user.getFullName());
61          property.setCreateDate(now);
62          property.setModifiedDate(now);
63          property.setEntryId(entryId);
64          property.setKey(key);
65          property.setValue(value);
66  
67          tagsPropertyPersistence.update(property, false);
68  
69          return property;
70      }
71  
72      public TagsProperty addProperty(
73              long userId, String entryName, String key, String value)
74          throws PortalException, SystemException {
75  
76          User user = userPersistence.findByPrimaryKey(userId);
77  
78          TagsEntry entry = tagsEntryLocalService.getEntry(
79              user.getCompanyId(), entryName);
80  
81          return addProperty(userId, entry.getEntryId(), key, value);
82      }
83  
84      public void deleteProperties(long entryId) throws SystemException {
85          List<TagsProperty> properties = tagsPropertyPersistence.findByEntryId(
86              entryId);
87  
88          for (TagsProperty property : properties) {
89              deleteProperty(property);
90          }
91      }
92  
93      public void deleteProperty(long propertyId)
94          throws PortalException, SystemException {
95  
96          TagsProperty property = tagsPropertyPersistence.findByPrimaryKey(
97              propertyId);
98  
99          deleteProperty(property);
100     }
101 
102     public void deleteProperty(TagsProperty property) throws SystemException {
103         tagsPropertyPersistence.remove(property);
104     }
105 
106     public List<TagsProperty> getProperties() throws SystemException {
107         return tagsPropertyPersistence.findAll();
108     }
109 
110     public List<TagsProperty> getProperties(long entryId)
111         throws SystemException {
112 
113         return tagsPropertyPersistence.findByEntryId(entryId);
114     }
115 
116     public TagsProperty getProperty(long propertyId)
117         throws PortalException, SystemException {
118 
119         return tagsPropertyPersistence.findByPrimaryKey(propertyId);
120     }
121 
122     public TagsProperty getProperty(long entryId, String key)
123         throws PortalException, SystemException {
124 
125         return tagsPropertyPersistence.findByE_K(entryId, key);
126     }
127 
128     public String[] getPropertyKeys(long companyId) throws SystemException {
129         return tagsPropertyKeyFinder.findByCompanyId(companyId);
130     }
131 
132     public List<TagsProperty> getPropertyValues(long companyId, String key)
133         throws SystemException {
134 
135         return tagsPropertyFinder.findByC_K(companyId, key);
136     }
137 
138     public TagsProperty updateProperty(
139             long propertyId, String key, String value)
140         throws PortalException, SystemException {
141 
142         validate(key, value);
143 
144         TagsProperty property = tagsPropertyPersistence.findByPrimaryKey(
145             propertyId);
146 
147         property.setModifiedDate(new Date());
148         property.setKey(key);
149         property.setValue(value);
150 
151         tagsPropertyPersistence.update(property, false);
152 
153         return property;
154     }
155 
156     protected void validate(String key, String value) throws PortalException {
157         if (!TagsUtil.isValidWord(key)) {
158             throw new PropertyKeyException();
159         }
160 
161         if (!TagsUtil.isValidWord(value)) {
162             throw new PropertyValueException();
163         }
164     }
165 
166 }