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    
059                    pastHour.set(Calendar.HOUR_OF_DAY, now.get(Calendar.HOUR_OF_DAY) - 1);
060    
061                    Calendar past24Hours = (Calendar)now.clone();
062    
063                    past24Hours.set(
064                            Calendar.DAY_OF_YEAR, now.get(Calendar.DAY_OF_YEAR) - 1);
065    
066                    Calendar pastWeek = (Calendar)now.clone();
067    
068                    pastWeek.set(Calendar.DAY_OF_YEAR, now.get(Calendar.DAY_OF_YEAR) - 7);
069    
070                    Calendar pastMonth = (Calendar)now.clone();
071    
072                    pastMonth.set(Calendar.MONTH, now.get(Calendar.MONTH) - 1);
073    
074                    Calendar pastYear = (Calendar)now.clone();
075    
076                    pastYear.set(Calendar.YEAR, now.get(Calendar.YEAR) - 1);
077    
078                    now.set(Calendar.HOUR_OF_DAY, now.get(Calendar.HOUR_OF_DAY) + 1);
079    
080                    DateFormat dateFormat = DateFormatFactoryUtil.getSimpleDateFormat(
081                            "yyyyMMddHHmmss");
082    
083                    JSONObject dataJSONObject = facetConfiguration.getData();
084    
085                    if (!dataJSONObject.has("ranges")) {
086                            return;
087                    }
088    
089                    JSONArray rangesJSONArray = dataJSONObject.getJSONArray("ranges");
090    
091                    for (int i = 0; i < rangesJSONArray.length(); i++) {
092                            JSONObject rangeObject = rangesJSONArray.getJSONObject(i);
093    
094                            String rangeString = rangeObject.getString("range");
095    
096                            rangeString = StringUtil.replace(
097                                    rangeString,
098                                    new String[] {
099                                            "past-hour", "past-24-hours", "past-week", "past-month",
100                                            "past-year", "*"
101                                    },
102                                    new String[] {
103                                            dateFormat.format(pastHour.getTime()),
104                                            dateFormat.format(past24Hours.getTime()),
105                                            dateFormat.format(pastWeek.getTime()),
106                                            dateFormat.format(pastMonth.getTime()),
107                                            dateFormat.format(pastYear.getTime()),
108                                            dateFormat.format(now.getTime())
109                                    });
110    
111                            rangeObject.put("range", rangeString);
112                    }
113            }
114    
115    }