001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portlet.tags.service.impl;
022:
023: import com.liferay.portal.PortalException;
024: import com.liferay.portal.SystemException;
025: import com.liferay.portal.kernel.json.JSONArrayWrapper;
026: import com.liferay.portal.kernel.util.GetterUtil;
027: import com.liferay.portal.kernel.util.StringPool;
028: import com.liferay.portal.kernel.util.StringUtil;
029: import com.liferay.portal.kernel.util.Validator;
030: import com.liferay.portal.model.User;
031: import com.liferay.portal.util.PortalUtil;
032: import com.liferay.portlet.tags.DuplicateEntryException;
033: import com.liferay.portlet.tags.EntryNameException;
034: import com.liferay.portlet.tags.model.TagsAsset;
035: import com.liferay.portlet.tags.model.TagsEntry;
036: import com.liferay.portlet.tags.model.TagsProperty;
037: import com.liferay.portlet.tags.service.base.TagsEntryLocalServiceBaseImpl;
038: import com.liferay.portlet.tags.util.TagsUtil;
039: import com.liferay.util.Autocomplete;
040: import com.liferay.util.CollectionFactory;
041: import com.liferay.util.ListUtil;
042:
043: import java.util.ArrayList;
044: import java.util.Date;
045: import java.util.Iterator;
046: import java.util.List;
047: import java.util.Set;
048:
049: /**
050: * <a href="TagsEntryLocalServiceImpl.java.html"><b><i>View Source</i></b></a>
051: *
052: * @author Brian Wing Shun Chan
053: *
054: */
055: public class TagsEntryLocalServiceImpl extends
056: TagsEntryLocalServiceBaseImpl {
057:
058: public static String[] DEFAULT_PROPERTIES = new String[] { "0:category:no category" };
059:
060: public TagsEntry addEntry(long userId, String name)
061: throws PortalException, SystemException {
062:
063: return addEntry(userId, name, new String[0]);
064: }
065:
066: public TagsEntry addEntry(long userId, String name,
067: String[] properties) throws PortalException,
068: SystemException {
069:
070: User user = userPersistence.findByPrimaryKey(userId);
071: Date now = new Date();
072: name = name.trim().toLowerCase();
073:
074: validate(name);
075:
076: if (hasEntry(user.getCompanyId(), name)) {
077: throw new DuplicateEntryException(
078: "A tag entry with the name " + name
079: + " already exists");
080: }
081:
082: long entryId = counterLocalService.increment();
083:
084: TagsEntry entry = tagsEntryPersistence.create(entryId);
085:
086: entry.setCompanyId(user.getCompanyId());
087: entry.setUserId(user.getUserId());
088: entry.setUserName(user.getFullName());
089: entry.setCreateDate(now);
090: entry.setModifiedDate(now);
091: entry.setName(name);
092:
093: tagsEntryPersistence.update(entry);
094:
095: for (int i = 0; i < properties.length; i++) {
096: String[] property = StringUtil.split(properties[i],
097: StringPool.COLON);
098:
099: 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(userId, entryId,
113: 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(user
130: .getCompanyId(), name);
131:
132: if (entry == null) {
133: addEntry(userId, names[i], DEFAULT_PROPERTIES);
134: }
135: }
136: }
137:
138: public void deleteEntry(long entryId) throws PortalException,
139: SystemException {
140:
141: TagsEntry entry = tagsEntryPersistence
142: .findByPrimaryKey(entryId);
143:
144: deleteEntry(entry);
145: }
146:
147: public void deleteEntry(TagsEntry entry) throws PortalException,
148: SystemException {
149:
150: // Properties
151:
152: tagsPropertyLocalService.deleteProperties(entry.getEntryId());
153:
154: // Entry
155:
156: tagsEntryPersistence.remove(entry.getEntryId());
157: }
158:
159: public boolean hasEntry(long companyId, String name)
160: throws PortalException, SystemException {
161:
162: if (tagsEntryPersistence.fetchByC_N(companyId, name) == null) {
163: return false;
164: } else {
165: return true;
166: }
167: }
168:
169: public List getAssetEntries(long assetId) throws PortalException,
170: SystemException {
171:
172: return tagsAssetPersistence.getTagsEntries(assetId);
173: }
174:
175: public List getEntries() throws SystemException {
176: return tagsEntryPersistence.findAll();
177: }
178:
179: public List 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 getEntries(long classNameId, long classPK)
188: throws PortalException, SystemException {
189:
190: TagsAsset asset = tagsAssetPersistence.fetchByC_C(classNameId,
191: classPK);
192:
193: if (asset == null) {
194: return new ArrayList();
195: } else {
196: return tagsAssetPersistence.getTagsEntries(asset
197: .getAssetId());
198: }
199: }
200:
201: public TagsEntry getEntry(long entryId) throws PortalException,
202: SystemException {
203:
204: return tagsEntryPersistence.findByPrimaryKey(entryId);
205: }
206:
207: public TagsEntry getEntry(long companyId, String name)
208: throws PortalException, SystemException {
209:
210: return tagsEntryPersistence.findByC_N(companyId, name);
211: }
212:
213: public long[] getEntryIds(long companyId, String[] names)
214: throws PortalException, SystemException {
215:
216: List list = new ArrayList(names.length);
217:
218: for (int i = 0; i < names.length; i++) {
219: String name = names[i];
220:
221: TagsEntry entry = tagsEntryPersistence.fetchByC_N(
222: companyId, name);
223:
224: if (entry != null) {
225: list.add(entry);
226: }
227: }
228:
229: long[] entryIds = new long[list.size()];
230:
231: for (int i = 0; i < list.size(); i++) {
232: TagsEntry entry = (TagsEntry) list.get(i);
233:
234: entryIds[i] = entry.getEntryId();
235: }
236:
237: return entryIds;
238: }
239:
240: public String[] getEntryNames() throws SystemException {
241: return getEntryNames(getEntries());
242: }
243:
244: public String[] getEntryNames(String className, long classPK)
245: throws PortalException, SystemException {
246:
247: return getEntryNames(getEntries(className, classPK));
248: }
249:
250: public String[] getEntryNames(long classNameId, long classPK)
251: throws PortalException, SystemException {
252:
253: return getEntryNames(getEntries(classNameId, classPK));
254: }
255:
256: public List search(long companyId, String name, String[] properties)
257: throws SystemException {
258:
259: return tagsEntryFinder.findByC_N_P(companyId, name, properties);
260: }
261:
262: public List search(long companyId, String name,
263: String[] properties, int begin, int end)
264: throws SystemException {
265:
266: return tagsEntryFinder.findByC_N_P(companyId, name, properties,
267: begin, end);
268: }
269:
270: public JSONArrayWrapper searchAutocomplete(long companyId,
271: String name, String[] properties, int begin, int end)
272: throws SystemException {
273:
274: List list = tagsEntryFinder.findByC_N_P(companyId, name,
275: properties, begin, end);
276:
277: return new JSONArrayWrapper(Autocomplete.listToJson(list,
278: "name", "name"));
279: }
280:
281: public int searchCount(long companyId, String name,
282: String[] properties) throws SystemException {
283:
284: return tagsEntryFinder
285: .countByC_N_P(companyId, name, properties);
286: }
287:
288: public TagsEntry updateEntry(long entryId, String name)
289: throws PortalException, SystemException {
290:
291: name = name.trim().toLowerCase();
292:
293: validate(name);
294:
295: TagsEntry entry = tagsEntryPersistence
296: .findByPrimaryKey(entryId);
297:
298: if (!entry.getName().equals(name)) {
299: if (hasEntry(entry.getCompanyId(), name)) {
300: throw new DuplicateEntryException();
301: }
302: }
303:
304: entry.setModifiedDate(new Date());
305: entry.setName(name);
306:
307: tagsEntryPersistence.update(entry);
308:
309: return entry;
310: }
311:
312: public TagsEntry updateEntry(long userId, long entryId,
313: String name, String[] properties) throws PortalException,
314: SystemException {
315:
316: TagsEntry entry = updateEntry(entryId, name);
317:
318: List curProperties = tagsPropertyPersistence
319: .findByEntryId(entryId);
320: Set keepProperties = CollectionFactory.getHashSet();
321:
322: for (int i = 0; i < properties.length; i++) {
323: String[] property = StringUtil.split(properties[i],
324: StringPool.COLON);
325:
326: Long propertyId = new Long(0);
327:
328: if (property.length > 0) {
329: propertyId = new Long(GetterUtil.getLong(property[0]));
330: }
331:
332: String key = StringPool.BLANK;
333:
334: if (property.length > 1) {
335: key = GetterUtil.getString(property[1]);
336: }
337:
338: String value = StringPool.BLANK;
339:
340: if (property.length > 2) {
341: value = GetterUtil.getString(property[2]);
342: }
343:
344: if (propertyId.longValue() == 0) {
345: if (Validator.isNotNull(key)) {
346: tagsPropertyLocalService.addProperty(userId,
347: entryId, key, value);
348: }
349: } else {
350: if (Validator.isNull(key)) {
351: tagsPropertyLocalService.deleteProperty(propertyId
352: .longValue());
353: } else {
354: tagsPropertyLocalService.updateProperty(propertyId
355: .longValue(), key, value);
356:
357: keepProperties
358: .add(new Long(propertyId.longValue()));
359: }
360: }
361: }
362:
363: Iterator itr = curProperties.iterator();
364:
365: while (itr.hasNext()) {
366: TagsProperty property = (TagsProperty) itr.next();
367:
368: if (!keepProperties.contains(new Long(property
369: .getPropertyId()))) {
370: tagsPropertyLocalService.deleteProperty(property);
371: }
372: }
373:
374: return entry;
375: }
376:
377: protected String[] getEntryNames(List entries) {
378: return StringUtil.split(ListUtil.toString(entries, "name"));
379: }
380:
381: protected void validate(String name) throws PortalException,
382: SystemException {
383:
384: if (!TagsUtil.isValidWord(name)) {
385: throw new EntryNameException();
386: }
387: }
388:
389: }
|