001// Copyright 2007, 2008, 2011 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.services.BeanBlockContribution;
019import org.apache.tapestry5.services.BeanBlockOverrideSource;
020import org.apache.tapestry5.services.BeanBlockSource;
021
022import java.util.Collection;
023
024public class BeanBlockSourceImpl implements BeanBlockSource
025{
026    // This is checked before masterSource
027    private final BeanBlockOverrideSource overrideSource;
028
029    private final BeanBlockOverrideSource masterSource;
030
031    public BeanBlockSourceImpl(RequestPageCache pageCache,
032                               BeanBlockOverrideSource overrideSource, Collection<BeanBlockContribution> configuration)
033    {
034        this.overrideSource = overrideSource;
035        masterSource = new BeanBlockOverrideSourceImpl(pageCache, configuration);
036    }
037
038    public boolean hasDisplayBlock(String datatype)
039    {
040        return overrideSource.hasDisplayBlock(datatype) || masterSource.hasDisplayBlock(datatype);
041    }
042
043    public Block getDisplayBlock(String datatype)
044    {
045        Block result = overrideSource.getDisplayBlock(datatype);
046
047        if (result == null)
048            result = masterSource.getDisplayBlock(datatype);
049
050        if (result == null)
051            throw new RuntimeException(String.format("There is no defined way to display data of type '%s'. Make a contribution to the BeanBlockSource service for this type.", datatype));
052
053        return result;
054    }
055
056    public Block getEditBlock(String datatype)
057    {
058        Block result = overrideSource.getEditBlock(datatype);
059
060        if (result == null)
061            result = masterSource.getEditBlock(datatype);
062
063        if (result == null)
064            throw new RuntimeException(String.format("There is no defined way to edit data of type '%s'.  Make a contribution to the BeanBlockSource service for this type.", datatype));
065
066        return result;
067    }
068
069}