/*
 *  Licensed to the Apache Software Foundation (ASF) under one
 *  or more contributor license agreements.  See the NOTICE file
 *  distributed with this work for additional information
 *  regarding copyright ownership.  The ASF licenses this file
 *  to you under the Apache License, Version 2.0 (the
 *  "License"); you may not use this file except in compliance
 *  with the License.  You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing,
 *  software distributed under the License is distributed on an
 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 *  KIND, either express or implied.  See the License for the
 *  specific language governing permissions and limitations
 *  under the License.
 */
package org.apache.isis.objectstore.jdo.service;

import java.util.Map;
import java.util.Set;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.jdo.annotations.PersistenceCapable;

import com.google.common.base.Function;
import com.google.common.base.Predicate;
import com.google.common.base.Splitter;
import com.google.common.base.Strings;
import com.google.common.collect.Iterables;

import org.reflections.Reflections;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.apache.isis.applib.annotation.Hidden;
import org.apache.isis.core.metamodel.spec.SpecificationLoaderSpi;
import org.apache.isis.core.runtime.system.context.IsisContext;

@Hidden
public class RegisterEntities {

    @SuppressWarnings("unused")
    private final static Logger LOG = LoggerFactory.getLogger(RegisterEntities.class);
    
    public final static String PACKAGE_PREFIX_KEY = "isis.persistor.datanucleus.RegisterEntities.packagePrefix";

    // //////////////////////////////////////

    private String packagePrefixes;

    @PostConstruct
    public void init(Map<String,String> configuration) {
        packagePrefixes = configuration.get(PACKAGE_PREFIX_KEY);
        if(Strings.isNullOrEmpty(packagePrefixes)) {
            throw new IllegalStateException("Could not locate '" + PACKAGE_PREFIX_KEY + "' key in property files - aborting");
        }
        
        registerAllPersistenceCapables();
    }

    @PreDestroy
    public void shutdown() {
    }

    private void registerAllPersistenceCapables() {

        for (final String packagePrefix : Iterables.transform(Splitter.on(",").split(packagePrefixes), trim())) {
            Reflections reflections = new Reflections(packagePrefix);
            
            Set<Class<?>> entityTypes = 
                    reflections.getTypesAnnotatedWith(PersistenceCapable.class);
            
            if(noEntitiesIn(entityTypes)) {
                throw new IllegalStateException("Could not locate any @PersistenceCapable entities in package " + packagePrefix);
            }
            for (Class<?> entityType : entityTypes) {
                getSpecificationLoader().loadSpecification(entityType);
            }
        }
    }

    private static Function<String,String> trim() {
        return new Function<String,String>(){
            @Override
            public String apply(String input) {
                return input.trim();
            }
        };
    }

    /**
     * {@link Reflections} seems to return a set with 1 null element if none can be found.
     */
    private static boolean noEntitiesIn(Set<Class<?>> entityTypes) {
        return Iterables.filter(entityTypes, nullClass()).iterator().hasNext();
    }

    private static Predicate<Class<?>> nullClass() {
        return new Predicate<Class<?>>() {

            @Override
            public boolean apply(Class<?> input) {
                return input == null;
            }
        };
    }

    // //////////////////////////////////////

    SpecificationLoaderSpi getSpecificationLoader() {
        return IsisContext.getSpecificationLoader();
    }


    
}

@@ -913,17 +913,16 @@
 il.Set;%0A
-%0A
 import j
@@ -1037,17 +1037,16 @@
 apable;%0A
-%0A
 import c
@@ -1241,17 +1241,16 @@
 rables;%0A
-%0A
 import o
@@ -1298,24 +1298,24 @@
 f4j.Logger;%0A
+
 import org.s
@@ -1334,17 +1334,16 @@
 actory;%0A
-%0A
 import o
@@ -2966,77 +2966,810 @@
     
-getSpecificationLoader().loadSpecification(entityType);%0A            %7D
+if(ignore(entityType)) %7B%0A                    // ignore (probably a testing class)%0A                    continue;%0A                %7D%0A                getSpecificationLoader().loadSpecification(entityType);%0A            %7D%0A        %7D%0A    %7D%0A%0A    private static boolean ignore(final Class%3C?%3E entityType) %7B%0A        try %7B%0A            if(entityType.isAnonymousClass() %7C%7C entityType.isLocalClass() %7C%7C entityType.isMemberClass()) %7B%0A                return true;%0A            %7D%0A            final PersistenceCapable persistenceCapable = entityType.getAnnotation(PersistenceCapable.class);%0A            final boolean hasPersistenceCapable = persistenceCapable != null;%0A            return !hasPersistenceCapable; // don't ignore if has @PersistenceCapable%0A        %7D catch (NoClassDefFoundError ex) %7B%0A            return true;
 %0A   

fix to RegisterEntities