Search in sources :

Example 1 with N2oSqlDataProvider

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

the class SqlDataProviderEngine method invoke.

@Override
public Object invoke(N2oSqlDataProvider invocation, Map<String, Object> data) {
    Map<String, Object> args = new HashMap<>(data);
    String query = loadQuery(invocation);
    query = replaceListPlaceholder(query, ":select", args.remove("select"), "*", QueryUtil::reduceComma);
    query = replaceListPlaceholder(query, ":join", args.remove("join"), "", QueryUtil::reduceSpace);
    query = replaceListPlaceholder(query, ":filters", args.remove("filters"), "1=1", QueryUtil::reduceAnd);
    query = replaceListPlaceholder(query, ":sorting", args.remove("sorting"), "1", s -> replaceSortDirection(s, data), QueryUtil::reduceComma);
    query = replacePlaceholder(query, ":limit", args.remove("limit"), "10");
    query = replacePlaceholder(query, ":offset", args.remove("offset"), "0");
    query = replacePlaceholder(query, ":count", args.remove("count"), "-1");
    if (invocation.getConnectionUrl() == null)
        return executeQuery(args, query, rowMapperFactory.produce(castDefault(invocation.getRowMapper(), "map")), namedParameterJdbcTemplate);
    NamedParameterJdbcTemplate jdbcTemplate = createJdbcTemplate(invocation);
    return executeQuery(args, query, rowMapperFactory.produce(castDefault(invocation.getRowMapper(), "map")), jdbcTemplate);
}
Also used : N2oSqlDataProvider(net.n2oapp.framework.api.metadata.dataprovider.N2oSqlDataProvider) EngineFactory(net.n2oapp.engine.factory.EngineFactory) QueryUtil(net.n2oapp.framework.engine.data.QueryUtil) NamedParameterJdbcTemplate(org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate) Autowired(org.springframework.beans.factory.annotation.Autowired) MapSqlParameterSource(org.springframework.jdbc.core.namedparam.MapSqlParameterSource) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) TypicalEngine(net.n2oapp.engine.factory.TypicalEngine) SpringEngineFactory(net.n2oapp.engine.factory.integration.spring.SpringEngineFactory) Map(java.util.Map) MapInvocationEngine(net.n2oapp.framework.api.data.MapInvocationEngine) ResourceLoader(org.springframework.core.io.ResourceLoader) ResourceLoaderAware(org.springframework.context.ResourceLoaderAware) N2oException(net.n2oapp.framework.api.exception.N2oException) BeansException(org.springframework.beans.BeansException) IOException(java.io.IOException) QueryBlank(net.n2oapp.framework.engine.util.QueryBlank) ApplicationContext(org.springframework.context.ApplicationContext) IOUtils(org.apache.commons.io.IOUtils) GeneratedKeyHolder(org.springframework.jdbc.support.GeneratedKeyHolder) CompileUtil.castDefault(net.n2oapp.framework.api.metadata.local.util.CompileUtil.castDefault) HikariConfig(com.zaxxer.hikari.HikariConfig) NamedParameterUtils(net.n2oapp.framework.engine.util.NamedParameterUtils) List(java.util.List) HikariDataSource(com.zaxxer.hikari.HikariDataSource) RowMapper(org.springframework.jdbc.core.RowMapper) ApplicationContextAware(org.springframework.context.ApplicationContextAware) InputStream(java.io.InputStream) HashMap(java.util.HashMap) NamedParameterJdbcTemplate(org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate)

Example 2 with N2oSqlDataProvider

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

the class InvocationProcessorTest method testMapsInvocation.

@Test
public void testMapsInvocation() {
    DataSet dataSet = new DataSet();
    dataSet.put("id", 1);
    List<AbstractParameter> inMapping = new ArrayList<>();
    ObjectSimpleField param1 = new ObjectSimpleField();
    param1.setId("id");
    param1.setMapping("['id']");
    inMapping.add(param1);
    List<ObjectSimpleField> outMapping = new ArrayList<>();
    ObjectSimpleField outParam = new ObjectSimpleField();
    outParam.setId("id");
    outParam.setMapping("[0][0][0]");
    outMapping.add(outParam);
    N2oSqlDataProvider invocation = new N2oSqlDataProvider();
    invocation.setQuery("select 1");
    DataSet result = invocationProcessor.invoke(invocation, dataSet, inMapping, outMapping);
    assert result.size() == 1;
    assert result.get("id").equals(1);
}
Also used : ObjectSimpleField(net.n2oapp.framework.api.metadata.global.dao.object.field.ObjectSimpleField) DataSet(net.n2oapp.criteria.dataset.DataSet) N2oSqlDataProvider(net.n2oapp.framework.api.metadata.dataprovider.N2oSqlDataProvider) AbstractParameter(net.n2oapp.framework.api.metadata.global.dao.object.AbstractParameter) Test(org.junit.Test)

