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

org.cloudfoundry.multiapps.controller.persistence.model.ConfigurationSubscription Maven / Gradle / Ivy

There is a newer version: 1.182.0
Show newest version
package org.cloudfoundry.multiapps.controller.persistence.model;

import static java.text.MessageFormat.format;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

import org.cloudfoundry.multiapps.controller.persistence.model.filters.ConfigurationFilter;
import org.cloudfoundry.multiapps.mta.Messages;
import org.cloudfoundry.multiapps.mta.model.AuditableConfiguration;
import org.cloudfoundry.multiapps.mta.model.ConfigurationIdentifier;
import org.cloudfoundry.multiapps.mta.model.Module;
import org.cloudfoundry.multiapps.mta.model.ProvidedDependency;
import org.cloudfoundry.multiapps.mta.model.RequiredDependency;
import org.cloudfoundry.multiapps.mta.model.Resource;

public class ConfigurationSubscription implements AuditableConfiguration {

    private long id;
    private String mtaId;
    private ConfigurationFilter filter;
    private String spaceId;
    private String appName;
    private ModuleDto moduleDto;
    private ResourceDto resourceDto;
    private String moduleId;
    private String resourceId;

    public ConfigurationSubscription() {
        // Required by Jackson
    }

    public ConfigurationSubscription(long id, String mtaId, String spaceId, String appName, ConfigurationFilter filter, ModuleDto moduleDto,
                                     ResourceDto resourceDto, String moduleId, String resourceId) {
        this.filter = filter;
        this.spaceId = spaceId;
        this.appName = appName;
        this.id = id;
        this.moduleDto = moduleDto;
        this.mtaId = mtaId;
        this.moduleId = moduleId;
        this.resourceDto = resourceDto;
        this.resourceId = resourceId;
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public ConfigurationFilter getFilter() {
        return filter;
    }

    public String getMtaId() {
        return mtaId;
    }

    public String getAppName() {
        return appName;
    }

    public String getSpaceId() {
        return spaceId;
    }
    
    public String getModuleId() {
        return moduleId;
    }
    
    public void setModuleId(String moduleId) {
        this.moduleId = moduleId;
    }
    
    public String getResourceId() {
        return resourceId;
    }
    
    public void setResourceId(String resourceId) {
        this.resourceId = resourceId;
    }

    public ModuleDto getModuleDto() {
        return moduleDto;
    }

    public ResourceDto getResourceDto() {
        return resourceDto;
    }

    public static ConfigurationSubscription from(String mtaId, String spaceId, String appName, ConfigurationFilter filter, Module module,
                                                 Resource resource, int majorSchemaVersion) {
        if (majorSchemaVersion == 2) {
            ResourceDto resourceDto = ResourceDto.from2(resource);
            ModuleDto moduleDto = ModuleDto.from2(module);
            return new ConfigurationSubscription(0, mtaId, spaceId, appName, filter, moduleDto, resourceDto, null, null);
        }
        throw new UnsupportedOperationException(format(Messages.UNSUPPORTED_VERSION, majorSchemaVersion));
    }

    public static class ModuleDto {

        private String name;
        private Map properties;
        private List providedDependencies;
        private List requiredDependencies;

        public ModuleDto() {

        }

        public ModuleDto(String name, Map properties, List providedDependencies,
                         List requiredDependencies) {
            this.name = name;
            this.properties = properties;
            this.providedDependencies = providedDependencies;
            this.requiredDependencies = requiredDependencies;
        }

        public String getName() {
            return name;
        }

        public Map getProperties() {
            return properties;
        }

        public List getProvidedDependencies() {
            return providedDependencies;
        }

        public List getRequiredDependencies() {
            return requiredDependencies;
        }

        public static ModuleDto from2(Module module) {
            return new ModuleDto(module.getName(),
                                 module.getProperties(),
                                 fromProvidedDependencies2(module.getProvidedDependencies()),
                                 fromRequiredDependencies2(module.getRequiredDependencies()));
        }

        private static List fromProvidedDependencies2(List providedDependencies) {
            return providedDependencies.stream()
                                       .map(ProvidedDependencyDto::from2)
                                       .collect(Collectors.toList());
        }

        private static List fromRequiredDependencies2(List requiredDependencies) {
            return requiredDependencies.stream()
                                       .map(RequiredDependencyDto::from2)
                                       .collect(Collectors.toList());
        }

    }

    public static class ResourceDto {

        private String name;
        private Map properties;

        public ResourceDto() {

        }

        public ResourceDto(String name, Map properties) {
            this.name = name;
            this.properties = properties;
        }

        public String getName() {
            return name;
        }

        public Map getProperties() {
            return properties;
        }

        public static ResourceDto from2(Resource resource) {
            return new ResourceDto(resource.getName(), resource.getProperties());
        }

    }

    public static class RequiredDependencyDto {

        private String name;
        private String list;
        private Map properties;

        public RequiredDependencyDto() {

        }

        public RequiredDependencyDto(String name, String list, Map properties) {
            this.name = name;
            this.list = list;
            this.properties = properties;
        }

        public String getName() {
            return name;
        }

        public String getList() {
            return list;
        }

        public Map getProperties() {
            return properties;
        }

        public static RequiredDependencyDto from2(RequiredDependency requiredDependency) {
            return new RequiredDependencyDto(requiredDependency.getName(),
                                             requiredDependency.getList(),
                                             requiredDependency.getProperties());
        }

    }

    public static class ProvidedDependencyDto {

        private String name;
        private Map properties;

        public ProvidedDependencyDto() {

        }

        public ProvidedDependencyDto(String name, Map properties) {
            this.name = name;
            this.properties = properties;
        }

        public String getName() {
            return name;
        }

        public Map getProperties() {
            return properties;
        }

        public static ProvidedDependencyDto from2(ProvidedDependency providedDependency) {
            return new ProvidedDependencyDto(providedDependency.getName(), providedDependency.getProperties());
        }

    }

    public boolean matches(List entries) {
        return entries.stream()
                      .anyMatch(entry -> filter.matches(entry));
    }

    public boolean matches(ConfigurationEntry entry) {
        return filter.matches(entry);
    }

    @Override
    public String getConfigurationType() {
        return "configuration subscription";
    }

    @Override
    public String getConfigurationName() {
        return String.valueOf(id);
    }

    @Override
    public List getConfigurationIdentifiers() {
        return Arrays.asList(new ConfigurationIdentifier("mta id", mtaId), new ConfigurationIdentifier("application name", appName),
                             new ConfigurationIdentifier("space id", spaceId));
    }

    @Override
    public boolean equals(Object o) {
        if (this == o)
            return true;
        if (o == null || getClass() != o.getClass())
            return false;
        ConfigurationSubscription that = (ConfigurationSubscription) o;
        return id == that.id && Objects.equals(mtaId, that.mtaId) && Objects.equals(filter, that.filter)
            && Objects.equals(spaceId, that.spaceId) && Objects.equals(appName, that.appName);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id, mtaId, filter, spaceId, appName);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy