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.AssetCategoryServiceUtil;
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 @Override
042 public String getJSON(
043 ActionMapping mapping, ActionForm form, HttpServletRequest request,
044 HttpServletResponse response)
045 throws Exception {
046
047 JSONArray jsonArray = JSONFactoryUtil.createJSONArray();
048
049 List<AssetCategory> categories = getCategories(request);
050
051 for (AssetCategory category : categories) {
052 JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
053
054 List<AssetCategory> childCategories =
055 AssetCategoryServiceUtil.getChildCategories(
056 category.getCategoryId());
057
058 jsonObject.put("categoryId", category.getCategoryId());
059 jsonObject.put("hasChildren", !childCategories.isEmpty());
060 jsonObject.put("name", category.getName());
061 jsonObject.put("parentCategoryId", category.getParentCategoryId());
062 jsonObject.put(
063 "titleCurrentValue", category.getTitleCurrentValue());
064
065 jsonArray.put(jsonObject);
066 }
067
068 return jsonArray.toString();
069 }
070
071 protected List<AssetCategory> getCategories(HttpServletRequest request)
072 throws Exception {
073
074 long categoryId = ParamUtil.getLong(request, "categoryId");
075 long vocabularyId = ParamUtil.getLong(request, "vocabularyId");
076 int start = ParamUtil.getInteger(request, "start", QueryUtil.ALL_POS);
077 int end = ParamUtil.getInteger(request, "end", QueryUtil.ALL_POS);
078
079 List<AssetCategory> categories = Collections.emptyList();
080
081 if (categoryId > 0) {
082 categories = AssetCategoryServiceUtil.getChildCategories(
083 categoryId, start, end, null);
084 }
085 else if (vocabularyId > 0) {
086 long parentCategoryId = ParamUtil.getLong(
087 request, "parentCategoryId",
088 AssetCategoryConstants.DEFAULT_PARENT_CATEGORY_ID);
089
090 categories = AssetCategoryServiceUtil.getVocabularyCategories(
091 parentCategoryId, vocabularyId, start, end, null);
092 }
093
094 return categories;
095 }
096
097 }