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 org.apache.cocoon.environment.Request;
019
020import org.ametys.plugins.repository.AmetysRepositoryException;
021import org.ametys.web.frontoffice.search.instance.SearchServiceInstance;
022import org.ametys.web.frontoffice.search.requesttime.SearchServiceGenerator;
023import org.ametys.web.repository.page.ZoneItem;
024import org.ametys.web.repository.page.ZoneItem.ZoneType;
025
026/**
027 * Generator for search service on calendar events
028 *
029 */
030public class CalendarSearchServiceGenerator extends SearchServiceGenerator
031{
032    @Override
033    protected SearchServiceInstance getInstance(Request request, String zoneItemId)
034    {
035        ZoneItem zoneItem = null;
036        try
037        {
038            zoneItem = _ametysObjectResolver.resolveById(zoneItemId);
039            
040        }
041        catch (AmetysRepositoryException e)
042        {
043            // Nothing
044        }
045        
046        if (zoneItem != null && zoneItem.getType() == ZoneType.SERVICE && CalendarSearchService.SERVICE_ID.equals(zoneItem.getServiceId()))
047        {
048            return _searchServiceInstanceManager.get(zoneItemId);
049        }
050        else
051        {
052            throw new IllegalArgumentException("No search service instance in zone item '" + zoneItemId + "'");
053        }
054    }
055}