Search in sources :

Example 6 with SecurityState

use of com.haulmont.cuba.core.entity.SecurityState 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

SecurityState (com.haulmont.cuba.core.entity.SecurityState)6 MetaProperty (com.haulmont.chile.core.model.MetaProperty)3 Entity (com.haulmont.cuba.core.entity.Entity)3 MetaClass (com.haulmont.chile.core.model.MetaClass)2 MetaPropertyPath (com.haulmont.chile.core.model.MetaPropertyPath)2 BaseGenericIdEntity (com.haulmont.cuba.core.entity.BaseGenericIdEntity)2 EntityManager (com.haulmont.cuba.core.EntityManager)1 Transaction (com.haulmont.cuba.core.Transaction)1 ParseException (java.text.ParseException)1 Element (org.dom4j.Element)1 JSONArray (org.json.JSONArray)1 JSONObject (org.json.JSONObject)1