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

org.joinedworkz.ux.angular.AngularCartridge Maven / Gradle / Ivy

package org.joinedworkz.ux.angular;

import com.google.common.collect.Iterables;
import java.util.LinkedHashSet;
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.CollectionLiterals;
import org.eclipse.xtext.xbase.lib.Extension;
import org.eclipse.xtext.xbase.lib.Functions.Function1;
import org.eclipse.xtext.xbase.lib.IterableExtensions;
import org.joinedworkz.common.helper.CmnModelHelper;
import org.joinedworkz.common.helper.OpenApiHelper;
import org.joinedworkz.core.facility.AbstractCartridge;
import org.joinedworkz.core.facility.Outlet;
import org.joinedworkz.core.facility.Outlets;
import org.joinedworkz.core.model.CmnComplexType;
import org.joinedworkz.core.model.CmnComponent;
import org.joinedworkz.core.model.CmnComponentFeature;
import org.joinedworkz.core.model.CmnContent;
import org.joinedworkz.core.model.CmnField;
import org.joinedworkz.core.model.CmnModel;
import org.joinedworkz.core.model.CmnObject;
import org.joinedworkz.core.model.CmnResource;
import org.joinedworkz.core.model.CmnResourceOperation;
import org.joinedworkz.core.model.CmnResponse;
import org.joinedworkz.core.model.CmnType;
import org.joinedworkz.core.model.data.CmnData;
import org.joinedworkz.core.model.data.CmnPathCallSegment;
import org.joinedworkz.core.model.data.CmnReferenceData;
import org.joinedworkz.core.model.ux.CmnPage;
import org.joinedworkz.ux.angular.context.PageGeneratorContext;
import org.joinedworkz.ux.angular.context.ServiceGeneratorContext;
import org.joinedworkz.ux.angular.context.TypeScriptGeneratorContext;
import org.joinedworkz.ux.angular.generator.ModelGenerator;
import org.joinedworkz.ux.angular.generator.PageGenerator;
import org.joinedworkz.ux.angular.generator.RoutesGenerator;
import org.joinedworkz.ux.angular.generator.ServiceGenerator;
import org.joinedworkz.ux.angular.generator.StoreGenerator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Singleton
@SuppressWarnings("all")
public class AngularCartridge extends AbstractCartridge {
  private static final Logger log = LoggerFactory.getLogger(AngularCartridge.class);

  @Inject
  @Extension
  protected CmnModelHelper cmnModelHelper;

  @Inject
  protected RoutesGenerator routesGenerator;

  @Inject
  protected StoreGenerator storeGenerator;

  @Inject
  protected PageGenerator pageGenerator;

  @Inject
  protected ModelGenerator modelGenerator;

  @Inject
  protected ServiceGenerator serviceGenerator;

  @Inject
  @Extension
  private OpenApiHelper _openApiHelper;

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

  @Override
  public void apply(final CmnObject obj, final Outlets outlets, final Properties properties) {
    AngularCartridge.log.info("Apply cartridge: \'{}\'", this.getClass().getCanonicalName());
    final Outlet generatedJavaSourceSlot = outlets.get("generatedAngularSource");
    if ((obj instanceof CmnModel)) {
      AngularCartridge.log.info("Invoke generator \'{}\' on model \'{}\'", this.pageGenerator.getClass().getCanonicalName(), ((CmnModel)obj).getNamespace());
      List _modelElements = ((CmnModel)obj).getModelElements();
      for (final CmnObject modelElement : _modelElements) {
        this.generateModelElement(modelElement, generatedJavaSourceSlot, properties);
      }
    }
  }

