1   /**
2    * Copyright (c) 2000-2009 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   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portlet.tags.service.impl;
21  
22  import com.liferay.portal.PortalException;
23  import com.liferay.portal.SystemException;
24  import com.liferay.portal.kernel.json.JSONArray;
25  import com.liferay.portal.kernel.util.GetterUtil;
26  import com.liferay.portal.kernel.util.ListUtil;
27  import com.liferay.portal.kernel.util.StringPool;
28  import com.liferay.portal.kernel.util.StringUtil;
29  import com.liferay.portal.kernel.util.Validator;
30  import com.liferay.portal.model.User;
31  import com.liferay.portal.util.PortalUtil;
32  import com.liferay.portlet.tags.DuplicateEntryException;
33  import com.liferay.portlet.tags.TagsEntryException;
34  import com.liferay.portlet.tags.model.TagsAsset;
35  import com.liferay.portlet.tags.model.TagsEntry;
36  import com.liferay.portlet.tags.model.TagsProperty;
37  import com.liferay.portlet.tags.service.base.TagsEntryLocalServiceBaseImpl;
38  import com.liferay.portlet.tags.util.TagsUtil;
39  import com.liferay.util.Autocomplete;
40  
41  import java.util.ArrayList;
42  import java.util.Date;
43  import java.util.HashSet;
44  import java.util.List;
45  import java.util.Set;
46  
47  /**
48   * <a href="TagsEntryLocalServiceImpl.java.html"><b><i>View Source</i></b></a>
49   *
50   * @author Brian Wing Shun Chan
51   *
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         // Properties
147 
148         tagsPropertyLocalService.deleteProperties(entry.getEntryId());
149 
150         // Entry
151 
152         tagsEntryPersistence.remove(entry.getEntryId());
153     }
154 
155     public boolean hasEntry(long companyId, String name)
156         throws SystemException {
157 
158         if (tagsEntryPersistence.fetchByC_N(companyId, name) == null) {
159             return false;
160         }
161         else {
162             return true;
163         }
164     }
165 
166     public List<TagsEntry> getAssetEntries(long assetId)
167         throws SystemException {
168 
169         return tagsAssetPersistence.getTagsEntries(assetId);
170     }
171 
172     public List<TagsEntry> getEntries() throws SystemException {
173         return tagsEntryPersistence.findAll();
174     }
175 
176     public List<TagsEntry> getEntries(String className, long classPK)
177         throws SystemException {
178 
179         long classNameId = PortalUtil.getClassNameId(className);
180 
181         return getEntries(classNameId, classPK);
182     }
183 
184     public List<TagsEntry> getEntries(long classNameId, long classPK)
185         throws SystemException {
186 
187         TagsAsset asset = tagsAssetPersistence.fetchByC_C(classNameId, classPK);
188 
189         if (asset == null) {
190             return new ArrayList<TagsEntry>();
191         }
192         else {
193             return tagsAssetPersistence.getTagsEntries(asset.getAssetId());
194         }
195     }
196 
197     public List<TagsEntry> getEntries(
198             long groupId, long companyId, long classNameId, String name)
199         throws SystemException {
200 
201         return tagsEntryFinder.findByG_C_C_N(
202             groupId, companyId, classNameId, name);
203     }
204 
205     public List<TagsEntry> getEntries(
206             long groupId, long companyId, long classNameId, String name,
207             int start, int end)
208         throws SystemException {
209 
210         return tagsEntryFinder.findByG_C_C_N(
211             groupId, companyId, classNameId, name, start, end);
212     }
213 
214     public int getEntriesSize(
215             long groupId, long companyId, long classNameId, String name)
216         throws SystemException {
217 
218         return tagsEntryFinder.countByG_C_C_N(
219             groupId, companyId, classNameId, name);
220     }
221 
222     public TagsEntry getEntry(long entryId)
223         throws PortalException, SystemException {
224 
225         return tagsEntryPersistence.findByPrimaryKey(entryId);
226     }
227 
228     public TagsEntry getEntry(long companyId, String name)
229         throws PortalException, SystemException {
230 
231         return tagsEntryPersistence.findByC_N(companyId, name);
232     }
233 
234     public long[] getEntryIds(long companyId, String[] names)
235         throws SystemException {
236 
237         List<TagsEntry> list = new ArrayList<TagsEntry>(names.length);
238 
239         for (String name : names) {
240             TagsEntry entry = tagsEntryPersistence.fetchByC_N(companyId, name);
241 
242             if (entry != null) {
243                 list.add(entry);
244             }
245         }
246 
247         long[] entryIds = new long[list.size()];
248 
249         for (int i = 0; i < list.size(); i++) {
250             TagsEntry entry = list.get(i);
251 
252             entryIds[i] = entry.getEntryId();
253         }
254 
255         return entryIds;
256     }
257 
258     public String[] getEntryNames() throws SystemException {
259         return getEntryNames(getEntries());
260     }
261 
262     public String[] getEntryNames(String className, long classPK)
263         throws SystemException {
264 
265         return getEntryNames(getEntries(className, classPK));
266     }
267 
268     public String[] getEntryNames(long classNameId, long classPK)
269         throws SystemException {
270 
271         return getEntryNames(getEntries(classNameId, classPK));
272     }
273 
274     public void mergeEntries(long fromEntryId, long toEntryId)
275         throws PortalException, SystemException {
276 
277         List<TagsAsset> assets = tagsEntryPersistence.getTagsAssets(
278             fromEntryId);
279 
280         tagsEntryPersistence.addTagsAssets(toEntryId, assets);
281 
282         List<TagsProperty> properties = tagsPropertyPersistence.findByEntryId(
283             fromEntryId);
284 
285         for (TagsProperty fromProperty : properties) {
286             TagsProperty toProperty = tagsPropertyPersistence.fetchByE_K(
287                 toEntryId, fromProperty.getKey());
288 
289             if (toProperty == null) {
290                 fromProperty.setEntryId(toEntryId);
291 
292                 tagsPropertyPersistence.update(fromProperty, false);
293             }
294         }
295 
296         deleteEntry(fromEntryId);
297     }
298 
299     public List<TagsEntry> search(
300             long companyId, String name, String[] properties)
301         throws SystemException {
302 
303         return tagsEntryFinder.findByC_N_P(companyId, name, properties);
304     }
305 
306     public List<TagsEntry> search(
307             long companyId, String name, String[] properties, int start,
308             int end)
309         throws SystemException {
310 
311         return tagsEntryFinder.findByC_N_P(
312             companyId, name, properties, start, end);
313     }
314 
315     public JSONArray searchAutocomplete(
316             long companyId, String name, String[] properties, int start,
317             int end)
318         throws SystemException {
319 
320         List<TagsEntry> list = tagsEntryFinder.findByC_N_P(
321             companyId, name, properties, start, end);
322 
323         return Autocomplete.listToJson(list, "name", "name");
324     }
325 
326     public int searchCount(long companyId, String name, String[] properties)
327         throws SystemException {
328 
329         return tagsEntryFinder.countByC_N_P(companyId, name, properties);
330     }
331 
332     public TagsEntry updateEntry(long entryId, String name)
333         throws PortalException, SystemException {
334 
335         name = name.trim().toLowerCase();
336 
337         validate(name);
338 
339         TagsEntry entry = tagsEntryPersistence.findByPrimaryKey(entryId);
340 
341         if (!entry.getName().equals(name)) {
342             if (hasEntry(entry.getCompanyId(), name)) {
343                 throw new DuplicateEntryException();
344             }
345         }
346 
347         entry.setModifiedDate(new Date());
348         entry.setName(name);
349 
350         tagsEntryPersistence.update(entry, false);
351 
352         return entry;
353     }
354 
355     public TagsEntry updateEntry(
356             long userId, long entryId, String name, String[] properties)
357         throws PortalException, SystemException {
358 
359         TagsEntry entry = updateEntry(entryId, name);
360 
361         Set<Long> newProperties = new HashSet<Long>();
362 
363         List<TagsProperty> oldProperties =
364             tagsPropertyPersistence.findByEntryId(entryId);
365 
366         for (int i = 0; i < properties.length; i++) {
367             String[] property = StringUtil.split(
368                 properties[i], StringPool.COLON);
369 
370             long propertyId = 0;
371 
372             if (property.length > 0) {
373                 propertyId = GetterUtil.getLong(property[0]);
374             }
375 
376             String key = StringPool.BLANK;
377 
378             if (property.length > 1) {
379                 key = GetterUtil.getString(property[1]);
380             }
381 
382             String value = StringPool.BLANK;
383 
384             if (property.length > 2) {
385                 value = GetterUtil.getString(property[2]);
386             }
387 
388             if (propertyId == 0) {
389                 if (Validator.isNotNull(key)) {
390                     tagsPropertyLocalService.addProperty(
391                         userId, entryId, key, value);
392                 }
393             }
394             else {
395                 if (Validator.isNull(key)) {
396                     tagsPropertyLocalService.deleteProperty(propertyId);
397                 }
398                 else {
399                     tagsPropertyLocalService.updateProperty(
400                         propertyId, key, value);
401 
402                     newProperties.add(propertyId);
403                 }
404             }
405         }
406 
407         for (TagsProperty property : oldProperties) {
408             if (!newProperties.contains(property.getPropertyId())) {
409                 tagsPropertyLocalService.deleteProperty(property);
410             }
411         }
412 
413         return entry;
414     }
415 
416     protected String[] getEntryNames(List <TagsEntry>entries) {
417         return StringUtil.split(ListUtil.toString(entries, "name"));
418     }
419 
420     protected void validate(String name) throws PortalException {
421         if (!TagsUtil.isValidWord(name)) {
422             throw new TagsEntryException(TagsEntryException.INVALID_CHARACTER);
423         }
424     }
425 
426 }