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

com.github._1c_syntax.bsl.mdo.ChildrenOwner Maven / Gradle / Ivy

/*
 * This file is a part of MDClasses.
 *
 * Copyright (c) 2019 - 2024
 * Tymko Oleg , Maximov Valery  and contributors
 *
 * SPDX-License-Identifier: LGPL-3.0-or-later
 *
 * MDClasses is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3.0 of the License, or (at your option) any later version.
 *
 * MDClasses 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with MDClasses.
 */
package com.github._1c_syntax.bsl.mdo;

import com.github._1c_syntax.bsl.types.MdoReference;

import java.net.URI;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.function.Predicate;

/**
 * Расширение - владелец дочерних объектов
 */
public interface ChildrenOwner {

  /**
   * Возвращает все дочерние элементы объекта
   */
  default List getChildren() {
    List children = new ArrayList<>();

    if (this instanceof AttributeOwner attributeOwner) {
      children.addAll(attributeOwner.getAllAttributes());
    }

    if (this instanceof TabularSectionOwner tabularSectionOwner) {
      children.addAll(tabularSectionOwner.getTabularSections());
    }

    if (this instanceof CommandOwner commandOwner) {
      children.addAll(commandOwner.getCommands());
    }

    if (this instanceof TemplateOwner templateOwner) {
      children.addAll(templateOwner.getTemplates());
    }

    if (this instanceof FormOwner formOwner) {
      children.addAll(formOwner.getForms());
    }

    return children;
  }

  /**
   * Возвращает все дочерние элементы объекта, являющиеся атрибутами или ТЧ
   */
  default List getMDOChildren() {
    List children = new ArrayList<>();

    if (this instanceof AttributeOwner attributeOwner) {
      children.addAll(attributeOwner.getAllAttributes());
    }

    if (this instanceof TabularSectionOwner tabularSectionOwner) {
      children.addAll(tabularSectionOwner.getTabularSections());
    }

    return children;
  }

  /**
   * Возвращает дочерние элементы объекта, являющиеся атрибутами или ТЧ, плоским списком.
   */
  default List getPlainChildren() {
    List children = new ArrayList<>(getChildren());
    getChildren().stream().filter(ChildrenOwner.class::isInstance)
      .map(ChildrenOwner.class::cast)
      .forEach(mdObject -> children.addAll(mdObject.getPlainChildren()));

    return children;
  }

  /**
   * Возвращает дочерние элементы объекта плоским списком.
   */
  default List getMDOPlainChildren() {
    List children = new ArrayList<>(getMDOChildren());
    getChildren().stream().filter(ChildrenOwner.class::isInstance)
      .map(ChildrenOwner.class::cast)
      .forEach(mdObject -> children.addAll(mdObject.getMDOPlainChildren()));

    return children;
  }

  /**
   * Выполняет поиск дочернего (включая все уровни) объекта по ссылке
   *
   * @param ref Ссылка MdoReference на искомый объект
   * @return Контейнер с найденным значением (может быть пустым)
   */
  default Optional findChild(MdoReference ref) {
    return getPlainChildren().stream()
      .filter(mdObject -> mdObject.getMdoReference().equals(ref))
      .findFirst();
  }

  /**
   * Выполняет поиск дочернего (включая все уровни) объекта по ссылке, переданной строкой
   *
   * @param mdoRef Строковое представление ссылки MdoReference на искомый объект
   * @return Контейнер с найденным значением (может быть пустым)
   */
  default Optional findChild(String mdoRef) {
    return getPlainChildren().stream()
      .filter(mdObject -> mdObject.getMdoReference().getMdoRef().equalsIgnoreCase(mdoRef)
        || mdObject.getMdoReference().getMdoRefRu().equalsIgnoreCase(mdoRef))
      .findFirst();
  }

  /**
   * Выполняет поиск дочернего (включая все уровни) объекта по ссылке на его модуль
   *
   * @param uri Ссылка на модуль исходного объекта
   * @return Контейнер с найденным значением (может быть пустым)
   */
  default Optional findChild(URI uri) {
    var collection = getPlainChildren().stream()
      .filter(ModuleOwner.class::isInstance)
      .filter(mdObject -> ((ModuleOwner) mdObject).getModuleByUri(uri).isPresent())
      .toList();

    if (collection.size() > 1) {
      var result = collection.stream().filter(md -> !(md instanceof MDObject)).findFirst();
      if (result.isPresent()) {
        return result;
      }
    }
    return collection.stream().findFirst();
  }

  /**
   * Выполняет поиск дочернего (включая все уровни) объекта по произвольному предикату
   *
   * @param predicate Произвольный предикат для поиска объекта
   * @return Контейнер с найденным значением (может быть пустым)
   */
  default Optional findChild(Predicate predicate) {
    return getPlainChildren().stream().filter(predicate).findFirst();
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy