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

org.nuiton.topia.service.script.table.TopiaSqlTablesFactory Maven / Gradle / Ivy

There is a newer version: 3.8
Show newest version
package org.nuiton.topia.service.script.table;

/*
 * #%L
 * ObServe Toolkit :: ToPIA Script service
 * %%
 * Copyright (C) 2017 - 2018 IRD, Ultreia.io
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU 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 Public License for more details.
 * 
 * You should have received a copy of the GNU General Public
 * License along with this program.  If not, see
 * .
 * #L%
 */

import java.util.LinkedHashSet;
import java.util.Set;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.nuiton.topia.persistence.TopiaEntityEnum;
import org.nuiton.topia.persistence.TopiaEntityEnumProvider;
import org.nuiton.topia.persistence.metadata.TopiaMetadataEntity;
import org.nuiton.topia.persistence.metadata.TopiaMetadataModel;
import org.nuiton.topia.persistence.metadata.TopiaMetadataModelVisitor;

/**
 * A factory of {@link TopiaSqlTables}.
 * 

* Created on 04/01/16. * * @author Tony Chemit - [email protected] * @since 3.0.1 */ public class TopiaSqlTablesFactory { /** Logger. */ private static final Log log = LogFactory.getLog(TopiaSqlTablesFactory.class); private final TopiaMetadataModel model; private final TopiaEntityEnumProvider entityEnumProvider; public TopiaSqlTablesFactory(TopiaMetadataModel model, TopiaEntityEnumProvider entityEnumProvider) { this.model = model; this.entityEnumProvider = entityEnumProvider; } public TopiaSqlTables newReplicateEntityTables(TopiaSqlTablesPredicate predicate, TopiaEntityEnum... entityEnums) { ReplicateTables tablesBuilder = new ReplicateTables(predicate); return tablesBuilder.getTables(entityEnums); } public interface TopiaSqlTablesPredicate { boolean acceptEntity(TopiaMetadataEntity metadataEntity); boolean acceptAssociation(TopiaMetadataEntity metadataEntity, String propertyName, TopiaMetadataEntity propertyType); boolean acceptReversedAssociation(TopiaMetadataEntity metadataEntity, String propertyName, TopiaMetadataEntity propertyType); boolean acceptNmAssociation(TopiaMetadataEntity metadataEntity, String propertyName, TopiaMetadataEntity propertyType); } public class ReplicateTables implements TopiaMetadataModelVisitor { protected final TopiaSqlTablesPredicate predicate; final Set dones; protected TopiaSqlTables.BuilderStepOnTable builder; protected TopiaSqlTables tables; ReplicateTables(TopiaSqlTablesPredicate predicate) { this.predicate = predicate; this.dones = new LinkedHashSet<>(); } TopiaSqlTables getTables(TopiaEntityEnum... entityEnums) { visitModelStart(model); for (TopiaEntityEnum entityEnum : entityEnums) { TopiaMetadataEntity entity = model.getEntity(entityEnum.name()); entity.accept(this, model); } visitModelEnd(model); return tables; } @Override public void visitModelStart(TopiaMetadataModel metadataModel) { } @Override public void visitModelEnd(TopiaMetadataModel metadataModel) { tables = builder.build(); } @Override public void visitEntiyStart(TopiaMetadataModel metadataModel, TopiaMetadataEntity metadataEntity) { if (predicate.acceptEntity(metadataEntity)) { boolean added = dones.add(metadataEntity); if (added) { log.debug("E → " + metadataEntity.getType()); TopiaEntityEnum entityEnum = entityEnumProvider.getEntityEnum(metadataEntity.getType()); builder = (builder == null ? TopiaSqlTables.builder(metadataModel, metadataEntity) : builder).addMainTable(entityEnum); } } } @Override public void visitEntiyEnd(TopiaMetadataModel metadataModel, TopiaMetadataEntity metadataEntity) { if (dones.contains(metadataEntity)) { log.debug("E ← " + metadataEntity.getType()); } } @Override public void visitReversedAssociation(TopiaMetadataModel metadataModel, TopiaMetadataEntity metadataEntity, String propertyName, TopiaMetadataEntity propertyType) { if (predicate.acceptReversedAssociation(metadataEntity, propertyName, propertyType)) { TopiaEntityEnum entityEnum = entityEnumProvider.getEntityEnum(propertyType.getType()); boolean withShell = propertyType.withShell(); log.debug(metadataEntity.getType() + "/" + propertyName + "→" + propertyType.getType() + " (withShell: " + withShell + ")"); if (withShell) { builder = builder.addAndEnterReverseJoinTable(entityEnum); visitChild(propertyType); } else { builder = builder.addReverseJoinTable(entityEnum); } } } @Override public void visitOneToManyAssociation(TopiaMetadataModel metadataModel, TopiaMetadataEntity metadataEntity, String propertyName, TopiaMetadataEntity propertyType) { if (predicate.acceptAssociation(metadataEntity, propertyName, propertyType)) { TopiaEntityEnum entityEnum = entityEnumProvider.getEntityEnum(propertyType.getType()); boolean withShell = propertyType.withShell(); log.debug(metadataEntity.getType() + "/" + propertyName + "→" + propertyType.getType() + " (withShell: " + withShell + ")"); if (withShell) { builder = builder.addAndEnterJoinTable(entityEnum); visitChild(propertyType); } else { builder = builder.addJoinTable(entityEnum); } } } @Override public void visitOneToManyAssociationInverse(TopiaMetadataModel metadataModel, TopiaMetadataEntity metadataEntity, String propertyName, TopiaMetadataEntity propertyType) { // Rien a faire } @Override public void visitManyToManyAssociation(TopiaMetadataModel metadataModel, TopiaMetadataEntity metadataEntity, String propertyName, TopiaMetadataEntity propertyType) { if (predicate.acceptNmAssociation(metadataEntity, propertyName, propertyType)) { log.debug(metadataEntity.getType() + "/" + propertyName + "→" + propertyType.getType()); TopiaEntityEnum entityEnum = entityEnumProvider.getEntityEnum(propertyType.getType()); builder = builder.addAssociationTable(entityEnum, propertyName); } } @Override public void visitManyToOneAssociation(TopiaMetadataModel metadataModel, TopiaMetadataEntity metadataEntity, String propertyName, TopiaMetadataEntity propertyType) { } @Override public void visitProperty(TopiaMetadataModel metadataModel, TopiaMetadataEntity metadataEntity, String propertyName, String propertyType) { } void visitChild(TopiaMetadataEntity propertyType) { dones.add(propertyType); propertyType.accept(this, model); builder = builder.backToParent(); } } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy