1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions 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.ListUtil;
29  import com.liferay.portal.kernel.util.Validator;
30  import com.liferay.portal.security.permission.ActionKeys;
31  import com.liferay.portal.security.permission.PermissionChecker;
32  import com.liferay.portal.service.ServiceContext;
33  import com.liferay.portlet.tags.model.TagsEntry;
34  import com.liferay.portlet.tags.model.TagsEntryConstants;
35  import com.liferay.portlet.tags.model.TagsVocabulary;
36  import com.liferay.portlet.tags.service.base.TagsEntryServiceBaseImpl;
37  import com.liferay.portlet.tags.service.permission.TagsEntryPermission;
38  
39  import java.util.Iterator;
40  import java.util.List;
41  
42  /**
43   * <a href="TagsEntryServiceImpl.java.html"><b><i>View Source</i></b></a>
44   *
45   * @author Brian Wing Shun Chan
46   * @author Jorge Ferrer
47   * @author Alvaro del Castillo
48   * @author Eduardo Lundgren
49   * @author Bruno Farache
50   *
51   */
52  public class TagsEntryServiceImpl extends TagsEntryServiceBaseImpl {
53  
54      public TagsEntry addEntry(
55              String parentEntryName, String name, String vocabularyName,
56              String[] properties, ServiceContext serviceContext)
57          throws PortalException, SystemException {
58  
59          long parentEntryId = TagsEntryConstants.DEFAULT_PARENT_ENTRY_ID;
60          long groupId = serviceContext.getScopeGroupId();
61  
62          if (Validator.isNotNull(parentEntryName)) {
63              TagsVocabulary vocabulary = tagsVocabularyPersistence.findByG_N(
64                  groupId, vocabularyName);
65  
66              TagsEntry parentEntry = tagsEntryLocalService.getEntry(
67                  groupId, parentEntryName, vocabulary.isFolksonomy());
68  
69              parentEntryId = parentEntry.getEntryId();
70          }
71  
72          TagsEntryPermission.check(
73              getPermissionChecker(), serviceContext.getScopeGroupId(),
74              parentEntryId, ActionKeys.ADD_ENTRY);
75  
76          return tagsEntryLocalService.addEntry(
77              getUserId(), parentEntryName, name, vocabularyName, properties,
78              serviceContext);
79      }
80  
81      public void deleteEntry(long entryId)
82          throws PortalException, SystemException {
83  
84          TagsEntryPermission.check(
85              getPermissionChecker(), entryId, ActionKeys.DELETE);
86  
87          tagsEntryLocalService.deleteEntry(entryId);
88      }
89  
90      public List<TagsEntry> getEntries(String className, long classPK)
91          throws PortalException, SystemException {
92  
93          return getEntries(tagsEntryLocalService.getEntries(className, classPK));
94      }
95  
96      public List<TagsEntry> getEntries(
97              long groupId, long classNameId, String name)
98          throws PortalException, SystemException {
99  
100         return getEntries(
101             tagsEntryLocalService.getEntries(groupId, classNameId, name));
102     }
103 
104     public TagsEntry getEntry(long entryId)
105         throws PortalException, SystemException {
106 
107         TagsEntryPermission.check(
108             getPermissionChecker(), entryId, ActionKeys.VIEW);
109 
110         return tagsEntryLocalService.getEntry(entryId);
111     }
112 
113     public List<TagsEntry> getGroupVocabularyEntries(
114             long groupId, String vocabularyName)
115         throws PortalException, SystemException {
116 
117         return getEntries(
118             tagsEntryLocalService.getGroupVocabularyEntries(
119                 groupId, vocabularyName));
120     }
121 
122     public List<TagsEntry> getGroupVocabularyEntries(
123             long groupId, String parentEntryName, String vocabularyName)
124         throws PortalException, SystemException {
125 
126         return getEntries(
127             tagsEntryLocalService.getGroupVocabularyEntries(
128                 groupId, parentEntryName, vocabularyName));
129     }
130 
131     public List<TagsEntry> getGroupVocabularyRootEntries(
132             long groupId, String vocabularyName)
133         throws PortalException, SystemException {
134 
135         return getEntries(
136             tagsEntryLocalService.getGroupVocabularyRootEntries(
137                 groupId, vocabularyName));
138     }
139 
140     public void mergeEntries(long fromEntryId, long toEntryId)
141         throws PortalException, SystemException {
142 
143         TagsEntryPermission.check(
144             getPermissionChecker(), fromEntryId, ActionKeys.VIEW);
145 
146         TagsEntryPermission.check(
147             getPermissionChecker(), toEntryId, ActionKeys.UPDATE);
148 
149         tagsEntryLocalService.mergeEntries(fromEntryId, toEntryId);
150     }
151 
152     public JSONArray search(
153             long groupId, String name, String[] properties, int start, int end)
154         throws SystemException {
155 
156         return tagsEntryLocalService.search(
157             groupId, name, properties, start, end);
158     }
159 
160     public TagsEntry updateEntry(
161             long entryId, String parentEntryName, String name,
162             String vocabularyName, String[] properties)
163         throws PortalException, SystemException {
164 
165         TagsEntryPermission.check(
166             getPermissionChecker(), entryId, ActionKeys.UPDATE);
167 
168         return tagsEntryLocalService.updateEntry(
169             getUserId(), entryId, parentEntryName, name, vocabularyName,
170             properties);
171     }
172 
173     protected List<TagsEntry> getEntries(List<TagsEntry> entries)
174         throws PortalException {
175 
176         PermissionChecker permissionChecker = getPermissionChecker();
177 
178         entries = ListUtil.copy(entries);
179 
180         Iterator<TagsEntry> itr = entries.iterator();
181 
182         while (itr.hasNext()) {
183             TagsEntry entry = itr.next();
184 
185             if (!TagsEntryPermission.contains(
186                     permissionChecker, entry, ActionKeys.VIEW)) {
187 
188                 itr.remove();
189             }
190         }
191 
192         return entries;
193     }
194 
195 }