ae.config.Project.java Source code

Java tutorial

Introduction

Here is the source code for ae.config.Project.java

Source

/*
 * Copyright 2007-2008 the original author or authors.
 *
 * Licensed 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 ae.config;

import static com.google.common.collect.Sets.newHashSet;
import static org.apache.commons.lang.StringUtils.isBlank;

import java.io.File;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.Set;

public class Project {
    protected static File file(final String path) {
        return new File(path);
    }

    private String author = "";
    private String name = "";
    private String version = "";
    private String defaultPackage = "";
    private Class<?> defaultSuperClass = ae.ActiveEntity.class;
    private File templatesDirectory = file("config/ae/templates");

    private final Set<ActiveEntity> activeEntities = newHashSet();

    public Project author(final String author) {
        this.author = author;
        return this;
    }

    public Project name(final String name) {
        this.name = name;
        return this;
    }

    public Project version(final String version) {
        this.version = version;
        return this;
    }

    public Project defaultPackage(final String defaultPackage) {
        this.defaultPackage = defaultPackage;
        return this;
    }

    public Project defaultSuperClass(final Class<?> defaultSuperClass) {
        this.defaultSuperClass = defaultSuperClass;
        return this;
    }

    public Project templatesDirectory(final String templatesDirectory) {
        return templatesDirectory(file(templatesDirectory));
    }

    public Project templatesDirectory(final File templatesDirectory) {
        this.templatesDirectory = templatesDirectory;
        return this;
    }

    public String getName() {
        return this.name;
    }

    public String getAuthor() {
        return this.author;
    }

    public String getVersion() {
        return this.version;
    }

    public String getDefaultPackage() {
        return this.defaultPackage;
    }

    public Class<?> getDefaultSuperClass() {
        return this.defaultSuperClass;
    }

    public File getTemplatesDirectory() {
        return this.templatesDirectory;
    }

    public Set<ActiveEntity> getActiveEntities() {
        return this.activeEntities;
    }

    public void prepareForCodeGeneration() {
        if (isBlank(this.defaultPackage)) {
            this.defaultPackage = getClass().getPackage().getName();
        }
        if (isBlank(this.name)) {
            this.name = getClass().getSimpleName();
        }
        for (final Class<?> declaredClass : getClass().getDeclaredClasses()) {
            if (declaresActiveEntity(declaredClass)) {
                final ActiveEntity declaredActiveEntity = readActiveEntityAt(declaredClass);
                declaredActiveEntity.prepareWith(this);
            }
        }
    }

    private boolean declaresActiveEntity(final Class<?> aClass) {
        assert aClass != null : "aClass is null";
        return ActiveEntity.class.isAssignableFrom(aClass);
    }

    private ActiveEntity readActiveEntityAt(final Class<?> aClass) {
        assert aClass != null : "aClass is null";
        final ActiveEntity declaredModel = loadModelDefinitionAt(aClass);
        this.activeEntities.add(declaredModel);
        return declaredModel;
    }

    private ActiveEntity loadModelDefinitionAt(final Class<?> aClass) {
        assert aClass != null : "aClass is null";
        final Object declaredModel;
        final Constructor<?> constructor;

        try {
            constructor = aClass.getDeclaredConstructor(getClass());
        } catch (final SecurityException e) {
            throw new IllegalStateException(e);
        } catch (final NoSuchMethodException e) {
            throw new IllegalStateException(e);
        }
        constructor.setAccessible(true);
        try {
            declaredModel = constructor.newInstance(this);
        } catch (final InstantiationException e) {
            throw new IllegalStateException(e);
        } catch (final IllegalAccessException e) {
            throw new IllegalStateException(e);
        } catch (final InvocationTargetException e) {
            throw new IllegalStateException(e);
        }
        return (ActiveEntity) declaredModel;
    }
}