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.kernel.json.JSONArrayWrapper;
28  import com.liferay.portal.kernel.util.GetterUtil;
29  import com.liferay.portal.kernel.util.StringPool;
30  import com.liferay.portal.kernel.util.StringUtil;
31  import com.liferay.portal.kernel.util.Validator;
32  import com.liferay.portal.model.User;
33  import com.liferay.portal.util.PortalUtil;
34  import com.liferay.portlet.tags.DuplicateEntryException;
35  import com.liferay.portlet.tags.EntryNameException;
36  import com.liferay.portlet.tags.model.TagsAsset;
37  import com.liferay.portlet.tags.model.TagsEntry;
38  import com.liferay.portlet.tags.model.TagsProperty;
39  import com.liferay.portlet.tags.service.base.TagsEntryLocalServiceBaseImpl;
40  import com.liferay.portlet.tags.util.TagsUtil;
41  import com.liferay.util.Autocomplete;
42  import com.liferay.util.CollectionFactory;
43  import com.liferay.util.ListUtil;
44  
45  import java.util.ArrayList;
46  import java.util.Date;
47  import java.util.Iterator;
48  import java.util.List;
49  import java.util.Set;
50  
51  /**
52   * <a href="TagsEntryLocalServiceImpl.java.html"><b><i>View Source</i></b></a>
53   *
54   * @author Brian Wing Shun Chan
55   *
56   */
57  public class TagsEntryLocalServiceImpl extends TagsEntryLocalServiceBaseImpl {
58  
59      public static String[] DEFAULT_PROPERTIES = new String[] {
60          "0:category:no category"
61      };
62  
63      public TagsEntry addEntry(long userId, String name)
64          throws PortalException, SystemException {
65  
66          return addEntry(userId, name, new String[0]);
67      }
68  
69      public TagsEntry addEntry(long userId, String name, String[] properties)
70          throws PortalException, SystemException {
71  
72          User user = userPersistence.findByPrimaryKey(userId);
73          Date now = new Date();
74          name = name.trim().toLowerCase();
75  
76          validate(name);
77  
78          if (hasEntry(user.getCompanyId(), name)) {
79              throw new DuplicateEntryException(
80                  "A tag entry with the name " + name + " already exists");
81          }
82  
83          long entryId = counterLocalService.increment();
84  
85          TagsEntry entry = tagsEntryPersistence.create(entryId);
86  
87          entry.setCompanyId(user.getCompanyId());
88          entry.setUserId(user.getUserId());
89          entry.setUserName(user.getFullName());
90          entry.setCreateDate(now);
91          entry.setModifiedDate(now);
92          entry.setName(name);
93  
94          tagsEntryPersistence.update(entry);
95  
96          for (int i = 0; i < properties.length; i++) {
97              String[] property = StringUtil.split(
98                  properties[i], StringPool.COLON);
99  
100             String key = StringPool.BLANK;
101 
102             if (property.length > 1) {
103                 key = GetterUtil.getString(property[1]);
104             }
105 
106             String value = StringPool.BLANK;
107 
108             if (property.length > 2) {
109                 value = GetterUtil.getString(property[2]);
110             }
111 
112             if (Validator.isNotNull(key)) {
113                 tagsPropertyLocalService.addProperty(
114                     userId, entryId, key, value);
115             }
116         }
117 
118         return entry;
119 
120     }
121 
122     public void checkEntries(long userId, String[] names)
123         throws PortalException, SystemException {
124 
125         User user = userPersistence.findByPrimaryKey(userId);
126 
127         for (int i = 0; i < names.length; i++) {
128             String name = names[i].trim().toLowerCase();
129 
130             TagsEntry entry = tagsEntryPersistence.fetchByC_N(
131                 user.getCompanyId(), name);
132 
133             if (entry == null) {
134                 addEntry(userId, names[i], DEFAULT_PROPERTIES);
135             }
136         }
137     }
138 
139     public void deleteEntry(long entryId)
140         throws PortalException, SystemException {
141 
142         TagsEntry entry = tagsEntryPersistence.findByPrimaryKey(entryId);
143 
144         deleteEntry(entry);
145     }
146 
147     public void deleteEntry(TagsEntry entry)
148         throws PortalException, SystemException {
149 
150         // Properties
151 
152         tagsPropertyLocalService.deleteProperties(entry.getEntryId());
153 
154         // Entry
155 
156         tagsEntryPersistence.remove(entry.getEntryId());
157     }
158 
159     public boolean hasEntry(long companyId, String name)
160         throws PortalException, SystemException {
161 
162         if (tagsEntryPersistence.fetchByC_N(companyId, name) == null) {
163             return false;
164         }
165         else {
166             return true;
167         }
168     }
169 
170     public List getAssetEntries(long assetId)
171         throws PortalException, SystemException {
172 
173         return tagsAssetPersistence.getTagsEntries(assetId);
174     }
175 
176     public List getEntries() throws SystemException {
177         return tagsEntryPersistence.findAll();
178     }
179 
180     public List getEntries(String className, long classPK)
181         throws PortalException, SystemException {
182 
183         long classNameId = PortalUtil.getClassNameId(className);
184 
185         return getEntries(classNameId, classPK);
186     }
187 
188     public List getEntries(long classNameId, long classPK)
189         throws PortalException, SystemException {
190 
191         TagsAsset asset = tagsAssetPersistence.fetchByC_C(classNameId, classPK);
192 
193         if (asset == null) {
194             return new ArrayList();
195         }
196         else {
197             return tagsAssetPersistence.getTagsEntries(asset.getAssetId());
198         }
199     }
200 
201     public TagsEntry getEntry(long entryId)
202         throws PortalException, SystemException {
203 
204         return tagsEntryPersistence.findByPrimaryKey(entryId);
205     }
206 
207     public TagsEntry getEntry(long companyId, String name)
208         throws PortalException, SystemException {
209 
210         return tagsEntryPersistence.findByC_N(companyId, name);
211     }
212 
213     public long[] getEntryIds(long companyId, String[] names)
214         throws PortalException, SystemException {
215 
216         List list = new ArrayList(names.length);
217 
218         for (int i = 0; i < names.length; i++) {
219             String name = names[i];
220 
221             TagsEntry entry = tagsEntryPersistence.fetchByC_N(companyId, name);
222 
223             if (entry != null) {
224                 list.add(entry);
225             }
226         }
227 
228         long[] entryIds = new long[list.size()];
229 
230         for (int i = 0; i < list.size(); i++) {
231             TagsEntry entry = (TagsEntry)list.get(i);
232 
233             entryIds[i] = entry.getEntryId();
234         }
235 
236         return entryIds;
237     }
238 
239     public String[] getEntryNames() throws SystemException {
240         return getEntryNames(getEntries());
241     }
242 
243     public String[] getEntryNames(String className, long classPK)
244         throws PortalException, SystemException {
245 
246         return getEntryNames(getEntries(className, classPK));
247     }
248 
249     public String[] getEntryNames(long classNameId, long classPK)
250         throws PortalException, SystemException {
251 
252         return getEntryNames(getEntries(classNameId, classPK));
253     }
254 
255     public List search(long companyId, String name, String[] properties)
256         throws SystemException {
257 
258         return tagsEntryFinder.findByC_N_P(companyId, name, properties);
259     }
260 
261     public List search(
262             long companyId, String name, String[] properties, int begin,
263             int end)
264         throws SystemException {
265 
266         return tagsEntryFinder.findByC_N_P(
267             companyId, name, properties, begin, end);
268     }
269 
270     public JSONArrayWrapper searchAutocomplete(
271             long companyId, String name, String[] properties, int begin,
272             int end)
273         throws SystemException {
274 
275         List list = tagsEntryFinder.findByC_N_P(
276             companyId, name, properties, begin, end);
277 
278         return new JSONArrayWrapper(
279             Autocomplete.listToJson(list, "name", "name"));
280     }
281 
282     public int searchCount(long companyId, String name, String[] properties)
283         throws SystemException {
284 
285         return tagsEntryFinder.countByC_N_P(companyId, name, properties);
286     }
287 
288     public TagsEntry updateEntry(long entryId, String name)
289         throws PortalException, SystemException {
290 
291         name = name.trim().toLowerCase();
292 
293         validate(name);
294 
295         TagsEntry entry = tagsEntryPersistence.findByPrimaryKey(entryId);
296 
297         if (!entry.getName().equals(name)) {
298             if (hasEntry(entry.getCompanyId(), name)) {
299                 throw new DuplicateEntryException();
300             }
301         }
302 
303         entry.setModifiedDate(new Date());
304         entry.setName(name);
305 
306         tagsEntryPersistence.update(entry);
307 
308         return entry;
309     }
310 
311     public TagsEntry updateEntry(
312             long userId, long entryId, String name, String[] properties)
313         throws PortalException, SystemException {
314 
315         TagsEntry entry = updateEntry(entryId, name);
316 
317         List curProperties = tagsPropertyPersistence.findByEntryId(entryId);
318         Set keepProperties = CollectionFactory.getHashSet();
319 
320         for (int i = 0; i < properties.length; i++) {
321             String[] property = StringUtil.split(
322                 properties[i], StringPool.COLON);
323 
324             Long propertyId = new Long(0);
325 
326             if (property.length > 0) {
327                 propertyId = new Long(GetterUtil.getLong(property[0]));
328             }
329 
330             String key = StringPool.BLANK;
331 
332             if (property.length > 1) {
333                 key = GetterUtil.getString(property[1]);
334             }
335 
336             String value = StringPool.BLANK;
337 
338             if (property.length > 2) {
339                 value = GetterUtil.getString(property[2]);
340             }
341 
342             if (propertyId.longValue() == 0) {
343                 if (Validator.isNotNull(key)) {
344                     tagsPropertyLocalService.addProperty(
345                         userId, entryId, key, value);
346                 }
347             }
348             else {
349                 if (Validator.isNull(key)) {
350                     tagsPropertyLocalService.deleteProperty(
351                         propertyId.longValue());
352                 }
353                 else {
354                     tagsPropertyLocalService.updateProperty(
355                         propertyId.longValue(), key, value);
356 
357                     keepProperties.add(new Long(propertyId.longValue()));
358                 }
359             }
360         }
361 
362         Iterator itr = curProperties.iterator();
363 
364         while (itr.hasNext()) {
365             TagsProperty property = (TagsProperty)itr.next();
366 
367             if (!keepProperties.contains(new Long(property.getPropertyId()))) {
368                 tagsPropertyLocalService.deleteProperty(property);
369             }
370         }
371 
372         return entry;
373     }
374 
375     protected String[] getEntryNames(List entries) {
376         return StringUtil.split(ListUtil.toString(entries, "name"));
377     }
378 
379     protected void validate(String name)
380         throws PortalException, SystemException {
381 
382         if (!TagsUtil.isValidWord(name)) {
383             throw new EntryNameException();
384         }
385     }
386 
387 }