001
014
015 package com.liferay.portlet.asset.service.impl;
016
017 import com.liferay.portal.kernel.exception.PortalException;
018 import com.liferay.portal.kernel.exception.SystemException;
019 import com.liferay.portal.kernel.util.ListUtil;
020 import com.liferay.portal.kernel.util.LocaleUtil;
021 import com.liferay.portal.kernel.util.StringPool;
022 import com.liferay.portal.kernel.util.Validator;
023 import com.liferay.portal.model.Group;
024 import com.liferay.portal.model.ResourceConstants;
025 import com.liferay.portal.model.User;
026 import com.liferay.portal.service.ServiceContext;
027 import com.liferay.portal.util.PropsValues;
028 import com.liferay.portlet.asset.DuplicateVocabularyException;
029 import com.liferay.portlet.asset.VocabularyNameException;
030 import com.liferay.portlet.asset.model.AssetVocabulary;
031 import com.liferay.portlet.asset.service.base.AssetVocabularyLocalServiceBaseImpl;
032
033 import java.util.ArrayList;
034 import java.util.Date;
035 import java.util.HashMap;
036 import java.util.List;
037 import java.util.Locale;
038 import java.util.Map;
039
040
045 public class AssetVocabularyLocalServiceImpl
046 extends AssetVocabularyLocalServiceBaseImpl {
047
048
051 public AssetVocabulary addVocabulary(
052 long userId, Map<Locale, String> titleMap,
053 Map<Locale, String> descriptionMap, String settings,
054 ServiceContext serviceContext)
055 throws PortalException, SystemException {
056
057 return addVocabulary(
058 userId, StringPool.BLANK, titleMap, descriptionMap, settings,
059 serviceContext);
060 }
061
062 public AssetVocabulary addVocabulary(
063 long userId, String title, Map<Locale, String> titleMap,
064 Map<Locale, String> descriptionMap, String settings,
065 ServiceContext serviceContext)
066 throws PortalException, SystemException {
067
068
069
070 User user = userPersistence.findByPrimaryKey(userId);
071 long groupId = serviceContext.getScopeGroupId();
072 String name = titleMap.get(LocaleUtil.getDefault());
073
074 Date now = new Date();
075
076 validate(groupId, name);
077
078 long vocabularyId = counterLocalService.increment();
079
080 AssetVocabulary vocabulary = assetVocabularyPersistence.create(
081 vocabularyId);
082
083 vocabulary.setUuid(serviceContext.getUuid());
084 vocabulary.setGroupId(groupId);
085 vocabulary.setCompanyId(user.getCompanyId());
086 vocabulary.setUserId(user.getUserId());
087 vocabulary.setUserName(user.getFullName());
088 vocabulary.setCreateDate(now);
089 vocabulary.setModifiedDate(now);
090 vocabulary.setName(name);
091
092 if (Validator.isNotNull(title)) {
093 vocabulary.setTitle(title);
094 }
095 else {
096 vocabulary.setTitleMap(titleMap);
097 }
098
099 vocabulary.setDescriptionMap(descriptionMap);
100 vocabulary.setSettings(settings);
101
102 assetVocabularyPersistence.update(vocabulary, false);
103
104
105
106 if (serviceContext.getAddCommunityPermissions() ||
107 serviceContext.getAddGuestPermissions()) {
108
109 addVocabularyResources(
110 vocabulary, serviceContext.getAddCommunityPermissions(),
111 serviceContext.getAddGuestPermissions());
112 }
113 else {
114 addVocabularyResources(
115 vocabulary, serviceContext.getCommunityPermissions(),
116 serviceContext.getGuestPermissions());
117 }
118
119 return vocabulary;
120 }
121
122 public void addVocabularyResources(
123 AssetVocabulary vocabulary, boolean addCommunityPermissions,
124 boolean addGuestPermissions)
125 throws PortalException, SystemException {
126
127 resourceLocalService.addResources(
128 vocabulary.getCompanyId(), vocabulary.getGroupId(),
129 vocabulary.getUserId(), AssetVocabulary.class.getName(),
130 vocabulary.getVocabularyId(), false, addCommunityPermissions,
131 addGuestPermissions);
132 }
133
134 public void addVocabularyResources(
135 AssetVocabulary vocabulary, String[] communityPermissions,
136 String[] guestPermissions)
137 throws PortalException, SystemException {
138
139 resourceLocalService.addModelResources(
140 vocabulary.getCompanyId(), vocabulary.getGroupId(),
141 vocabulary.getUserId(), AssetVocabulary.class.getName(),
142 vocabulary.getVocabularyId(), communityPermissions,
143 guestPermissions);
144 }
145
146 public void deleteVocabulary(AssetVocabulary vocabulary)
147 throws PortalException, SystemException {
148
149
150
151 assetVocabularyPersistence.remove(vocabulary);
152
153
154
155 resourceLocalService.deleteResource(
156 vocabulary.getCompanyId(), AssetVocabulary.class.getName(),
157 ResourceConstants.SCOPE_INDIVIDUAL, vocabulary.getVocabularyId());
158
159
160
161 assetCategoryLocalService.deleteVocabularyCategories(
162 vocabulary.getVocabularyId());
163 }
164
165 public void deleteVocabulary(long vocabularyId)
166 throws PortalException, SystemException {
167
168 AssetVocabulary vocabulary =
169 assetVocabularyPersistence.findByPrimaryKey(vocabularyId);
170
171 deleteVocabulary(vocabulary);
172 }
173
174 public List<AssetVocabulary> getCompanyVocabularies(long companyId)
175 throws SystemException {
176
177 return assetVocabularyPersistence.findByCompanyId(companyId);
178 }
179
180 public List<AssetVocabulary> getGroupsVocabularies(long[] groupIds)
181 throws PortalException, SystemException {
182
183 List<AssetVocabulary> vocabularies = new ArrayList<AssetVocabulary>();
184
185 for (long groupId : groupIds) {
186 vocabularies.addAll(getGroupVocabularies(groupId));
187 }
188
189 return vocabularies;
190 }
191
192 public List<AssetVocabulary> getGroupVocabularies(long groupId)
193 throws PortalException, SystemException {
194
195 List<AssetVocabulary> vocabularies =
196 assetVocabularyPersistence.findByGroupId(groupId);
197
198 if (vocabularies.isEmpty()) {
199 Group group = groupLocalService.getGroup(groupId);
200
201 long defaultUserId = userLocalService.getDefaultUserId(
202 group.getCompanyId());
203
204 ServiceContext serviceContext = new ServiceContext();
205
206 serviceContext.setScopeGroupId(groupId);
207
208 Map<Locale, String> titleMap = new HashMap<Locale, String>();
209
210 titleMap.put(
211 LocaleUtil.getDefault(), PropsValues.ASSET_VOCABULARY_DEFAULT);
212
213 AssetVocabulary vocabulary =
214 assetVocabularyLocalService.addVocabulary(
215 defaultUserId, StringPool.BLANK, titleMap, null,
216 StringPool.BLANK, serviceContext);
217
218 vocabularies = ListUtil.copy(vocabularies);
219
220 vocabularies.add(vocabulary);
221 }
222
223 return vocabularies;
224 }
225
226 public AssetVocabulary getGroupVocabulary(long groupId, String name)
227 throws PortalException, SystemException {
228
229 return assetVocabularyPersistence.findByG_N(groupId, name);
230 }
231
232 public AssetVocabulary getVocabulary(long vocabularyId)
233 throws PortalException, SystemException {
234
235 return assetVocabularyPersistence.findByPrimaryKey(vocabularyId);
236 }
237
238
241 public AssetVocabulary updateVocabulary(
242 long vocabularyId, Map<Locale, String> titleMap,
243 Map<Locale, String> descriptionMap, String settings,
244 ServiceContext serviceContext)
245 throws PortalException, SystemException {
246
247 return updateVocabulary(
248 vocabularyId, StringPool.BLANK, titleMap, descriptionMap, settings,
249 serviceContext);
250 }
251
252 public AssetVocabulary updateVocabulary(
253 long vocabularyId, String title, Map<Locale, String> titleMap,
254 Map<Locale, String> descriptionMap, String settings,
255 ServiceContext serviceContext)
256 throws PortalException, SystemException {
257
258 long groupId = serviceContext.getScopeGroupId();
259 String name = titleMap.get(LocaleUtil.getDefault());
260
261 AssetVocabulary vocabulary =
262 assetVocabularyPersistence.findByPrimaryKey(vocabularyId);
263
264 if (!vocabulary.getName().equals(name)) {
265 validate(groupId, name);
266 }
267
268 vocabulary.setModifiedDate(new Date());
269 vocabulary.setName(name);
270 vocabulary.setTitleMap(titleMap);
271
272 if (Validator.isNotNull(title)) {
273 vocabulary.setTitle(title);
274 }
275
276 vocabulary.setDescriptionMap(descriptionMap);
277 vocabulary.setSettings(settings);
278
279 assetVocabularyPersistence.update(vocabulary, false);
280
281 return vocabulary;
282 }
283
284 protected boolean hasVocabulary(long groupId, String name)
285 throws SystemException {
286
287 if (assetVocabularyPersistence.countByG_N(groupId, name) == 0) {
288 return false;
289 }
290 else {
291 return true;
292 }
293 }
294
295 protected void validate(long groupId, String name)
296 throws PortalException, SystemException {
297
298 if (Validator.isNull(name)) {
299 throw new VocabularyNameException();
300 }
301
302 if (hasVocabulary(groupId, name)) {
303 throw new DuplicateVocabularyException(
304 "A category vocabulary with the name " + name +
305 " already exists");
306 }
307 }
308
309 }