1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions 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   */
45  public class TagsPropertyLocalServiceImpl
46      extends TagsPropertyLocalServiceBaseImpl {
47  
48      public TagsProperty addProperty(
49              long userId, long entryId, String key, String value)
50          throws PortalException, SystemException {
51  
52          User user = userPersistence.findByPrimaryKey(userId);
53          Date now = new Date();
54  
55          validate(key, value);
56  
57          long propertyId = counterLocalService.increment();
58  
59          TagsProperty property = tagsPropertyPersistence.create(propertyId);
60  
61          property.setCompanyId(user.getCompanyId());
62          property.setUserId(user.getUserId());
63          property.setUserName(user.getFullName());
64          property.setCreateDate(now);
65          property.setModifiedDate(now);
66          property.setEntryId(entryId);
67          property.setKey(key);
68          property.setValue(value);
69  
70          tagsPropertyPersistence.update(property, false);
71  
72          return property;
73      }
74  
75      public TagsProperty addProperty(
76              long userId, String entryName, String key, String value)
77          throws PortalException, SystemException {
78  
79          User user = userPersistence.findByPrimaryKey(userId);
80  
81          TagsEntry entry = tagsEntryLocalService.getEntry(
82              user.getCompanyId(), entryName);
83  
84          return addProperty(userId, entry.getEntryId(), key, value);
85      }
86  
87      public void deleteProperties(long entryId) throws SystemException {
88          List<TagsProperty> properties = tagsPropertyPersistence.findByEntryId(
89              entryId);
90  
91          for (TagsProperty property : properties) {
92              deleteProperty(property);
93          }
94      }
95  
96      public void deleteProperty(long propertyId)
97          throws PortalException, SystemException {
98  
99          TagsProperty property = tagsPropertyPersistence.findByPrimaryKey(
100             propertyId);
101 
102         deleteProperty(property);
103     }
104 
105     public void deleteProperty(TagsProperty property) throws SystemException {
106         tagsPropertyPersistence.remove(property);
107     }
108 
109     public List<TagsProperty> getProperties() throws SystemException {
110         return tagsPropertyPersistence.findAll();
111     }
112 
113     public List<TagsProperty> getProperties(long entryId)
114         throws SystemException {
115 
116         return tagsPropertyPersistence.findByEntryId(entryId);
117     }
118 
119     public TagsProperty getProperty(long propertyId)
120         throws PortalException, SystemException {
121 
122         return tagsPropertyPersistence.findByPrimaryKey(propertyId);
123     }
124 
125     public TagsProperty getProperty(long entryId, String key)
126         throws PortalException, SystemException {
127 
128         return tagsPropertyPersistence.findByE_K(entryId, key);
129     }
130 
131     public String[] getPropertyKeys(long companyId) throws SystemException {
132         return tagsPropertyKeyFinder.findByCompanyId(companyId);
133     }
134 
135     public List<TagsProperty> getPropertyValues(long companyId, String key)
136         throws SystemException {
137 
138         return tagsPropertyFinder.findByC_K(companyId, key);
139     }
140 
141     public TagsProperty updateProperty(
142             long propertyId, String key, String value)
143         throws PortalException, SystemException {
144 
145         validate(key, value);
146 
147         TagsProperty property = tagsPropertyPersistence.findByPrimaryKey(
148             propertyId);
149 
150         property.setModifiedDate(new Date());
151         property.setKey(key);
152         property.setValue(value);
153 
154         tagsPropertyPersistence.update(property, false);
155 
156         return property;
157     }
158 
159     protected void validate(String key, String value) throws PortalException {
160         if (!TagsUtil.isValidWord(key)) {
161             throw new PropertyKeyException();
162         }
163 
164         if (!TagsUtil.isValidWord(value)) {
165             throw new PropertyValueException();
166         }
167     }
168 
169 }