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