1
14
15 package com.liferay.portlet.asset.action;
16
17 import com.liferay.portal.kernel.dao.orm.QueryUtil;
18 import com.liferay.portal.kernel.json.JSONArray;
19 import com.liferay.portal.kernel.json.JSONFactoryUtil;
20 import com.liferay.portal.kernel.json.JSONObject;
21 import com.liferay.portal.kernel.util.ParamUtil;
22 import com.liferay.portal.struts.JSONAction;
23 import com.liferay.portlet.asset.model.AssetCategory;
24 import com.liferay.portlet.asset.model.AssetCategoryConstants;
25 import com.liferay.portlet.asset.service.AssetCategoryLocalServiceUtil;
26
27 import java.util.Collections;
28 import java.util.List;
29
30 import javax.servlet.http.HttpServletRequest;
31 import javax.servlet.http.HttpServletResponse;
32
33 import org.apache.struts.action.ActionForm;
34 import org.apache.struts.action.ActionMapping;
35
36
41 public class GetCategoriesAction extends JSONAction {
42
43 public String getJSON(
44 ActionMapping mapping, ActionForm form, HttpServletRequest request,
45 HttpServletResponse response)
46 throws Exception {
47
48 JSONArray jsonArray = JSONFactoryUtil.createJSONArray();
49
50 List<AssetCategory> categories = getCategories(request);
51
52 for (AssetCategory category : categories) {
53 JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
54
55 int childCategoriesCount =
56 AssetCategoryLocalServiceUtil.getChildCategoriesCount(
57 category.getCategoryId());
58
59 jsonObject.put("categoryId", category.getCategoryId());
60 jsonObject.put("hasChildren", childCategoriesCount > 0);
61 jsonObject.put("name", category.getName());
62 jsonObject.put("parentCategoryId", category.getParentCategoryId());
63 jsonObject.put("title", category.getTitle());
64
65 jsonArray.put(jsonObject);
66 }
67
68 return jsonArray.toString();
69 }
70
71 protected List<AssetCategory> getCategories(HttpServletRequest request)
72 throws Exception {
73
74 long categoryId = ParamUtil.getLong(request, "categoryId");
75 long vocabularyId = ParamUtil.getLong(request, "vocabularyId");
76 int start = ParamUtil.getInteger(request, "start", QueryUtil.ALL_POS);
77 int end = ParamUtil.getInteger(request, "end", QueryUtil.ALL_POS);
78
79 List<AssetCategory> categories = Collections.EMPTY_LIST;
80
81 if (categoryId > 0) {
82 categories = AssetCategoryLocalServiceUtil.getChildCategories(
83 categoryId, start, end, null);
84 }
85 else if (vocabularyId > 0) {
86 long parentCategoryId = ParamUtil.getLong(
87 request, "parentCategoryId",
88 AssetCategoryConstants.DEFAULT_PARENT_CATEGORY_ID);
89
90 categories = AssetCategoryLocalServiceUtil.getVocabularyCategories(
91 parentCategoryId, vocabularyId, start, end, null);
92 }
93
94 return categories;
95 }
96
97 }