Search in sources :

Example 1 with ObjectReferenceField

use of net.n2oapp.framework.api.metadata.global.dao.object.field.ObjectReferenceField in project n2o-framework by i-novus-llc.

the class InvocationValidationCompiler method compileInvocationValidation.

protected void compileInvocationValidation(D compiled, S source, CompileProcessor p) {
    compileValidation(compiled, source, p);
    compiled.setId(source.getId());
    // in
    List<AbstractParameter> inParams = new ArrayList<>();
    if (source.getInFields() != null)
        for (AbstractParameter parameter : source.getInFields()) inParams.add(parameter instanceof ObjectSimpleField ? new ObjectSimpleField((ObjectSimpleField) parameter) : new ObjectReferenceField((ObjectReferenceField) parameter));
    compiled.setInParametersList(inParams);
    // out
    List<ObjectSimpleField> outParams = new ArrayList<>();
    if (source.getOutFields() != null)
        for (ObjectSimpleField parameter : source.getOutFields()) outParams.add(new ObjectSimpleField(parameter));
    ObjectSimpleField resultParam = new ObjectSimpleField();
    resultParam.setId(CompiledObject.VALIDATION_RESULT_PARAM);
    resultParam.setMapping(source.getResult());
    outParams.add(resultParam);
    compiled.setOutParametersList(outParams);
    compiled.setInvocation(source.getN2oInvocation());
}
Also used : ObjectSimpleField(net.n2oapp.framework.api.metadata.global.dao.object.field.ObjectSimpleField) ObjectReferenceField(net.n2oapp.framework.api.metadata.global.dao.object.field.ObjectReferenceField) ArrayList(java.util.ArrayList) AbstractParameter(net.n2oapp.framework.api.metadata.global.dao.object.AbstractParameter)

Example 2 with ObjectReferenceField

use of net.n2oapp.framework.api.metadata.global.dao.object.field.ObjectReferenceField in project n2o-framework by i-novus-llc.

the class InvocationProcessorTest method testAdvancedNestingWithMapInvocationProvider.

