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.object.callbacks.persist; 021 022import java.lang.reflect.Method; 023import java.util.ArrayList; 024import java.util.List; 025 026import org.apache.isis.core.metamodel.facetapi.Facet; 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.facets.object.callbacks.PersistedCallbackFacet; 031import org.apache.isis.core.metamodel.facets.object.callbacks.PersistingCallbackFacet; 032import org.apache.isis.core.metamodel.methodutils.MethodScope; 033import org.apache.isis.core.progmodel.facets.MethodFinderUtils; 034import org.apache.isis.core.progmodel.facets.MethodPrefixBasedFacetFactoryAbstract; 035import org.apache.isis.core.progmodel.facets.MethodPrefixConstants; 036 037public class PersistCallbackViaSaveMethodFacetFactory extends MethodPrefixBasedFacetFactoryAbstract { 038 039 private static final String[] PREFIXES = { MethodPrefixConstants.SAVED_PREFIX, MethodPrefixConstants.SAVING_PREFIX, }; 040 041 public PersistCallbackViaSaveMethodFacetFactory() { 042 super(FeatureType.OBJECTS_ONLY, OrphanValidation.VALIDATE, PREFIXES); 043 } 044 045 @Override 046 public void process(final ProcessClassContext processClassContext) { 047 final Class<?> cls = processClassContext.getCls(); 048 final FacetHolder facetHolder = processClassContext.getFacetHolder(); 049 050 final List<Facet> facets = new ArrayList<Facet>(); 051 final List<Method> methods = new ArrayList<Method>(); 052 053 Method method = null; 054 method = MethodFinderUtils.findMethod(cls, MethodScope.OBJECT, MethodPrefixConstants.SAVING_PREFIX, void.class, NO_PARAMETERS_TYPES); 055 if (method != null) { 056 methods.add(method); 057 final PersistingCallbackFacet facet = facetHolder.getFacet(PersistingCallbackFacet.class); 058 if (facet == null) { 059 facets.add(new PersistingCallbackFacetViaMethod(method, facetHolder)); 060 } else { 061 facet.addMethod(method); 062 } 063 } 064 065 method = MethodFinderUtils.findMethod(cls, MethodScope.OBJECT, MethodPrefixConstants.SAVED_PREFIX, void.class, NO_PARAMETERS_TYPES); 066 if (method != null) { 067 methods.add(method); 068 final PersistedCallbackFacet facet = facetHolder.getFacet(PersistedCallbackFacet.class); 069 if (facet == null) { 070 facets.add(new PersistedCallbackFacetViaMethod(method, facetHolder)); 071 } else { 072 facet.addMethod(method); 073 } 074 } 075 076 processClassContext.removeMethods(methods); 077 FacetUtil.addFacets(facets); 078 } 079 080}