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

org.joinedworkz.spring.boot.PersistenceCartridge Maven / Gradle / Ivy

There is a newer version: 1.3.46
Show newest version
package org.joinedworkz.spring.boot;

import com.google.common.collect.Iterables;
import java.util.List;
import java.util.Properties;
import java.util.Set;
import javax.inject.Inject;
import javax.inject.Singleton;
import org.eclipse.xtext.xbase.lib.Extension;
import org.eclipse.xtext.xbase.lib.Functions.Function1;
import org.eclipse.xtext.xbase.lib.IterableExtensions;
import org.eclipse.xtext.xbase.lib.StringExtensions;
import org.joinedworkz.common.helper.CmnModelHelper;
import org.joinedworkz.common.java.ClassGeneratorContext;
import org.joinedworkz.core.facility.AbstractCartridge;
import org.joinedworkz.core.facility.GeneratorContext;
import org.joinedworkz.core.facility.Outlet;
import org.joinedworkz.core.facility.Outlets;
import org.joinedworkz.core.model.CmnComplexType;
import org.joinedworkz.core.model.CmnField;
import org.joinedworkz.core.model.CmnFieldContainer;
import org.joinedworkz.core.model.CmnModel;
import org.joinedworkz.core.model.CmnObject;
import org.joinedworkz.core.model.CmnOperation;
import org.joinedworkz.core.model.CmnRelation;
import org.joinedworkz.core.model.CmnService;
import org.joinedworkz.core.model.CmnType;
import org.joinedworkz.spring.boot.adapter.PersistenceAdapter;
import org.joinedworkz.spring.boot.context.DataAccessServiceGeneratorContext;
import org.joinedworkz.spring.boot.context.EntityMapperGeneratorContext;
import org.joinedworkz.spring.boot.context.RepositoryGeneratorContext;
import org.joinedworkz.spring.boot.generator.DDLGenerator;
import org.joinedworkz.spring.boot.generator.DataAccessServiceGenerator;
import org.joinedworkz.spring.boot.generator.EntityClassGenerator;
import org.joinedworkz.spring.boot.generator.EntityMapperGenerator;
import org.joinedworkz.spring.boot.generator.EntityObjectClassGenerator;
import org.joinedworkz.spring.boot.generator.RelationDaoGenerator;
import org.joinedworkz.spring.boot.generator.RelationEntityClassGenerator;
import org.joinedworkz.spring.boot.generator.RepositoryGenerator;
import org.joinedworkz.spring.boot.generator.ServiceDaoGenerator;
import org.joinedworkz.spring.boot.generator.TupleConverterGenerator;
import org.joinedworkz.spring.boot.helper.AdapterHelper;
import org.joinedworkz.spring.boot.helper.OutletHelper;
import org.joinedworkz.spring.boot.helper.PersistenceHelper;
import org.joinedworkz.spring.boot.info.RelationInfo;

@Singleton
@SuppressWarnings("all")
public class PersistenceCartridge extends AbstractCartridge {
  @Inject
  protected DDLGenerator ddlGenerator;

  @Inject
  protected EntityClassGenerator entityClassGenerator;

  @Inject
  protected RelationEntityClassGenerator relationEntityClassGenerator;

  @Inject
  protected RepositoryGenerator daoGenerator;

  @Inject
  protected DataAccessServiceGenerator dasGenerator;

  @Inject
  protected EntityMapperGenerator entityMapperGenerator;

  @Inject
  protected TupleConverterGenerator tupleConverterGenerator;

  @Inject
  protected EntityObjectClassGenerator entityObjectClassGenerator;

  @Inject
  protected ServiceDaoGenerator serviceDaoGenerator;

  @Inject
  protected RelationDaoGenerator relationDaoGenerator;

  @Inject
  @Extension
  protected PersistenceHelper jpaHelper;

  @Inject
  @Extension
  protected AdapterHelper _adapterHelper;

  @Inject
  @Extension
  protected CmnModelHelper _cmnModelHelper;

  @Inject
  @Extension
  private OutletHelper _outletHelper;

