Search in sources :

Example 1 with ICustomType

use of org.sablo.specification.property.ICustomType in project servoy-client by Servoy.

the class WebComponentSpecTest method testOwnTypeProperyTypeRefernceInOtherOwnType.

@Test
public void testOwnTypeProperyTypeRefernceInOtherOwnType() throws JSONException {
    String property = "{name:'test',definition:'/test.js', model: {myproperty:'mytype'}, types: {mytype:{model:{typeproperty:'mytype2'}},mytype2:{model:{typeproperty:'string'}}}}";
    WebObjectSpecification spec = WebObjectSpecification.parseSpec(property, "sample", null, null);
    Assert.assertEquals(1, spec.getProperties().size());
    PropertyDescription pd = spec.getProperties().get("myproperty");
    Assert.assertNotNull(pd);
    Assert.assertNotNull(((ICustomType) pd.getType()).getCustomJSONTypeDefinition());
    Object config = pd.getConfig();
    Assert.assertNull(config);
    Assert.assertFalse(pd.getType() instanceof CustomJSONArrayType<?, ?>);
    PropertyDescription wct = ((ICustomType) pd.getType()).getCustomJSONTypeDefinition();
    Assert.assertEquals("test.mytype", wct.getName());
    Assert.assertEquals(1, wct.getProperties().size());
    PropertyDescription pd2 = wct.getProperties().get("typeproperty");
    Assert.assertNotNull(pd2);
    Assert.assertNotNull(((ICustomType) pd2.getType()).getCustomJSONTypeDefinition());
    Assert.assertFalse(pd2.getType() instanceof CustomJSONArrayType<?, ?>);
    config = pd2.getConfig();
    Assert.assertNull(config);
    PropertyDescription wct2 = ((ICustomType) pd2.getType()).getCustomJSONTypeDefinition();
    Assert.assertEquals("test.mytype2", wct2.getName());
    Assert.assertEquals(1, wct2.getProperties().size());
    PropertyDescription pd3 = wct2.getProperty("typeproperty");
    Assert.assertNotNull(pd3);
    Assert.assertTrue(pd3.getType() == ServoyStringPropertyType.INSTANCE);
    Assert.assertFalse(pd3.getType() instanceof CustomJSONArrayType<?, ?>);
}
Also used : PropertyDescription(org.sablo.specification.PropertyDescription) WebObjectSpecification(org.sablo.specification.WebObjectSpecification) ICustomType(org.sablo.specification.property.ICustomType) Test(org.junit.Test)

Example 2 with ICustomType

use of org.sablo.specification.property.ICustomType in project servoy-client by Servoy.

the class WebObjectImpl method updatePersistMappedPropertyFromJSON.

