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