package de.cooperate.modeling.graphical.papyrus.extensions;

import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.eclipse.papyrus.infra.architecture.ArchitectureDomainManager;
import org.eclipse.papyrus.infra.core.architecture.ArchitectureDescriptionLanguage;
import org.eclipse.papyrus.infra.core.architecture.ArchitectureDomain;
import org.eclipse.papyrus.infra.core.architecture.RepresentationKind;
import org.eclipse.papyrus.infra.gmfdiag.representation.PapyrusDiagram;

public class PapyrusViewpointCustomizationHelper {

    public static void mergeDiagramKinds(URI architectureURI) {
        ArchitectureDomainManager domainManager = ArchitectureDomainManager.getInstance();

        ResourceSet rs = new ResourceSetImpl();
        Resource r = rs.getResource(architectureURI, true);

        List<PapyrusDiagram> matchingDiagrams = r.getContents().stream().filter(ArchitectureDomain.class::isInstance)
                .map(ArchitectureDomain.class::cast).map(ArchitectureDomain::getContexts).flatMap(Collection::stream)
                .filter(ArchitectureDescriptionLanguage.class::isInstance)
                .map(ArchitectureDescriptionLanguage.class::cast)
                .map(ArchitectureDescriptionLanguage::getRepresentationKinds).flatMap(Collection::stream)
                .filter(PapyrusDiagram.class::isInstance).map(PapyrusDiagram.class::cast).collect(Collectors.toList());

        for (PapyrusDiagram sourceDiagram : matchingDiagrams) {
            RepresentationKind classRepresentation = domainManager.getRepresentationKindById(sourceDiagram.getId());
            if (!(classRepresentation instanceof PapyrusDiagram))
                continue;
            PapyrusDiagram targetDiagram = (PapyrusDiagram) classRepresentation;
            targetDiagram.getAssistantRules().addAll(sourceDiagram.getAssistantRules());
            targetDiagram.getPaletteRules().addAll(sourceDiagram.getPaletteRules());
            targetDiagram.getChildRules().addAll(sourceDiagram.getChildRules());
            targetDiagram.getModelRules().addAll(sourceDiagram.getModelRules());
            targetDiagram.getOwningRules().addAll(sourceDiagram.getOwningRules());
        }

    }

}

@@ -127,27 +127,96 @@
 til.
-stream.Collectors;%0A
+Optional;%0Aimport java.util.stream.Collectors;%0A%0Aimport org.eclipse.emf.common.util.EList;
 %0Aimp
@@ -755,37 +755,503 @@
 iag.
-representation.PapyrusDiagram
+paletteconfiguration.DrawerConfiguration;%0Aimport org.eclipse.papyrus.infra.gmfdiag.paletteconfiguration.ElementDescriptor;%0Aimport org.eclipse.papyrus.infra.gmfdiag.paletteconfiguration.PaletteConfiguration;%0Aimport org.eclipse.papyrus.infra.gmfdiag.paletteconfiguration.ToolConfiguration;%0Aimport org.eclipse.papyrus.infra.gmfdiag.representation.PapyrusDiagram;%0Aimport org.eclipse.papyrus.infra.types.ElementTypeConfiguration;%0Aimport org.eclipse.papyrus.infra.types.SpecializationTypeConfiguration
 ;%0A%0Ap
@@ -2935,32 +2935,1424 @@
 OwningRules());%0A
+%0A            Optional%3CToolConfiguration%3E toolConfig = targetDiagram.getPalettes().stream()%0A                    .filter(p -%3E p.getId().equals(%22org.eclipse.papyrus.uml.diagram.usecase.paletteconfiguration%22))%0A                    .map(PaletteConfiguration::getDrawerConfigurations).flatMap(l -%3E l.stream())%0A                    .filter(dc -%3E dc.getId().equals(%22usecase.group.nodes%22))%0A                    .map(DrawerConfiguration::getOwnedConfigurations).flatMap(l -%3E l.stream())%0A                    .filter(config -%3E config.getId().equals(%22usecase.tool.subject%22))%0A                    .filter(ToolConfiguration.class::isInstance).map(ToolConfiguration.class::cast).findAny();%0A%0A            // Patch the available set of classifiers%0A            if (toolConfig.isPresent()) %7B%0A                ToolConfiguration t = toolConfig.get();%0A                ElementDescriptor e0 = t.getElementDescriptors().get(0);%0A                ElementDescriptor e1 = t.getElementDescriptors().get(1);%0A%0A                EList%3CElementTypeConfiguration%3E types = ((SpecializationTypeConfiguration) e0.getElementType())%0A                        .getSpecializedTypes();%0A                EList%3CElementTypeConfiguration%3E componentTypes = ((SpecializationTypeConfiguration) e1.getElementType())%0A                        .getSpecializedTypes();%0A                types.clear();%0A                types.addAll(componentTypes);%0A%0A            %7D%0A%0A
         %7D%0A%0A    %7D

Patch for use case subject insertion