001    /**
002     * Copyright (c) 2000-2013 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.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    /**
037     * @author Eduardo Lundgren
038     */
039    public class GetCategoriesAction extends JSONAction {
040    
041            @Override
042            public String getJSON(
043                            ActionMapping actionMapping, ActionForm actionForm,
044                            HttpServletRequest request, 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("childrenCount", childCategories.size());
060                            jsonObject.put("hasChildren", !childCategories.isEmpty());
061                            jsonObject.put("name", category.getName());
062                            jsonObject.put("parentCategoryId", category.getParentCategoryId());
063                            jsonObject.put(
064                                    "titleCurrentValue", category.getTitleCurrentValue());
065    
066                            jsonArray.put(jsonObject);
067                    }
068    
069                    return jsonArray.toString();
070            }
071    
072            protected List<AssetCategory> getCategories(HttpServletRequest request)
073                    throws Exception {
074    
075                    long categoryId = ParamUtil.getLong(request, "categoryId");
076                    long vocabularyId = ParamUtil.getLong(request, "vocabularyId");
077                    int start = ParamUtil.getInteger(request, "start", QueryUtil.ALL_POS);
078                    int end = ParamUtil.getInteger(request, "end", QueryUtil.ALL_POS);
079    
080                    List<AssetCategory> categories = Collections.emptyList();
081    
082                    if (categoryId > 0) {
083                            categories = AssetCategoryServiceUtil.getChildCategories(
084                                    categoryId, start, end, null);
085                    }
086                    else if (vocabularyId > 0) {
087                            long parentCategoryId = ParamUtil.getLong(
088                                    request, "parentCategoryId",
089                                    AssetCategoryConstants.DEFAULT_PARENT_CATEGORY_ID);
090    
091                            categories = AssetCategoryServiceUtil.getVocabularyCategories(
092                                    parentCategoryId, vocabularyId, start, end, null);
093                    }
094    
095                    return categories;
096            }
097    
098    }