1   /**
2    * Copyright (c) 2000-2008 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)
88          throws PortalException, SystemException {
89  
90          List<TagsProperty> properties = tagsPropertyPersistence.findByEntryId(
91              entryId);
92  
93          for (TagsProperty property : properties) {
94              deleteProperty(property);
95          }
96      }
97  
98      public void deleteProperty(long propertyId)
99          throws PortalException, SystemException {
100 
101         TagsProperty property = tagsPropertyPersistence.findByPrimaryKey(
102             propertyId);
103 
104         deleteProperty(property);
105     }
106 
107     public void deleteProperty(TagsProperty property)
108         throws PortalException, SystemException {
109 
110         tagsPropertyPersistence.remove(property.getPropertyId());
111     }
112 
113     public List<TagsProperty> getProperties() throws SystemException {
114         return tagsPropertyPersistence.findAll();
115     }
116 
117     public List<TagsProperty> getProperties(long entryId)
118         throws SystemException {
119 
120         return tagsPropertyPersistence.findByEntryId(entryId);
121     }
122 
123     public TagsProperty getProperty(long propertyId)
124         throws PortalException, SystemException {
125 
126         return tagsPropertyPersistence.findByPrimaryKey(propertyId);
127     }
128 
129     public TagsProperty getProperty(long entryId, String key)
130         throws PortalException, SystemException {
131 
132         return tagsPropertyPersistence.findByE_K(entryId, key);
133     }
134 
135     public String[] getPropertyKeys(long companyId) throws SystemException {
136         return tagsPropertyKeyFinder.findByCompanyId(companyId);
137     }
138 
139     public List<TagsProperty> getPropertyValues(long companyId, String key)
140         throws SystemException {
141 
142         return tagsPropertyFinder.findByC_K(companyId, key);
143     }
144 
145     public TagsProperty updateProperty(
146             long propertyId, String key, String value)
147         throws PortalException, SystemException {
148 
149         validate(key, value);
150 
151         TagsProperty property = tagsPropertyPersistence.findByPrimaryKey(
152             propertyId);
153 
154         property.setModifiedDate(new Date());
155         property.setKey(key);
156         property.setValue(value);
157 
158         tagsPropertyPersistence.update(property, false);
159 
160         return property;
161     }
162 
163     protected void validate(String key, String value)
164         throws PortalException, SystemException {
165 
166         if (!TagsUtil.isValidWord(key)) {
167             throw new PropertyKeyException();
168         }
169 
170         if (!TagsUtil.isValidWord(value)) {
171             throw new PropertyValueException();
172         }
173     }
174 
175 }