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.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 displayScopeFacet = GetterUtil.getBoolean(
059                            getParameter(actionRequest, "displayScopeFacet"));
060                    boolean displayAssetCategoriesFacet = GetterUtil.getBoolean(
061                            getParameter(actionRequest, "displayAssetCategoriesFacet"));
062                    boolean displayAssetTagsFacet = GetterUtil.getBoolean(
063                            getParameter(actionRequest, "displayAssetTagsFacet"));
064                    boolean displayAssetTypeFacet = GetterUtil.getBoolean(
065                            getParameter(actionRequest, "displayAssetTypeFacet"));
066                    boolean displayFolderFacet = GetterUtil.getBoolean(
067                            getParameter(actionRequest, "displayFolderFacet"));
068                    boolean displayUserFacet = GetterUtil.getBoolean(
069                            getParameter(actionRequest, "displayUserFacet"));
070                    boolean displayModifiedRangeFacet = GetterUtil.getBoolean(
071                            getParameter(actionRequest, "displayModifiedRangeFacet"));
072    
073                    String searchConfiguration = ContentUtil.get(
074                            PropsValues.SEARCH_FACET_CONFIGURATION);
075    
076                    JSONObject configurationJSONObject = JSONFactoryUtil.createJSONObject(
077                            searchConfiguration);
078    
079                    JSONArray oldFacetsJSONArray = configurationJSONObject.getJSONArray(
080                            "facets");
081    
082                    if (oldFacetsJSONArray == null) {
083                            if (_log.isWarnEnabled()) {
084                                    _log.warn(
085                                            "The resource " + PropsValues.SEARCH_FACET_CONFIGURATION +
086                                                    " is missing a valid facets JSON array");
087                            }
088                    }
089    
090                    JSONArray newFacetsJSONArray = JSONFactoryUtil.createJSONArray();
091    
092                    for (int i = 0; i < oldFacetsJSONArray.length(); i++) {
093                            JSONObject oldFacetJSONObject = oldFacetsJSONArray.getJSONObject(i);
094    
095                            String fieldName = oldFacetJSONObject.getString("fieldName");
096    
097                            if ((displayScopeFacet && fieldName.equals("groupId")) ||
098                                    (displayAssetCategoriesFacet &&
099                                     fieldName.equals("assetCategoryTitles")) ||
100                                    (displayAssetTagsFacet && fieldName.equals("assetTagNames")) ||
101                                    (displayAssetTypeFacet && fieldName.equals("entryClassName")) ||
102                                    (displayFolderFacet && fieldName.equals("folderId")) ||
103                                    (displayUserFacet && fieldName.equals("userId")) ||
104                                    (displayModifiedRangeFacet && fieldName.equals("modified"))) {
105    
106                                    newFacetsJSONArray.put(oldFacetJSONObject);
107                            }
108                    }
109    
110                    configurationJSONObject.put("facets", newFacetsJSONArray);
111    
112                    searchConfiguration = configurationJSONObject.toString();
113    
114                    setPreference(
115                            actionRequest, "searchConfiguration", searchConfiguration);
116            }
117    
118            private static Log _log = LogFactoryUtil.getLog(
119                    ConfigurationActionImpl.class);
120    
121    }