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