1   /**
2    * Copyright (c) 2000-2010 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   *
12   *
13   */
14  
15  package com.liferay.portlet.tags.service.impl;
16  
17  import com.liferay.portal.PortalException;
18  import com.liferay.portal.SystemException;
19  import com.liferay.portal.model.User;
20  import com.liferay.portlet.tags.PropertyKeyException;
21  import com.liferay.portlet.tags.PropertyValueException;
22  import com.liferay.portlet.tags.model.TagsProperty;
23  import com.liferay.portlet.tags.service.base.TagsPropertyLocalServiceBaseImpl;
24  import com.liferay.portlet.tags.util.TagsUtil;
25  
26  import java.util.Date;
27  import java.util.List;
28  
29  /**
30   * <a href="TagsPropertyLocalServiceImpl.java.html"><b><i>View Source</i></b>
31   * </a>
32   *
33   * @author Brian Wing Shun Chan
34   */
35  public class TagsPropertyLocalServiceImpl
36      extends TagsPropertyLocalServiceBaseImpl {
37  
38      public TagsProperty addProperty(
39              long userId, long entryId, 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 propertyId = counterLocalService.increment();
48  
49          TagsProperty property = tagsPropertyPersistence.create(propertyId);
50  
51          property.setCompanyId(user.getCompanyId());
52          property.setUserId(user.getUserId());
53          property.setUserName(user.getFullName());
54          property.setCreateDate(now);
55          property.setModifiedDate(now);
56          property.setEntryId(entryId);
57          property.setKey(key);
58          property.setValue(value);
59  
60          tagsPropertyPersistence.update(property, false);
61  
62          return property;
63      }
64  
65      public void deleteProperties(long entryId) throws SystemException {
66          List<TagsProperty> properties = tagsPropertyPersistence.findByEntryId(
67              entryId);
68  
69          for (TagsProperty property : properties) {
70              deleteProperty(property);
71          }
72      }
73  
74      public void deleteProperty(long propertyId)
75          throws PortalException, SystemException {
76  
77          TagsProperty property = tagsPropertyPersistence.findByPrimaryKey(
78              propertyId);
79  
80          deleteProperty(property);
81      }
82  
83      public void deleteProperty(TagsProperty property) throws SystemException {
84          tagsPropertyPersistence.remove(property);
85      }
86  
87      public List<TagsProperty> getProperties() throws SystemException {
88          return tagsPropertyPersistence.findAll();
89      }
90  
91      public List<TagsProperty> getProperties(long entryId)
92          throws SystemException {
93  
94          return tagsPropertyPersistence.findByEntryId(entryId);
95      }
96  
97      public TagsProperty getProperty(long propertyId)
98          throws PortalException, SystemException {
99  
100         return tagsPropertyPersistence.findByPrimaryKey(propertyId);
101     }
102 
103     public TagsProperty getProperty(long entryId, String key)
104         throws PortalException, SystemException {
105 
106         return tagsPropertyPersistence.findByE_K(entryId, key);
107     }
108 
109     public String[] getPropertyKeys(long groupId) throws SystemException {
110         return tagsPropertyKeyFinder.findByGroupId(groupId);
111     }
112 
113     public List<TagsProperty> getPropertyValues(long groupId, String key)
114         throws SystemException {
115 
116         return tagsPropertyFinder.findByG_K(groupId, key);
117     }
118 
119     public TagsProperty updateProperty(
120             long propertyId, String key, String value)
121         throws PortalException, SystemException {
122 
123         validate(key, value);
124 
125         TagsProperty property = tagsPropertyPersistence.findByPrimaryKey(
126             propertyId);
127 
128         property.setModifiedDate(new Date());
129         property.setKey(key);
130         property.setValue(value);
131 
132         tagsPropertyPersistence.update(property, false);
133 
134         return property;
135     }
136 
137     protected void validate(String key, String value) throws PortalException {
138         if (!TagsUtil.isValidWord(key)) {
139             throw new PropertyKeyException();
140         }
141 
142         if (!TagsUtil.isValidWord(value)) {
143             throw new PropertyValueException();
144         }
145     }
146 
147 }