001/*
002 *  Copyright 2011 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.events;
017
018import java.util.Map;
019
020import org.apache.avalon.framework.parameters.Parameters;
021import org.apache.avalon.framework.service.ServiceException;
022import org.apache.avalon.framework.service.ServiceManager;
023import org.apache.avalon.framework.service.Serviceable;
024import org.apache.cocoon.environment.ObjectModelHelper;
025import org.apache.cocoon.environment.Request;
026import org.apache.cocoon.matching.WildcardURIMatcher;
027import org.apache.cocoon.sitemap.PatternException;
028import org.apache.commons.lang.StringUtils;
029
030import org.ametys.core.util.URIUtils;
031import org.ametys.plugins.repository.AmetysObjectResolver;
032import org.ametys.web.repository.page.ZoneItem;
033
034/**
035 * Matcher which retrieves the zone item from its ID in the request URI and sets it as a request attribute.
036 */
037public class ZoneItemMatcher extends WildcardURIMatcher implements Serviceable
038{
039    /** The ametys object resolver. */
040    protected AmetysObjectResolver _resolver;
041    
042    @Override
043    public void service(ServiceManager manager) throws ServiceException
044    {
045        _resolver = (AmetysObjectResolver) manager.lookup(AmetysObjectResolver.ROLE);
046    }
047    
048    @Override
049    public Map match(String pattern, Map objectModel, Parameters parameters) throws PatternException
050    {
051        Map result = super.match(pattern, objectModel, parameters);
052        
053        String index = parameters.getParameter("zoneItemIndex", "1");
054        
055        if (result != null && StringUtils.isNotEmpty(index))
056        {
057            String zoneItemId = (String) result.get(index);
058            if (StringUtils.isNotEmpty(zoneItemId))
059            {
060                String decodedId = URIUtils.decode(zoneItemId);
061                
062                ZoneItem zoneItem = _resolver.resolveById(decodedId);
063                
064                Request request = ObjectModelHelper.getRequest(objectModel);
065                request.setAttribute(ZoneItem.class.getName(), zoneItem);
066            }
067        }
068        
069        return result;
070    }
071}