  @Override
  public void prepare(final CmnObject cmnObj) {
    if ((cmnObj instanceof CmnModel)) {
      this._adapterHelper.getOrCreatePersistenceAdapter(((CmnModel)cmnObj));
    }
  }

  @Override
  public void apply(final CmnObject cmnObj, final Outlets outlets) {
    this.apply(cmnObj, outlets, null);
  }

  @Override
  public void apply(final CmnObject cmnObj, final Outlets outlets, final Properties properties) {
    if ((cmnObj instanceof CmnModel)) {
      final Outlet generatedJavaSourceSlot = outlets.get("generatedJavaSource");
      final Outlet generatedConfigurationOutlet = outlets.get("generatedConfiguration");
      final Outlet generatedMapperOutlet = this._outletHelper.getGeneratedDtoAndMapperOutlet(outlets, cmnObj, properties);
      final PersistenceAdapter persistenceAdapter = this._adapterHelper.getOrCreatePersistenceAdapter(((CmnModel)cmnObj));
      List _modelElements = ((CmnModel)cmnObj).getModelElements();
      for (final CmnObject modelElement : _modelElements) {
        this.generateModelElement(modelElement, persistenceAdapter, generatedJavaSourceSlot, properties);
      }
      final Iterable complexTypes = Iterables.filter(((CmnModel)cmnObj).getModelElements(), CmnComplexType.class);
      final Function1 _function = (CmnComplexType it) -> {
        return Boolean.valueOf(this.jpaHelper.isEntity(it));
      };
      final Function1> _function_1 = (CmnComplexType it) -> {
        final Function1 _function_2 = (CmnOperation it_1) -> {
          return Boolean.valueOf(this.jpaHelper.hasNativeSql(it_1));
        };
        return IterableExtensions.filter(it.getOperations(), _function_2);
      };
      final Iterable operationsWithNativeSql = IterableExtensions.flatMap(IterableExtensions.filter(complexTypes, _function), _function_1);
      final Function1 _function_2 = (CmnOperation it) -> {
        return it.getReturnType();
      };
      final Set returnTypesInOperationsWithNativeSql = IterableExtensions.toSet(IterableExtensions.filterNull(IterableExtensions.map(operationsWithNativeSql, _function_2)));
      boolean _isEmpty = IterableExtensions.isEmpty(complexTypes);
      boolean _not = (!_isEmpty);
      if (_not) {
        final Function1 _function_3 = (CmnComplexType it) -> {
          return Boolean.valueOf(((this.jpaHelper.isEntity(it) || this.isMappedType(it)) || ((!this.jpaHelper.isEntity(it)) && this.jpaHelper.hasFieldOfTypeEntity(it))));
        };
        final Iterable mappedTypes = IterableExtensions.filter(complexTypes, _function_3);
        for (final CmnComplexType mappedType : mappedTypes) {
          this.entityMapperGenerator.generateClass(this.createEntityMapperGeneratorContext(mappedType, false, generatedMapperOutlet, properties));
        }
        final Function1 _function_4 = (CmnComplexType it) -> {
          return Boolean.valueOf(((!this.jpaHelper.isEntity(it)) && this.jpaHelper.hasFieldOfTypeEntity(it)));
        };
        final Iterable otherModelTypes = IterableExtensions.filter(complexTypes, _function_4);
        for (final CmnComplexType otherModelType : otherModelTypes) {
          ClassGeneratorContext _classGeneratorContext = new ClassGeneratorContext(otherModelType, generatedJavaSourceSlot, properties);
          this.entityObjectClassGenerator.generateClass(_classGeneratorContext);
        }
        boolean _isEmpty_1 = returnTypesInOperationsWithNativeSql.isEmpty();
        boolean _not_1 = (!_isEmpty_1);
        if (_not_1) {
          ClassGeneratorContext _classGeneratorContext_1 = new ClassGeneratorContext(((CmnModel)cmnObj), returnTypesInOperationsWithNativeSql, generatedJavaSourceSlot, properties);
          this.tupleConverterGenerator.generateClass(_classGeneratorContext_1);
        }
      }
      List _relationInfos = persistenceAdapter.getRelationInfos();
      for (final RelationInfo relationInfo : _relationInfos) {
        {
          final CmnRelation relation = relationInfo.getRelation();
          boolean _relationHasOwnDao = this.jpaHelper.relationHasOwnDao(relation);
          if (_relationHasOwnDao) {
            this.relationEntityClassGenerator.generateClass(this.createEntityGeneratorContext(relation, relationInfo, generatedJavaSourceSlot, properties));
            this.relationDaoGenerator.generateClass(this.createDaoGeneratorContext(relation, generatedJavaSourceSlot, properties));
          }
        }
      }
      GeneratorContext _generatorContext = new GeneratorContext(generatedConfigurationOutlet, properties);
      this.ddlGenerator.generateConfigurationFile(((CmnModel)cmnObj), _generatorContext);
    }
  }

