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    
015    package org.apache.tapestry5.internal.services;
016    
017    import org.apache.tapestry5.Block;
018    import org.apache.tapestry5.internal.structure.Page;
019    import org.apache.tapestry5.ioc.internal.util.CollectionFactory;
020    import org.apache.tapestry5.services.BeanBlockContribution;
021    import org.apache.tapestry5.services.BeanBlockOverrideSource;
022    
023    import java.util.Collection;
024    import java.util.Map;
025    
026    public 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    
043                map.put(contribution.getDataType(), contribution);
044            }
045        }
046    
047        public boolean hasDisplayBlock(String datatype)
048        {
049            return display.containsKey(datatype);
050        }
051    
052        public Block getDisplayBlock(String datatype)
053        {
054            return toBlock(display.get(datatype));
055        }
056    
057        private Block toBlock(BeanBlockContribution contribution)
058        {
059            if (contribution == null) return null;
060    
061            Page page = pageCache.get(contribution.getPageName());
062    
063            return page.getRootElement().getBlock(contribution.getBlockId());
064        }
065    
066        public Block getEditBlock(String datatype)
067        {
068            return toBlock(edit.get(datatype));
069        }
070    
071    }