001/* 002 * Copyright 2022 Anyware Services 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 */ 016package org.ametys.plugins.calendar.search; 017 018import java.time.ZonedDateTime; 019 020import org.ametys.cms.search.query.AndQuery; 021import org.ametys.cms.search.query.DateTimeQuery; 022import org.ametys.cms.search.query.NotQuery; 023import org.ametys.cms.search.query.OrQuery; 024import org.ametys.cms.search.query.Query; 025import org.ametys.cms.search.query.Query.Operator; 026import org.ametys.plugins.calendar.events.EventsFilterHelper.DateTimeRange; 027import org.ametys.web.frontoffice.search.requesttime.SearchComponent; 028import org.ametys.web.frontoffice.search.requesttime.SearchComponentArguments; 029import org.ametys.web.frontoffice.search.requesttime.impl.SearchComponentHelper; 030 031 032/** 033 * {@link SearchComponent} to add date range to query 034 */ 035public class DateRangeCriterionSearchComponent implements SearchComponent 036{ 037 /** The helper for search component */ 038 protected SearchComponentHelper _searchComponentHelper; 039 040 @Override 041 public int priority() 042 { 043 return SEARCH_PRIORITY - 7999; // after CriterionTreeSearchComponent 044 } 045 046 @Override 047 public boolean supports(SearchComponentArguments args) 048 { 049 return CalendarSearchService.isFormSubmit(args) && CalendarSearchService.isActive(args); 050 } 051 052 @Override 053 public void execute(SearchComponentArguments args) throws Exception 054 { 055 DateTimeRange dateRange = CalendarSearchService.getDateRange(args); 056 if (dateRange != null) 057 { 058 ZonedDateTime fromDateTime = dateRange.fromDate(); 059 ZonedDateTime untilDateTime = dateRange.untilDate(); 060 061 Query startBeforeQuery = new DateTimeQuery(CalendarSearchService.START_DATE_ATTRIBUTE_NAME, Operator.LT, untilDateTime); 062 Query endAfterExpr = new DateTimeQuery(CalendarSearchService.END_DATE_ATTRIBUTE_NAME, Operator.GE, fromDateTime); 063 Query startAfterExpr = new DateTimeQuery(CalendarSearchService.START_DATE_ATTRIBUTE_NAME, Operator.GE, fromDateTime); 064 Query noEndExpr = new NotQuery(new DateTimeQuery(CalendarSearchService.END_DATE_ATTRIBUTE_NAME)); 065 066 // start-date < untilDate AND (end-date >= fromDate OR not(end-date) AND start-date >= fromDate) 067 args.searcher() 068 .addFilterQuery(new AndQuery(startBeforeQuery, new OrQuery(endAfterExpr, new AndQuery(noEndExpr, startAfterExpr)))); 069 } 070 else 071 { 072 // filter on start-date exists 073 args.searcher() 074 .addFilterQuery(new DateTimeQuery(CalendarSearchService.START_DATE_ATTRIBUTE_NAME)); 075 } 076 } 077}