001    /**
002     * Copyright (c) 2000-2012 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 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    }