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.portal.kernel.search.facet.config;
016    
017    import com.liferay.portal.kernel.json.JSONArray;
018    import com.liferay.portal.kernel.json.JSONFactoryUtil;
019    import com.liferay.portal.kernel.json.JSONObject;
020    import com.liferay.portal.kernel.log.Log;
021    import com.liferay.portal.kernel.log.LogFactoryUtil;
022    import com.liferay.portal.kernel.util.Validator;
023    
024    import java.util.ArrayList;
025    import java.util.List;
026    
027    /**
028     * @author Raymond Aug??
029     */
030    public class FacetConfigurationUtil {
031    
032            public static List<FacetConfiguration> load(String configuration) {
033                    List<FacetConfiguration> facetConfigurations =
034                            new ArrayList<FacetConfiguration>();
035    
036                    try {
037                            if (Validator.isNull(configuration)) {
038                                    return facetConfigurations;
039                            }
040    
041                            JSONObject configurationJSONObject =
042                                    JSONFactoryUtil.createJSONObject(configuration);
043    
044                            JSONArray facetsJSONArray = configurationJSONObject.getJSONArray(
045                                    "facets");
046    
047                            if (facetsJSONArray == null) {
048                                    return facetConfigurations;
049                            }
050    
051                            for (int i = 0; i < facetsJSONArray.length(); i++) {
052                                    JSONObject facetJSONObject = facetsJSONArray.getJSONObject(i);
053    
054                                    FacetConfiguration facetConfiguration = _toFacetConfiguration(
055                                            facetJSONObject);
056    
057                                    facetConfigurations.add(facetConfiguration);
058                            }
059                    }
060                    catch (Exception e) {
061                            _log.error(e, e);
062                    }
063    
064                    return facetConfigurations;
065            }
066    
067            private static FacetConfiguration _toFacetConfiguration(
068                    JSONObject facetJSONObject) {
069    
070                    FacetConfiguration facetConfiguration = new FacetConfiguration();
071    
072                    facetConfiguration.setClassName(facetJSONObject.getString("className"));
073    
074                    if (facetJSONObject.has("data")) {
075                            facetConfiguration.setDataJSONObject(
076                                    facetJSONObject.getJSONObject("data"));
077                    }
078    
079                    facetConfiguration.setDisplayStyle(
080                            facetJSONObject.getString("displayStyle"));
081                    facetConfiguration.setFieldName(facetJSONObject.getString("fieldName"));
082                    facetConfiguration.setLabel(facetJSONObject.getString("label"));
083                    facetConfiguration.setOrder(facetJSONObject.getString("order"));
084                    facetConfiguration.setStatic(facetJSONObject.getBoolean("static"));
085                    facetConfiguration.setWeight(facetJSONObject.getDouble("weight"));
086    
087                    return facetConfiguration;
088            }
089    
090            private static Log _log = LogFactoryUtil.getLog(
091                    FacetConfigurationUtil.class);
092    
093    }