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