Example 3 with N2oSqlDataProvider

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

the class TestDynamicProvider method read.

@Override
public List<? extends SourceMetadata> read(String context) {
    N2oSimplePage page = new N2oSimplePage();
    N2oForm form = new N2oForm();
    form.setName(context);
    form.setRefId("formForTestDynamic");
    form.setQueryId("testDynamic?Dummy");
    form.adapterV4();
    page.setWidget(form);
    N2oObject n2oObject = new N2oObject();
    n2oObject.setName(TEST_DYNAMIC + "Object");
    N2oQuery query = new N2oQuery();
    query.setId(TEST_DYNAMIC + "?" + context);
    N2oQuery.Selection selection = new N2oQuery.Selection(N2oQuery.Selection.Type.list);
    N2oSqlDataProvider invocation = new N2oSqlDataProvider();
    invocation.setQuery("test select");
    selection.setInvocation(invocation);
    query.setLists(new N2oQuery.Selection[] { selection });
    query.setFields(new N2oQuery.Field[] { new N2oQuery.Field("id") });
    query.setObjectId("testDynamic?Dummy");
    N2oTable table = new N2oTable();
    table.setQueryId("testDynamic?Dummy");
    N2oSimpleColumn idColumn = new N2oSimpleColumn();
    idColumn.setId("id");
    idColumn.setTextFieldId("id");
    table.setColumns(new AbstractColumn[] { idColumn });
    table.setId("testDynamic?Dummy");
    N2oToolbar toolbar = new N2oToolbar();
    toolbar.setPlace("topLeft");
    N2oButton create = new N2oButton();
    create.setId("create");
    N2oShowModal showModal = new N2oShowModal();
    showModal.setPageId("testDynamic?Dummy");
    create.setModel(ReduxModel.filter);
    create.setAction(showModal);
    N2oButton update = new N2oButton();
    update.setId("update");
    N2oShowModal updShowModal = new N2oShowModal();
    updShowModal.setPageId("testDynamic?Dummy");
    update.setModel(ReduxModel.resolve);
    update.setAction(updShowModal);
    toolbar.setItems(new ToolbarItem[] { create, update });
    table.setToolbars(new N2oToolbar[] { toolbar });
    table.adapterV4();
    return Arrays.asList(n2oObject, query, table, page);
}
Also used : N2oSqlDataProvider(net.n2oapp.framework.api.metadata.dataprovider.N2oSqlDataProvider) N2oObject(net.n2oapp.framework.api.metadata.global.dao.object.N2oObject) N2oShowModal(net.n2oapp.framework.api.metadata.event.action.N2oShowModal) N2oTable(net.n2oapp.framework.api.metadata.global.view.widget.table.N2oTable) N2oSimplePage(net.n2oapp.framework.api.metadata.global.view.page.N2oSimplePage) N2oButton(net.n2oapp.framework.api.metadata.global.view.widget.toolbar.N2oButton) N2oQuery(net.n2oapp.framework.api.metadata.global.dao.N2oQuery) N2oToolbar(net.n2oapp.framework.api.metadata.global.view.widget.toolbar.N2oToolbar) N2oForm(net.n2oapp.framework.api.metadata.global.view.widget.N2oForm) N2oSimpleColumn(net.n2oapp.framework.api.metadata.global.view.widget.table.column.N2oSimpleColumn)

Example 4 with N2oSqlDataProvider

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

the class StandardFieldCompileTest method testValidations.

