001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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.kernel.util.StringPool;
025    import com.liferay.portal.kernel.util.Validator;
026    import com.liferay.portal.util.PropsValues;
027    import com.liferay.util.ContentUtil;
028    
029    import java.util.HashMap;
030    import java.util.Map;
031    
032    import javax.portlet.ActionRequest;
033    import javax.portlet.ActionResponse;
034    import javax.portlet.PortletConfig;
035    import javax.portlet.PortletPreferences;
036    
037    /**
038     * @author Alexander Chow
039     */
040    public class ConfigurationActionImpl extends DefaultConfigurationAction {
041    
042            @Override
043            public void processAction(
044                            PortletConfig portletConfig, ActionRequest actionRequest,
045                            ActionResponse actionResponse)
046                    throws Exception {
047    
048                    boolean advancedConfiguration = GetterUtil.getBoolean(
049                            getParameter(actionRequest, "advancedConfiguration"));
050    
051                    if (!advancedConfiguration) {
052                            updateBasicConfiguration(actionRequest);
053                    }
054    
055                    super.processAction(portletConfig, actionRequest, actionResponse);
056            }
057    
058            protected void updateBasicConfiguration(ActionRequest actionRequest)
059                    throws Exception {
060    
061                    PortletPreferences portletPreferences = actionRequest.getPreferences();
062    
063                    String searchConfiguration = portletPreferences.getValue(
064                            "searchConfiguration", StringPool.BLANK);
065    
066                    if (Validator.isBlank(searchConfiguration)) {
067                            searchConfiguration = ContentUtil.get(
068                                    PropsValues.SEARCH_FACET_CONFIGURATION);
069                    }
070    
071                    JSONObject configurationJSONObject = JSONFactoryUtil.createJSONObject(
072                            searchConfiguration);
073    
074                    JSONArray oldFacetsJSONArray = configurationJSONObject.getJSONArray(
075                            "facets");
076    
077                    if (oldFacetsJSONArray == null) {
078                            if (_log.isWarnEnabled()) {
079                                    _log.warn(
080                                            "The resource " + PropsValues.SEARCH_FACET_CONFIGURATION +
081                                                    " is missing a valid facets JSON array");
082                            }
083                    }
084    
085                    JSONArray newFacetsJSONArray = JSONFactoryUtil.createJSONArray();
086    
087                    for (int i = 0; i < oldFacetsJSONArray.length(); i++) {
088                            JSONObject oldFacetJSONObject = oldFacetsJSONArray.getJSONObject(i);
089    
090                            boolean displayFacet = true;
091    
092                            String parameterName = _DISPLAY_FACET_PARAMETER_NAMES.get(
093                                    oldFacetJSONObject.getString("fieldName"));
094    
095                            if (parameterName != null) {
096                                    displayFacet = GetterUtil.getBoolean(
097                                            getParameter(actionRequest, parameterName));
098                            }
099    
100                            if (displayFacet) {
101                                    newFacetsJSONArray.put(oldFacetJSONObject);
102                            }
103                    }
104    
105                    configurationJSONObject.put("facets", newFacetsJSONArray);
106    
107                    searchConfiguration = configurationJSONObject.toString();
108    
109                    setPreference(
110                            actionRequest, "searchConfiguration", searchConfiguration);
111            }
112    
113            private static final Map<String, String> _DISPLAY_FACET_PARAMETER_NAMES =
114                    new HashMap<String, String>() {
115                            {
116                                    put("assetCategoryIds", "displayAssetCategoriesFacet");
117                                    put("assetTagNames", "displayAssetTagsFacet");
118                                    put("entryClassName", "displayAssetTypeFacet");
119                                    put("folderId", "displayFolderFacet");
120                                    put("groupId", "displayScopeFacet");
121                                    put("modified", "displayModifiedRangeFacet");
122                                    put("userName", "displayUserFacet");
123                            }
124                    };
125    
126            private static Log _log = LogFactoryUtil.getLog(
127                    ConfigurationActionImpl.class);
128    
129    }