protected void updatePersistMappedPropertyFromJSON(String beanJSONKey, Object object) {
    List<IChildWebObject> newChildPersistPropertiesToFire = null;
    try {
        synchronized (this) {
            if (object != null && getChildPropertyDescription(beanJSONKey) != null) {
                PropertyDescription childPd = getChildPropertyDescription(beanJSONKey);
                IPropertyType<?> propertyType = childPd.getType();
                String simpleTypeName = PropertyUtils.getSimpleNameOfCustomJSONTypeProperty(propertyType);
                if (isPersistMappedProperty(beanJSONKey)) {
                    if (ServoyJSONObject.isJavascriptUndefined(object)) {
                        persistMappedProperties.remove(beanJSONKey);
                        persistMappedPropetiesByUUID = null;
                    } else if (ServoyJSONObject.isJavascriptNull(object)) {
                        persistMappedProperties.put(beanJSONKey, null);
                        persistMappedPropetiesByUUID = null;
                    } else {
                        boolean arrayReturnType = PropertyUtils.isCustomJSONArrayPropertyType(propertyType);
                        if (!arrayReturnType) {
                            IChildWebObject childWebObject;
                            IChildWebObject existingWebObject = null;
                            // find the UUID that is stored for that key already in the .frm file - if any
                            UUID childChildWebObjectUUID = null;
                            JSONObject childWebObjectJSON = WebObjectImpl.getFullJSONInFrmFile(webObject, beanJSONKey, -1, false);
                            if (childWebObjectJSON != null && childWebObjectJSON.has(IChildWebObject.UUID_KEY)) {
                                try {
                                    childChildWebObjectUUID = UUID.fromString(childWebObjectJSON.getString(IChildWebObject.UUID_KEY));
                                } catch (IllegalArgumentException ex) {
                                    Debug.error(ex);
                                }
                                if (childChildWebObjectUUID != null) {
                                    IChildWebObject currentPersist = null;
                                    if (persistMappedProperties.containsKey(beanJSONKey) && persistMappedProperties.get(beanJSONKey) instanceof IChildWebObject) {
                                        currentPersist = (IChildWebObject) persistMappedProperties.get(beanJSONKey);
                                        if (currentPersist != null && childChildWebObjectUUID.equals(currentPersist.getUUID())) {
                                            existingWebObject = currentPersist;
                                        }
                                    }
                                }
                            }
                            if (existingWebObject == null) {
                                // "object" is not null/undefined here - there are checks for that above; so create the needed persists
                                if (isComponent(propertyType)) {
                                    childWebObject = ChildWebComponent.createNewInstance(webObject, childPd, beanJSONKey, -1, false, childChildWebObjectUUID);
                                    persistMappedProperties.put(beanJSONKey, childWebObject);
                                    if (newChildPersistPropertiesToFire == null)
                                        newChildPersistPropertiesToFire = new ArrayList<>();
                                    newChildPersistPropertiesToFire.add(childWebObject);
                                    persistMappedPropetiesByUUID = null;
                                } else if (PropertyUtils.isCustomJSONObjectProperty(propertyType)) {
                                    childWebObject = WebCustomType.createNewInstance(webObject, childPd, beanJSONKey, -1, false, childChildWebObjectUUID);
                                    childWebObject.setTypeName(simpleTypeName);
                                    persistMappedProperties.put(beanJSONKey, childWebObject);
                                    if (newChildPersistPropertiesToFire == null)
                                        newChildPersistPropertiesToFire = new ArrayList<>();
                                    newChildPersistPropertiesToFire.add(childWebObject);
                                    persistMappedPropetiesByUUID = null;
                                }
                            }
                        } else if (object instanceof JSONArray) {
                            PropertyDescription elementPD = (propertyType instanceof ICustomType<?>) ? ((ICustomType<?>) propertyType).getCustomJSONTypeDefinition() : null;
                            if (elementPD != null) {
                                ArrayList<IChildWebObject> persistMappedPropertyArray = new ArrayList<IChildWebObject>();
                                if (PropertyUtils.isCustomJSONObjectProperty(elementPD.getType()) || isComponent(propertyType)) {
                                    ArrayList<IChildWebObject> currentPersistMappedPropertyArray = new ArrayList<IChildWebObject>();
                                    if (persistMappedProperties.containsKey(beanJSONKey) && persistMappedProperties.get(beanJSONKey) instanceof IChildWebObject[]) {
                                        currentPersistMappedPropertyArray.addAll(Arrays.asList((IChildWebObject[]) persistMappedProperties.get(beanJSONKey)));
                                    }
                                    for (int i = 0; i < ((JSONArray) object).length(); i++) {
                                        IChildWebObject existingWebObject = null;
                                        UUID childChildWebObjectUUID = null;
                                        JSONObject childWebObjectJSON = WebObjectImpl.getFullJSONInFrmFile(webObject, beanJSONKey, i, false);
                                        if (childWebObjectJSON != null && childWebObjectJSON.has(IChildWebObject.UUID_KEY)) {
                                            try {
                                                childChildWebObjectUUID = UUID.fromString(childWebObjectJSON.getString(IChildWebObject.UUID_KEY));
                                            } catch (IllegalArgumentException ex) {
                                                Debug.error(ex);
                                            }
                                            if (childChildWebObjectUUID != null) {
                                                for (IChildWebObject wo : currentPersistMappedPropertyArray) {
                                                    if (wo != null && childChildWebObjectUUID.equals(wo.getUUID())) {
                                                        persistMappedPropertyArray.add(wo);
                                                        existingWebObject = wo;
                                                        // just to be sure we didn't find it at some other index
                                                        existingWebObject.setIndex(i);
                                                        break;
                                                    }
                                                }
                                            }
                                        }
                                        if (existingWebObject == null) {
                                            // we couldn't find an old array item with the same UUID; add a fresh value
                                            Object newJSNOValAtIdx = ((JSONArray) object).opt(i);
                                            if (ServoyJSONObject.isJavascriptNullOrUndefined(newJSNOValAtIdx)) {
                                                persistMappedPropertyArray.add(null);
                                            } else {
                                                if (PropertyUtils.isCustomJSONObjectProperty(elementPD.getType())) {
                                                    WebCustomType webCustomType = WebCustomType.createNewInstance(webObject, elementPD, beanJSONKey, i, false, childChildWebObjectUUID);
                                                    webCustomType.setTypeName(simpleTypeName);
                                                    persistMappedPropertyArray.add(webCustomType);
                                                    if (newChildPersistPropertiesToFire == null)
                                                        newChildPersistPropertiesToFire = new ArrayList<>();
                                                    newChildPersistPropertiesToFire.add(webCustomType);
                                                } else if (isComponent(propertyType)) {
                                                    ChildWebComponent childComponent = ChildWebComponent.createNewInstance(webObject, elementPD, beanJSONKey, i, false, childChildWebObjectUUID);
                                                    persistMappedPropertyArray.add(childComponent);
                                                    if (newChildPersistPropertiesToFire == null)
                                                        newChildPersistPropertiesToFire = new ArrayList<>();
                                                    newChildPersistPropertiesToFire.add(childComponent);
                                                }
                                            }
                                        }
                                    }
                                }
                                persistMappedProperties.put(beanJSONKey, persistMappedPropertyArray.toArray(new IChildWebObject[persistMappedPropertyArray.size()]));
                                persistMappedPropetiesByUUID = null;
                            }
                        } else {
                            Debug.error("Typed property value ('" + beanJSONKey + "') is not JSONArray although in spec it is defined as array... " + this + " - " + object);
                        }
                    }
                }
            } else {
                if (persistMappedProperties.remove(beanJSONKey) != null)
                    persistMappedPropetiesByUUID = null;
            }
        }
    } finally {
        // fire these later outside the synchronized block to avoid potential deadlocks - who knows what code will execute in listeners
        if (newChildPersistPropertiesToFire != null)
            newChildPersistPropertiesToFire.forEach((IChildWebObject p) -> {
                fireChildCreated(p);
            });
    }
}
Also used : ArrayList(java.util.ArrayList) ServoyJSONArray(com.servoy.j2db.util.ServoyJSONArray) JSONArray(org.json.JSONArray) PropertyDescription(org.sablo.specification.PropertyDescription) JSONObject(org.json.JSONObject) ServoyJSONObject(com.servoy.j2db.util.ServoyJSONObject) ICustomType(org.sablo.specification.property.ICustomType) JSONObject(org.json.JSONObject) ServoyJSONObject(com.servoy.j2db.util.ServoyJSONObject) UUID(com.servoy.j2db.util.UUID)

