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