1
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.ListUtil;
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
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
151 tagsPropertyLocalService.deleteProperties(entry.getEntryId());
152
153
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 PortalException, 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 PortalException, 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 PortalException, 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 begin, int end)
211 throws SystemException {
212
213 return tagsEntryFinder.findByG_C_C_N(
214 groupId, companyId, classNameId, name, begin, 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 PortalException, 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 PortalException, SystemException {
267
268 return getEntryNames(getEntries(className, classPK));
269 }
270
271 public String[] getEntryNames(long classNameId, long classPK)
272 throws PortalException, 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 begin,
311 int end)
312 throws SystemException {
313
314 return tagsEntryFinder.findByC_N_P(
315 companyId, name, properties, begin, end);
316 }
317
318 public JSONArrayWrapper searchAutocomplete(
319 long companyId, String name, String[] properties, int begin,
320 int end)
321 throws SystemException {
322
323 List<TagsEntry> list = tagsEntryFinder.findByC_N_P(
324 companyId, name, properties, begin, end);
325
326 return new JSONArrayWrapper(
327 Autocomplete.listToJson(list, "name", "name"));
328 }
329
330 public int searchCount(long companyId, String name, String[] properties)
331 throws SystemException {
332
333 return tagsEntryFinder.countByC_N_P(companyId, name, properties);
334 }
335
336 public TagsEntry updateEntry(long entryId, String name)
337 throws PortalException, SystemException {
338
339 name = name.trim().toLowerCase();
340
341 validate(name);
342
343 TagsEntry entry = tagsEntryPersistence.findByPrimaryKey(entryId);
344
345 if (!entry.getName().equals(name)) {
346 if (hasEntry(entry.getCompanyId(), name)) {
347 throw new DuplicateEntryException();
348 }
349 }
350
351 entry.setModifiedDate(new Date());
352 entry.setName(name);
353
354 tagsEntryPersistence.update(entry, false);
355
356 return entry;
357 }
358
359 public TagsEntry updateEntry(
360 long userId, long entryId, String name, String[] properties)
361 throws PortalException, SystemException {
362
363 TagsEntry entry = updateEntry(entryId, name);
364
365 Set<Long> newProperties = new HashSet<Long>();
366
367 List<TagsProperty> oldProperties =
368 tagsPropertyPersistence.findByEntryId(entryId);
369
370 for (int i = 0; i < properties.length; i++) {
371 String[] property = StringUtil.split(
372 properties[i], StringPool.COLON);
373
374 long propertyId = 0;
375
376 if (property.length > 0) {
377 propertyId = GetterUtil.getLong(property[0]);
378 }
379
380 String key = StringPool.BLANK;
381
382 if (property.length > 1) {
383 key = GetterUtil.getString(property[1]);
384 }
385
386 String value = StringPool.BLANK;
387
388 if (property.length > 2) {
389 value = GetterUtil.getString(property[2]);
390 }
391
392 if (propertyId == 0) {
393 if (Validator.isNotNull(key)) {
394 tagsPropertyLocalService.addProperty(
395 userId, entryId, key, value);
396 }
397 }
398 else {
399 if (Validator.isNull(key)) {
400 tagsPropertyLocalService.deleteProperty(propertyId);
401 }
402 else {
403 tagsPropertyLocalService.updateProperty(
404 propertyId, key, value);
405
406 newProperties.add(propertyId);
407 }
408 }
409 }
410
411 for (TagsProperty property : oldProperties) {
412 if (!newProperties.contains(property.getPropertyId())) {
413 tagsPropertyLocalService.deleteProperty(property);
414 }
415 }
416
417 return entry;
418 }
419
420 protected String[] getEntryNames(List <TagsEntry>entries) {
421 return StringUtil.split(ListUtil.toString(entries, "name"));
422 }
423
424 protected void validate(String name)
425 throws PortalException, SystemException {
426
427 if (!TagsUtil.isValidWord(name)) {
428 throw new EntryNameException();
429 }
430 }
431
432 }