Example 3 with ICustomType

use of org.sablo.specification.property.ICustomType in project servoy-client by Servoy.

the class WebComponentSpecTest method testOwnTypeProperyType.

@Test
public void testOwnTypeProperyType() throws JSONException {
    String property = "{name:'test',definition:'/test.js', model: {myproperty:'mytype'}, types: {mytype:{model:{typeproperty:'string'}}}}";
    WebObjectSpecification spec = WebObjectSpecification.parseSpec(property, "sample", null, null);
    Assert.assertEquals(1, spec.getProperties().size());
    PropertyDescription pd = spec.getProperties().get("myproperty");
    Assert.assertNotNull(pd);
    Assert.assertNotNull(((ICustomType) pd.getType()).getCustomJSONTypeDefinition());
    Object config = pd.getConfig();
    Assert.assertFalse(pd.getType() instanceof CustomJSONArrayType<?, ?>);
    PropertyDescription wct = ((ICustomType) pd.getType()).getCustomJSONTypeDefinition();
    Assert.assertEquals("test.mytype", wct.getName());
    Assert.assertEquals(1, wct.getProperties().size());
    PropertyDescription pd2 = wct.getProperty("typeproperty");
    Assert.assertNotNull(pd2);
    Assert.assertTrue(pd2.getType() == ServoyStringPropertyType.INSTANCE);
    Assert.assertFalse(pd2.getType() instanceof CustomJSONArrayType<?, ?>);
}
Also used : PropertyDescription(org.sablo.specification.PropertyDescription) WebObjectSpecification(org.sablo.specification.WebObjectSpecification) ICustomType(org.sablo.specification.property.ICustomType) Test(org.junit.Test)

Aggregations

PropertyDescription (org.sablo.specification.PropertyDescription)3 ICustomType (org.sablo.specification.property.ICustomType)3 Test (org.junit.Test)2 WebObjectSpecification (org.sablo.specification.WebObjectSpecification)2 ServoyJSONArray (com.servoy.j2db.util.ServoyJSONArray)1 ServoyJSONObject (com.servoy.j2db.util.ServoyJSONObject)1 UUID (com.servoy.j2db.util.UUID)1 ArrayList (java.util.ArrayList)1 JSONArray (org.json.JSONArray)1 JSONObject (org.json.JSONObject)1