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.collections.sortedby; 021 022import java.util.Comparator; 023import java.util.List; 024 025import org.apache.isis.applib.annotation.SortedBy; 026import org.apache.isis.core.commons.config.IsisConfiguration; 027import org.apache.isis.core.metamodel.facetapi.FacetHolder; 028import org.apache.isis.core.metamodel.facetapi.FacetUtil; 029import org.apache.isis.core.metamodel.facetapi.FeatureType; 030import org.apache.isis.core.metamodel.facetapi.MetaModelValidatorRefiner; 031import org.apache.isis.core.metamodel.facets.Annotations; 032import org.apache.isis.core.metamodel.facets.FacetFactoryAbstract; 033import org.apache.isis.core.metamodel.facets.collections.sortedby.SortedByFacet; 034import org.apache.isis.core.metamodel.spec.ObjectSpecification; 035import org.apache.isis.core.metamodel.spec.feature.Contributed; 036import org.apache.isis.core.metamodel.spec.feature.OneToManyAssociation; 037import org.apache.isis.core.metamodel.specloader.validator.MetaModelValidatorComposite; 038import org.apache.isis.core.metamodel.specloader.validator.MetaModelValidatorVisiting; 039import org.apache.isis.core.metamodel.specloader.validator.MetaModelValidatorVisiting.Visitor; 040import org.apache.isis.core.metamodel.specloader.validator.ValidationFailures; 041 042/** 043 * There is no check that the value is a {@link Comparator}; instead this is done through 044 * the {@link #refineMetaModelValidator(MetaModelValidatorComposite, IsisConfiguration)}. 045 */ 046public class SortedByAnnotationFacetFactory extends FacetFactoryAbstract implements MetaModelValidatorRefiner { 047 048 public SortedByAnnotationFacetFactory() { 049 super(FeatureType.COLLECTIONS_ONLY); 050 } 051 052 @Override 053 public void process(final ProcessMethodContext processMethodContext) { 054 final SortedBy renderAnnotation = Annotations.getAnnotation(processMethodContext.getMethod(), SortedBy.class); 055 FacetUtil.addFacet(create(renderAnnotation, processMethodContext.getFacetHolder())); 056 } 057 058 private SortedByFacet create(final SortedBy annotation, final FacetHolder holder) { 059 if (annotation == null) { 060 return null; 061 } 062 final Class<?> annotationValue = annotation.value(); 063 @SuppressWarnings({ "rawtypes", "unchecked" }) 064 final Class<? extends Comparator<?>> comparatorType = (Class)annotationValue; 065 return new SortedByFacetAnnotation(holder, comparatorType); 066 } 067 068 @Override 069 public void refineMetaModelValidator(MetaModelValidatorComposite metaModelValidator, IsisConfiguration configuration) { 070 metaModelValidator.add(new MetaModelValidatorVisiting(newValidatorVisitor())); 071 } 072 073 protected Visitor newValidatorVisitor() { 074 return new MetaModelValidatorVisiting.Visitor() { 075 076 @Override 077 public boolean visit(ObjectSpecification objectSpec, ValidationFailures validationFailures) { 078 List<OneToManyAssociation> objectCollections = objectSpec.getCollections(Contributed.EXCLUDED); 079 for (OneToManyAssociation objectCollection : objectCollections) { 080 final SortedByFacet facet = objectCollection.getFacet(SortedByFacet.class); 081 if(facet != null) { 082 final Class<? extends Comparator<?>> cls = facet.value(); 083 if(!Comparator.class.isAssignableFrom(cls)) { 084 validationFailures.add("%s#%s is annotated with @SortedBy, but the class specified '%s' is not a Comparator", objectSpec.getIdentifier().getClassName(), objectCollection.getId(), facet.value().getName()); 085 } 086 } 087 } 088 return true; 089 } 090 }; 091 } 092 093 094}