Search in sources :

Example 1 with CategoryImpl

use of cern.modesti.schema.category.CategoryImpl in project modesti by jlsalmon.

the class SchemaInitialiser method loadSchemas.

/**
 * Load all schemas inside plugins.
 *
 * @return the list of schemas that were found on the classpath
 * @throws IOException
 * @throws URISyntaxException
 */
private List<SchemaImpl> loadSchemas() throws IOException, URISyntaxException {
    List<SchemaImpl> schemas = new ArrayList<>();
    Map<String, List<SchemaImpl>> pluginSchemas = loadPluginSchemas();
    Map<String, List<CategoryImpl>> pluginCategories = loadPluginCategories();
    Map<String, List<DatasourceImpl>> pluginDatasources = loadPluginDatasources();
    for (Map.Entry<String, List<SchemaImpl>> entry : pluginSchemas.entrySet()) {
        for (SchemaImpl schema : entry.getValue()) {
            List<Category> categories = new ArrayList<>();
            List<Datasource> datasources = new ArrayList<>();
            // Skip over abstract schemas
            if (schema.isAbstract()) {
                continue;
            }
            // Merge in the parent schema if it is specified
            String parent = schema.getParent();
            if (parent != null) {
                Schema parentSchema = getSchema(pluginSchemas, parent);
                if (parentSchema == null) {
                    throw new IllegalArgumentException(format("Schema %s extends from unknown parent schema %s", schema.getId(), parent));
                }
                categories.addAll(parentSchema.getCategories());
                datasources.addAll(parentSchema.getDatasources());
                schema.setIdProperty(parentSchema.getIdProperty());
                schema.setSelectableStates(parentSchema.getSelectableStates());
                schema.setRowCommentStates(parentSchema.getRowCommentStates());
            }
            // Attach categories that are specified in the schema
            for (Category category : schema.getCategories()) {
                boolean categoryFound = false;
                for (Map.Entry<String, List<CategoryImpl>> categoryEntry : pluginCategories.entrySet()) {
                    if (categoryEntry.getValue().contains(category)) {
                        categories.add(categoryEntry.getValue().get(categoryEntry.getValue().indexOf(category)));
                        categoryFound = true;
                    }
                }
                if (!categoryFound) {
                    throw new IllegalArgumentException(format("Category %s was not found for schema %s", category.getId(), schema.getId()));
                }
            // TODO: support referencing another field by name to avoid duplicate field definitions
            }
            // Attach datasources that are specified in the schema
            for (Datasource datasource : schema.getDatasources()) {
                boolean datasourceFound = false;
                for (Map.Entry<String, List<DatasourceImpl>> datasourceEntry : pluginDatasources.entrySet()) {
                    if (datasourceEntry.getValue().contains(datasource)) {
                        datasources.add(datasourceEntry.getValue().get(datasourceEntry.getValue().indexOf(datasource)));
                        datasourceFound = true;
                    }
                }
                if (!datasourceFound) {
                    throw new IllegalArgumentException(format("Datasource %s was not found for schema %s", datasource.getId(), schema.getId()));
                }
            }
            // Process any overrides to the core categories
            for (Category override : schema.getOverrides()) {
                if (categories.contains(override)) {
                    Category overridden = mergeCategories(categories.get(categories.indexOf(override)), override);
                    categories.set(categories.indexOf(override), overridden);
                }
            }
            // Process any overrides to the core datasources
            for (Datasource override : schema.getDatasourceOverrides()) {
                if (datasources.contains(override)) {
                    Category overridden = mergeCategories(datasources.get(datasources.indexOf(override)), override);
                    datasources.set(datasources.indexOf(override), new DatasourceImpl((DatasourceImpl) overridden));
                }
            }
            schema.setCategories(categories);
            schema.setDatasources(datasources);
            // Invoke any post processors
            Map<String, SchemaPostProcessor> postProcessors = applicationContext.getBeansOfType(SchemaPostProcessor.class);
            for (SchemaPostProcessor postProcessor : postProcessors.values()) {
                schema = (SchemaImpl) postProcessor.postProcess(schema);
            }
            schemas.add(schema);
        }
    }
    String loadedPlugIns = schemas.stream().map(p -> p.getId()).collect(Collectors.joining(", "));
    log.trace("loaded {} schemas [{}]", schemas.size(), loadedPlugIns);
    return schemas;
}
Also used : Datasource(cern.modesti.schema.category.Datasource) java.util(java.util) URISyntaxException(java.net.URISyntaxException) Autowired(org.springframework.beans.factory.annotation.Autowired) SerializationUtils(org.apache.commons.lang3.SerializationUtils) PathMatchingResourcePatternResolver(org.springframework.core.io.support.PathMatchingResourcePatternResolver) URLConnection(java.net.URLConnection) JarURLConnection(java.net.JarURLConnection) Resource(org.springframework.core.io.Resource) DatasourceImpl(cern.modesti.schema.category.DatasourceImpl) SchemaPostProcessor(cern.modesti.schema.spi.SchemaPostProcessor) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) IOException(java.io.IOException) Datasource(cern.modesti.schema.category.Datasource) ApplicationContext(org.springframework.context.ApplicationContext) Collectors(java.util.stream.Collectors) File(java.io.File) StandardCharsets(java.nio.charset.StandardCharsets) String.format(java.lang.String.format) Slf4j(lombok.extern.slf4j.Slf4j) Component(org.springframework.stereotype.Component) Field(cern.modesti.schema.field.Field) ByteStreams(com.google.common.io.ByteStreams) PostConstruct(javax.annotation.PostConstruct) Category(cern.modesti.schema.category.Category) CategoryImpl(cern.modesti.schema.category.CategoryImpl) ResourcePatternResolver(org.springframework.core.io.support.ResourcePatternResolver) BeanUtils(org.springframework.beans.BeanUtils) Category(cern.modesti.schema.category.Category) SchemaPostProcessor(cern.modesti.schema.spi.SchemaPostProcessor) DatasourceImpl(cern.modesti.schema.category.DatasourceImpl)

