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

org.eclipse.jnosql.lite.mapping.metadata.LiteEntitiesMetadata Maven / Gradle / Ivy

/*
*  Copyright (c) 2023 Otávio Santana and others
*   All rights reserved. This program and the accompanying materials
*   are made available under the terms of the Eclipse Public License v1.0
*   and Apache License v2.0 which accompanies this distribution.
*   The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
*   and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php.
*
*   You may elect to redistribute this code under either of these licenses.
*
*   Contributors:
*
*   Otavio Santana
*/
package org.eclipse.jnosql.lite.mapping.metadata;


import jakarta.enterprise.context.ApplicationScoped;
import org.eclipse.jnosql.mapping.metadata.ClassInformationNotFoundException;
import org.eclipse.jnosql.mapping.metadata.EntitiesMetadata;
import org.eclipse.jnosql.mapping.metadata.EntityMetadata;
import org.eclipse.jnosql.mapping.metadata.InheritanceMetadata;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;

import javax.annotation.processing.Generated;

@Generated(value= "JNoSQL Lite EntitiesMetadata Generator", date = "2023-12-02T14:20:40.743255693")
@ApplicationScoped
public final class LiteEntitiesMetadata implements EntitiesMetadata {

    /**
    * The singleton instance
    */
    public static final LiteEntitiesMetadata INSTANCE = new LiteEntitiesMetadata();

    private static final Predicate HAS_NAME = EntityMetadata::hasEntityName;

    private final List entities;
    private final Map findByClassName;
    private final Map findBySimpleName;
    private final Map, EntityMetadata> classes;
    private final Map mappings;


    public LiteEntitiesMetadata() {
        this.findByClassName = new HashMap<>();
        this.findBySimpleName = new HashMap<>();
        this.classes = new HashMap<>();
        this.mappings = new HashMap<>();
        this.entities = new ArrayList<>();

         this.entities.add(new org.eclipse.jnosql.lite.mapping.entities.JobEntityMetaData());
         this.entities.add(new org.eclipse.jnosql.lite.mapping.entities.PersonEntityMetaData());
         this.entities.add(new org.eclipse.jnosql.lite.mapping.entities.UserEntityMetaData());
         this.entities.add(new org.eclipse.jnosql.lite.mapping.entities.WorkerEntityMetaData());
         this.entities.add(new org.eclipse.jnosql.lite.mapping.entities.CarEntityMetaData());

        for (LiteEntityMetadata entity : entities) {
            this.classes.put(entity.type(), entity);
            if (entity.isEntity() && entity.hasEntityName()) {
                this.mappings.put(entity.name().toUpperCase(Locale.US), entity);
                this.findBySimpleName.put(entity.simpleName(), entity);
                this.findByClassName.put(entity.className(), entity);
            }
        }
    }


    @Override
    public EntityMetadata get(Class entity) {
        Objects.requireNonNull(entity, "entity is required");
        EntityMetadata metadata = classes.get(entity);
        if (metadata == null) {
            throw new ClassInformationNotFoundException("The entity " + entity + " was not found");
        }
        return metadata;
    }

    @Override
    public Map findByParentGroupByDiscriminatorValue(Class parent) {
        Objects.requireNonNull(parent, "parent is required");
        return this.entities.stream()
                .flatMap(c -> c.inheritance().stream())
                .filter(p -> p.isParent(parent))
                .collect(Collectors.toMap(InheritanceMetadata::discriminatorValue, Function.identity()));
    }

    @Override
    public EntityMetadata findByName(String name) {
        Objects.requireNonNull(name, "name is required");
        return Optional.ofNullable(mappings.get(name.toUpperCase(Locale.US)))
                .orElseThrow(() -> new ClassInformationNotFoundException("There is not entity found with the name: " + name));

    }

    @Override
    public Optional findBySimpleName(String name) {
        Objects.requireNonNull(name, "name is required");
        return Optional.ofNullable(findBySimpleName.get(name));
    }

    @Override
    public Optional findByClassName(String name) {
        Objects.requireNonNull(name, "name is required");
        return Optional.ofNullable(findByClassName.get(name));
    }

    @Override
    public String toString() {
        return "DefaultEntitiesMetadata{" +
                "entities=" + entities +
                ", findByClassName=" + findByClassName +
                ", findBySimpleName=" + findBySimpleName +
                ", classes=" + classes +
                ", mappings=" + mappings +
                '}';
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy