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    
020    package org.apache.isis.core.progmodel.facets.fallback;
021    
022    import java.lang.reflect.Method;
023    import java.util.ArrayList;
024    import java.util.HashMap;
025    import java.util.List;
026    import java.util.Map;
027    
028    import org.apache.isis.core.metamodel.facetapi.Facet;
029    import org.apache.isis.core.metamodel.facetapi.FacetHolder;
030    import org.apache.isis.core.metamodel.facetapi.FacetUtil;
031    import org.apache.isis.core.metamodel.facetapi.FeatureType;
032    import org.apache.isis.core.metamodel.facets.FacetFactoryAbstract;
033    import org.apache.isis.core.metamodel.facets.FacetedMethod;
034    import org.apache.isis.core.metamodel.facets.TypedHolder;
035    
036    /**
037     * Central point for providing some kind of default for any {@link Facet}s required by the Apache Isis framework itself.
038     * 
039     */
040    public class FallbackFacetFactory extends FacetFactoryAbstract {
041    
042        @SuppressWarnings("unused")
043        private final static Map<Class<?>, Integer> TYPICAL_LENGTHS_BY_CLASS = new HashMap<Class<?>, Integer>() {
044            private static final long serialVersionUID = 1L;
045            {
046                putTypicalLength(byte.class, Byte.class, 3);
047                putTypicalLength(short.class, Short.class, 5);
048                putTypicalLength(int.class, Integer.class, 10);
049                putTypicalLength(long.class, Long.class, 20);
050                putTypicalLength(float.class, Float.class, 20);
051                putTypicalLength(double.class, Double.class, 20);
052                putTypicalLength(char.class, Character.class, 1);
053                putTypicalLength(boolean.class, Boolean.class, 1);
054            }
055    
056            private void putTypicalLength(final Class<?> primitiveClass, final Class<?> wrapperClass, final int length) {
057                put(primitiveClass, Integer.valueOf(length));
058                put(wrapperClass, Integer.valueOf(length));
059            }
060        };
061    
062        public FallbackFacetFactory() {
063            super(FeatureType.EVERYTHING);
064        }
065    
066        public boolean recognizes(final Method method) {
067            return false;
068        }
069    
070        @Override
071        public void process(final ProcessClassContext processClassContaxt) {
072            final FacetHolder facetHolder = processClassContaxt.getFacetHolder();
073    
074            final DescribedAsFacetNone describedAsFacet = new DescribedAsFacetNone(facetHolder);
075            final NotPersistableFacetNull notPersistableFacet = new NotPersistableFacetNull(facetHolder);
076            final TitleFacetNone titleFacet = new TitleFacetNone(facetHolder);
077    
078            final Facet[] facets = new Facet[] { describedAsFacet,
079                // commenting these out, think this whole isNoop business is a little bogus
080                // new ImmutableFacetNever(holder),
081                notPersistableFacet, titleFacet, };
082            FacetUtil.addFacets(facets);
083        }
084    
085        @Override
086        public void process(final ProcessMethodContext processMethodContext) {
087            final List<Facet> facets = new ArrayList<Facet>();
088    
089            if (processMethodContext.getFacetHolder() instanceof FacetedMethod) {
090                facets.add(new NamedFacetNone(processMethodContext.getFacetHolder()));
091                facets.add(new DescribedAsFacetNone(processMethodContext.getFacetHolder()));
092                facets.add(new HelpFacetNone(processMethodContext.getFacetHolder()));
093    
094                final FacetedMethod facetedMethod = processMethodContext.getFacetHolder();
095                final FeatureType featureType = facetedMethod.getFeatureType();
096                if (featureType.isProperty()) {
097                    facets.add(new MaxLengthFacetUnlimited(processMethodContext.getFacetHolder()));
098                    facets.add(new MultiLineFacetNone(true, processMethodContext.getFacetHolder()));
099                }
100                if (featureType.isAction()) {
101                    facets.add(new ExecutedFacetAtDefault(processMethodContext.getFacetHolder()));
102                    facets.add(new ActionDefaultsFacetNone(processMethodContext.getFacetHolder()));
103                    facets.add(new ActionChoicesFacetNone(processMethodContext.getFacetHolder()));
104                }
105            }
106    
107            FacetUtil.addFacets(facets);
108        }
109    
110        @Override
111        public void processParams(final ProcessParameterContext processParameterContext) {
112            final List<Facet> facets = new ArrayList<Facet>();
113    
114            if (processParameterContext.getFacetHolder() instanceof TypedHolder) {
115    
116                final TypedHolder typedHolder = processParameterContext.getFacetHolder();
117                if (typedHolder.getFeatureType().isActionParameter()) {
118                    facets.add(new NamedFacetNone(processParameterContext.getFacetHolder()));
119                    facets.add(new DescribedAsFacetNone(processParameterContext.getFacetHolder()));
120                    facets.add(new HelpFacetNone(processParameterContext.getFacetHolder()));
121                    facets.add(new MultiLineFacetNone(false, processParameterContext.getFacetHolder()));
122    
123                    facets.add(new MaxLengthFacetUnlimited(processParameterContext.getFacetHolder()));
124                }
125            }
126    
127            FacetUtil.addFacets(facets);
128        }
129    
130    }