001    /**
002     * Copyright (c) 2000-present 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;
016    
017    import com.liferay.portal.kernel.json.JSONArray;
018    import com.liferay.portal.kernel.json.JSONObject;
019    import com.liferay.portal.kernel.search.BooleanClause;
020    import com.liferay.portal.kernel.search.Field;
021    import com.liferay.portal.kernel.search.SearchContext;
022    import com.liferay.portal.kernel.search.facet.config.FacetConfiguration;
023    import com.liferay.portal.kernel.search.filter.Filter;
024    import com.liferay.portal.kernel.util.DateFormatFactoryUtil;
025    import com.liferay.portal.kernel.util.StringUtil;
026    
027    import java.text.DateFormat;
028    
029    import java.util.Calendar;
030    
031    /**
032     * @author Raymond Aug??
033     */
034    public class ModifiedFacet extends RangeFacet {
035    
036            public ModifiedFacet(SearchContext searchContext) {
037                    super(searchContext);
038    
039                    setFieldName(Field.MODIFIED_DATE);
040            }
041    
042            @Override
043            protected BooleanClause<Filter> doGetFacetFilterBooleanClause() {
044                    FacetConfiguration facetConfiguration = getFacetConfiguration();
045    
046                    normalizeDates(facetConfiguration);
047    
048                    return super.doGetFacetFilterBooleanClause();
049            }
050    
051            protected void normalizeDates(FacetConfiguration facetConfiguration) {
052                    Calendar now = Calendar.getInstance();
053    
054                    now.set(Calendar.SECOND, 0);
055                    now.set(Calendar.MINUTE, 0);
056    
057                    Calendar pastHour = (Calendar)now.clone();
058                    pastHour.set(Calendar.HOUR_OF_DAY, now.get(Calendar.HOUR_OF_DAY) - 1);
059    
060                    Calendar past24Hours = (Calendar)now.clone();
061                    past24Hours.set(
062                            Calendar.DAY_OF_YEAR, now.get(Calendar.DAY_OF_YEAR) - 1);
063    
064                    Calendar pastWeek = (Calendar)now.clone();
065                    pastWeek.set(Calendar.DAY_OF_YEAR, now.get(Calendar.DAY_OF_YEAR) - 7);
066    
067                    Calendar pastMonth = (Calendar)now.clone();
068                    pastMonth.set(Calendar.MONTH, now.get(Calendar.MONTH) - 1);
069    
070                    Calendar pastYear = (Calendar)now.clone();
071                    pastYear.set(Calendar.YEAR, now.get(Calendar.YEAR) - 1);
072    
073                    now.set(Calendar.HOUR_OF_DAY, now.get(Calendar.HOUR_OF_DAY) + 1);
074    
075                    DateFormat dateFormat = DateFormatFactoryUtil.getSimpleDateFormat(
076                            "yyyyMMddHHmmss");
077    
078                    JSONObject dataJSONObject = facetConfiguration.getData();
079    
080                    if (!dataJSONObject.has("ranges")) {
081                            return;
082                    }
083    
084                    JSONArray rangesJSONArray = dataJSONObject.getJSONArray("ranges");
085    
086                    for (int i = 0; i < rangesJSONArray.length(); i++) {
087                            JSONObject rangeObject = rangesJSONArray.getJSONObject(i);
088    
089                            String rangeString = rangeObject.getString("range");
090    
091                            rangeString = StringUtil.replace(
092                                    rangeString,
093                                    new String[] {
094                                            "past-hour", "past-24-hours", "past-week", "past-month",
095                                            "past-year", "*"
096                                    },
097                                    new String[] {
098                                            dateFormat.format(pastHour.getTime()),
099                                            dateFormat.format(past24Hours.getTime()),
100                                            dateFormat.format(pastWeek.getTime()),
101                                            dateFormat.format(pastMonth.getTime()),
102                                            dateFormat.format(pastYear.getTime()),
103                                            dateFormat.format(now.getTime())
104                                    }
105                            );
106    
107                            rangeObject.put("range", rangeString);
108                    }
109            }
110    
111    }