@Test
public void testAdvancedNestingWithMapInvocationProvider() {
    N2oTestDataProvider invocation = new N2oTestDataProvider();
    invocation.setOperation(N2oTestDataProvider.Operation.echo);
    // STRUCTURE
    // Reference
    ObjectReferenceField refParam = new ObjectReferenceField();
    refParam.setId("entity");
    refParam.setEntityClass("net.n2oapp.framework.engine.util.TestEntity");
    refParam.setMapping("['refEntity']");
    // Simple
    ObjectSimpleField simpleParam = new ObjectSimpleField();
    simpleParam.setId("valueStr");
    // List
    ObjectListField listParam = new ObjectListField();
    listParam.setId("entities");
    listParam.setMapping("['innerObjList']");
    listParam.setEntityClass("net.n2oapp.framework.engine.util.TestEntity$InnerEntity");
    ObjectSimpleField childParam1 = new ObjectSimpleField();
    childParam1.setId("id");
    childParam1.setMapping("['valueInt']");
    ObjectSimpleField childParam2 = new ObjectSimpleField();
    childParam2.setId("name");
    childParam2.setMapping("['valueStr']");
    // List Set
    ObjectSetField setParam = new ObjectSetField();
    setParam.setId("set");
    setParam.setEntityClass("net.n2oapp.framework.engine.util.TestEntity$InnerEntity$InnerInnerEntity");
    setParam.setMapping("['innerInnerObjSet']");
    ObjectSimpleField setChildParam = new ObjectSimpleField();
    setChildParam.setId("name");
    setChildParam.setMapping("['innerName']");
    setParam.setFields(new AbstractParameter[] { setChildParam });
    listParam.setFields(new AbstractParameter[] { childParam1, childParam2, setParam });
    refParam.setFields(new AbstractParameter[] { simpleParam, listParam });
    // DATASET
    DataSet innerDataSet1 = new DataSet();
    innerDataSet1.put("id", 666);
    innerDataSet1.put("name", "testStr1");
    HashSet innerInnerDataSet = new HashSet();
    innerDataSet1.put("set", innerInnerDataSet);
    innerInnerDataSet.add(new DataSet("name", "code1"));
    innerInnerDataSet.add(new DataSet("name", "code2"));
    DataSet innerDataSet2 = new DataSet();
    innerDataSet2.put("id", 777);
    innerDataSet2.put("name", "testStr2");
    innerInnerDataSet = new HashSet();
    innerDataSet2.put("set", innerInnerDataSet);
    innerInnerDataSet.add(new DataSet("name", "code3"));
    List list = Arrays.asList(innerDataSet1, innerDataSet2);
    DataSet refParamFields = new DataSet();
    refParamFields.put("entities", list);
    refParamFields.put("valueStr", "test");
    DataSet dataSet = new DataSet();
    dataSet.put("entity", refParamFields);
    List<AbstractParameter> inParameters = Arrays.asList(refParam);
    List<ObjectSimpleField> outParameters = new ArrayList<>();
    // Result
    DataSet result = invocationProcessor.invoke(invocation, dataSet, inParameters, outParameters);
    TestEntity entity = (TestEntity) result.get("entity");
    assertThat(entity.getValueInt(), nullValue());
    assertThat(entity.getInnerObj(), nullValue());
    assertThat(entity.getValueStr(), is("test"));
    List<TestEntity.InnerEntity> innerObjList = entity.getInnerObjList();
    assertThat(innerObjList.size(), is(2));
    assertThat(innerObjList.get(0).getValueInt(), is(666));
    assertThat(innerObjList.get(0).getValueStr(), is("testStr1"));
    Set<TestEntity.InnerEntity.InnerInnerEntity> innerInnerObjSet = innerObjList.get(0).getInnerInnerObjSet();
    assertThat(innerInnerObjSet.size(), is(2));
    assertThat(innerInnerObjSet.contains(new TestEntity.InnerEntity.InnerInnerEntity("code1")), is(true));
    assertThat(innerInnerObjSet.contains(new TestEntity.InnerEntity.InnerInnerEntity("code2")), is(true));
    assertThat(innerObjList.get(1).getValueInt(), is(777));
    assertThat(innerObjList.get(1).getValueStr(), is("testStr2"));
    innerInnerObjSet = innerObjList.get(1).getInnerInnerObjSet();
    assertThat(innerInnerObjSet.size(), is(1));
    assertThat(innerInnerObjSet.contains(new TestEntity.InnerEntity.InnerInnerEntity("code3")), is(true));
}
Also used : TestEntity(net.n2oapp.framework.engine.util.TestEntity) ObjectReferenceField(net.n2oapp.framework.api.metadata.global.dao.object.field.ObjectReferenceField) DataSet(net.n2oapp.criteria.dataset.DataSet) N2oTestDataProvider(net.n2oapp.framework.api.metadata.dataprovider.N2oTestDataProvider) ObjectSimpleField(net.n2oapp.framework.api.metadata.global.dao.object.field.ObjectSimpleField) ObjectSetField(net.n2oapp.framework.api.metadata.global.dao.object.field.ObjectSetField) AbstractParameter(net.n2oapp.framework.api.metadata.global.dao.object.AbstractParameter) ObjectListField(net.n2oapp.framework.api.metadata.global.dao.object.field.ObjectListField) Test(org.junit.Test)

Example 3 with ObjectReferenceField

use of net.n2oapp.framework.api.metadata.global.dao.object.field.ObjectReferenceField in project n2o-framework by i-novus-llc.

the class InvocationProcessorTest method fullMappingTest.

