001/*
002 *  Licensed to the Apache Software Foundation (ASF) under one
003 *  or more contributor license agreements.  See the NOTICE file
004 *  distributed with this work for additional information
005 *  regarding copyright ownership.  The ASF licenses this file
006 *  to you under the Apache License, Version 2.0 (the
007 *  "License"); you may not use this file except in compliance
008 *  with the License.  You may obtain a copy of the License at
009 *
010 *        http://www.apache.org/licenses/LICENSE-2.0
011 *
012 *  Unless required by applicable law or agreed to in writing,
013 *  software distributed under the License is distributed on an
014 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 *  KIND, either express or implied.  See the License for the
016 *  specific language governing permissions and limitations
017 *  under the License.
018 */
019
020package org.apache.isis.core.progmodel.facets.fallback;
021
022import java.lang.reflect.Method;
023import java.util.ArrayList;
024import java.util.HashMap;
025import java.util.List;
026import java.util.Map;
027
028import org.apache.isis.core.commons.config.IsisConfiguration;
029import org.apache.isis.core.commons.config.IsisConfigurationAware;
030import org.apache.isis.core.metamodel.facetapi.Facet;
031import org.apache.isis.core.metamodel.facetapi.FacetHolder;
032import org.apache.isis.core.metamodel.facetapi.FacetUtil;
033import org.apache.isis.core.metamodel.facetapi.FeatureType;
034import org.apache.isis.core.metamodel.facets.FacetFactoryAbstract;
035import org.apache.isis.core.metamodel.facets.FacetedMethod;
036import org.apache.isis.core.metamodel.facets.TypedHolder;
037
038/**
039 * Central point for providing some kind of default for any {@link Facet}s
040 * required by the Apache Isis framework itself.
041 * 
042 */
043public class FallbackFacetFactory extends FacetFactoryAbstract implements IsisConfigurationAware {
044
045    public final static int PAGE_SIZE_STANDALONE_DEFAULT = 25;
046    public final static int PAGE_SIZE_PARENTED_DEFAULT = 12;
047
048    private IsisConfiguration configuration;
049
050    @SuppressWarnings("unused")
051    private final static Map<Class<?>, Integer> TYPICAL_LENGTHS_BY_CLASS = new HashMap<Class<?>, Integer>() {
052        private static final long serialVersionUID = 1L;
053        {
054            putTypicalLength(byte.class, Byte.class, 3);
055            putTypicalLength(short.class, Short.class, 5);
056            putTypicalLength(int.class, Integer.class, 10);
057            putTypicalLength(long.class, Long.class, 20);
058            putTypicalLength(float.class, Float.class, 20);
059            putTypicalLength(double.class, Double.class, 20);
060            putTypicalLength(char.class, Character.class, 1);
061            putTypicalLength(boolean.class, Boolean.class, 1);
062        }
063
064        private void putTypicalLength(final Class<?> primitiveClass, final Class<?> wrapperClass, final int length) {
065            put(primitiveClass, Integer.valueOf(length));
066            put(wrapperClass, Integer.valueOf(length));
067        }
068    };
069
070    public FallbackFacetFactory() {
071        super(FeatureType.EVERYTHING);
072    }
073
074    public boolean recognizes(final Method method) {
075        return false;
076    }
077
078    @Override
079    public void process(final ProcessClassContext processClassContaxt) {
080        final FacetHolder facetHolder = processClassContaxt.getFacetHolder();
081
082        final DescribedAsFacetNone describedAsFacet = new DescribedAsFacetNone(facetHolder);
083        final NotPersistableFacetNull notPersistableFacet = new NotPersistableFacetNull(facetHolder);
084        final TitleFacetNone titleFacet = new TitleFacetNone(facetHolder);
085        final PagedFacetDefault pagedFacet = new PagedFacetDefault(facetHolder, getConfiguration().getInteger("isis.viewers.paged.standalone", PAGE_SIZE_STANDALONE_DEFAULT));
086        
087        final Facet[] facets = new Facet[] { describedAsFacet,
088                // commenting these out, think this whole isNoop business is a little bogus
089                // new ImmutableFacetNever(holder),
090                notPersistableFacet, titleFacet, pagedFacet};
091        FacetUtil.addFacets(facets);
092    }
093
094    @Override
095    public void process(final ProcessMethodContext processMethodContext) {
096        final List<Facet> facets = new ArrayList<Facet>();
097
098        final FacetedMethod facetedMethod = processMethodContext.getFacetHolder();
099        
100        
101        facets.add(new NamedFacetNone(facetedMethod));
102        facets.add(new DescribedAsFacetNone(facetedMethod));
103        facets.add(new HelpFacetNone(facetedMethod));
104
105        
106        final FeatureType featureType = facetedMethod.getFeatureType();
107        if (featureType.isProperty()) {
108            facets.add(new MaxLengthFacetUnlimited(facetedMethod));
109            facets.add(new MultiLineFacetNone(true, facetedMethod));
110        }
111        if (featureType.isAction()) {
112            facets.add(new ActionDefaultsFacetNone(facetedMethod));
113            facets.add(new ActionChoicesFacetNone(facetedMethod));
114        }
115        if (featureType.isCollection()) {
116            facets.add(new PagedFacetDefault(facetedMethod, getConfiguration().getInteger("isis.viewers.paged.parented", PAGE_SIZE_PARENTED_DEFAULT)));
117        }
118
119        FacetUtil.addFacets(facets);
120    }
121
122    @Override
123    public void processParams(final ProcessParameterContext processParameterContext) {
124        final List<Facet> facets = new ArrayList<Facet>();
125
126        final TypedHolder typedHolder = processParameterContext.getFacetHolder();
127        if (typedHolder.getFeatureType().isActionParameter()) {
128            facets.add(new NamedFacetNone(typedHolder));
129            facets.add(new DescribedAsFacetNone(typedHolder));
130            facets.add(new HelpFacetNone(typedHolder));
131            facets.add(new MultiLineFacetNone(false, typedHolder));
132
133            facets.add(new MaxLengthFacetUnlimited(typedHolder));
134        }
135
136        FacetUtil.addFacets(facets);
137    }
138
139    @Override
140    public void setConfiguration(IsisConfiguration configuration) {
141        this.configuration = configuration;
142    }
143    
144    public IsisConfiguration getConfiguration() {
145        return configuration;
146    }
147
148}