1
22
23 package com.liferay.portlet.tags.service.impl;
24
25 import com.liferay.counter.service.CounterLocalServiceUtil;
26 import com.liferay.portal.PortalException;
27 import com.liferay.portal.SystemException;
28 import com.liferay.portal.kernel.json.JSONArrayWrapper;
29 import com.liferay.portal.kernel.util.GetterUtil;
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.service.persistence.UserUtil;
35 import com.liferay.portal.util.PortalUtil;
36 import com.liferay.portlet.tags.DuplicateEntryException;
37 import com.liferay.portlet.tags.EntryNameException;
38 import com.liferay.portlet.tags.model.TagsAsset;
39 import com.liferay.portlet.tags.model.TagsEntry;
40 import com.liferay.portlet.tags.model.TagsProperty;
41 import com.liferay.portlet.tags.service.TagsPropertyLocalServiceUtil;
42 import com.liferay.portlet.tags.service.base.TagsEntryLocalServiceBaseImpl;
43 import com.liferay.portlet.tags.service.persistence.TagsAssetUtil;
44 import com.liferay.portlet.tags.service.persistence.TagsEntryFinder;
45 import com.liferay.portlet.tags.service.persistence.TagsEntryUtil;
46 import com.liferay.portlet.tags.service.persistence.TagsPropertyUtil;
47 import com.liferay.portlet.tags.util.TagsUtil;
48 import com.liferay.util.Autocomplete;
49 import com.liferay.util.CollectionFactory;
50
51 import java.util.ArrayList;
52 import java.util.Date;
53 import java.util.Iterator;
54 import java.util.List;
55 import java.util.Set;
56
57
63 public class TagsEntryLocalServiceImpl extends TagsEntryLocalServiceBaseImpl {
64
65 public TagsEntry addEntry(long userId, String name)
66 throws PortalException, SystemException {
67
68 return addEntry(userId, name, new String[0]);
69 }
70
71 public TagsEntry addEntry(long userId, String name, String[] properties)
72 throws PortalException, SystemException {
73
74 User user = UserUtil.findByPrimaryKey(userId);
75 Date now = new Date();
76 name = name.trim().toLowerCase();
77
78 validate(name);
79
80 if (hasEntry(user.getCompanyId(), name)) {
81 throw new DuplicateEntryException(
82 "A tag entry with the name " + name + " already exists");
83 }
84
85 long entryId = CounterLocalServiceUtil.increment();
86
87 TagsEntry entry = TagsEntryUtil.create(entryId);
88
89 entry.setCompanyId(user.getCompanyId());
90 entry.setUserId(user.getUserId());
91 entry.setUserName(user.getFullName());
92 entry.setCreateDate(now);
93 entry.setModifiedDate(now);
94 entry.setName(name);
95
96 TagsEntryUtil.update(entry);
97
98 for (int i = 0; i < properties.length; i++) {
99 String[] property = StringUtil.split(
100 properties[i], StringPool.COLON);
101
102 String key = StringPool.BLANK;
103
104 if (property.length > 1) {
105 key = GetterUtil.getString(property[1]);
106 }
107
108 String value = StringPool.BLANK;
109
110 if (property.length > 2) {
111 value = GetterUtil.getString(property[2]);
112 }
113
114 if (Validator.isNotNull(key)) {
115 TagsPropertyLocalServiceUtil.addProperty(
116 userId, entryId, key, value);
117 }
118 }
119
120 return entry;
121
122 }
123
124 public void deleteEntry(long entryId)
125 throws PortalException, SystemException {
126
127 TagsEntry entry = TagsEntryUtil.findByPrimaryKey(entryId);
128
129 deleteEntry(entry);
130 }
131
132 public void deleteEntry(TagsEntry entry)
133 throws PortalException, SystemException {
134
135
137 TagsPropertyLocalServiceUtil.deleteProperties(entry.getEntryId());
138
139
141 TagsEntryUtil.remove(entry.getEntryId());
142 }
143
144 public boolean hasEntry(long companyId, String name)
145 throws PortalException, SystemException {
146
147 if (TagsEntryUtil.fetchByC_N(companyId, name) == null) {
148 return false;
149 }
150 else {
151 return true;
152 }
153 }
154
155 public List getAssetEntries(long assetId)
156 throws PortalException, SystemException {
157
158 return TagsAssetUtil.getTagsEntries(assetId);
159 }
160
161 public List getEntries() throws SystemException {
162 return TagsEntryUtil.findAll();
163 }
164
165 public List getEntries(String className, long classPK)
166 throws PortalException, SystemException {
167
168 long classNameId = PortalUtil.getClassNameId(className);
169
170 TagsAsset asset = TagsAssetUtil.fetchByC_C(classNameId, classPK);
171
172 if (asset == null) {
173 return new ArrayList();
174 }
175 else {
176 return TagsAssetUtil.getTagsEntries(asset.getAssetId());
177 }
178 }
179
180 public TagsEntry getEntry(long entryId)
181 throws PortalException, SystemException {
182
183 return TagsEntryUtil.findByPrimaryKey(entryId);
184 }
185
186 public TagsEntry getEntry(long companyId, String name)
187 throws PortalException, SystemException {
188
189 return TagsEntryUtil.findByC_N(companyId, name);
190 }
191
192 public long[] getEntryIds(long companyId, String[] names)
193 throws PortalException, SystemException {
194
195 List list = new ArrayList(names.length);
196
197 for (int i = 0; i < names.length; i++) {
198 String name = names[i];
199
200 TagsEntry entry = TagsEntryUtil.fetchByC_N(companyId, name);
201
202 if (entry != null) {
203 list.add(entry);
204 }
205 }
206
207 long[] entryIds = new long[list.size()];
208
209 for (int i = 0; i < list.size(); i++) {
210 TagsEntry entry = (TagsEntry)list.get(i);
211
212 entryIds[i] = entry.getEntryId();
213 }
214
215 return entryIds;
216 }
217
218 public List search(long companyId, String name, String[] properties)
219 throws SystemException {
220
221 return TagsEntryFinder.findByC_N_P(companyId, name, properties);
222 }
223
224 public List search(
225 long companyId, String name, String[] properties, int begin,
226 int end)
227 throws SystemException {
228
229 return TagsEntryFinder.findByC_N_P(
230 companyId, name, properties, begin, end);
231 }
232
233 public JSONArrayWrapper searchAutocomplete(
234 long companyId, String name, String[] properties, int begin,
235 int end)
236 throws SystemException {
237
238 List list = TagsEntryFinder.findByC_N_P(
239 companyId, name, properties, begin, end);
240
241 return new JSONArrayWrapper(
242 Autocomplete.listToJson(list, "name", "name"));
243 }
244
245 public int searchCount(long companyId, String name, String[] properties)
246 throws SystemException {
247
248 return TagsEntryFinder.countByC_N_P(companyId, name, properties);
249 }
250
251 public TagsEntry updateEntry(long entryId, String name)
252 throws PortalException, SystemException {
253
254 name = name.trim().toLowerCase();
255
256 validate(name);
257
258 TagsEntry entry = TagsEntryUtil.findByPrimaryKey(entryId);
259
260 if (!entry.getName().equals(name)) {
261 if (hasEntry(entry.getCompanyId(), name)) {
262 throw new DuplicateEntryException();
263 }
264 }
265
266 entry.setModifiedDate(new Date());
267 entry.setName(name);
268
269 TagsEntryUtil.update(entry);
270
271 return entry;
272 }
273
274 public TagsEntry updateEntry(
275 long userId, long entryId, String name, String[] properties)
276 throws PortalException, SystemException {
277
278 TagsEntry entry = updateEntry(entryId, name);
279
280 List curProperties = TagsPropertyUtil.findByEntryId(entryId);
281 Set keepProperties = CollectionFactory.getHashSet();
282
283 for (int i = 0; i < properties.length; i++) {
284 String[] property = StringUtil.split(
285 properties[i], StringPool.COLON);
286
287 Long propertyId = new Long(0);
288
289 if (property.length > 0) {
290 propertyId = new Long(GetterUtil.getLong(property[0]));
291 }
292
293 String key = StringPool.BLANK;
294
295 if (property.length > 1) {
296 key = GetterUtil.getString(property[1]);
297 }
298
299 String value = StringPool.BLANK;
300
301 if (property.length > 2) {
302 value = GetterUtil.getString(property[2]);
303 }
304
305 if (propertyId.longValue() == 0) {
306 if (Validator.isNotNull(key)) {
307 TagsPropertyLocalServiceUtil.addProperty(
308 userId, entryId, key, value);
309 }
310 }
311 else {
312 if (Validator.isNull(key)) {
313 TagsPropertyLocalServiceUtil.deleteProperty(
314 propertyId.longValue());
315 }
316 else {
317 TagsPropertyLocalServiceUtil.updateProperty(
318 propertyId.longValue(), key, value);
319
320 keepProperties.add(new Long(propertyId.longValue()));
321 }
322 }
323 }
324
325 Iterator itr = curProperties.iterator();
326
327 while (itr.hasNext()) {
328 TagsProperty property = (TagsProperty)itr.next();
329
330 if (!keepProperties.contains(new Long(property.getPropertyId()))) {
331 TagsPropertyLocalServiceUtil.deleteProperty(property);
332 }
333 }
334
335 return entry;
336 }
337
338 protected void validate(String name)
339 throws PortalException, SystemException {
340
341 if (!TagsUtil.isValidWord(name)) {
342 throw new EntryNameException();
343 }
344 }
345
346 }