@Test
public void fullMappingTest() {
    N2oJavaDataProvider method = new N2oJavaDataProvider();
    method.setMethod("methodReturnedEntity");
    method.setClassName("net.n2oapp.framework.engine.test.source.StaticInvocationTestClass");
    Argument entityTypeArgument = new Argument();
    entityTypeArgument.setName("argument");
    entityTypeArgument.setClassName("net.n2oapp.framework.engine.util.TestEntity");
    entityTypeArgument.setType(Argument.Type.ENTITY);
    method.setArguments(new Argument[] { entityTypeArgument });
    DataSet dataSet = new DataSet();
    DataSet inDataSet = new DataSet();
    inDataSet.put("name", "testName");
    inDataSet.put("id", 123);
    dataSet.put("entity", inDataSet);
    List<DataSet> list = new ArrayList<>();
    inDataSet = new DataSet();
    inDataSet.put("id", 1234);
    inDataSet.put("name", "testName1");
    list.add(inDataSet);
    inDataSet = new DataSet();
    inDataSet.put("name", "testName2");
    inDataSet.put("id", 12345);
    list.add(inDataSet);
    dataSet.put("entities", list);
    List<AbstractParameter> inMapping = new ArrayList<>();
    ObjectReferenceField param = new ObjectReferenceField();
    param.setId("entity");
    param.setMapping("[0].innerObj");
    param.setEntityClass("net.n2oapp.framework.engine.util.TestEntity$InnerEntity");
    ObjectSimpleField inParam1 = new ObjectSimpleField();
    inParam1.setId("name");
    inParam1.setMapping("valueStr");
    ObjectSimpleField inParam2 = new ObjectSimpleField();
    inParam2.setId("id");
    inParam2.setMapping("valueInt");
    param.setFields(new AbstractParameter[] { inParam1, inParam2 });
    inMapping.add(param);
    ObjectListField listParam = new ObjectListField();
    listParam.setId("entities");
    listParam.setMapping("[0].innerObjList");
    listParam.setEntityClass("net.n2oapp.framework.engine.util.TestEntity$InnerEntity");
    inParam1 = new ObjectSimpleField();
    inParam1.setId("name");
    inParam1.setMapping("valueStr");
    inParam1.setDefaultValue("testName1");
    inParam1.setNormalize("#this.toLowerCase()");
    inParam2 = new ObjectSimpleField();
    inParam2.setId("id");
    inParam2.setMapping("valueInt");
    listParam.setFields(new AbstractParameter[] { inParam1, inParam2 });
    inMapping.add(listParam);
    List<ObjectSimpleField> outMapping = new ArrayList<>();
    ObjectSimpleField outParam = new ObjectSimpleField();
    outParam.setId("testValue");
    outParam.setMapping("#this");
    outMapping.add(outParam);
    DataSet resultDataSet = invocationProcessor.invoke(method, dataSet, inMapping, outMapping);
    assertThat(resultDataSet.size(), is(3));
    assertThat(((TestEntity.InnerEntity) resultDataSet.get("entity")).getValueInt(), is(123));
    assertThat(((TestEntity.InnerEntity) resultDataSet.get("entity")).getValueStr(), is("testName"));
    assertThat(((List<TestEntity.InnerEntity>) resultDataSet.get("entities")).get(0).getValueStr(), is("testname1"));
    assertThat(((List<TestEntity.InnerEntity>) resultDataSet.get("entities")).get(1).getValueInt(), is(12345));
    assertThat(((List<TestEntity.InnerEntity>) resultDataSet.get("entities")).get(1).getValueStr(), is("testname2"));
}
Also used : N2oJavaDataProvider(net.n2oapp.framework.api.metadata.dataprovider.N2oJavaDataProvider) TestEntity(net.n2oapp.framework.engine.util.TestEntity) ObjectReferenceField(net.n2oapp.framework.api.metadata.global.dao.object.field.ObjectReferenceField) Argument(net.n2oapp.framework.api.metadata.global.dao.invocation.model.Argument) DataSet(net.n2oapp.criteria.dataset.DataSet) ObjectSimpleField(net.n2oapp.framework.api.metadata.global.dao.object.field.ObjectSimpleField) AbstractParameter(net.n2oapp.framework.api.metadata.global.dao.object.AbstractParameter) ObjectListField(net.n2oapp.framework.api.metadata.global.dao.object.field.ObjectListField) Test(org.junit.Test)

Example 4 with ObjectReferenceField

use of net.n2oapp.framework.api.metadata.global.dao.object.field.ObjectReferenceField in project n2o-framework by i-novus-llc.

the class MappingProcessorTest method testEntityMapping.

private void testEntityMapping() {
    ObjectReferenceField param = new ObjectReferenceField();
    param.setId("entity");
    param.setEntityClass("net.n2oapp.framework.engine.util.TestEntity$InnerEntity");
    ObjectSimpleField childParam1 = new ObjectSimpleField();
    childParam1.setId("id");
    childParam1.setMapping("valueInt");
    ObjectSimpleField childParam2 = new ObjectSimpleField();
    childParam2.setId("name");
    childParam2.setMapping("valueStr");
    param.setFields(new AbstractParameter[] { childParam1, childParam2 });
    DataSet innerDataSet = new DataSet();
    innerDataSet.put("id", 666);
    innerDataSet.put("name", "testStr");
    DataSet outerDataSet = new DataSet();
    outerDataSet.put("entity", innerDataSet);
    MappingProcessor.mapParameter(param, outerDataSet);
    assert outerDataSet.get("entity") instanceof TestEntity.InnerEntity;
    assert ((TestEntity.InnerEntity) outerDataSet.get("entity")).getValueInt().equals(666);
    assert ((TestEntity.InnerEntity) outerDataSet.get("entity")).getValueStr().equals("testStr");
}
Also used : ObjectSimpleField(net.n2oapp.framework.api.metadata.global.dao.object.field.ObjectSimpleField) ObjectReferenceField(net.n2oapp.framework.api.metadata.global.dao.object.field.ObjectReferenceField) DataSet(net.n2oapp.criteria.dataset.DataSet)

Example 5 with ObjectReferenceField

use of net.n2oapp.framework.api.metadata.global.dao.object.field.ObjectReferenceField in project n2o-framework by i-novus-llc.

the class N2oObjectCompiler method initReferenceFieldByObjectId.

/**
 * Инициализация структуры составного поля по содержимому objectId, на который он ссылается
 *
 * @param refField     Исходная модель поля, ссылающегося на другой объект
 * @param p            Процессор сборки метаданных
 * @param currentDepth Текущая глубина вложенности
 */
