    001// Copyright 2008 The Apache Software Foundation
    002//
    003// Licensed under the Apache License, Version 2.0 (the "License");
    004// you may not use this file except in compliance with the License.
    005// You may obtain a copy of the License at
    006//
    007//     http://www.apache.org/licenses/LICENSE-2.0
    008//
    009// Unless required by applicable law or agreed to in writing, software
    010// distributed under the License is distributed on an "AS IS" BASIS,
    011// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    012// See the License for the specific language governing permissions and
    013// limitations under the License.
    014
    015package org.apache.tapestry5.internal.services;
    016
    017import org.apache.tapestry5.Block;
    018import org.apache.tapestry5.internal.structure.Page;
    019import org.apache.tapestry5.ioc.internal.util.CollectionFactory;
    020import org.apache.tapestry5.services.BeanBlockContribution;
    021import org.apache.tapestry5.services.BeanBlockOverrideSource;
    022
    023import java.util.Collection;
    024import java.util.Map;
    025
    026public class BeanBlockOverrideSourceImpl implements BeanBlockOverrideSource
    027{
    028    private final RequestPageCache pageCache;
    029
    030    private final Map<String, BeanBlockContribution> display = CollectionFactory.newCaseInsensitiveMap();
    031
    032    private final Map<String, BeanBlockContribution> edit = CollectionFactory.newCaseInsensitiveMap();
    033
    034    public BeanBlockOverrideSourceImpl(RequestPageCache pageCache,
    035                                       Collection<BeanBlockContribution> configuration)
    036    {
    037        this.pageCache = pageCache;
    038
    039        for (BeanBlockContribution contribution : configuration)
    040        {
    041            Map<String, BeanBlockContribution> map = contribution.isEdit() ? edit : display;
    042            String dataType = contribution.getDataType();
    043
    044            BeanBlockContribution previousValue = map.put(dataType, contribution);
    045            if (previousValue != null)
    046            {
    047                throw new IllegalArgumentException("The BeanBlockOverrideSource configuration contains multiple "
    048                    + (contribution.isEdit() ? "edit" : "display") + " block overrides for data type '" + dataType+ "'.");
    049            }
    050        }
    051    }
    052
    053    public boolean hasDisplayBlock(String datatype)
    054    {
    055        return display.containsKey(datatype);
    056    }
    057
    058    public Block getDisplayBlock(String datatype)
    059    {
    060        return toBlock(display.get(datatype));
    061    }
    062
    063    private Block toBlock(BeanBlockContribution contribution)
    064    {
    065        if (contribution == null) return null;
    066
    067        Page page = pageCache.get(contribution.getPageName());
    068
    069        return page.getRootElement().getBlock(contribution.getBlockId());
    070    }
    071
    072    public Block getEditBlock(String datatype)
    073    {
    074        return toBlock(edit.get(datatype));
    075    }
    076
    077}