1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
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.JSONArray;
28  import com.liferay.portal.kernel.util.GetterUtil;
29  import com.liferay.portal.kernel.util.ListUtil;
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.util.PortalUtil;
35  import com.liferay.portlet.tags.DuplicateEntryException;
36  import com.liferay.portlet.tags.TagsEntryException;
37  import com.liferay.portlet.tags.model.TagsAsset;
38  import com.liferay.portlet.tags.model.TagsEntry;
39  import com.liferay.portlet.tags.model.TagsProperty;
40  import com.liferay.portlet.tags.service.base.TagsEntryLocalServiceBaseImpl;
41  import com.liferay.portlet.tags.util.TagsUtil;
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         // 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         List<TagsProperty> oldProperties =
362             tagsPropertyPersistence.findByEntryId(entryId);
363 
364         for (TagsProperty property : oldProperties) {
365             tagsPropertyLocalService.deleteProperty(property);
366         }
367 
368         for (int i = 0; i < properties.length; i++) {
369             String[] property = StringUtil.split(
370                 properties[i], StringPool.COLON);
371 
372             String key = StringPool.BLANK;
373 
374             if (property.length > 1) {
375                 key = GetterUtil.getString(property[1]);
376             }
377 
378             String value = StringPool.BLANK;
379 
380             if (property.length > 2) {
381                 value = GetterUtil.getString(property[2]);
382             }
383 
384             if (Validator.isNotNull(key)) {
385                 tagsPropertyLocalService.addProperty(
386                     userId, entryId, key, value);
387             }
388         }
389 
390         return entry;
391     }
392 
393     protected String[] getEntryNames(List <TagsEntry>entries) {
394         return StringUtil.split(ListUtil.toString(entries, "name"));
395     }
396 
397     protected void validate(String name) throws PortalException {
398         if (!TagsUtil.isValidWord(name)) {
399             throw new TagsEntryException(TagsEntryException.INVALID_CHARACTER);
400         }
401     }
402 
403 }