1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portlet.tags.service.impl;
16  
17  import com.liferay.portal.PortalException;
18  import com.liferay.portal.SystemException;
19  import com.liferay.portal.kernel.json.JSONArray;
20  import com.liferay.portal.kernel.util.ListUtil;
21  import com.liferay.portal.kernel.util.Validator;
22  import com.liferay.portal.security.permission.ActionKeys;
23  import com.liferay.portal.security.permission.PermissionChecker;
24  import com.liferay.portal.service.ServiceContext;
25  import com.liferay.portlet.tags.model.TagsEntry;
26  import com.liferay.portlet.tags.model.TagsEntryConstants;
27  import com.liferay.portlet.tags.model.TagsVocabulary;
28  import com.liferay.portlet.tags.service.base.TagsEntryServiceBaseImpl;
29  import com.liferay.portlet.tags.service.permission.TagsEntryPermission;
30  
31  import java.util.Iterator;
32  import java.util.List;
33  
34  /**
35   * <a href="TagsEntryServiceImpl.java.html"><b><i>View Source</i></b></a>
36   *
37   * @author Brian Wing Shun Chan
38   * @author Jorge Ferrer
39   * @author Alvaro del Castillo
40   * @author Eduardo Lundgren
41   * @author Bruno Farache
42   */
43  public class TagsEntryServiceImpl extends TagsEntryServiceBaseImpl {
44  
45      public TagsEntry addEntry(
46              String parentEntryName, String name, String vocabularyName,
47              String[] properties, ServiceContext serviceContext)
48          throws PortalException, SystemException {
49  
50          long parentEntryId = TagsEntryConstants.DEFAULT_PARENT_ENTRY_ID;
51          long groupId = serviceContext.getScopeGroupId();
52  
53          if (Validator.isNotNull(parentEntryName)) {
54              TagsVocabulary vocabulary = tagsVocabularyPersistence.findByG_N(
55                  groupId, vocabularyName);
56  
57              TagsEntry parentEntry = tagsEntryLocalService.getEntry(
58                  groupId, parentEntryName, vocabulary.isFolksonomy());
59  
60              parentEntryId = parentEntry.getEntryId();
61          }
62  
63          TagsEntryPermission.check(
64              getPermissionChecker(), serviceContext.getScopeGroupId(),
65              parentEntryId, ActionKeys.ADD_ENTRY);
66  
67          return tagsEntryLocalService.addEntry(
68              getUserId(), parentEntryName, name, vocabularyName, properties,
69              serviceContext);
70      }
71  
72      public void deleteEntry(long entryId)
73          throws PortalException, SystemException {
74  
75          TagsEntryPermission.check(
76              getPermissionChecker(), entryId, ActionKeys.DELETE);
77  
78          tagsEntryLocalService.deleteEntry(entryId);
79      }
80  
81      public List<TagsEntry> getEntries(
82              long groupId, long classNameId, String name)
83          throws PortalException, SystemException {
84  
85          return getEntries(
86              tagsEntryLocalService.getEntries(groupId, classNameId, name));
87      }
88  
89      public List<TagsEntry> getEntries(String className, long classPK)
90          throws PortalException, SystemException {
91  
92          return getEntries(tagsEntryLocalService.getEntries(className, classPK));
93      }
94  
95      public TagsEntry getEntry(long entryId)
96          throws PortalException, SystemException {
97  
98          TagsEntryPermission.check(
99              getPermissionChecker(), entryId, ActionKeys.VIEW);
100 
101         return tagsEntryLocalService.getEntry(entryId);
102     }
103 
104     public List<TagsEntry> getGroupVocabularyEntries(
105             long groupId, String vocabularyName)
106         throws PortalException, SystemException {
107 
108         return getEntries(
109             tagsEntryLocalService.getGroupVocabularyEntries(
110                 groupId, vocabularyName));
111     }
112 
113     public List<TagsEntry> getGroupVocabularyEntries(
114             long groupId, String parentEntryName, String vocabularyName)
115         throws PortalException, SystemException {
116 
117         return getEntries(
118             tagsEntryLocalService.getGroupVocabularyEntries(
119                 groupId, parentEntryName, vocabularyName));
120     }
121 
122     public List<TagsEntry> getGroupVocabularyRootEntries(
123             long groupId, String vocabularyName)
124         throws PortalException, SystemException {
125 
126         return getEntries(
127             tagsEntryLocalService.getGroupVocabularyRootEntries(
128                 groupId, vocabularyName));
129     }
130 
131     public void mergeEntries(long fromEntryId, long toEntryId)
132         throws PortalException, SystemException {
133 
134         TagsEntryPermission.check(
135             getPermissionChecker(), fromEntryId, ActionKeys.VIEW);
136 
137         TagsEntryPermission.check(
138             getPermissionChecker(), toEntryId, ActionKeys.UPDATE);
139 
140         tagsEntryLocalService.mergeEntries(fromEntryId, toEntryId);
141     }
142 
143     public JSONArray search(
144             long groupId, String name, String[] properties, int start, int end)
145         throws SystemException {
146 
147         return tagsEntryLocalService.search(
148             groupId, name, properties, start, end);
149     }
150 
151     public TagsEntry updateEntry(
152             long entryId, String parentEntryName, String name,
153             String vocabularyName, String[] properties)
154         throws PortalException, SystemException {
155 
156         TagsEntryPermission.check(
157             getPermissionChecker(), entryId, ActionKeys.UPDATE);
158 
159         return tagsEntryLocalService.updateEntry(
160             getUserId(), entryId, parentEntryName, name, vocabularyName,
161             properties);
162     }
163 
164     protected List<TagsEntry> getEntries(List<TagsEntry> entries)
165         throws PortalException {
166 
167         PermissionChecker permissionChecker = getPermissionChecker();
168 
169         entries = ListUtil.copy(entries);
170 
171         Iterator<TagsEntry> itr = entries.iterator();
172 
173         while (itr.hasNext()) {
174             TagsEntry entry = itr.next();
175 
176             if (!TagsEntryPermission.contains(
177                     permissionChecker, entry, ActionKeys.VIEW)) {
178 
179                 itr.remove();
180             }
181         }
182 
183         return entries;
184     }
185 
186 }