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