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.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  public class TagsEntryServiceImpl extends TagsEntryServiceBaseImpl {
52  
53      public TagsEntry addEntry(
54              String parentEntryName, String name, String vocabularyName,
55              String[] properties, ServiceContext serviceContext)
56          throws PortalException, SystemException {
57  
58          long parentEntryId = TagsEntryConstants.DEFAULT_PARENT_ENTRY_ID;
59          long groupId = serviceContext.getScopeGroupId();
60  
61          if (Validator.isNotNull(parentEntryName)) {
62              TagsVocabulary vocabulary = tagsVocabularyPersistence.findByG_N(
63                  groupId, vocabularyName);
64  
65              TagsEntry parentEntry = tagsEntryLocalService.getEntry(
66                  groupId, parentEntryName, vocabulary.isFolksonomy());
67  
68              parentEntryId = parentEntry.getEntryId();
69          }
70  
71          TagsEntryPermission.check(
72              getPermissionChecker(), serviceContext.getScopeGroupId(),
73              parentEntryId, ActionKeys.ADD_ENTRY);
74  
75          return tagsEntryLocalService.addEntry(
76              getUserId(), parentEntryName, name, vocabularyName, properties,
77              serviceContext);
78      }
79  
80      public void deleteEntry(long entryId)
81          throws PortalException, SystemException {
82  
83          TagsEntryPermission.check(
84              getPermissionChecker(), entryId, ActionKeys.DELETE);
85  
86          tagsEntryLocalService.deleteEntry(entryId);
87      }
88  
89      public List<TagsEntry> getEntries(
90              long groupId, long classNameId, String name)
91          throws PortalException, SystemException {
92  
93          return getEntries(
94              tagsEntryLocalService.getEntries(groupId, classNameId, name));
95      }
96  
97      public List<TagsEntry> getEntries(String className, long classPK)
98          throws PortalException, SystemException {
99  
100         return getEntries(tagsEntryLocalService.getEntries(className, classPK));
101     }
102 
103     public TagsEntry getEntry(long entryId)
104         throws PortalException, SystemException {
105 
106         TagsEntryPermission.check(
107             getPermissionChecker(), entryId, ActionKeys.VIEW);
108 
109         return tagsEntryLocalService.getEntry(entryId);
110     }
111 
112     public List<TagsEntry> getGroupVocabularyEntries(
113             long groupId, String vocabularyName)
114         throws PortalException, SystemException {
115 
116         return getEntries(
117             tagsEntryLocalService.getGroupVocabularyEntries(
118                 groupId, vocabularyName));
119     }
120 
121     public List<TagsEntry> getGroupVocabularyEntries(
122             long groupId, String parentEntryName, String vocabularyName)
123         throws PortalException, SystemException {
124 
125         return getEntries(
126             tagsEntryLocalService.getGroupVocabularyEntries(
127                 groupId, parentEntryName, vocabularyName));
128     }
129 
130     public List<TagsEntry> getGroupVocabularyRootEntries(
131             long groupId, String vocabularyName)
132         throws PortalException, SystemException {
133 
134         return getEntries(
135             tagsEntryLocalService.getGroupVocabularyRootEntries(
136                 groupId, vocabularyName));
137     }
138 
139     public void mergeEntries(long fromEntryId, long toEntryId)
140         throws PortalException, SystemException {
141 
142         TagsEntryPermission.check(
143             getPermissionChecker(), fromEntryId, ActionKeys.VIEW);
144 
145         TagsEntryPermission.check(
146             getPermissionChecker(), toEntryId, ActionKeys.UPDATE);
147 
148         tagsEntryLocalService.mergeEntries(fromEntryId, toEntryId);
149     }
150 
151     public JSONArray search(
152             long groupId, String name, String[] properties, int start, int end)
153         throws SystemException {
154 
155         return tagsEntryLocalService.search(
156             groupId, name, properties, start, end);
157     }
158 
159     public TagsEntry updateEntry(
160             long entryId, String parentEntryName, String name,
161             String vocabularyName, String[] properties)
162         throws PortalException, SystemException {
163 
164         TagsEntryPermission.check(
165             getPermissionChecker(), entryId, ActionKeys.UPDATE);
166 
167         return tagsEntryLocalService.updateEntry(
168             getUserId(), entryId, parentEntryName, name, vocabularyName,
169             properties);
170     }
171 
172     protected List<TagsEntry> getEntries(List<TagsEntry> entries)
173         throws PortalException {
174 
175         PermissionChecker permissionChecker = getPermissionChecker();
176 
177         entries = ListUtil.copy(entries);
178 
179         Iterator<TagsEntry> itr = entries.iterator();
180 
181         while (itr.hasNext()) {
182             TagsEntry entry = itr.next();
183 
184             if (!TagsEntryPermission.contains(
185                     permissionChecker, entry, ActionKeys.VIEW)) {
186 
187                 itr.remove();
188             }
189         }
190 
191         return entries;
192     }
193 
194 }