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

org.contextmapper.dsl.formatting2.ContextMappingDSLFormatter Maven / Gradle / Ivy

There is a newer version: 6.12.0
Show newest version
/**
 * Copyright 2018 The Context Mapper Project Team
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *    http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.contextmapper.dsl.formatting2;

import com.google.common.base.Objects;
import com.google.inject.Inject;
import java.util.Arrays;
import java.util.function.Consumer;
import org.contextmapper.dsl.contextMappingDSL.Aggregate;
import org.contextmapper.dsl.contextMappingDSL.BoundedContext;
import org.contextmapper.dsl.contextMappingDSL.ContextMap;
import org.contextmapper.dsl.contextMappingDSL.ContextMappingModel;
import org.contextmapper.dsl.contextMappingDSL.Domain;
import org.contextmapper.dsl.contextMappingDSL.Import;
import org.contextmapper.dsl.contextMappingDSL.Relationship;
import org.contextmapper.dsl.contextMappingDSL.SculptorModule;
import org.contextmapper.dsl.contextMappingDSL.Subdomain;
import org.contextmapper.dsl.contextMappingDSL.UserRequirement;
import org.contextmapper.dsl.services.ContextMappingDSLGrammarAccess;
import org.contextmapper.tactic.dsl.formatting2.TacticDDDLanguageFormatter;
import org.contextmapper.tactic.dsl.tacticdsl.Attribute;
import org.contextmapper.tactic.dsl.tacticdsl.CommandEvent;
import org.contextmapper.tactic.dsl.tacticdsl.ComplexType;
import org.contextmapper.tactic.dsl.tacticdsl.DomainEvent;
import org.contextmapper.tactic.dsl.tacticdsl.DomainObjectOperation;
import org.contextmapper.tactic.dsl.tacticdsl.Entity;
import org.contextmapper.tactic.dsl.tacticdsl.Parameter;
import org.contextmapper.tactic.dsl.tacticdsl.Reference;
import org.contextmapper.tactic.dsl.tacticdsl.Service;
import org.contextmapper.tactic.dsl.tacticdsl.ServiceOperation;
import org.contextmapper.tactic.dsl.tacticdsl.SimpleDomainObject;
import org.contextmapper.tactic.dsl.tacticdsl.ValueObject;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.xtext.formatting2.IFormattableDocument;
import org.eclipse.xtext.formatting2.IHiddenRegionFormatter;
import org.eclipse.xtext.formatting2.regionaccess.ISemanticRegion;
import org.eclipse.xtext.resource.XtextResource;
import org.eclipse.xtext.xbase.lib.Extension;
import org.eclipse.xtext.xbase.lib.Procedures.Procedure1;

@SuppressWarnings("all")
public class ContextMappingDSLFormatter extends TacticDDDLanguageFormatter {
  @Inject
  @Extension
  private ContextMappingDSLGrammarAccess _contextMappingDSLGrammarAccess;
  
  protected void _format(final ContextMappingModel contextMappingModel, @Extension final IFormattableDocument document) {
    boolean ignoredFirst = ((!Objects.equal(contextMappingModel.getFirstLineComment(), null)) && (!"".equals(contextMappingModel.getFirstLineComment())));
    EList _imports = contextMappingModel.getImports();
    for (final Import cmlImport : _imports) {
      if (ignoredFirst) {
        final Procedure1 _function = (IHiddenRegionFormatter it) -> {
          it.newLine();
        };
        document.prepend(cmlImport, _function);
      } else {
        ignoredFirst = true;
      }
    }
    document.format(contextMappingModel.getMap());
    EList _boundedContexts = contextMappingModel.getBoundedContexts();
    for (final BoundedContext boundedContext : _boundedContexts) {
      document.format(boundedContext);
    }
    EList _domains = contextMappingModel.getDomains();
    for (final Domain domain : _domains) {
      document.format(domain);
    }
  }
  
  protected void _format(final ContextMap contextMap, @Extension final IFormattableDocument document) {
    final Procedure1 _function = (IHiddenRegionFormatter it) -> {
      it.newLine();
    };
    final Procedure1 _function_1 = (IHiddenRegionFormatter it) -> {
      it.setNewLines(2);
    };
    final Procedure1 _function_2 = (IHiddenRegionFormatter it) -> {
      it.setNewLines(2);
    };
    final Procedure1 _function_3 = (IHiddenRegionFormatter it) -> {
      it.indent();
    };
    document.interior(
      document.append(this.textRegionExtensions.regionFor(contextMap).ruleCallTo(this._contextMappingDSLGrammarAccess.getOPENRule()), _function), 
      document.append(document.prepend(this.textRegionExtensions.regionFor(contextMap).ruleCallTo(this._contextMappingDSLGrammarAccess.getCLOSERule()), _function_1), _function_2), _function_3);
    final Consumer _function_4 = (ISemanticRegion it) -> {
      final Procedure1 _function_5 = (IHiddenRegionFormatter it_1) -> {
        it_1.setNewLines(1);
      };
      document.prepend(it, _function_5);
    };
    this.textRegionExtensions.regionFor(contextMap).keywords("contains").forEach(_function_4);
    EList _relationships = contextMap.getRelationships();
    for (final Relationship relationship : _relationships) {
      document.format(relationship);
    }
  }
  
  protected void _format(final BoundedContext boundedContext, @Extension final IFormattableDocument document) {
    final Procedure1 _function = (IHiddenRegionFormatter it) -> {
      it.newLine();
    };
    final Procedure1 _function_1 = (IHiddenRegionFormatter it) -> {
      it.newLine();
    };
    final Procedure1 _function_2 = (IHiddenRegionFormatter it) -> {
      it.setNewLines(2);
    };
    final Procedure1 _function_3 = (IHiddenRegionFormatter it) -> {
      it.indent();
    };
    document.interior(
      document.append(this.textRegionExtensions.regionFor(boundedContext).ruleCallTo(this._contextMappingDSLGrammarAccess.getOPENRule()), _function), 
      document.append(document.prepend(this.textRegionExtensions.regionFor(boundedContext).ruleCallTo(this._contextMappingDSLGrammarAccess.getCLOSERule()), _function_1), _function_2), _function_3);
    final Procedure1 _function_4 = (IHiddenRegionFormatter it) -> {
      it.setNewLines(2);
    };
    document.prepend(boundedContext, _function_4);
    if (((boundedContext.getComment() != null) && (!"".equals(boundedContext.getComment())))) {
      final Procedure1 _function_5 = (IHiddenRegionFormatter it) -> {
        it.newLine();
      };
      document.prepend(this.textRegionExtensions.regionFor(boundedContext).keyword("BoundedContext"), _function_5);
    }
    final Procedure1 _function_6 = (IHiddenRegionFormatter it) -> {
      it.newLine();
    };
    document.prepend(this.textRegionExtensions.regionFor(boundedContext).keyword("domainVisionStatement"), _function_6);
    final Procedure1 _function_7 = (IHiddenRegionFormatter it) -> {
      it.newLine();
    };
    document.prepend(this.textRegionExtensions.regionFor(boundedContext).keyword("type"), _function_7);
    final Procedure1 _function_8 = (IHiddenRegionFormatter it) -> {
      it.newLine();
    };
    document.prepend(this.textRegionExtensions.regionFor(boundedContext).keyword("responsibilities"), _function_8);
    final Procedure1 _function_9 = (IHiddenRegionFormatter it) -> {
      it.newLine();
    };
    document.prepend(this.textRegionExtensions.regionFor(boundedContext).keyword("implementationTechnology"), _function_9);
    final Procedure1 _function_10 = (IHiddenRegionFormatter it) -> {
      it.newLine();
    };
    document.prepend(this.textRegionExtensions.regionFor(boundedContext).keyword("knowledgeLevel"), _function_10);
    EList _aggregates = boundedContext.getAggregates();
    for (final Aggregate aggregate : _aggregates) {
      document.format(aggregate);
    }
    EList _modules = boundedContext.getModules();
    for (final SculptorModule module : _modules) {
      document.format(module);
    }
  }
  
  protected void _format(final Domain domain, @Extension final IFormattableDocument document) {
    final Procedure1 _function = (IHiddenRegionFormatter it) -> {
      it.newLine();
    };
    final Procedure1 _function_1 = (IHiddenRegionFormatter it) -> {
      it.setNewLines(2);
    };
    final Procedure1 _function_2 = (IHiddenRegionFormatter it) -> {
      it.setNewLines(2);
    };
    final Procedure1 _function_3 = (IHiddenRegionFormatter it) -> {
      it.indent();
    };
    document.interior(
      document.append(this.textRegionExtensions.regionFor(domain).ruleCallTo(this._contextMappingDSLGrammarAccess.getOPENRule()), _function), 
      document.append(document.prepend(this.textRegionExtensions.regionFor(domain).ruleCallTo(this._contextMappingDSLGrammarAccess.getCLOSERule()), _function_1), _function_2), _function_3);
    EList _subdomains = domain.getSubdomains();
    for (final Subdomain subdomain : _subdomains) {
      document.format(subdomain);
    }
  }
  
  protected void _format(final Subdomain subdomain, @Extension final IFormattableDocument document) {
    final Procedure1 _function = (IHiddenRegionFormatter it) -> {
      it.newLine();
    };
    final Procedure1 _function_1 = (IHiddenRegionFormatter it) -> {
      it.setNewLines(1);
    };
    final Procedure1 _function_2 = (IHiddenRegionFormatter it) -> {
      it.setNewLines(2);
    };
    final Procedure1 _function_3 = (IHiddenRegionFormatter it) -> {
      it.indent();
    };
    document.interior(
      document.append(this.textRegionExtensions.regionFor(subdomain).ruleCallTo(this._contextMappingDSLGrammarAccess.getOPENRule()), _function), 
      document.append(document.prepend(this.textRegionExtensions.regionFor(subdomain).ruleCallTo(this._contextMappingDSLGrammarAccess.getCLOSERule()), _function_1), _function_2), _function_3);
    EList _entities = subdomain.getEntities();
    for (final Entity entity : _entities) {
      document.format(entity);
    }
    EList _services = subdomain.getServices();
    for (final Service service : _services) {
      document.format(service);
    }
  }
  
  protected void _format(final UserRequirement requirement, @Extension final IFormattableDocument document) {
    final Procedure1 _function = (IHiddenRegionFormatter it) -> {
      it.newLine();
    };
    final Procedure1 _function_1 = (IHiddenRegionFormatter it) -> {
      it.newLine();
    };
    final Procedure1 _function_2 = (IHiddenRegionFormatter it) -> {
      it.setNewLines(2);
    };
    final Procedure1 _function_3 = (IHiddenRegionFormatter it) -> {
      it.indent();
    };
    document.interior(
      document.append(this.textRegionExtensions.regionFor(requirement).ruleCallTo(this._contextMappingDSLGrammarAccess.getOPENRule()), _function), 
      document.append(document.prepend(this.textRegionExtensions.regionFor(requirement).ruleCallTo(this._contextMappingDSLGrammarAccess.getCLOSERule()), _function_1), _function_2), _function_3);
    final Procedure1 _function_4 = (IHiddenRegionFormatter it) -> {
      it.setNewLines(2);
    };
    document.prepend(requirement, _function_4);
  }
  
  protected void _format(final Relationship relationship, @Extension final IFormattableDocument document) {
    final Procedure1 _function = (IHiddenRegionFormatter it) -> {
      it.newLine();
    };
    final Procedure1 _function_1 = (IHiddenRegionFormatter it) -> {
      it.newLine();
    };
    final Procedure1 _function_2 = (IHiddenRegionFormatter it) -> {
      it.setNewLines(2);
    };
    final Procedure1 _function_3 = (IHiddenRegionFormatter it) -> {
      it.indent();
    };
    document.interior(
      document.append(this.textRegionExtensions.regionFor(relationship).ruleCallTo(this._contextMappingDSLGrammarAccess.getOPENRule()), _function), 
      document.append(document.prepend(this.textRegionExtensions.regionFor(relationship).ruleCallTo(this._contextMappingDSLGrammarAccess.getCLOSERule()), _function_1), _function_2), _function_3);
    final Procedure1 _function_4 = (IHiddenRegionFormatter it) -> {
      it.newLine();
    };
    document.prepend(this.textRegionExtensions.regionFor(relationship).keyword("implementationTechnology"), _function_4);
    final Procedure1 _function_5 = (IHiddenRegionFormatter it) -> {
      it.newLine();
    };
    document.prepend(this.textRegionExtensions.regionFor(relationship).keyword("exposedAggregates"), _function_5);
    final Procedure1 _function_6 = (IHiddenRegionFormatter it) -> {
      it.newLine();
    };
    document.prepend(this.textRegionExtensions.regionFor(relationship).keyword("downstreamRights"), _function_6);
    final Procedure1 _function_7 = (IHiddenRegionFormatter it) -> {
      it.noSpace();
    };
    document.surround(this.textRegionExtensions.regionFor(relationship).keyword("U"), _function_7);
    final Procedure1 _function_8 = (IHiddenRegionFormatter it) -> {
      it.noSpace();
    };
    document.surround(this.textRegionExtensions.regionFor(relationship).keyword("D"), _function_8);
    final Procedure1 _function_9 = (IHiddenRegionFormatter it) -> {
      it.noSpace();
    };
    document.surround(this.textRegionExtensions.regionFor(relationship).keyword("OHS"), _function_9);
    final Procedure1 _function_10 = (IHiddenRegionFormatter it) -> {
      it.noSpace();
    };
    document.surround(this.textRegionExtensions.regionFor(relationship).keyword("PL"), _function_10);
    final Procedure1 _function_11 = (IHiddenRegionFormatter it) -> {
      it.noSpace();
    };
    document.surround(this.textRegionExtensions.regionFor(relationship).keyword("ACL"), _function_11);
    final Procedure1 _function_12 = (IHiddenRegionFormatter it) -> {
      it.noSpace();
    };
    document.surround(this.textRegionExtensions.regionFor(relationship).keyword("CF"), _function_12);
    final Consumer _function_13 = (ISemanticRegion it) -> {
      final Procedure1 _function_14 = (IHiddenRegionFormatter it_1) -> {
        it_1.noSpace();
      };
      document.surround(it, _function_14);
    };
    this.textRegionExtensions.regionFor(relationship).keywords("SK").forEach(_function_13);
    final Consumer _function_14 = (ISemanticRegion it) -> {
      final Procedure1 _function_15 = (IHiddenRegionFormatter it_1) -> {
        it_1.noSpace();
      };
      document.surround(it, _function_15);
    };
    this.textRegionExtensions.regionFor(relationship).keywords("P").forEach(_function_14);
    final Consumer _function_15 = (ISemanticRegion it) -> {
      final Procedure1 _function_16 = (IHiddenRegionFormatter it_1) -> {
        it_1.noSpace();
      };
      document.prepend(it, _function_16);
      final Procedure1 _function_17 = (IHiddenRegionFormatter it_1) -> {
        it_1.oneSpace();
      };
      document.append(it, _function_17);
    };
    this.textRegionExtensions.regionFor(relationship).keywords(",").forEach(_function_15);
    final Procedure1 _function_16 = (IHiddenRegionFormatter it) -> {
      it.setNewLines(2);
    };
    document.prepend(relationship, _function_16);
  }
  
  protected void _format(final Aggregate aggregate, @Extension final IFormattableDocument document) {
    final Procedure1 _function = (IHiddenRegionFormatter it) -> {
      it.newLine();
    };
    final Procedure1 _function_1 = (IHiddenRegionFormatter it) -> {
      it.newLine();
    };
    final Procedure1 _function_2 = (IHiddenRegionFormatter it) -> {
      it.newLine();
    };
    final Procedure1 _function_3 = (IHiddenRegionFormatter it) -> {
      it.indent();
    };
    document.interior(
      document.append(this.textRegionExtensions.regionFor(aggregate).ruleCallTo(this._contextMappingDSLGrammarAccess.getOPENRule()), _function), 
      document.append(document.prepend(this.textRegionExtensions.regionFor(aggregate).ruleCallTo(this._contextMappingDSLGrammarAccess.getCLOSERule()), _function_1), _function_2), _function_3);
    final Procedure1 _function_4 = (IHiddenRegionFormatter it) -> {
      it.setNewLines(1);
    };
    document.prepend(aggregate, _function_4);
    final Procedure1 _function_5 = (IHiddenRegionFormatter it) -> {
      it.newLine();
    };
    document.prepend(this.textRegionExtensions.regionFor(aggregate).keyword("Aggregate"), _function_5);
    document.format(aggregate.getComment());
    EList _domainObjects = aggregate.getDomainObjects();
    for (final SimpleDomainObject domainObject : _domainObjects) {
      document.format(domainObject);
    }
    EList _services = aggregate.getServices();
    for (final Service service : _services) {
      document.format(service);
    }
  }
  
  protected void _format(final SculptorModule module, @Extension final IFormattableDocument document) {
    final Procedure1 _function = (IHiddenRegionFormatter it) -> {
      it.newLine();
    };
    final Procedure1 _function_1 = (IHiddenRegionFormatter it) -> {
      it.newLine();
    };
    final Procedure1 _function_2 = (IHiddenRegionFormatter it) -> {
      it.newLine();
    };
    final Procedure1 _function_3 = (IHiddenRegionFormatter it) -> {
      it.indent();
    };
    document.interior(
      document.append(this.textRegionExtensions.regionFor(module).ruleCallTo(this._contextMappingDSLGrammarAccess.getOPENRule()), _function), 
      document.append(document.prepend(this.textRegionExtensions.regionFor(module).ruleCallTo(this._contextMappingDSLGrammarAccess.getCLOSERule()), _function_1), _function_2), _function_3);
    EList _aggregates = module.getAggregates();
    for (final Aggregate aggregate : _aggregates) {
      document.format(aggregate);
    }
  }
  
  public void format(final Object domain, final IFormattableDocument document) {
    if (domain instanceof CommandEvent) {
      _format((CommandEvent)domain, document);
      return;
    } else if (domain instanceof DomainEvent) {
      _format((DomainEvent)domain, document);
      return;
    } else if (domain instanceof Attribute) {
      _format((Attribute)domain, document);
      return;
    } else if (domain instanceof Entity) {
      _format((Entity)domain, document);
      return;
    } else if (domain instanceof Reference) {
      _format((Reference)domain, document);
      return;
    } else if (domain instanceof ValueObject) {
      _format((ValueObject)domain, document);
      return;
    } else if (domain instanceof XtextResource) {
      _format((XtextResource)domain, document);
      return;
    } else if (domain instanceof Domain) {
      _format((Domain)domain, document);
      return;
    } else if (domain instanceof Subdomain) {
      _format((Subdomain)domain, document);
      return;
    } else if (domain instanceof DomainObjectOperation) {
      _format((DomainObjectOperation)domain, document);
      return;
    } else if (domain instanceof Parameter) {
      _format((Parameter)domain, document);
      return;
    } else if (domain instanceof Service) {
      _format((Service)domain, document);
      return;
    } else if (domain instanceof ServiceOperation) {
      _format((ServiceOperation)domain, document);
      return;
    } else if (domain instanceof Aggregate) {
      _format((Aggregate)domain, document);
      return;
    } else if (domain instanceof BoundedContext) {
      _format((BoundedContext)domain, document);
      return;
    } else if (domain instanceof ContextMap) {
      _format((ContextMap)domain, document);
      return;
    } else if (domain instanceof ContextMappingModel) {
      _format((ContextMappingModel)domain, document);
      return;
    } else if (domain instanceof Relationship) {
      _format((Relationship)domain, document);
      return;
    } else if (domain instanceof SculptorModule) {
      _format((SculptorModule)domain, document);
      return;
    } else if (domain instanceof UserRequirement) {
      _format((UserRequirement)domain, document);
      return;
    } else if (domain instanceof ComplexType) {
      _format((ComplexType)domain, document);
      return;
    } else if (domain instanceof EObject) {
      _format((EObject)domain, document);
      return;
    } else if (domain == null) {
      _format((Void)null, document);
      return;
    } else if (domain != null) {
      _format(domain, document);
      return;
    } else {
      throw new IllegalArgumentException("Unhandled parameter types: " +
        Arrays.asList(domain, document).toString());
    }
  }
}