1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
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  /**
37   * <a href="GetCategoriesAction.java.html"><b><i>View Source</i></b></a>
38   *
39   * @author Eduardo Lundgren
40   */
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  }