Search in sources :

Example 1 with SourceMetadata

use of net.n2oapp.framework.api.metadata.SourceMetadata in project n2o-framework by i-novus-llc.

the class DynamicConfigReaderTest method test.

@Test
public void test() throws Exception {
    List<SourceMetadata> cache = new ArrayList<>();
    N2oDynamicMetadataProviderFactory providerFactory = new N2oDynamicMetadataProviderFactory();
    providerFactory.add(new TestDynamicMetadataProvider("sec", asList(setId(new N2oStandardPage(), "sec?role"), setId(new N2oObject(), "sec?role"))), new TestDynamicMetadataProvider("amb", asList(setId(new N2oStandardPage(), "amb?page1"), setId(new N2oStandardPage(), "amb?page2"), setId(new N2oObject(), "amb?object1"))));
    JavaSourceLoader reader = new JavaSourceLoader(providerFactory, cache::add);
    // проверяем чтение
    SourceMetadata metadata = reader.load(new JavaInfo("sec", N2oPage.class), "role");
    assert metadata.getId().equals("sec?role");
    cache.clear();
    metadata = reader.load(new JavaInfo("sec", N2oObject.class), "role");
    assert metadata.getId().equals("sec?role");
    // проверяем кэширование
    List ids = cache.stream().map(SourceMetadata::getId).collect(Collectors.toList());
    assert ids.size() == 2;
    assert ids.contains("sec?role");
    cache.clear();
    // проверяем чтение
    metadata = reader.load(new JavaInfo("amb", N2oPage.class), "page1");
    // проверяем кэширование
    ids = cache.stream().map(SourceMetadata::getId).collect(Collectors.toList());
    assert ids.size() == 3;
    assert ids.contains("amb?page1");
    assert ids.contains("amb?page2");
    assert ids.contains("amb?object1");
    assert metadata.getId().equals("amb?page1");
    metadata = reader.load(new JavaInfo("amb", N2oPage.class), "page2");
    assert metadata.getId().equals("amb?page2");
    metadata = reader.load(new JavaInfo("amb", N2oObject.class), "object1");
    assert metadata.getId().equals("amb?object1");
}
Also used : N2oPage(net.n2oapp.framework.api.metadata.global.view.page.N2oPage) SourceMetadata(net.n2oapp.framework.api.metadata.SourceMetadata) N2oStandardPage(net.n2oapp.framework.api.metadata.global.view.page.N2oStandardPage) N2oDynamicMetadataProviderFactory(net.n2oapp.framework.config.register.dynamic.N2oDynamicMetadataProviderFactory) N2oObject(net.n2oapp.framework.api.metadata.global.dao.object.N2oObject) ArrayList(java.util.ArrayList) TestDynamicMetadataProvider(net.n2oapp.framework.config.register.mock.TestDynamicMetadataProvider) List(java.util.List) Arrays.asList(java.util.Arrays.asList) ArrayList(java.util.ArrayList) JavaSourceLoader(net.n2oapp.framework.config.register.dynamic.JavaSourceLoader) Test(org.junit.Test)

Example 2 with SourceMetadata

use of net.n2oapp.framework.api.metadata.SourceMetadata in project n2o-framework by i-novus-llc.

the class N2oReadPipeline method read.