  public boolean isMappedType(final CmnObject modelElement) {
    if ((modelElement instanceof CmnComplexType)) {
      final Function1 _function = (CmnField it) -> {
        CmnField _sourceField = it.getSourceField();
        return Boolean.valueOf((_sourceField != null));
      };
      return IterableExtensions.exists(((CmnComplexType)modelElement).getFields(), _function);
    }
    return false;
  }

  public void generateModelElement(final CmnObject obj, final PersistenceAdapter persistenceAdapter, final Outlet outlet, final Properties properties) {
    if ((obj instanceof CmnComplexType)) {
      boolean _isEntity = this.jpaHelper.isEntity(obj);
      if (_isEntity) {
        this.entityClassGenerator.generateClass(this.createGeneratorContext(((CmnComplexType)obj), outlet, properties));
        Boolean _abstract = ((CmnComplexType)obj).getAbstract();
        boolean _not = (!(_abstract).booleanValue());
        if (_not) {
          this.daoGenerator.generateClass(this.createDaoGeneratorContext(((CmnComplexType)obj), outlet, properties));
          this.dasGenerator.generateClass(this.createDataAccessServiceGeneratorContext(((CmnComplexType)obj), outlet, properties));
        }
      }
    } else {
      if ((obj instanceof CmnService)) {
        boolean _isDao = this.jpaHelper.isDao(obj);
        if (_isDao) {
          this.serviceDaoGenerator.generateClass(this.createDaoGeneratorContext(((CmnService)obj), outlet, properties));
        }
      }
    }
  }

  public  ClassGeneratorContext createGeneratorContext(final T classObject, final Outlet outlet, final Properties properties) {
    return new ClassGeneratorContext(classObject, outlet, properties);
  }

  public ClassEntityGeneratorContext createEntityGeneratorContext(final CmnRelation classObject, final RelationInfo relationInfo, final Outlet outlet, final Properties properties) {
    return new ClassEntityGeneratorContext(classObject, relationInfo, outlet, properties);
  }

  public  RepositoryGeneratorContext createDaoGeneratorContext(final T classObject, final Outlet outlet, final Properties properties) {
    return new RepositoryGeneratorContext(classObject, outlet, properties, this.jpaHelper);
  }

  public  DataAccessServiceGeneratorContext createDataAccessServiceGeneratorContext(final T classObject, final Outlet outlet, final Properties properties) {
    final String entityMapperClassShort = StringExtensions.toFirstLower(this._cmnModelHelper.lastSegment(this.jpaHelper.entityMapperClass(((CmnComplexType) classObject))));
    return new DataAccessServiceGeneratorContext(classObject, entityMapperClassShort, outlet, properties, this.jpaHelper);
  }

  public  EntityMapperGeneratorContext createEntityMapperGeneratorContext(final CmnComplexType classObject, final boolean entityMapper, final Outlet outlet, final Properties properties) {
    return new EntityMapperGeneratorContext(classObject, entityMapper, outlet, properties, this.jpaHelper);
  }

  public ServiceDaoGeneratorContext createDaoGeneratorContext(final CmnService classObject, final Outlet outlet, final Properties properties) {
    return new ServiceDaoGeneratorContext(classObject, outlet, properties, this.jpaHelper);
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy