1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   * 
13   */
14  
15  package com.liferay.portlet.tags.service.impl;
16  
17  import com.liferay.portal.PortalException;
18  import com.liferay.portal.SystemException;
19  import com.liferay.portal.kernel.json.JSONArray;
20  import com.liferay.portal.kernel.util.GetterUtil;
21  import com.liferay.portal.kernel.util.ListUtil;
22  import com.liferay.portal.kernel.util.StringPool;
23  import com.liferay.portal.kernel.util.StringUtil;
24  import com.liferay.portal.kernel.util.Validator;
25  import com.liferay.portal.model.User;
26  import com.liferay.portal.util.PortalUtil;
27  import com.liferay.portlet.blogs.model.BlogsEntry;
28  import com.liferay.portlet.bookmarks.model.BookmarksEntry;
29  import com.liferay.portlet.documentlibrary.model.DLFileEntry;
30  import com.liferay.portlet.imagegallery.model.IGImage;
31  import com.liferay.portlet.journal.model.JournalArticle;
32  import com.liferay.portlet.messageboards.model.MBMessage;
33  import com.liferay.portlet.softwarecatalog.model.SCProductEntry;
34  import com.liferay.portlet.tags.DuplicateEntryException;
35  import com.liferay.portlet.tags.TagsEntryException;
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.portlet.wiki.model.WikiPage;
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  public class TagsEntryLocalServiceImpl extends TagsEntryLocalServiceBaseImpl {
54  
55      public static String[] DEFAULT_PROPERTIES = new String[] {
56          "0:category:no category"
57      };
58  
59      public TagsEntry addEntry(long userId, String name)
60          throws PortalException, SystemException {
61  
62          return addEntry(userId, name, new String[0]);
63      }
64  
65      public TagsEntry addEntry(long userId, String name, String[] properties)
66          throws PortalException, SystemException {
67  
68          User user = userPersistence.findByPrimaryKey(userId);
69          Date now = new Date();
70          name = name.trim().toLowerCase();
71  
72          validate(name);
73  
74          if (hasEntry(user.getCompanyId(), name)) {
75              throw new DuplicateEntryException(
76                  "A tag entry with the name " + name + " already exists");
77          }
78  
79          long entryId = counterLocalService.increment();
80  
81          TagsEntry entry = tagsEntryPersistence.create(entryId);
82  
83          entry.setCompanyId(user.getCompanyId());
84          entry.setUserId(user.getUserId());
85          entry.setUserName(user.getFullName());
86          entry.setCreateDate(now);
87          entry.setModifiedDate(now);
88          entry.setName(name);
89  
90          tagsEntryPersistence.update(entry, false);
91  
92          for (int i = 0; i < properties.length; i++) {
93              String[] property = StringUtil.split(
94                  properties[i], StringPool.COLON);
95  
96              String key = StringPool.BLANK;
97  
98              if (property.length > 1) {
99                  key = GetterUtil.getString(property[1]);
100             }
101 
102             String value = StringPool.BLANK;
103 
104             if (property.length > 2) {
105                 value = GetterUtil.getString(property[2]);
106             }
107 
108             if (Validator.isNotNull(key)) {
109                 tagsPropertyLocalService.addProperty(
110                     userId, entryId, key, value);
111             }
112         }
113 
114         return entry;
115 
116     }
117 
118     public void checkEntries(long userId, String[] names)
119         throws PortalException, SystemException {
120 
121         User user = userPersistence.findByPrimaryKey(userId);
122 
123         for (int i = 0; i < names.length; i++) {
124             String name = names[i].trim().toLowerCase();
125 
126             TagsEntry entry = tagsEntryPersistence.fetchByC_N(
127                 user.getCompanyId(), name);
128 
129             if (entry == null) {
130                 addEntry(userId, names[i], DEFAULT_PROPERTIES);
131             }
132         }
133     }
134 
135     public void deleteEntry(long entryId)
136         throws PortalException, SystemException {
137 
138         TagsEntry entry = tagsEntryPersistence.findByPrimaryKey(entryId);
139 
140         deleteEntry(entry);
141     }
142 
143     public void deleteEntry(TagsEntry entry)
144         throws PortalException, SystemException {
145 
146         // Assets
147 
148         List<TagsAsset> assets =  tagsEntryPersistence.getTagsAssets(
149             entry.getEntryId());
150 
151         // Entry
152 
153         tagsEntryPersistence.remove(entry.getEntryId());
154 
155         // Properties
156 
157         tagsPropertyLocalService.deleteProperties(entry.getEntryId());
158 
159         // Indexer
160 
161         reIndex(assets);
162     }
163 
164     public List<TagsEntry> getAssetEntries(long assetId)
165         throws SystemException {
166 
167         return tagsAssetPersistence.getTagsEntries(assetId);
168     }
169 
170     public List<TagsEntry> getEntries() throws SystemException {
171         return tagsEntryPersistence.findAll();
172     }
173 
174     public List<TagsEntry> getEntries(long classNameId, long classPK)
175         throws SystemException {
176 
177         return tagsEntryFinder.findByC_C(classNameId, classPK);
178     }
179 
180     public List<TagsEntry> getEntries(
181             long groupId, long companyId, long classNameId, String name)
182         throws SystemException {
183 
184         return tagsEntryFinder.findByG_C_C_N(
185             groupId, companyId, classNameId, name);
186     }
187 
188     public List<TagsEntry> getEntries(
189             long groupId, long companyId, long classNameId, String name,
190             int start, int end)
191         throws SystemException {
192 
193         return tagsEntryFinder.findByG_C_C_N(
194             groupId, companyId, classNameId, name, start, end);
195     }
196 
197     public List<TagsEntry> getEntries(String className, long classPK)
198         throws SystemException {
199 
200         long classNameId = PortalUtil.getClassNameId(className);
201 
202         return getEntries(classNameId, classPK);
203     }
204 
205     public int getEntriesSize(
206             long groupId, long companyId, long classNameId, String name)
207         throws SystemException {
208 
209         return tagsEntryFinder.countByG_C_C_N(
210             groupId, companyId, classNameId, name);
211     }
212 
213     public TagsEntry getEntry(long entryId)
214         throws PortalException, SystemException {
215 
216         return tagsEntryPersistence.findByPrimaryKey(entryId);
217     }
218 
219     public TagsEntry getEntry(long companyId, String name)
220         throws PortalException, SystemException {
221 
222         return tagsEntryPersistence.findByC_N(companyId, name);
223     }
224 
225     public long[] getEntryIds(long companyId, String[] names)
226         throws SystemException {
227 
228         List<TagsEntry> list = new ArrayList<TagsEntry>(names.length);
229 
230         for (String name : names) {
231             TagsEntry entry = tagsEntryPersistence.fetchByC_N(companyId, name);
232 
233             if (entry != null) {
234                 list.add(entry);
235             }
236         }
237 
238         long[] entryIds = new long[list.size()];
239 
240         for (int i = 0; i < list.size(); i++) {
241             TagsEntry entry = list.get(i);
242 
243             entryIds[i] = entry.getEntryId();
244         }
245 
246         return entryIds;
247     }
248 
249     public String[] getEntryNames() throws SystemException {
250         return getEntryNames(getEntries());
251     }
252 
253     public String[] getEntryNames(long classNameId, long classPK)
254         throws SystemException {
255 
256         return getEntryNames(getEntries(classNameId, classPK));
257     }
258 
259     public String[] getEntryNames(String className, long classPK)
260         throws SystemException {
261 
262         return getEntryNames(getEntries(className, classPK));
263     }
264 
265     public boolean hasEntry(long companyId, String name)
266         throws SystemException {
267 
268         if (tagsEntryPersistence.fetchByC_N(companyId, name) == null) {
269             return false;
270         }
271         else {
272             return true;
273         }
274     }
275 
276     public void mergeEntries(long fromEntryId, long toEntryId)
277         throws PortalException, SystemException {
278 
279         List<TagsAsset> assets = tagsEntryPersistence.getTagsAssets(
280             fromEntryId);
281 
282         tagsEntryPersistence.addTagsAssets(toEntryId, assets);
283 
284         List<TagsProperty> properties = tagsPropertyPersistence.findByEntryId(
285             fromEntryId);
286 
287         for (TagsProperty fromProperty : properties) {
288             TagsProperty toProperty = tagsPropertyPersistence.fetchByE_K(
289                 toEntryId, fromProperty.getKey());
290 
291             if (toProperty == null) {
292                 fromProperty.setEntryId(toEntryId);
293 
294                 tagsPropertyPersistence.update(fromProperty, false);
295             }
296         }
297 
298         deleteEntry(fromEntryId);
299     }
300 
301     public List<TagsEntry> search(
302             long companyId, String name, String[] properties)
303         throws SystemException {
304 
305         return tagsEntryFinder.findByC_N_P(companyId, name, properties);
306     }
307 
308     public List<TagsEntry> search(
309             long companyId, String name, String[] properties, int start,
310             int end)
311         throws SystemException {
312 
313         return tagsEntryFinder.findByC_N_P(
314             companyId, name, properties, start, end);
315     }
316 
317     public JSONArray searchAutocomplete(
318             long companyId, String name, String[] properties, int start,
319             int end)
320         throws SystemException {
321 
322         List<TagsEntry> list = tagsEntryFinder.findByC_N_P(
323             companyId, name, properties, start, end);
324 
325         return Autocomplete.listToJson(list, "name", "name");
326     }
327 
328     public int searchCount(long companyId, String name, String[] properties)
329         throws SystemException {
330 
331         return tagsEntryFinder.countByC_N_P(companyId, name, properties);
332     }
333 
334     public TagsEntry updateEntry(
335             long userId, long entryId, String name, String[] properties)
336         throws PortalException, SystemException {
337 
338         TagsEntry entry = updateEntry(entryId, name);
339 
340         List<TagsProperty> oldProperties =
341             tagsPropertyPersistence.findByEntryId(entryId);
342 
343         for (TagsProperty property : oldProperties) {
344             tagsPropertyLocalService.deleteProperty(property);
345         }
346 
347         for (int i = 0; i < properties.length; i++) {
348             String[] property = StringUtil.split(
349                 properties[i], StringPool.COLON);
350 
351             String key = StringPool.BLANK;
352 
353             if (property.length > 1) {
354                 key = GetterUtil.getString(property[1]);
355             }
356 
357             String value = StringPool.BLANK;
358 
359             if (property.length > 2) {
360                 value = GetterUtil.getString(property[2]);
361             }
362 
363             if (Validator.isNotNull(key)) {
364                 tagsPropertyLocalService.addProperty(
365                     userId, entryId, key, value);
366             }
367         }
368 
369         return entry;
370     }
371 
372     public TagsEntry updateEntry(long entryId, String name)
373         throws PortalException, SystemException {
374 
375         name = name.trim().toLowerCase();
376 
377         validate(name);
378 
379         TagsEntry entry = tagsEntryPersistence.findByPrimaryKey(entryId);
380 
381         String oldName = entry.getName();
382 
383         if (!entry.getName().equals(name)) {
384             if (hasEntry(entry.getCompanyId(), name)) {
385                 throw new DuplicateEntryException();
386             }
387         }
388 
389         entry.setModifiedDate(new Date());
390         entry.setName(name);
391 
392         tagsEntryPersistence.update(entry, false);
393 
394         // Indexer
395 
396         if (!oldName.equals(name)) {
397             List<TagsAsset> assets =  tagsEntryPersistence.getTagsAssets(
398                 entry.getEntryId());
399 
400             reIndex(assets);
401         }
402 
403         return entry;
404     }
405 
406     protected String[] getEntryNames(List <TagsEntry>entries) {
407         return StringUtil.split(ListUtil.toString(entries, "name"));
408     }
409 
410     protected void reIndex(List<TagsAsset> assets) throws SystemException {
411         for (TagsAsset asset : assets) {
412             String className = PortalUtil.getClassName(asset.getClassNameId());
413 
414             if (className.equals(BlogsEntry.class.getName())) {
415                 blogsEntryLocalService.reIndex(asset.getClassPK());
416             }
417             else if (className.equals(BookmarksEntry.class.getName())) {
418                 bookmarksEntryLocalService.reIndex(asset.getClassPK());
419             }
420             else if (className.equals(DLFileEntry.class.getName())) {
421                 dlFolderLocalService.reIndex(
422                     new String[] {String.valueOf(asset.getCompanyId())});
423             }
424             else if (className.equals(IGImage.class.getName())) {
425                 igImageLocalService.reIndex(asset.getClassPK());
426             }
427             else if (className.equals(JournalArticle.class.getName())) {
428                 journalArticleLocalService.reIndex(asset.getClassPK());
429             }
430             else if (className.equals(MBMessage.class.getName())) {
431                 mbMessageLocalService.reIndex(asset.getClassPK());
432             }
433             else if (className.equals(SCProductEntry.class.getName())) {
434                 scProductEntryLocalService.reIndex(asset.getClassPK());
435             }
436             else if (className.equals(WikiPage.class.getName())) {
437                 wikiPageLocalService.reIndex(asset.getClassPK());
438             }
439         }
440     }
441 
442     protected void validate(String name) throws PortalException {
443         if (!TagsUtil.isValidWord(name)) {
444             throw new TagsEntryException(TagsEntryException.INVALID_CHARACTER);
445         }
446     }
447 
448 }