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