  public void generateModelElement(final CmnObject obj, final Outlet outlet, final Properties properties) {
    if ((obj instanceof CmnComponent)) {
      boolean _hasStereoType = this.cmnModelHelper.hasStereoType(obj, "ux");
      boolean _not = (!_hasStereoType);
      if (_not) {
        return;
      }
      boolean _isEmpty = ((CmnComponent)obj).getUsedData().isEmpty();
      boolean _not_1 = (!_isEmpty);
      if (_not_1) {
        LinkedHashSet usedResources = CollectionLiterals.newLinkedHashSet();
        List _usedData = ((CmnComponent)obj).getUsedData();
        for (final CmnData usedDataItem : _usedData) {
          if ((usedDataItem instanceof CmnReferenceData)) {
            List _resourcePath = ((CmnReferenceData)usedDataItem).getResourcePath();
            boolean _tripleNotEquals = (_resourcePath != null);
            if (_tripleNotEquals) {
              final CmnResource usedResource = ((CmnReferenceData)usedDataItem).getResourcePath().get(0).getResource();
              usedResources.add(usedResource);
              final CmnComponent component = ((CmnReferenceData)usedDataItem).getReferencedComponent();
              String basePackage = component.getString("basePackage");
              if ((basePackage == null)) {
                basePackage = this.cmnModelHelper.getModel(component).getNamespace();
              }
              final LinkedHashSet usedResourcesWithServices = CollectionLiterals.newLinkedHashSet();
              this.collectResourcesWithServices(usedResource, usedResourcesWithServices);
              for (final CmnResource resourceWithService : usedResourcesWithServices) {
                {
                  final ServiceGeneratorContext ctx = this.createServiceGeneratorContext(basePackage, usedResource, resourceWithService, outlet, properties);
                  ctx.setMergedOperationInfos(this._openApiHelper.createMergedResourceOperationInfos(component, resourceWithService, ctx));
                  this.serviceGenerator.generate(((CmnComponent)obj), resourceWithService, ctx);
                }
              }
            }
          }
        }
        final Function1> _function = (CmnResource it) -> {
          return this.collectUsedTypes(it);
        };
        Set usedTypes = IterableExtensions.toSet(IterableExtensions.flatMap(usedResources, _function));
        Iterable _filter = Iterables.filter(usedTypes, CmnComplexType.class);
        for (final CmnComplexType usedType : _filter) {
          this.modelGenerator.generate(((CmnComponent)obj), usedType, this.createTypeScriptGeneratorContext(outlet, properties));
        }
      }
      boolean _isEmpty_1 = ((CmnComponent)obj).getRoutes().isEmpty();
      boolean _not_2 = (!_isEmpty_1);
      if (_not_2) {
        this.routesGenerator.generateRoutes(((CmnComponent)obj), ((CmnComponent)obj).getRoutes(), this.createTypeScriptGeneratorContext(outlet, properties));
      }
      List _features = ((CmnComponent)obj).getFeatures();
      boolean _tripleNotEquals_1 = (_features != null);
      if (_tripleNotEquals_1) {
        final Iterable pages = Iterables.filter(((CmnComponent)obj).getFeatures(), CmnPage.class);
        for (final CmnPage page : pages) {
          this.pageGenerator.generatePage(((CmnComponent)obj), page, this.createPageGeneratorContext(outlet, properties));
        }
      }
    }
  }

  public void collectResourcesWithServices(final CmnResource resource, final Set collectedResources) {
    if (((resource.getName() != null) && (resource.getRepresentation() != null))) {
      collectedResources.add(resource);
    }
    List _subResources = resource.getSubResources();
    boolean _tripleNotEquals = (_subResources != null);
    if (_tripleNotEquals) {
      List _subResources_1 = resource.getSubResources();
      for (final CmnResource subResource : _subResources_1) {
        this.collectResourcesWithServices(subResource, collectedResources);
      }
    }
  }

  public Set collectUsedTypes(final CmnResource resource) {
    final LinkedHashSet usedTypes = CollectionLiterals.newLinkedHashSet();
    this.collectUsedTypes(usedTypes, resource);
    return usedTypes;
  }

  public void collectUsedTypes(final Set usedTypes, final CmnResource resource) {
    CmnType _representation = resource.getRepresentation();
    boolean _tripleNotEquals = (_representation != null);
    if (_tripleNotEquals) {
      this.collectUsedTypes(usedTypes, resource.getRepresentation());
    }
    List _operations = resource.getOperations();
    boolean _tripleNotEquals_1 = (_operations != null);
    if (_tripleNotEquals_1) {
      List _operations_1 = resource.getOperations();
      for (final CmnResourceOperation operation : _operations_1) {
        {
          final Object consumes = this.cmnModelHelper.getPropertyValue(operation, "consumes");
          if ((consumes instanceof CmnComplexType)) {
            this.collectUsedTypes(usedTypes, ((CmnType)consumes));
          }
          List _responses = operation.getResponses();
          boolean _tripleNotEquals_2 = (_responses != null);
          if (_tripleNotEquals_2) {
            List _responses_1 = operation.getResponses();
            for (final CmnResponse response : _responses_1) {
              CmnContent _content = response.getContent();
              boolean _tripleNotEquals_3 = (_content != null);
              if (_tripleNotEquals_3) {
                final CmnType responseType = response.getContent().getType();
                if ((responseType != null)) {
                  this.collectUsedTypes(usedTypes, responseType);
                }
              }
            }
          }
        }
      }
    }
    List _subResources = resource.getSubResources();
    boolean _tripleNotEquals_2 = (_subResources != null);
    if (_tripleNotEquals_2) {
      List _subResources_1 = resource.getSubResources();
      for (final CmnResource subResource : _subResources_1) {
        this.collectUsedTypes(usedTypes, subResource);
      }
    }
  }

  public void collectUsedTypes(final Set usedTypes, final CmnType type) {
    boolean _contains = usedTypes.contains(type);
    boolean _not = (!_contains);
    if (_not) {
      usedTypes.add(type);
      if ((type instanceof CmnComplexType)) {
        List _fields = ((CmnComplexType)type).getFields();
        for (final CmnField field : _fields) {
          this.collectUsedTypes(usedTypes, field.getType());
        }
      }
    }
  }

  public PageGeneratorContext createPageGeneratorContext(final Outlet outlet, final Properties properties) {
    return new PageGeneratorContext(outlet, properties);
  }

  public TypeScriptGeneratorContext createTypeScriptGeneratorContext(final Outlet outlet, final Properties properties) {
    return new TypeScriptGeneratorContext(outlet, properties);
  }

  public ServiceGeneratorContext createServiceGeneratorContext(final String basePackage, final CmnResource rootResource, final CmnResource mainResource, final Outlet outlet, final Properties properties) {
    return new ServiceGeneratorContext(basePackage, rootResource, mainResource, outlet, properties);
  }
}