001/* 
002 * (C) Copyright 2006-2011 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     anguenot
018 *
019 * $Id: DateRangeParser.java 20577 2007-06-16 09:26:07Z sfermigier $
020 */
021
022package org.nuxeo.ecm.platform.audit.api.query;
023
024import java.util.Calendar;
025import java.util.Date;
026import java.util.HashMap;
027import java.util.Map;
028
029/**
030 * Date range parser.
031 *
032 * @author <a href="mailto:ja@nuxeo.com">Julien Anguenot</a>
033 */
034public final class DateRangeParser {
035
036    // Utility class.
037    private DateRangeParser() {
038    }
039
040    public static Date parseDateRangeQuery(Date now, String dateRangeQuery) throws AuditQueryException {
041        try {
042            Calendar calendar = Calendar.getInstance();
043            calendar.setTime(now);
044            if (dateRangeQuery != null) {
045                Map<String, Integer> parsed = parseQuery(dateRangeQuery);
046                if (parsed.containsKey(DateRangeQueryConstants.HOUR)) {
047                    calendar.add(Calendar.HOUR_OF_DAY, -parsed.get(DateRangeQueryConstants.HOUR));
048                }
049                if (parsed.containsKey(DateRangeQueryConstants.MIN)) {
050                    calendar.add(Calendar.MINUTE, -parsed.get(DateRangeQueryConstants.MIN));
051                }
052            }
053            return calendar.getTime();
054        } catch (NumberFormatException nfe) {
055            throw new AuditQueryException("Invalid query format...", nfe);
056        }
057    }
058
059    private static Map<String, Integer> parseQuery(String query) throws AuditQueryException {
060        Map<String, Integer> parsed = new HashMap<String, Integer>();
061
062        query = query.trim();
063        query = query.replace(" ", "");
064
065        int offsetMinutes = query.indexOf(DateRangeQueryConstants.MIN);
066        int offsetHours = query.indexOf(DateRangeQueryConstants.HOUR);
067
068        if (offsetMinutes != -1) {
069            String sub = query.substring(0, offsetMinutes);
070            try {
071                parsed.put(DateRangeQueryConstants.MIN, Integer.parseInt(sub));
072            } catch (NumberFormatException nfe) {
073                throw new AuditQueryException(nfe.getMessage(), nfe);
074            }
075        }
076        if (offsetHours != -1) {
077            String sub;
078            if (offsetMinutes == -1) {
079                sub = query.substring(0, offsetHours);
080            } else {
081                sub = query.substring(offsetMinutes + 1, offsetHours);
082            }
083            try {
084                parsed.put(DateRangeQueryConstants.HOUR, Integer.parseInt(sub));
085            } catch (NumberFormatException nfe) {
086                throw new AuditQueryException(nfe.getMessage(), nfe);
087            }
088        }
089        return parsed;
090    }
091
092}