@Test
public void testValidations() {
    PageContext pageContext = new PageContext("testStandardField");
    pageContext.setSubmitOperationId("update");
    SimplePage page = (SimplePage) compile("net/n2oapp/framework/config/mapping/testStandardField.page.xml", "net/n2oapp/framework/config/mapping/testCell.object.xml").get(pageContext);
    List<Validation> clientValidations = page.getDatasources().get("testStandardField_form").getValidations().get("test3");
    assertThat(clientValidations.size(), is(2));
    ConstraintValidation validation = (ConstraintValidation) clientValidations.get(0);
    assertThat(validation.getId(), is("val1"));
    assertThat(validation.getMessage(), is("Message"));
    assertThat(validation.getSeverity(), is(SeverityType.danger));
    assertThat(validation.getMoment(), is(N2oValidation.ServerMoment.beforeOperation));
    assertThat(validation.getRequiredFields().size(), is(1));
    assertThat(validation.getRequiredFields().contains("param"), is(true));
    assertThat(validation.getInvocation(), instanceOf(N2oSqlDataProvider.class));
    assertThat(((N2oSqlDataProvider) validation.getInvocation()).getQuery(), is("select * from table"));
    assertThat(validation.getInParametersList().size(), is(1));
    ObjectSimpleField parameter = ((ObjectSimpleField) validation.getInParametersList().get(0));
    assertThat(parameter.getDomain(), is("boolean"));
    assertThat(parameter.getRequired(), is(true));
    assertThat(parameter.getMapping(), is("mapping"));
    assertThat(parameter.getNormalize(), is("normalize"));
    ConditionValidation validation2 = (ConditionValidation) clientValidations.get(1);
    assertThat(validation2.getId(), is("val2"));
    assertThat(validation2.getMessage(), is("Message"));
    assertThat(validation2.getSeverity(), is(SeverityType.warning));
    assertThat(validation2.getSide(), is("client,server"));
    ActionContext actionContext = (ActionContext) route("/testStandardField/submit", CompiledObject.class);
    List<Validation> serverValidations = actionContext.getValidations();
    assertThat(serverValidations.size(), is(4));
    assertThat(serverValidations.get(0).getFieldId(), is("test1"));
    assertThat(serverValidations.get(1), is(validation));
    assertThat(serverValidations.get(2), is(validation2));
    MandatoryValidation validation3 = (MandatoryValidation) serverValidations.get(3);
    assertThat(validation3.getId(), is("val3"));
    assertThat(validation3.getMessage(), is("Message"));
    assertThat(validation3.getSeverity(), is(SeverityType.danger));
    assertThat(validation3.getEnabled(), is(false));
    assertThat(validation3.getSide(), is("server"));
}
Also used : ConditionValidation(net.n2oapp.framework.api.data.validation.ConditionValidation) N2oValidation(net.n2oapp.framework.api.metadata.global.dao.validation.N2oValidation) Validation(net.n2oapp.framework.api.data.validation.Validation) MandatoryValidation(net.n2oapp.framework.api.data.validation.MandatoryValidation) ConstraintValidation(net.n2oapp.framework.api.data.validation.ConstraintValidation) ObjectSimpleField(net.n2oapp.framework.api.metadata.global.dao.object.field.ObjectSimpleField) ConditionValidation(net.n2oapp.framework.api.data.validation.ConditionValidation) N2oSqlDataProvider(net.n2oapp.framework.api.metadata.dataprovider.N2oSqlDataProvider) CompiledObject(net.n2oapp.framework.api.metadata.local.CompiledObject) PageContext(net.n2oapp.framework.config.metadata.compile.context.PageContext) SimplePage(net.n2oapp.framework.api.metadata.meta.page.SimplePage) ConstraintValidation(net.n2oapp.framework.api.data.validation.ConstraintValidation) ActionContext(net.n2oapp.framework.config.metadata.compile.context.ActionContext) MandatoryValidation(net.n2oapp.framework.api.data.validation.MandatoryValidation) Test(org.junit.Test)

Aggregations

N2oSqlDataProvider (net.n2oapp.framework.api.metadata.dataprovider.N2oSqlDataProvider)4 ObjectSimpleField (net.n2oapp.framework.api.metadata.global.dao.object.field.ObjectSimpleField)2 Test (org.junit.Test)2 HikariConfig (com.zaxxer.hikari.HikariConfig)1 HikariDataSource (com.zaxxer.hikari.HikariDataSource)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 DataSet (net.n2oapp.criteria.dataset.DataSet)1 EngineFactory (net.n2oapp.engine.factory.EngineFactory)1 TypicalEngine (net.n2oapp.engine.factory.TypicalEngine)1 SpringEngineFactory (net.n2oapp.engine.factory.integration.spring.SpringEngineFactory)1 MapInvocationEngine (net.n2oapp.framework.api.data.MapInvocationEngine)1 ConditionValidation (net.n2oapp.framework.api.data.validation.ConditionValidation)1 ConstraintValidation (net.n2oapp.framework.api.data.validation.ConstraintValidation)1 MandatoryValidation (net.n2oapp.framework.api.data.validation.MandatoryValidation)1 Validation (net.n2oapp.framework.api.data.validation.Validation)1