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.TagsProperty;
31  import com.liferay.portlet.tags.service.base.TagsPropertyLocalServiceBaseImpl;
32  import com.liferay.portlet.tags.util.TagsUtil;
33  
34  import java.util.Date;
35  import java.util.List;
36  
37  /**
38   * <a href="TagsPropertyLocalServiceImpl.java.html"><b><i>View Source</i></b>
39   * </a>
40   *
41   * @author Brian Wing Shun Chan
42   *
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 void deleteProperties(long entryId) throws SystemException {
75          List<TagsProperty> properties = tagsPropertyPersistence.findByEntryId(
76              entryId);
77  
78          for (TagsProperty property : properties) {
79              deleteProperty(property);
80          }
81      }
82  
83      public void deleteProperty(long propertyId)
84          throws PortalException, SystemException {
85  
86          TagsProperty property = tagsPropertyPersistence.findByPrimaryKey(
87              propertyId);
88  
89          deleteProperty(property);
90      }
91  
92      public void deleteProperty(TagsProperty property) throws SystemException {
93          tagsPropertyPersistence.remove(property);
94      }
95  
96      public List<TagsProperty> getProperties() throws SystemException {
97          return tagsPropertyPersistence.findAll();
98      }
99  
100     public List<TagsProperty> getProperties(long entryId)
101         throws SystemException {
102 
103         return tagsPropertyPersistence.findByEntryId(entryId);
104     }
105 
106     public TagsProperty getProperty(long propertyId)
107         throws PortalException, SystemException {
108 
109         return tagsPropertyPersistence.findByPrimaryKey(propertyId);
110     }
111 
112     public TagsProperty getProperty(long entryId, String key)
113         throws PortalException, SystemException {
114 
115         return tagsPropertyPersistence.findByE_K(entryId, key);
116     }
117 
118     public String[] getPropertyKeys(long groupId) throws SystemException {
119         return tagsPropertyKeyFinder.findByGroupId(groupId);
120     }
121 
122     public List<TagsProperty> getPropertyValues(long groupId, String key)
123         throws SystemException {
124 
125         return tagsPropertyFinder.findByG_K(groupId, key);
126     }
127 
128     public TagsProperty updateProperty(
129             long propertyId, String key, String value)
130         throws PortalException, SystemException {
131 
132         validate(key, value);
133 
134         TagsProperty property = tagsPropertyPersistence.findByPrimaryKey(
135             propertyId);
136 
137         property.setModifiedDate(new Date());
138         property.setKey(key);
139         property.setValue(value);
140 
141         tagsPropertyPersistence.update(property, false);
142 
143         return property;
144     }
145 
146     protected void validate(String key, String value) throws PortalException {
147         if (!TagsUtil.isValidWord(key)) {
148             throw new PropertyKeyException();
149         }
150 
151         if (!TagsUtil.isValidWord(value)) {
152             throw new PropertyValueException();
153         }
154     }
155 
156 }