@Override
public ReadTerminalPipeline<ReadCompileTerminalPipeline<ReadCompileBindTerminalPipeline>> read() {
    pullOp(READ);
    return new ReadTerminalPipeline<ReadCompileTerminalPipeline<ReadCompileBindTerminalPipeline>>() {

        @Override
        public <S extends SourceMetadata> S get(String id, Class<S> sourceClass) {
            return execute(new DummyCompileContext<>(id, sourceClass), null, null);
        }

        @Override
        public <S extends SourceMetadata> S get(String id, Class<S> sourceClass, SourceProcessor p) {
            return execute(ops(), new DummyCompileContext<>(id, sourceClass), null, null, null, null, p);
        }

        @Override
        public ReadPersistTerminalPipeline persist() {
            pullOp(PERSIST);
            return new ReadPersistTerminalPipeline() {

                @Override
                public <S extends SourceMetadata> InputStream get(String id, Class<S> sourceClass) {
                    return execute(new DummyCompileContext<>(id, sourceClass), null, null);
                }

                @Override
                public <S extends SourceMetadata> void set(String id, Class<S> sourceClass, OutputStream output) {
                    try (InputStream is = get(id, sourceClass)) {
                        IOUtils.copy(is, output);
                    } catch (IOException e) {
                        throw new IllegalStateException(e);
                    }
                }

                @Override
                public <S extends SourceMetadata> void set(String id, MetaType metaType, String directory) {
                    String path = PathUtil.concatFileNameAndBasePath(id + "." + metaType.getSourceType() + ".xml", directory);
                    FileSystemUtil.saveContentToFile(get(id, metaType.getBaseSourceClass()), new File(path));
                }
            };
        }

        @Override
        public ReadCompileTerminalPipeline<ReadCompileBindTerminalPipeline> compile() {
            pullOp(COMPILE);
            return new ReadCompileTerminalPipeline<ReadCompileBindTerminalPipeline>() {

                @Override
                public <D extends Compiled> D get(CompileContext<D, ?> ctx) {
                    return execute(ctx, null, null);
                }

                @Override
                public <D extends Compiled> D get(CompileContext<D, ?> ctx, CompileProcessor p) {
                    return execute(ctx, null, null, (N2oCompileProcessor) p);
                }

                @Override
                public ReadCompileBindTerminalPipeline bind() {
                    pullOp(BIND);
                    return new ReadCompileBindTerminalPipeline() {

                        @Override
                        public <D extends Compiled> D get(CompileContext<D, ?> context, DataSet data) {
                            return execute(context, data, null);
                        }

                        @Override
                        public <D extends Compiled> D get(CompileContext<D, ?> context, DataSet data, SubModelsProcessor subModelsProcessor) {
                            return execute(context, data, null, subModelsProcessor);
                        }

                        @Override
                        public ReadCompileBindTerminalPipeline bind() {
                            pullOp(BIND);
                            return this;
                        }
                    };
                }

                @Override
                public ReadCompileTerminalPipeline<ReadCompileBindTerminalPipeline> transform() {
                    pullOp(COMPILE_TRANSFORM);
                    return this;
                }

                @Override
                public ReadCompileTerminalPipeline<ReadCompileBindTerminalPipeline> cache() {
                    pullOp(COMPILE_CACHE);
                    return this;
                }

                @Override
                public ReadCompileTerminalPipeline<ReadCompileBindTerminalPipeline> copy() {
                    pullOp(COPY);
                    return this;
                }
            };
        }

        @Override
        public ReadTerminalPipeline<ReadCompileTerminalPipeline<ReadCompileBindTerminalPipeline>> validate() {
            pullOp(VALIDATE);
            return this;
        }

        @Override
        public ReadTerminalPipeline<ReadCompileTerminalPipeline<ReadCompileBindTerminalPipeline>> merge() {
            pullOp(MERGE);
            return this;
        }

        @Override
        public ReadTerminalPipeline<ReadCompileTerminalPipeline<ReadCompileBindTerminalPipeline>> transform() {
            pullOp(SOURCE_TRANSFORM);
            return this;
        }

        @Override
        public ReadTerminalPipeline<ReadCompileTerminalPipeline<ReadCompileBindTerminalPipeline>> cache() {
            pullOp(SOURCE_CACHE);
            return this;
        }

        @Override
        public ReadTerminalPipeline<ReadCompileTerminalPipeline<ReadCompileBindTerminalPipeline>> copy() {
            pullOp(COPY);
            return this;
        }
    };
}
Also used : SourceMetadata(net.n2oapp.framework.api.metadata.SourceMetadata) DataSet(net.n2oapp.criteria.dataset.DataSet) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) CompileProcessor(net.n2oapp.framework.api.metadata.compile.CompileProcessor) N2oCompileProcessor(net.n2oapp.framework.config.metadata.compile.N2oCompileProcessor) IOException(java.io.IOException) CompileContext(net.n2oapp.framework.api.metadata.compile.CompileContext) BaseCompileContext(net.n2oapp.framework.config.metadata.compile.context.BaseCompileContext) Compiled(net.n2oapp.framework.api.metadata.Compiled) SourceProcessor(net.n2oapp.framework.api.metadata.compile.SourceProcessor) SubModelsProcessor(net.n2oapp.framework.api.util.SubModelsProcessor) File(java.io.File) MetaType(net.n2oapp.framework.api.register.MetaType)

Example 3 with SourceMetadata

use of net.n2oapp.framework.api.metadata.SourceMetadata in project n2o-framework by i-novus-llc.

the class JavaSourceLoader method readContent.

