use of com.haulmont.chile.core.model.MetaProperty in project cuba by cuba-platform.
the class CommitRequest method generateId.
private String generateId(String entityName) {
Metadata metadata = AppBeans.get(Metadata.NAME);
MetaClass metaClass = metadata.getSession().getClass(entityName);
MetaProperty primaryKeyProp = metadata.getTools().getPrimaryKeyProperty(metaClass);
if (primaryKeyProp == null)
throw new UnsupportedOperationException("Cannot generate ID for " + entityName);
if (primaryKeyProp.getJavaType().equals(UUID.class)) {
UuidSource uuidSource = AppBeans.get(UuidSource.NAME);
UUID uuid = uuidSource.createUuid();
return uuid.toString();
} else if (primaryKeyProp.getJavaType().equals(Long.class)) {
NumberIdSource numberIdSource = AppBeans.get(NumberIdSource.NAME);
Long longId = numberIdSource.createLongId(entityName);
return longId.toString();
} else if (primaryKeyProp.getJavaType().equals(Integer.class)) {
NumberIdSource numberIdSource = AppBeans.get(NumberIdSource.NAME);
Integer intId = numberIdSource.createIntegerId(entityName);
return intId.toString();
} else {
throw new UnsupportedOperationException("Cannot generate ID for " + entityName);
}
}
use of com.haulmont.chile.core.model.MetaProperty in project cuba by cuba-platform.
the class ConverterHelper method getActualMetaProperties.
public static List<MetaProperty> getActualMetaProperties(MetaClass metaClass, Entity entity) {
List<MetaProperty> result = new ArrayList<MetaProperty>(metaClass.getProperties());
if (entity instanceof BaseGenericIdEntity && ((BaseGenericIdEntity) entity).getDynamicAttributes() != null) {
Collection<CategoryAttribute> dynamicAttributes = AppBeans.get(DynamicAttributes.NAME, DynamicAttributes.class).getAttributesForMetaClass(metaClass);
for (CategoryAttribute dynamicAttribute : dynamicAttributes) {
result.add(DynamicAttributesUtils.getMetaPropertyPath(metaClass, dynamicAttribute).getMetaProperty());
}
}
Collections.sort(result, PROPERTY_COMPARATOR);
return result;
}
use of com.haulmont.chile.core.model.MetaProperty in project cuba by cuba-platform.
the class JSONConverter method asJavaTree.
protected void asJavaTree(CommitRequest commitRequest, Entity entity, MetaClass metaClass, JSONObject json) throws JSONException, InvocationTargetException, IllegalAccessException, NoSuchMethodException, InstantiationException, IntrospectionException, ParseException {
Iterator iter = json.keys();
while (iter.hasNext()) {
String propertyName = (String) iter.next();
if ("id".equals(propertyName)) {
// id was parsed already
continue;
}
// version is readonly property
if ("version".equals(propertyName)) {
continue;
}
if (entity instanceof BaseGenericIdEntity && "__securityToken".equals(propertyName)) {
byte[] securityToken = Base64.getDecoder().decode(json.getString("__securityToken"));
SecurityState securityState = BaseEntityInternalAccess.getOrCreateSecurityState((BaseGenericIdEntity) entity);
BaseEntityInternalAccess.setSecurityToken(securityState, securityToken);
continue;
}
MetaPropertyPath metaPropertyPath = metadata.getTools().resolveMetaPropertyPath(metaClass, propertyName);
checkNotNullArgument(metaPropertyPath, "Could not resolve property '%s' in '%s'", propertyName, metaClass);
MetaProperty property = metaPropertyPath.getMetaProperty();
if (!attrModifyPermitted(metaClass, property.getName()))
continue;
if (entity instanceof BaseGenericIdEntity && DynamicAttributesUtils.isDynamicAttribute(propertyName) && ((BaseGenericIdEntity) entity).getDynamicAttributes() == null) {
ConverterHelper.fetchDynamicAttributes(entity);
}
if (json.get(propertyName) == null) {
setField(entity, propertyName, null);
continue;
}
switch(property.getType()) {
case DATATYPE:
String value = null;
if (!json.isNull(propertyName)) {
value = json.get(propertyName).toString();
}
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);
}
break;
case COMPOSITION:
case ASSOCIATION:
if ("null".equals(json.get(propertyName).toString())) {
setField(entity, propertyName, null);
break;
}
MetaClass propertyMetaClass = propertyMetaClass(property);
// checks if the user permitted to read and update a property
if (!updatePermitted(propertyMetaClass) && !readPermitted(propertyMetaClass))
break;
if (!property.getRange().getCardinality().isMany()) {
JSONObject jsonChild = json.getJSONObject(propertyName);
Entity child;
MetaClass childMetaClass;
if (jsonChild.has("id")) {
String id = jsonChild.getString("id");
// will be registered later
if (commitRequest.getCommitIds().contains(id)) {
EntityLoadInfo loadInfo = EntityLoadInfo.parse(id);
if (loadInfo == null)
throw new IllegalArgumentException("Unable to parse ID: " + id);
Entity ref = metadata.create(loadInfo.getMetaClass());
ref.setValue("id", loadInfo.getId());
setField(entity, propertyName, ref);
break;
}
InstanceRef ref = commitRequest.parseInstanceRefAndRegister(id);
childMetaClass = ref.getMetaClass();
child = ref.getInstance();
} else {
childMetaClass = property.getRange().asClass();
child = metadata.create(childMetaClass);
}
asJavaTree(commitRequest, child, childMetaClass, jsonChild);
setField(entity, propertyName, child);
} else {
JSONArray jsonArray = json.getJSONArray(propertyName);
Collection<Object> coll = property.getRange().isOrdered() ? new ArrayList<>() : new HashSet<>();
setField(entity, propertyName, coll);
for (int i = 0; i < jsonArray.length(); i++) {
Object arrayValue = jsonArray.get(i);
if (arrayValue == null)
coll.add(null);
else {
// assuming no simple type here
JSONObject jsonChild = (JSONObject) arrayValue;
InstanceRef ref = commitRequest.parseInstanceRefAndRegister(jsonChild.getString("id"));
Entity child = ref.getInstance();
coll.add(child);
asJavaTree(commitRequest, child, ref.getMetaClass(), jsonChild);
}
}
}
break;
default:
throw new IllegalStateException("Unknown property type");
}
}
}
use of com.haulmont.chile.core.model.MetaProperty in project cuba by cuba-platform.
the class JSONConverter method createEmptyInstance.
/**
* Creates new entity instance from {@link com.haulmont.cuba.core.global.EntityLoadInfo}
* and reset fields values
*/
protected Entity createEmptyInstance(EntityLoadInfo loadInfo) throws IllegalAccessException, InstantiationException {
MetaClass metaClass = loadInfo.getMetaClass();
Entity instance = metadata.create(metaClass);
for (MetaProperty metaProperty : metaClass.getProperties()) {
if (!metaProperty.isReadOnly())
instance.setValue(metaProperty.getName(), null);
}
return instance;
}
use of com.haulmont.chile.core.model.MetaProperty in project cuba by cuba-platform.
the class JSONConverter method encodeInstance.
/**
* Encodes the closure of a persistent instance into JSON.
*
* @param entity the managed instance to be encoded. Can be null.
* @param visited the persistent instances that had been encoded already. Must not be null or immutable.
* @param view view on which loaded the entity
* @return the new element. The element has been appended as a child to the given parent in this method.
*/
protected MyJSONObject encodeInstance(final Entity entity, final Set<Entity> visited, MetaClass metaClass, View view) throws InvocationTargetException, NoSuchMethodException, IllegalAccessException {
if (visited == null) {
throw new IllegalArgumentException("null closure for encoder");
}
if (entity == null) {
return null;
}
boolean ref = !visited.add(entity);
MyJSONObject root = new MyJSONObject(idof(entity), false);
if (ref) {
return root;
}
if (entity instanceof BaseGenericIdEntity) {
byte[] securityToken = BaseEntityInternalAccess.getSecurityToken((BaseGenericIdEntity) entity);
if (securityToken != null) {
BaseGenericIdEntity baseGenericIdEntity = (BaseGenericIdEntity) entity;
root.set("__securityToken", Base64.getEncoder().encodeToString(securityToken));
String[] filteredAttributes = BaseEntityInternalAccess.getFilteredAttributes(baseGenericIdEntity);
if (filteredAttributes != null) {
MyJSONObject.Array array = new MyJSONObject.Array();
Arrays.stream(filteredAttributes).forEach(obj -> array.add("\"" + obj + "\""));
root.set("__filteredAttributes", array);
}
}
}
MetadataTools metadataTools = AppBeans.get(MetadataTools.NAME);
List<MetaProperty> properties = ConverterHelper.getActualMetaProperties(metaClass, entity);
for (MetaProperty property : properties) {
if (metadataTools.isPersistent(property) && !PersistenceHelper.isLoaded(entity, property.getName())) {
continue;
}
if (!attrViewPermitted(metaClass, property.getName()))
continue;
if (property.equals(metadataTools.getPrimaryKeyProperty(metaClass)) && !property.getJavaType().equals(String.class)) {
// skipping id for non-String-key entities
continue;
}
if (!isPropertyIncluded(view, property, metadataTools) && !DynamicAttributesUtils.isDynamicAttribute(property.getName())) {
continue;
}
Object value = entity.getValue(property.getName());
switch(property.getType()) {
case DATATYPE:
if (value != null) {
root.set(property.getName(), property.getRange().asDatatype().format(value));
} else if (!DynamicAttributesUtils.isDynamicAttribute(property.getName())) {
root.set(property.getName(), null);
}
break;
case ENUM:
if (value != null) {
// noinspection unchecked
root.set(property.getName(), property.getRange().asEnumeration().format(value));
} else {
root.set(property.getName(), null);
}
break;
case COMPOSITION:
case ASSOCIATION:
{
MetaClass meta = propertyMetaClass(property);
// checks if the user permitted to read a property's entity
if (!readPermitted(meta))
break;
View propertyView = (view == null || view.getProperty(property.getName()) == null ? null : view.getProperty(property.getName()).getView());
if (!property.getRange().getCardinality().isMany()) {
if (value == null) {
root.set(property.getName(), null);
} else {
root.set(property.getName(), encodeInstance((Entity) value, visited, property.getRange().asClass(), propertyView));
}
} else {
if (value == null) {
root.set(property.getName(), null);
break;
}
MyJSONObject.Array array = new MyJSONObject.Array();
root.set(property.getName(), array);
Collection<?> members = (Collection<?>) value;
for (Object o : members) {
if (o == null) {
array.add(null);
} else {
array.add(encodeInstance((Entity) o, visited, property.getRange().asClass(), propertyView));
}
}
}
break;
}
default:
throw new IllegalStateException("Unknown property type");
}
}
return root;
}
Aggregations