001
014
015 package com.liferay.portlet.asset.action;
016
017 import com.liferay.portal.kernel.dao.orm.QueryUtil;
018 import com.liferay.portal.kernel.json.JSONArray;
019 import com.liferay.portal.kernel.json.JSONFactoryUtil;
020 import com.liferay.portal.kernel.json.JSONObject;
021 import com.liferay.portal.kernel.util.ParamUtil;
022 import com.liferay.portal.struts.JSONAction;
023 import com.liferay.portlet.asset.model.AssetCategory;
024 import com.liferay.portlet.asset.model.AssetCategoryConstants;
025 import com.liferay.portlet.asset.service.AssetCategoryLocalServiceUtil;
026
027 import java.util.Collections;
028 import java.util.List;
029
030 import javax.servlet.http.HttpServletRequest;
031 import javax.servlet.http.HttpServletResponse;
032
033 import org.apache.struts.action.ActionForm;
034 import org.apache.struts.action.ActionMapping;
035
036
039 public class GetCategoriesAction extends JSONAction {
040
041 public String getJSON(
042 ActionMapping mapping, ActionForm form, HttpServletRequest request,
043 HttpServletResponse response)
044 throws Exception {
045
046 JSONArray jsonArray = JSONFactoryUtil.createJSONArray();
047
048 List<AssetCategory> categories = getCategories(request);
049
050 for (AssetCategory category : categories) {
051 JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
052
053 int childCategoriesCount =
054 AssetCategoryLocalServiceUtil.getChildCategoriesCount(
055 category.getCategoryId());
056
057 jsonObject.put("categoryId", category.getCategoryId());
058 jsonObject.put("hasChildren", childCategoriesCount > 0);
059 jsonObject.put("name", category.getName());
060 jsonObject.put("parentCategoryId", category.getParentCategoryId());
061 jsonObject.put("title", category.getTitle());
062
063 jsonArray.put(jsonObject);
064 }
065
066 return jsonArray.toString();
067 }
068
069 protected List<AssetCategory> getCategories(HttpServletRequest request)
070 throws Exception {
071
072 long categoryId = ParamUtil.getLong(request, "categoryId");
073 long vocabularyId = ParamUtil.getLong(request, "vocabularyId");
074 int start = ParamUtil.getInteger(request, "start", QueryUtil.ALL_POS);
075 int end = ParamUtil.getInteger(request, "end", QueryUtil.ALL_POS);
076
077 List<AssetCategory> categories = Collections.EMPTY_LIST;
078
079 if (categoryId > 0) {
080 categories = AssetCategoryLocalServiceUtil.getChildCategories(
081 categoryId, start, end, null);
082 }
083 else if (vocabularyId > 0) {
084 long parentCategoryId = ParamUtil.getLong(
085 request, "parentCategoryId",
086 AssetCategoryConstants.DEFAULT_PARENT_CATEGORY_ID);
087
088 categories = AssetCategoryLocalServiceUtil.getVocabularyCategories(
089 parentCategoryId, vocabularyId, start, end, null);
090 }
091
092 return categories;
093 }
094
095 }