@SuppressWarnings("unchecked")
private <T extends SourceMetadata> T readContent(String id, Class<T> metadataClass, String params) {
    DynamicMetadataProvider provider = providerFactory.produce(id);
    List<SourceMetadata> metadataList = provider.read(params);
    // проверяем что id динамические
    metadataList.stream().filter(m -> m.getId() == null).forEach(m -> m.setId(provider.getCode() + "?" + params));
    DynamicUtil.checkDynamicIds(metadataList.stream().map(SourceMetadata::getId).collect(Collectors.toList()), provider.getCode());
    // кэшируем все что нашли
    if (cacheService != null && provider.cache(params))
        cache(metadataList);
    // ищем ту единственную
    Optional<? extends SourceMetadata> optional = metadataList.stream().filter(m -> m.getId().equals(id + "?" + params)).filter(m -> metadataClass.isAssignableFrom(m.getClass())).findAny();
    if (optional.isPresent())
        return (T) optional.get();
    else
        throw new ReferentialIntegrityViolationException(id, metadataClass);
}
Also used : Consumer(java.util.function.Consumer) ReferentialIntegrityViolationException(net.n2oapp.framework.config.reader.ReferentialIntegrityViolationException) DynamicUtil(net.n2oapp.framework.api.DynamicUtil) JavaInfo(net.n2oapp.framework.config.register.JavaInfo) List(java.util.List) DynamicMetadataProviderFactory(net.n2oapp.framework.api.register.DynamicMetadataProviderFactory) DynamicMetadataProvider(net.n2oapp.framework.api.register.DynamicMetadataProvider) Optional(java.util.Optional) SourceLoader(net.n2oapp.framework.api.reader.SourceLoader) Collectors(java.util.stream.Collectors) SourceMetadata(net.n2oapp.framework.api.metadata.SourceMetadata) SourceMetadata(net.n2oapp.framework.api.metadata.SourceMetadata) ReferentialIntegrityViolationException(net.n2oapp.framework.config.reader.ReferentialIntegrityViolationException) DynamicMetadataProvider(net.n2oapp.framework.api.register.DynamicMetadataProvider)

Example 4 with SourceMetadata

use of net.n2oapp.framework.api.metadata.SourceMetadata in project n2o-framework by i-novus-llc.

the class XmlMetadataLoaderTest method testLoadWithParams.

/**
 * Проверка, что параметры переданные в load резолвятся в xml
 */
@Test
public void testLoadWithParams() {
    XmlInfo info = new XmlInfo("testXmlInfo", N2oSimplePage.class, "net/n2oapp/framework/config/metadata/testXmlInfo.page.xml");
    XmlMetadataLoader xmlMetadataLoader = new XmlMetadataLoader(new ReaderFactoryByMap().register(new SimplePageElementIOv3()).register(new FormElementIOV4()));
    SourceMetadata metadata = xmlMetadataLoader.load(info, "formName=Patients");
    assertThat(((N2oSimplePage) metadata).getWidget().getName(), is("Patients"));
    assertThat(MetadataParamHolder.getParams().isEmpty(), is(true));
}
Also used : XmlMetadataLoader(net.n2oapp.framework.config.reader.XmlMetadataLoader) SourceMetadata(net.n2oapp.framework.api.metadata.SourceMetadata) FormElementIOV4(net.n2oapp.framework.config.io.widget.v4.FormElementIOV4) ReaderFactoryByMap(net.n2oapp.framework.config.selective.reader.ReaderFactoryByMap) SimplePageElementIOv3(net.n2oapp.framework.config.io.page.v3.SimplePageElementIOv3) XmlInfo(net.n2oapp.framework.config.register.XmlInfo) N2oSimplePage(net.n2oapp.framework.api.metadata.global.view.page.N2oSimplePage) Test(org.junit.Test)

Aggregations

SourceMetadata (net.n2oapp.framework.api.metadata.SourceMetadata)4 List (java.util.List)2 Test (org.junit.Test)2 File (java.io.File)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 ArrayList (java.util.ArrayList)1 Arrays.asList (java.util.Arrays.asList)1 Optional (java.util.Optional)1 Consumer (java.util.function.Consumer)1 Collectors (java.util.stream.Collectors)1 DataSet (net.n2oapp.criteria.dataset.DataSet)1 DynamicUtil (net.n2oapp.framework.api.DynamicUtil)1 Compiled (net.n2oapp.framework.api.metadata.Compiled)1 CompileContext (net.n2oapp.framework.api.metadata.compile.CompileContext)1 CompileProcessor (net.n2oapp.framework.api.metadata.compile.CompileProcessor)1 SourceProcessor (net.n2oapp.framework.api.metadata.compile.SourceProcessor)1 N2oObject (net.n2oapp.framework.api.metadata.global.dao.object.N2oObject)1 N2oPage (net.n2oapp.framework.api.metadata.global.view.page.N2oPage)1