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.json.JSONArray;
020 import com.liferay.portal.kernel.json.JSONFactoryUtil;
021 import com.liferay.portal.kernel.json.JSONObject;
022 import com.liferay.portal.kernel.util.ListUtil;
023 import com.liferay.portal.kernel.util.OrderByComparator;
024 import com.liferay.portal.kernel.util.Validator;
025 import com.liferay.portal.security.permission.ActionKeys;
026 import com.liferay.portal.security.permission.PermissionChecker;
027 import com.liferay.portal.service.ServiceContext;
028 import com.liferay.portlet.asset.model.AssetCategory;
029 import com.liferay.portlet.asset.service.base.AssetCategoryServiceBaseImpl;
030 import com.liferay.portlet.asset.service.permission.AssetCategoryPermission;
031 import com.liferay.util.Autocomplete;
032 import com.liferay.util.dao.orm.CustomSQLUtil;
033
034 import java.util.Iterator;
035 import java.util.List;
036 import java.util.Locale;
037 import java.util.Map;
038
039
046 public class AssetCategoryServiceImpl extends AssetCategoryServiceBaseImpl {
047
048 public AssetCategory addCategory(
049 long parentCategoryId, Map<Locale, String> titleMap,
050 Map<Locale, String> descriptionMap, long vocabularyId,
051 String[] categoryProperties, ServiceContext serviceContext)
052 throws PortalException, SystemException {
053
054 AssetCategoryPermission.check(
055 getPermissionChecker(), serviceContext.getScopeGroupId(),
056 parentCategoryId, ActionKeys.ADD_CATEGORY);
057
058 return assetCategoryLocalService.addCategory(
059 getUserId(), parentCategoryId, titleMap, descriptionMap,
060 vocabularyId, categoryProperties, serviceContext);
061 }
062
063 public void deleteCategories(long[] categoryIds)
064 throws PortalException, SystemException {
065
066 PermissionChecker permissionChecker = getPermissionChecker();
067
068 for (long categoryId : categoryIds) {
069 AssetCategory category = assetCategoryPersistence.fetchByPrimaryKey(
070 categoryId);
071
072 if (category == null) {
073 continue;
074 }
075
076 AssetCategoryPermission.check(
077 permissionChecker, categoryId, ActionKeys.DELETE);
078
079 assetCategoryLocalService.deleteCategory(category);
080 }
081 }
082
083 public void deleteCategory(long categoryId)
084 throws PortalException, SystemException {
085
086 AssetCategoryPermission.check(
087 getPermissionChecker(), categoryId, ActionKeys.DELETE);
088
089 assetCategoryLocalService.deleteCategory(categoryId);
090 }
091
092 public List<AssetCategory> getCategories(String className, long classPK)
093 throws PortalException, SystemException {
094
095 return filterCategories(
096 assetCategoryLocalService.getCategories(className, classPK));
097 }
098
099 public AssetCategory getCategory(long categoryId)
100 throws PortalException, SystemException {
101
102 AssetCategoryPermission.check(
103 getPermissionChecker(), categoryId, ActionKeys.VIEW);
104
105 return assetCategoryLocalService.getCategory(categoryId);
106 }
107
108 public List<AssetCategory> getChildCategories(long parentCategoryId)
109 throws PortalException, SystemException {
110
111 return filterCategories(
112 assetCategoryLocalService.getChildCategories(parentCategoryId));
113 }
114
115 public List<AssetCategory> getChildCategories(
116 long parentCategoryId, int start, int end, OrderByComparator obc)
117 throws PortalException, SystemException {
118
119 return filterCategories(
120 assetCategoryLocalService.getChildCategories(
121 parentCategoryId, start, end, obc));
122 }
123
124 public JSONObject getJSONVocabularyCategories(
125 long groupId, String name, long vocabularyId, int start, int end,
126 OrderByComparator obc)
127 throws PortalException, SystemException {
128
129 JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
130
131 int page = end / (end - start);
132
133 jsonObject.put("page", page);
134
135 List<AssetCategory> categories;
136 int total = 0;
137
138 if (Validator.isNotNull(name)) {
139 name = (CustomSQLUtil.keywords(name))[0];
140
141 categories = getVocabularyCategories(
142 groupId, name, vocabularyId, start, end, obc);
143 total = getVocabularyCategoriesCount(groupId, name, vocabularyId);
144 }
145 else {
146 categories = getVocabularyCategories(vocabularyId, start, end, obc);
147 total = getVocabularyCategoriesCount(groupId, vocabularyId);
148 }
149
150 String categoriesJSON = JSONFactoryUtil.looseSerialize(categories);
151
152 JSONArray categoriesJSONArray =
153 JSONFactoryUtil.createJSONArray(categoriesJSON);
154
155 jsonObject.put("categories", categoriesJSONArray);
156
157 jsonObject.put("total", total);
158
159 return jsonObject;
160 }
161
162 public List<AssetCategory> getVocabularyCategories(
163 long vocabularyId, int start, int end, OrderByComparator obc)
164 throws PortalException, SystemException {
165
166 return filterCategories(
167 assetCategoryLocalService.getVocabularyCategories(
168 vocabularyId, start, end, obc));
169 }
170
171 public List<AssetCategory> getVocabularyCategories(
172 long parentCategoryId, long vocabularyId, int start, int end,
173 OrderByComparator obc)
174 throws PortalException, SystemException {
175
176 return filterCategories(
177 assetCategoryLocalService.getVocabularyCategories(
178 parentCategoryId, vocabularyId, start, end, obc));
179 }
180
181 public List<AssetCategory> getVocabularyCategories(
182 long groupId, String name, long vocabularyId, int start, int end,
183 OrderByComparator obc)
184 throws SystemException {
185
186 return assetCategoryFinder.filterFindByG_N_V(
187 groupId, name, vocabularyId, start, end, obc);
188 }
189
190 public int getVocabularyCategoriesCount(long groupId, long vocabularyId)
191 throws SystemException {
192
193 return assetCategoryPersistence.filterCountByG_V(groupId, vocabularyId);
194 }
195
196 public int getVocabularyCategoriesCount(
197 long groupId, String name, long vocabularyId)
198 throws SystemException {
199
200 return assetCategoryFinder.filterCountByG_N_V(
201 groupId, name, vocabularyId);
202 }
203
204 public List<AssetCategory> getVocabularyRootCategories(
205 long vocabularyId, int start, int end, OrderByComparator obc)
206 throws PortalException, SystemException {
207
208 return filterCategories(
209 assetCategoryLocalService.getVocabularyRootCategories(
210 vocabularyId, start, end, obc));
211 }
212
213 public AssetCategory moveCategory(
214 long categoryId, long parentCategoryId, long vocabularyId,
215 ServiceContext serviceContext)
216 throws PortalException, SystemException {
217
218 AssetCategoryPermission.check(
219 getPermissionChecker(), categoryId, ActionKeys.UPDATE);
220
221 return assetCategoryLocalService.moveCategory(
222 categoryId, parentCategoryId, vocabularyId, serviceContext);
223 }
224
225 public List<AssetCategory> search(
226 long groupId, String keywords, long vocabularyId, int start,
227 int end, OrderByComparator obc)
228 throws PortalException, SystemException {
229
230 return filterCategories(
231 assetCategoryFinder.findByG_N_V(
232 groupId, CustomSQLUtil.keywords(keywords)[0], vocabularyId,
233 start, end, obc));
234 }
235
236 public JSONArray search(
237 long groupId, String name, String[] categoryProperties, int start,
238 int end)
239 throws PortalException, SystemException {
240
241 List<AssetCategory> categories = assetCategoryLocalService.search(
242 groupId, name, categoryProperties, start, end);
243
244 categories = filterCategories(categories);
245
246 return Autocomplete.listToJson(categories, "name", "name");
247 }
248
249 public AssetCategory updateCategory(
250 long categoryId, long parentCategoryId,
251 Map<Locale, String> titleMap, Map<Locale, String> descriptionMap,
252 long vocabularyId, String[] categoryProperties,
253 ServiceContext serviceContext)
254 throws PortalException, SystemException {
255
256 AssetCategoryPermission.check(
257 getPermissionChecker(), categoryId, ActionKeys.UPDATE);
258
259 return assetCategoryLocalService.updateCategory(
260 getUserId(), categoryId, parentCategoryId, titleMap, descriptionMap,
261 vocabularyId, categoryProperties, serviceContext);
262 }
263
264 protected List<AssetCategory> filterCategories(
265 List<AssetCategory> categories)
266 throws PortalException {
267
268 PermissionChecker permissionChecker = getPermissionChecker();
269
270 categories = ListUtil.copy(categories);
271
272 Iterator<AssetCategory> itr = categories.iterator();
273
274 while (itr.hasNext()) {
275 AssetCategory category = itr.next();
276
277 if (!AssetCategoryPermission.contains(
278 permissionChecker, category, ActionKeys.VIEW)) {
279
280 itr.remove();
281 }
282 }
283
284 return categories;
285 }
286
287 }