All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.nuiton.web.SecurityEntityEnum Maven / Gradle / Ivy

There is a newer version: 1.20
Show newest version
package org.nuiton.web;

/*-
 * #%L
 * Nuiton Web :: Nuiton Security
 * %%
 * Copyright (C) 2010 - 2017 CodeLutin
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Lesser Public License for more details.
 * 
 * You should have received a copy of the GNU General Lesser Public
 * License along with this program.  If not, see
 * .
 * #L%
 */

import com.google.common.base.Predicate;
import com.google.common.collect.Iterables;
import com.google.common.collect.Sets;
import java.lang.reflect.Array;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import javax.annotation.Generated;
import org.apache.commons.lang3.ArrayUtils;
import org.nuiton.topia.persistence.TopiaEntity;
import org.nuiton.topia.persistence.TopiaEntityEnum;
import org.nuiton.topia.persistence.TopiaException;
import org.nuiton.topia.persistence.util.TopiaEntityHelper;
import org.nuiton.web.security.SecurityRole;
import org.nuiton.web.security.SecurityUser;

@Generated(value = "org.nuiton.topia.templates.EntityEnumTransformer", date = "Thu Sep 07 10:56:15 CEST 2017")

public enum SecurityEntityEnum implements TopiaEntityEnum {
    SecurityRole(SecurityRole.class, null, "securityrole", ArrayUtils.EMPTY_STRING_ARRAY),
    SecurityUser(SecurityUser.class, null, "securityuser", ArrayUtils.EMPTY_STRING_ARRAY);

    /**
     * The contract of the entity.
     */
    private final Class contract;

    /**
     * The optional name of database schema of the entity (if none was filled, will be {@code null}).
     */
    private final String dbSchemaName;

    /**
     * The name of the database table for the entity.
     */
    private final String dbTableName;

    /**
     * The fully qualified name of the implementation of the entity.
     */
    private String implementationFQN;

    /**
     * The implementation class of the entity (will be lazy computed at runtime).
     */
    private Class implementation;

    /**
     * The array of property involved in the natural key of the entity.
     */
    private final String[] naturalIds;

    /**
     * The array of not null properties of the entity.
     */
    private final String[] notNulls;

    SecurityEntityEnum(Class contract, String dbSchemaName, String dbTableName, String[] notNulls, String ... naturalIds) {
        this.contract = contract;
        this.dbSchemaName = dbSchemaName;
        this.dbTableName = dbTableName;
        this.notNulls = Arrays.copyOf(notNulls, notNulls.length);
        this.naturalIds = naturalIds;
        implementationFQN = contract.getName() + "Impl";
    }

    @Override
    public Class getContract() {
        return contract;
    }

    @Override
    public String dbSchemaName() {
        return dbSchemaName;
    }

    @Override
    public String dbTableName() {
        return dbTableName;
    }

    @Override
    public String[] getNaturalIds() {
        return naturalIds;
    }

    @Override
    public boolean isUseNaturalIds() {
        return naturalIds.length > 0;
    }

    @Override
    public String[] getNotNulls() {
        return notNulls;
    }

    @Override
    public boolean isUseNotNulls() {
        return notNulls.length > 0;
    }

    @Override
    public String getImplementationFQN() {
        return implementationFQN;
    }

    @Override
    public void setImplementationFQN(String implementationFQN) {
        this.implementationFQN = implementationFQN;
        this.implementation = null;
    }

    @Override
    public boolean accept(Class klass) {
        SecurityEntityEnum constant = valueOf(klass);
        boolean result = constant.getContract() == contract;
        return result;
    }

    @Override
    public Class getImplementation() {
        if (implementation == null) {
            try {
                implementation = (Class) Class.forName(implementationFQN);
            } catch (ClassNotFoundException e) {
                throw new TopiaException("could not find class " + implementationFQN, e);
            }
        }
        return implementation;
    }

    public static SecurityEntityEnum valueOf(TopiaEntity entity) {
        return valueOf(entity.getClass());
    }

    public static SecurityEntityEnum valueOf(final Class klass) {
        if (klass.isInterface()) {
            return valueOf(klass.getSimpleName());
        }

        Class contractClass = TopiaEntityHelper.getContractClass(SecurityEntityEnum.values(), (Class) klass);

        if (contractClass != null) {

            return valueOf(contractClass.getSimpleName());
        }

        throw new IllegalArgumentException("no entity defined for the class " + klass + " in : " + Arrays.toString(SecurityEntityEnum.values()));
    }

    public static SecurityEntityEnum[] getContracts() {
        SecurityEntityEnum[] result = values();
        return result;
    }

    public static  Class getContractClass(Class klass) {
        SecurityEntityEnum constant = valueOf(klass);
        Class result = (Class) constant.getContract();
        return result;
    }

    public static Class[] getContractClasses() {
        SecurityEntityEnum[] values = values();
        Class[] result = (Class[]) Array.newInstance(Class.class, values.length);
        for (int i = 0; i < values.length; i++) {
            result[i] = values[i].getContract();
        }
        return result;
    }

    public static  Class getImplementationClass(Class klass) {
        SecurityEntityEnum constant = valueOf(klass);
        Class result = (Class) constant.getImplementation();
        return result;
    }

    public static Set> getImplementationClasses() {
        SecurityEntityEnum[] values = values();
        Set> result = new LinkedHashSet>();
        for (int i = 0; i < values.length; i++) {
            result.add(values[i].getImplementation());
        }
        return result;
    }

    public static String getImplementationClassesAsString() {
        StringBuilder buffer = new StringBuilder();
        for (Class aClass : getImplementationClasses()) {
            buffer.append(',').append(aClass.getName());
        }
        String result = buffer.substring(1);
        return result;
    }

} //SecurityEntityEnum




© 2015 - 2025 Weber Informatics LLC | Privacy Policy