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

tech.jhipster.lite.module.domain.resource.JHipsterModulesResources Maven / Gradle / Ivy

There is a newer version: 1.22.0
Show newest version
package tech.jhipster.lite.module.domain.resource;

import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import tech.jhipster.lite.module.domain.JHipsterModule;
import tech.jhipster.lite.module.domain.JHipsterModuleSlug;
import tech.jhipster.lite.module.domain.properties.JHipsterModuleProperties;
import tech.jhipster.lite.shared.error.domain.Assert;

public class JHipsterModulesResources {

  private static final Logger log = LoggerFactory.getLogger(JHipsterModulesResources.class);

  private final Map resources;

  public JHipsterModulesResources(Collection modulesResources, JHipsterHiddenModules hiddenModules) {
    Assert.field("modulesResources", modulesResources).notEmpty().noNullElement();
    Assert.notNull("jhipsterHiddenModules", hiddenModules);

    assertUniqueSlugs(modulesResources);

    resources = Collections.unmodifiableMap(
      removeHiddenModules(modulesResources, hiddenModules).collect(
        Collectors.toMap(JHipsterModuleResource::slug, Function.identity(), (x, y) -> y, LinkedHashMap::new)
      )
    );
  }

  private Stream removeHiddenModules(
    Collection modulesResources,
    JHipsterHiddenModules hiddenModules
  ) {
    Collection nestedDependenciesSlugs = findNestedDependencies(modulesResources, hiddenModules);
    DisplayHiddenResources displayHiddenResources = findDisplayAndHiddenResources(modulesResources, hiddenModules, nestedDependenciesSlugs);
    if (displayHiddenResources.hasHiddenResources()) {
      String hiddenModulesSlugs = displayHiddenResources.hiddenSlugs();
      log.info("The following modules are hidden: {}.", hiddenModulesSlugs);
    }
    return displayHiddenResources.displayedStream();
  }

  private DisplayHiddenResources findDisplayAndHiddenResources(
    Collection modulesResources,
    JHipsterHiddenModules hiddenModules,
    Collection nestedDependenciesSlugs
  ) {
    Map> partitionedResources = modulesResources
      .stream()
      .collect(Collectors.partitioningBy(resource -> allowed(resource, hiddenModules, nestedDependenciesSlugs)));
    return new DisplayHiddenResources(partitionedResources.get(true), partitionedResources.get(false));
  }

  private boolean allowed(
    JHipsterModuleResource resource,
    JHipsterHiddenModules hiddenModules,
    Collection nestedDependenciesSlugs
  ) {
    return notExcludedSlug(resource, hiddenModules, nestedDependenciesSlugs) && noExcludedTag(resource, hiddenModules);
  }

  private boolean notExcludedSlug(
    JHipsterModuleResource resource,
    JHipsterHiddenModules hiddenModules,
    Collection nestedDependenciesSlugs
  ) {
    return !hiddenModules.slugs().contains(resource.slug().get()) && !nestedDependenciesSlugs.contains(resource.slug().get());
  }

  private boolean noExcludedTag(JHipsterModuleResource resource, JHipsterHiddenModules hiddenModules) {
    return hiddenModules.tags().stream().noneMatch(tag -> resource.tags().contains(tag));
  }

  private Collection findNestedDependencies(
    Collection modulesResources,
    JHipsterHiddenModules hiddenModules
  ) {
    return findNestedDependenciesBySlugs(hiddenModules.slugs(), modulesResources);
  }

  private Collection findNestedDependenciesBySlugs(Collection slugs, Collection modulesResources) {
    return slugs.stream().flatMap(slug -> allSlugsNestedDependenciesOf(slug, modulesResources)).toList();
  }

  private Stream allSlugsNestedDependenciesOf(String slug, Collection modulesResources) {
    return allResourcesNestedDependenciesOf(new JHipsterModuleSlug(slug), modulesResources).map(moduleResource ->
      moduleResource.slug().get()
    );
  }

  private Stream allResourcesNestedDependenciesOf(
    JHipsterModuleSlug slug,
    Collection modulesResources
  ) {
    Collection childrenDependencies = this.getChildrenDependencies(slug, modulesResources);
    if (noMoreNestedResource(childrenDependencies)) {
      return Stream.of();
    }
    return Stream.concat(childrenDependencies.stream(), childrenDependencies.stream().flatMap(moveToNextNestedResource(modulesResources)));
  }

  private boolean noMoreNestedResource(Collection childrenDependencies) {
    return childrenDependencies.isEmpty();
  }

  private Function> moveToNextNestedResource(
    Collection modulesResources
  ) {
    return resource -> this.allResourcesNestedDependenciesOf(resource.slug(), modulesResources);
  }

  private Collection getChildrenDependencies(
    JHipsterModuleSlug slug,
    Collection modulesResources
  ) {
    return modulesResources.stream().filter(moduleResource -> isChildrenOf(slug, moduleResource)).toList();
  }

  private boolean isChildrenOf(JHipsterModuleSlug slug, JHipsterModuleResource moduleResource) {
    return moduleResource.organization().dependencies().stream().anyMatch(dependency -> dependency.slug().equals(slug));
  }

  private void assertUniqueSlugs(Collection modulesResources) {
    if (duplicatedSlug(modulesResources)) {
      throw new DuplicatedSlugException();
    }
  }

  private boolean duplicatedSlug(Collection modulesResources) {
    return modulesResources.stream().map(JHipsterModuleResource::slug).collect(Collectors.toSet()).size() != modulesResources.size();
  }

  public Stream stream() {
    return resources.values().stream();
  }

  public JHipsterModuleResource get(JHipsterModuleSlug slug) {
    assertKnownSlug(slug);

    return resources.get(slug);
  }

  public JHipsterModule build(JHipsterModuleSlug slug, JHipsterModuleProperties properties) {
    Assert.notNull("slug", slug);
    Assert.notNull("properties", properties);

    return get(slug).factory().create(properties);
  }

  private void assertKnownSlug(JHipsterModuleSlug slug) {
    Assert.notNull("slug", slug);

    if (!resources.containsKey(slug)) {
      throw new UnknownSlugException(slug);
    }
  }

  private record DisplayHiddenResources(Collection displayed, Collection hidden) {
    public Stream displayedStream() {
      return displayed().stream();
    }

    private boolean hasHiddenResources() {
      return !hidden().isEmpty();
    }

    private String hiddenSlugs() {
      return hidden().stream().map(hidden -> hidden.slug().get()).collect(Collectors.joining(", "));
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy