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.search.action;
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.portlet.DefaultConfigurationAction;
023    import com.liferay.portal.kernel.util.GetterUtil;
024    import com.liferay.portal.util.PropsValues;
025    import com.liferay.util.ContentUtil;
026    
027    import javax.portlet.ActionRequest;
028    import javax.portlet.ActionResponse;
029    import javax.portlet.PortletConfig;
030    
031    /**
032     * @author Alexander Chow
033     */
034    public class ConfigurationActionImpl extends DefaultConfigurationAction {
035    
036            @Override
037            public void processAction(
038                            PortletConfig portletConfig, ActionRequest actionRequest,
039                            ActionResponse actionResponse)
040                    throws Exception {
041    
042                    boolean advancedConfiguration = GetterUtil.getBoolean(
043                            getParameter(actionRequest, "advancedConfiguration"));
044    
045                    if (!advancedConfiguration) {
046                            updateBasicConfiguration(
047                                    portletConfig, actionRequest, actionResponse);
048                    }
049    
050                    super.processAction(portletConfig, actionRequest, actionResponse);
051            }
052    
053            protected void updateBasicConfiguration(
054                            PortletConfig portletConfig, ActionRequest actionRequest,
055                            ActionResponse actionResponse)
056                    throws Exception {
057    
058                    boolean displayAssetCategoriesFacet = GetterUtil.getBoolean(
059                            getParameter(actionRequest, "displayAssetCategoriesFacet"));
060                    boolean displayAssetTagsFacet = GetterUtil.getBoolean(
061                            getParameter(actionRequest, "displayAssetTagsFacet"));
062                    boolean displayAssetTypeFacet = GetterUtil.getBoolean(
063                            getParameter(actionRequest, "displayAssetTypeFacet"));
064                    boolean displayModifiedRangeFacet = GetterUtil.getBoolean(
065                            getParameter(actionRequest, "displayModifiedRangeFacet"));
066    
067                    String searchConfiguration = ContentUtil.get(
068                            PropsValues.SEARCH_FACET_CONFIGURATION);
069    
070                    JSONObject configurationJSONObject = JSONFactoryUtil.createJSONObject(
071                            searchConfiguration);
072    
073                    JSONArray oldFacetsJSONArray = configurationJSONObject.getJSONArray(
074                            "facets");
075    
076                    if (oldFacetsJSONArray == null) {
077                            if (_log.isWarnEnabled()) {
078                                    _log.warn(
079                                            "The resource " + PropsValues.SEARCH_FACET_CONFIGURATION +
080                                                    " is missing a valid facets JSON array");
081                            }
082                    }
083    
084                    JSONArray newFacetsJSONArray = JSONFactoryUtil.createJSONArray();
085    
086                    for (int i = 0; i < oldFacetsJSONArray.length(); i++) {
087                            JSONObject oldFacetJSONObject = oldFacetsJSONArray.getJSONObject(i);
088    
089                            String fieldName = oldFacetJSONObject.getString("fieldName");
090    
091                            if ((displayAssetCategoriesFacet &&
092                                            fieldName.equals("assetCategoryTitles")) ||
093                                    (displayAssetTagsFacet && fieldName.equals("entryClassName")) ||
094                                    (displayAssetTypeFacet && fieldName.equals("assetTagNames")) ||
095                                    (displayModifiedRangeFacet && fieldName.equals("modified"))) {
096    
097                                    newFacetsJSONArray.put(oldFacetJSONObject);
098                            }
099                    }
100    
101                    configurationJSONObject.put("facets", newFacetsJSONArray);
102    
103                    searchConfiguration = configurationJSONObject.toString();
104    
105                    setPreference(
106                            actionRequest, "searchConfiguration", searchConfiguration);
107            }
108    
109            private static Log _log = LogFactoryUtil.getLog(
110                    ConfigurationActionImpl.class);
111    
112    }