@Override public Collection<? extends Entity> parseEntitiesCollection(String content, Class<? extends Collection> collectionClass) { try { @SuppressWarnings("unchecked") Collection<Entity> collection = newCollectionInstance(collectionClass); JSONArray jsonArray = new JSONArray(content); for (int i = 0; i < jsonArray.length(); i++) { JSONObject instanceObj = jsonArray.getJSONObject(i); Entity entity = _parseEntity(instanceObj); collection.add(entity); } return collection; } catch (Exception e) { throw new RuntimeException("Unable to parse entities collection", e); } }
@Override public String process(List<Entity> entities, MetaClass metaClass, View view) throws Exception { MyJSONObject.Array array = _process(entities, metaClass, view); return array.toString(); }
protected boolean attrViewPermitted(MetaClass metaClass, String property) { return attrPermitted(metaClass, property, EntityAttrAccess.VIEW); }
if (!attrModifyPermitted(metaClass, property.getName())) continue; setField(entity, propertyName, null); continue; setField(entity, propertyName, property.getRange().asDatatype().parse(value)); break; case ENUM: if (!json.isNull(propertyName)) { setField(entity, propertyName, property.getRange().asEnumeration().parse(json.getString(propertyName))); } else { setField(entity, propertyName, null); case ASSOCIATION: if ("null".equals(json.get(propertyName).toString())) { setField(entity, propertyName, null); break; MetaClass propertyMetaClass = propertyMetaClass(property); if (!updatePermitted(propertyMetaClass) && !readPermitted(propertyMetaClass)) break; Entity ref = metadata.create(loadInfo.getMetaClass()); ref.setValue("id", loadInfo.getId()); setField(entity, propertyName, ref); break;
MyJSONObject root = new MyJSONObject(idof(entity), false); if (!attrViewPermitted(metaClass, property.getName())) continue; if (!isPropertyIncluded(view, property, metadataTools) && !DynamicAttributesUtils.isDynamicAttribute(property.getName())) { continue; case COMPOSITION: case ASSOCIATION: { MetaClass meta = propertyMetaClass(property); if (!readPermitted(meta)) break; root.set(property.getName(), null); } else { root.set(property.getName(), encodeInstance((Entity) value, visited, property.getRange().asClass(), propertyView)); array.add(null); } else { array.add(encodeInstance((Entity) o, visited, property.getRange().asClass(), propertyView));
@Override public String processServiceMethodResult(Object result, Class resultType) throws Exception { MyJSONObject root = new MyJSONObject(); if (result instanceof Entity) { Entity entity = (Entity) result; MyJSONObject entityObject = _process(entity); root.set("result", entityObject); } else if (result instanceof Collection) { if (!checkCollectionItemTypes((Collection) result, Entity.class)) throw new IllegalArgumentException("Items that are not instances of Entity found in service method result"); //noinspection unchecked ArrayList list = new ArrayList((Collection) result); MetaClass metaClass; if (!list.isEmpty()) metaClass = ((Entity) list.get(0)).getMetaClass(); else metaClass = AppBeans.get(Metadata.class).getClasses().iterator().next(); MyJSONObject.Array processed = _process(list, metaClass, null); root.set("result", processed); } else { if (result != null && resultType != Void.TYPE) { Datatype datatype = getDatatype(resultType); root.set("result", datatype != null ? datatype.format(result) : result.toString()); } else { root.set("result", null); } } return root.toString(); }
Entity instance = createEmptyInstance(loadInfo); setField(instance, "id", loadInfo.getId()); setField(instance, key, property.getRange().asDatatype().parse(value)); break; case ENUM: if (!json.isNull(key)) { setField(instance, key, property.getRange().asEnumeration().parse(json.getString(key))); } else { setField(instance, key, null); case ASSOCIATION: if ("null".equals(json.get(key).toString())) { setField(instance, key, null); break; Object childInstance = _parseEntity(jsonChild); setField(instance, key, childInstance); } else { JSONArray jsonArray = json.getJSONArray(key); throw new RuntimeException("Datatype " + propertyJavaType.getName() + " of " + metaClass.getName() + "#" + property.getName() + " is not supported"); setField(instance, key, coll); for (int i = 0; i < jsonArray.length(); i++) { Object arrayValue = jsonArray.get(i); Object child = _parseEntity(jsonChild); coll.add(child);
@Override public String process(Set<Entity> entities) throws Exception { MyJSONObject.Array result = new MyJSONObject.Array(); for (Entity entity : entities) { MyJSONObject entityJson = encodeInstance(entity, new HashSet<Entity>(), getMetaClass(entity), null); if (restConfig.getRestApiVersion() == 1) { //return map for old version of rest api MyJSONObject valueJson = encodeInstance(entity, new HashSet<Entity>(), getMetaClass(entity), null); MyJSONObject.Array mapping = new MyJSONObject.Array(); mapping.add(entityJson); mapping.add(valueJson); result.add(mapping); } else { result.add(entityJson); } } return result.toString(); }
@Override public String process(Entity entity, MetaClass metaclass, View view) throws Exception { MyJSONObject jsonObject = encodeInstance(entity, new HashSet<Entity>(), metaclass, view); return jsonObject.toString(); }
protected boolean readPermitted(MetaClass metaClass) { return entityOpPermitted(metaClass, EntityOp.READ); }
@Override public Entity parseEntity(String content) { try { JSONObject json = new JSONObject(content); return _parseEntity(json); } catch (Exception e) { throw new RuntimeException("Unable to parse entity", e); } }
protected List<Entity> parseIntoList(CommitRequest commitRequest, JSONArray nodeList) throws JSONException, InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException, IntrospectionException, ParseException { List<Entity> result = new ArrayList<>(nodeList.length()); for (int j = 0; j < nodeList.length(); j++) { JSONObject jsonObject = nodeList.getJSONObject(j); InstanceRef ref = commitRequest.parseInstanceRefAndRegister(jsonObject.getString("id")); MetaClass metaClass = ref.getMetaClass(); Entity instance = ref.getInstance(); asJavaTree(commitRequest, instance, metaClass, jsonObject); result.add(instance); } return result; }
protected MyJSONObject _process(Entity entity) throws Exception { return encodeInstance(entity, new HashSet<Entity>(), entity.getMetaClass(), null); }
protected boolean updatePermitted(MetaClass metaClass) { return entityOpPermitted(metaClass, EntityOp.UPDATE); }
protected MyJSONObject.Array _process(List<Entity> entities, MetaClass metaClass, View view) throws Exception { MyJSONObject.Array result = new MyJSONObject.Array(); for (Entity entity : entities) { MyJSON item = encodeInstance(entity, new HashSet<Entity>(), metaClass, view); result.add(item); } return result; }
protected boolean attrModifyPermitted(MetaClass metaClass, String property) { return attrPermitted(metaClass, property, EntityAttrAccess.MODIFY); }