private void initReferenceFieldByObjectId(ObjectReferenceField refField, CompileProcessor p, int currentDepth) {
    if (currentDepth > OBJECT_REFERENCE_NESTING_MAX_DEPTH)
        return;
    N2oObject refObject = p.getSource(refField.getReferenceObjectId(), N2oObject.class);
    refField.setEntityClass(p.cast(refField.getEntityClass(), refObject.getEntityClass()));
    if (isNotEmpty(refField.getFields())) {
        if (isNotEmpty(refObject.getObjectFields())) {
            for (int i = 0; i < refField.getFields().length; i++) {
                AbstractParameter parameter = refField.getFields()[i];
                Optional<AbstractParameter> objectRefField = Arrays.stream(refObject.getObjectFields()).filter(f -> f.getId().equals(parameter.getId())).findFirst();
                if (objectRefField.isPresent() && objectRefField.get().getClass().equals(parameter.getClass()))
                    refField.getFields()[i] = p.merge(objectRefField.get(), parameter);
            }
        }
    } else
        refField.setFields(refObject.getObjectFields());
    refField.setReferenceObjectId(null);
    // рекурсивная инициализация внутренних вложенных полей
    if (isNotEmpty(refField.getFields()))
        for (AbstractParameter field : refField.getFields()) if (field instanceof ObjectReferenceField && ((ObjectReferenceField) field).getReferenceObjectId() != null)
            initReferenceFieldByObjectId((ObjectReferenceField) field, p, currentDepth++);
}
Also used : N2oInvocation(net.n2oapp.framework.api.metadata.global.dao.invocation.model.N2oInvocation) java.util(java.util) N2oJavaDataProvider(net.n2oapp.framework.api.metadata.dataprovider.N2oJavaDataProvider) StrictMap(net.n2oapp.framework.api.metadata.local.util.StrictMap) N2oException(net.n2oapp.framework.api.exception.N2oException) BaseSourceCompiler(net.n2oapp.framework.config.metadata.compile.BaseSourceCompiler) Validation(net.n2oapp.framework.api.data.validation.Validation) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) N2oObject(net.n2oapp.framework.api.metadata.global.dao.object.N2oObject) N2oInvocationValidation(net.n2oapp.framework.api.metadata.global.dao.validation.N2oInvocationValidation) ObjectContext(net.n2oapp.framework.config.metadata.compile.context.ObjectContext) CompileProcessor(net.n2oapp.framework.api.metadata.compile.CompileProcessor) ObjectSimpleField(net.n2oapp.framework.api.metadata.global.dao.object.field.ObjectSimpleField) CompileUtil.castDefault(net.n2oapp.framework.api.metadata.local.util.CompileUtil.castDefault) Component(org.springframework.stereotype.Component) MandatoryValidation(net.n2oapp.framework.api.data.validation.MandatoryValidation) N2oValidation(net.n2oapp.framework.api.metadata.global.dao.validation.N2oValidation) DefaultActions(net.n2oapp.framework.config.metadata.compile.action.DefaultActions) ActionContext(net.n2oapp.framework.config.metadata.compile.context.ActionContext) ObjectReferenceField(net.n2oapp.framework.api.metadata.global.dao.object.field.ObjectReferenceField) Argument(net.n2oapp.framework.api.metadata.global.dao.invocation.model.Argument) AbstractParameter(net.n2oapp.framework.api.metadata.global.dao.object.AbstractParameter) CompiledObject(net.n2oapp.framework.api.metadata.local.CompiledObject) ObjectReferenceField(net.n2oapp.framework.api.metadata.global.dao.object.field.ObjectReferenceField) N2oObject(net.n2oapp.framework.api.metadata.global.dao.object.N2oObject) AbstractParameter(net.n2oapp.framework.api.metadata.global.dao.object.AbstractParameter)

Aggregations

ObjectReferenceField (net.n2oapp.framework.api.metadata.global.dao.object.field.ObjectReferenceField)10 ObjectSimpleField (net.n2oapp.framework.api.metadata.global.dao.object.field.ObjectSimpleField)9 AbstractParameter (net.n2oapp.framework.api.metadata.global.dao.object.AbstractParameter)8 DataSet (net.n2oapp.criteria.dataset.DataSet)5 Test (org.junit.Test)5 N2oJavaDataProvider (net.n2oapp.framework.api.metadata.dataprovider.N2oJavaDataProvider)4 Argument (net.n2oapp.framework.api.metadata.global.dao.invocation.model.Argument)4 ObjectListField (net.n2oapp.framework.api.metadata.global.dao.object.field.ObjectListField)4 TestEntity (net.n2oapp.framework.engine.util.TestEntity)4 ObjectSetField (net.n2oapp.framework.api.metadata.global.dao.object.field.ObjectSetField)3 CompiledObject (net.n2oapp.framework.api.metadata.local.CompiledObject)2 ObjectContext (net.n2oapp.framework.config.metadata.compile.context.ObjectContext)2 java.util (java.util)1 ArrayList (java.util.ArrayList)1 Function (java.util.function.Function)1 Collectors (java.util.stream.Collectors)1 MandatoryValidation (net.n2oapp.framework.api.data.validation.MandatoryValidation)1 Validation (net.n2oapp.framework.api.data.validation.Validation)1 N2oException (net.n2oapp.framework.api.exception.N2oException)1 CompileProcessor (net.n2oapp.framework.api.metadata.compile.CompileProcessor)1