Example 2 with CategoryImpl

use of cern.modesti.schema.category.CategoryImpl in project modesti by jlsalmon.

the class SchemaInitialiser method mergeCategories.

/**
 * Merge two {@link Category} instances, copying the fields defined in the
 * overriding category to the overridden category.
 *
 * @param overridden the base category to merge into
 * @param override   the overriding category to merge from
 * @return a new category containing the result of the merge
 */
private Category mergeCategories(Category overridden, Category override) {
    overridden = new CategoryImpl((CategoryImpl) overridden);
    List<Field> newFields = new ArrayList<>();
    List<Field> oldFields = overridden.getFields();
    for (Field newField : override.getFields()) {
        if (!oldFields.contains(newField)) {
            newFields.add(newField);
        } else {
            oldFields.set(oldFields.indexOf(newField), mergeFields(oldFields.get(oldFields.indexOf(newField)), newField));
        }
    }
    // Copy the editable state list.
    if (override.getEditable() != null) {
        overridden.setEditable(override.getEditable());
    }
    // Copy the constraint list
    if (override.getConstraints() != null) {
        overridden.setConstraints(override.getConstraints());
    }
    overridden.getFields().addAll(newFields);
    return overridden;
}
Also used : CategoryImpl(cern.modesti.schema.category.CategoryImpl) Field(cern.modesti.schema.field.Field)

Aggregations

CategoryImpl (cern.modesti.schema.category.CategoryImpl)2 Field (cern.modesti.schema.field.Field)2 Category (cern.modesti.schema.category.Category)1 Datasource (cern.modesti.schema.category.Datasource)1 DatasourceImpl (cern.modesti.schema.category.DatasourceImpl)1 SchemaPostProcessor (cern.modesti.schema.spi.SchemaPostProcessor)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 ByteStreams (com.google.common.io.ByteStreams)1 File (java.io.File)1 IOException (java.io.IOException)1 String.format (java.lang.String.format)1 JarURLConnection (java.net.JarURLConnection)1 URISyntaxException (java.net.URISyntaxException)1 URLConnection (java.net.URLConnection)1 StandardCharsets (java.nio.charset.StandardCharsets)1 java.util (java.util)1 Collectors (java.util.stream.Collectors)1 PostConstruct (javax.annotation.PostConstruct)1 Slf4j (lombok.extern.slf4j.Slf4j)1 SerializationUtils (org.apache.commons.lang3.SerializationUtils)1