Search in sources :

Example 21 with BaseGenericIdEntity

use of com.haulmont.cuba.core.entity.BaseGenericIdEntity in project cuba by cuba-platform.

the class XMLConverter2 method parseEntity.

/**
 * Converts a content of XML element to an entity.
 *
 * @param instanceEl    element that contains entity description
 * @param entity        if this parameter is not null then its fields will be filled,
 *                      if it is null then new entity will be created.
 * @param commitRequest must not be null if method is called when parsing a {@code CommitRequest}.
 *                      Security permissions checks are performed based on existing/absence of this
 *                      parameter.
 */
protected Entity parseEntity(Element instanceEl, @Nullable Entity entity, @Nullable CommitRequest commitRequest) {
    try {
        if (entity == null) {
            String id = instanceEl.attributeValue("id");
            EntityLoadInfo loadInfo = EntityLoadInfo.parse(id);
            if (loadInfo == null)
                throw new IllegalArgumentException("XML description of entity doesn't contain valid 'id' attribute");
            entity = createEmptyInstance(loadInfo);
            entity.setValue("id", loadInfo.getId());
        }
        MetaClass metaClass = entity.getMetaClass();
        List propertyEls = instanceEl.elements();
        for (Object el : propertyEls) {
            Element propertyEl = (Element) el;
            if (entity instanceof BaseGenericIdEntity && "__securityToken".equals(propertyEl.getName())) {
                byte[] securityToken = Base64.getDecoder().decode(propertyEl.getText());
                SecurityState securityState = BaseEntityInternalAccess.getOrCreateSecurityState((BaseGenericIdEntity) entity);
                BaseEntityInternalAccess.setSecurityToken(securityState, securityToken);
                continue;
            }
            String propertyName = propertyEl.attributeValue("name");
            MetaPropertyPath metaPropertyPath = metadata.getTools().resolveMetaPropertyPath(metaClass, propertyName);
            Preconditions.checkNotNullArgument(metaPropertyPath, "Could not resolve property '%s' in '%s'", propertyName, metaClass);
            MetaProperty property = metaPropertyPath.getMetaProperty();
            if (commitRequest != null && !attrModifyPermitted(metaClass, propertyName))
                continue;
            if (commitRequest != null && metadataTools.isNotPersistent(property) && !DynamicAttributesUtils.isDynamicAttribute(propertyName))
                continue;
            if (Boolean.parseBoolean(propertyEl.attributeValue("null"))) {
                entity.setValue(propertyName, null);
                continue;
            }
            if (entity instanceof BaseGenericIdEntity && DynamicAttributesUtils.isDynamicAttribute(propertyName) && ((BaseGenericIdEntity) entity).getDynamicAttributes() == null) {
                ConverterHelper.fetchDynamicAttributes(entity);
            }
            String stringValue = propertyEl.getText();
            Object value;
            switch(property.getType()) {
                case DATATYPE:
                    value = property.getRange().asDatatype().parse(stringValue);
                    entity.setValue(propertyName, value);
                    break;
                case ENUM:
                    value = property.getRange().asEnumeration().parse(stringValue);
                    entity.setValue(propertyName, value);
                    break;
                case COMPOSITION:
                case ASSOCIATION:
                    MetaClass propertyMetaClass = propertyMetaClass(property);
                    // checks if the user permitted to read and update a property
                    if (commitRequest != null && !updatePermitted(propertyMetaClass) && !readPermitted(propertyMetaClass))
                        break;
                    if (!property.getRange().getCardinality().isMany()) {
                        Element refInstanceEl = propertyEl.element("instance");
                        if (metadataTools.isEmbedded(property)) {
                            MetaClass embeddedMetaClass = property.getRange().asClass();
                            Entity embeddedEntity = metadata.create(embeddedMetaClass);
                            value = parseEntity(refInstanceEl, embeddedEntity, commitRequest);
                        } else {
                            String id = refInstanceEl.attributeValue("id");
                            // will be registered later
                            if (commitRequest != null && commitRequest.getCommitIds().contains(id)) {
                                EntityLoadInfo loadInfo = EntityLoadInfo.parse(id);
                                Entity ref = metadata.create(loadInfo.getMetaClass());
                                ref.setValue("id", loadInfo.getId());
                                entity.setValue(propertyName, ref);
                                break;
                            }
                            value = parseEntity(refInstanceEl, null, commitRequest);
                        }
                        entity.setValue(propertyName, value);
                    } else {
                        Class<?> propertyJavaType = property.getJavaType();
                        Collection<Object> coll;
                        if (List.class.isAssignableFrom(propertyJavaType))
                            coll = new ArrayList<>();
                        else if (Set.class.isAssignableFrom(propertyJavaType))
                            coll = new HashSet<>();
                        else
                            throw new RuntimeException("Datatype " + propertyJavaType.getName() + " of " + metaClass.getName() + "#" + property.getName() + " is not supported");
                        entity.setValue(propertyName, coll);
                        for (Object childInstenceEl : propertyEl.elements("instance")) {
                            Entity childEntity = parseEntity((Element) childInstenceEl, null, commitRequest);
                            coll.add(childEntity);
                        }
                    }
                    break;
                default:
                    throw new IllegalStateException("Unknown property type");
            }
        }
        return entity;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : BaseGenericIdEntity(com.haulmont.cuba.core.entity.BaseGenericIdEntity) Entity(com.haulmont.cuba.core.entity.Entity) BaseGenericIdEntity(com.haulmont.cuba.core.entity.BaseGenericIdEntity) Element(org.dom4j.Element) MetaPropertyPath(com.haulmont.chile.core.model.MetaPropertyPath) SecurityState(com.haulmont.cuba.core.entity.SecurityState) ParseException(java.text.ParseException) MetaClass(com.haulmont.chile.core.model.MetaClass) MetaProperty(com.haulmont.chile.core.model.MetaProperty)

Aggregations

BaseGenericIdEntity (com.haulmont.cuba.core.entity.BaseGenericIdEntity)21 Entity (com.haulmont.cuba.core.entity.Entity)13 MetaProperty (com.haulmont.chile.core.model.MetaProperty)12 MetaClass (com.haulmont.chile.core.model.MetaClass)11 DynamicAttributesGuiTools (com.haulmont.cuba.gui.dynamicattributes.DynamicAttributesGuiTools)3 MetaPropertyPath (com.haulmont.chile.core.model.MetaPropertyPath)2 Categorized (com.haulmont.cuba.core.entity.Categorized)2 CategoryAttribute (com.haulmont.cuba.core.entity.CategoryAttribute)2 SecurityState (com.haulmont.cuba.core.entity.SecurityState)2 Element (org.dom4j.Element)2 FetchGroup (org.eclipse.persistence.queries.FetchGroup)2 FetchGroupTracker (org.eclipse.persistence.queries.FetchGroupTracker)2 JSONArray (org.json.JSONArray)2 JSONObject (org.json.JSONObject)2 MetadataObject (com.haulmont.chile.core.model.MetadataObject)1 AbstractInstance (com.haulmont.chile.core.model.impl.AbstractInstance)1 DataService (com.haulmont.cuba.core.app.DataService)1 DynamicAttributes (com.haulmont.cuba.core.app.dynamicattributes.DynamicAttributes)1 BaseDbGeneratedIdEntity (com.haulmont.cuba.core.entity.BaseDbGeneratedIdEntity)1 CategoryAttributeValue (com.haulmont.cuba.core.entity.CategoryAttributeValue)1