001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
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    /**
037     * @author Eduardo Lundgren
038     */
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    }