Search in sources :

Example 11 with MetaClass

use of com.haulmont.chile.core.model.MetaClass in project cuba by cuba-platform.

the class XMLConverter method encodeEntityInstance.

/**
 * Encodes the closure of a persistent instance into a XML element.
 *
 * @param visited
 * @param entity    the managed instance to be encoded. Can be null.
 * @param parent    the parent XML element to which the new XML element be added. Must not be null. Must be
 *                  owned by a document.
 * @param isRef
 * @param metaClass @return the new element. The element has been appended as a child to the given parent in this method.
 * @param view view on which loaded the entity
 */
private Element encodeEntityInstance(HashSet<Entity> visited, final Entity entity, final Element parent, boolean isRef, MetaClass metaClass, View view) throws InvocationTargetException, NoSuchMethodException, IllegalAccessException {
    if (!readPermitted(metaClass))
        return null;
    if (parent == null)
        throw new NullPointerException("No parent specified");
    Document doc = parent.getOwnerDocument();
    if (doc == null)
        throw new NullPointerException("No document specified");
    if (entity == null) {
        return encodeRef(parent, entity);
    }
    isRef |= !visited.add(entity);
    if (isRef) {
        return encodeRef(parent, entity);
    }
    Element root = doc.createElement(ELEMENT_INSTANCE);
    parent.appendChild(root);
    root.setAttribute(ATTR_ID, ior(entity));
    MetadataTools metadataTools = AppBeans.get(MetadataTools.NAME);
    List<MetaProperty> properties = ConverterHelper.getOrderedProperties(metaClass);
    for (MetaProperty property : properties) {
        Element child;
        if (!attrViewPermitted(metaClass, property.getName()))
            continue;
        if (!isPropertyIncluded(view, property, metadataTools)) {
            continue;
        }
        Object value = entity.getValue(property.getName());
        switch(property.getType()) {
            case DATATYPE:
                String nodeType;
                if (property.equals(metadataTools.getPrimaryKeyProperty(metaClass)) && !property.getJavaType().equals(String.class)) {
                    // skipping id for non-String-key entities
                    continue;
                } else if (property.getAnnotatedElement().isAnnotationPresent(Version.class)) {
                    nodeType = "version";
                } else {
                    nodeType = "basic";
                }
                child = doc.createElement(nodeType);
                child.setAttribute(ATTR_NAME, property.getName());
                if (value == null) {
                    encodeNull(child);
                } else {
                    String str = property.getRange().asDatatype().format(value);
                    encodeBasic(child, str, property.getJavaType());
                }
                break;
            case ENUM:
                child = doc.createElement("enum");
                child.setAttribute(ATTR_NAME, property.getName());
                if (value == null) {
                    encodeNull(child);
                } else {
                    // noinspection unchecked
                    String str = property.getRange().asEnumeration().format(value);
                    encodeBasic(child, str, property.getJavaType());
                }
                break;
            case COMPOSITION:
            case ASSOCIATION:
                {
                    MetaClass meta = propertyMetaClass(property);
                    // checks if the user permitted to read a property
                    if (!readPermitted(meta)) {
                        child = null;
                        break;
                    }
                    View propertyView = (view == null ? null : view.getProperty(property.getName()).getView());
                    if (!property.getRange().getCardinality().isMany()) {
                        boolean isEmbedded = property.getAnnotatedElement().isAnnotationPresent(Embedded.class);
                        child = doc.createElement(isEmbedded ? "embedded" : property.getRange().getCardinality().name().replace(UNDERSCORE, DASH).toLowerCase());
                        child.setAttribute(ATTR_NAME, property.getName());
                        if (isEmbedded) {
                            encodeEntityInstance(visited, (Entity) value, child, false, property.getRange().asClass(), propertyView);
                        } else {
                            encodeEntityInstance(visited, (Entity) value, child, false, property.getRange().asClass(), propertyView);
                        }
                    } else {
                        child = doc.createElement(getCollectionReferenceTag(property));
                        child.setAttribute(ATTR_NAME, property.getName());
                        child.setAttribute(ATTR_MEMBER_TYPE, typeOfEntityProperty(property));
                        if (value == null) {
                            encodeNull(child);
                            break;
                        }
                        Collection<?> members = (Collection<?>) value;
                        for (Object o : members) {
                            Element member = doc.createElement(ELEMENT_MEMBER);
                            child.appendChild(member);
                            if (o == null) {
                                encodeNull(member);
                            } else {
                                encodeEntityInstance(visited, (Entity) o, member, true, property.getRange().asClass(), propertyView);
                            }
                        }
                    }
                    break;
                }
            default:
                throw new IllegalStateException("Unknown property type");
        }
        if (child != null) {
            root.appendChild(child);
        }
    }
    return root;
}
Also used : Entity(com.haulmont.cuba.core.entity.Entity) MetaClass(com.haulmont.chile.core.model.MetaClass) Version(javax.persistence.Version) Embedded(javax.persistence.Embedded) MetaProperty(com.haulmont.chile.core.model.MetaProperty)

Example 12 with MetaClass

use of com.haulmont.chile.core.model.MetaClass in project cuba by cuba-platform.

the class XMLConverter2 method encodeEntity.

protected void encodeEntity(Entity entity, HashSet<Entity> visited, View view, Element parentEl) throws Exception {
    if (entity == null) {
        parentEl.addAttribute("null", "true");
        return;
    }
    if (!readPermitted(entity.getMetaClass()))
        throw new IllegalAccessException();
    Element instanceEl = parentEl.addElement("instance");
    instanceEl.addAttribute("id", EntityLoadInfo.create(entity).toString());
    boolean entityAlreadyVisited = !visited.add(entity);
    if (entityAlreadyVisited) {
        return;
    }
    if (entity instanceof BaseGenericIdEntity) {
        byte[] securityToken = BaseEntityInternalAccess.getSecurityToken((BaseGenericIdEntity) entity);
        if (securityToken != null) {
            BaseGenericIdEntity baseGenericIdEntity = (BaseGenericIdEntity) entity;
            instanceEl.addElement("__securityToken").setText(Base64.getEncoder().encodeToString(securityToken));
            String[] filteredAttributes = BaseEntityInternalAccess.getFilteredAttributes(baseGenericIdEntity);
            if (filteredAttributes != null) {
                Element filteredAttributesElement = instanceEl.addElement("__filteredAttributes");
                Arrays.stream(filteredAttributes).forEach(obj -> filteredAttributesElement.addElement("a").setText(obj));
            }
        }
    }
    MetaClass metaClass = entity.getMetaClass();
    List<MetaProperty> orderedProperties = ConverterHelper.getActualMetaProperties(metaClass, entity);
    for (MetaProperty property : orderedProperties) {
        if (metadataTools.isPersistent(property) && !PersistenceHelper.isLoaded(entity, property.getName())) {
            continue;
        }
        if (!attrViewPermitted(metaClass, property.getName()))
            continue;
        if (!isPropertyIncluded(view, property) && !DynamicAttributesUtils.isDynamicAttribute(property.getName())) {
            continue;
        }
        Object value = entity.getValue(property.getName());
        switch(property.getType()) {
            case DATATYPE:
                if (property.equals(metadataTools.getPrimaryKeyProperty(metaClass)) && !property.getJavaType().equals(String.class)) {
                    // skipping id for non-String-key entities
                    continue;
                }
                Element fieldEl = instanceEl.addElement("field");
                fieldEl.addAttribute("name", property.getName());
                if (value != null) {
                    fieldEl.setText(property.getRange().asDatatype().format(value));
                } else if (!DynamicAttributesUtils.isDynamicAttribute(property.getName())) {
                    encodeNull(fieldEl);
                }
                break;
            case ENUM:
                fieldEl = instanceEl.addElement("field");
                fieldEl.addAttribute("name", property.getName());
                if (value == null) {
                    encodeNull(fieldEl);
                } else {
                    fieldEl.setText(property.getRange().asEnumeration().format(value));
                }
                break;
            case COMPOSITION:
            case ASSOCIATION:
                MetaClass meta = propertyMetaClass(property);
                // checks if the user permitted to read a property
                if (!readPermitted(meta)) {
                    break;
                }
                View propertyView = null;
                if (view != null) {
                    ViewProperty vp = view.getProperty(property.getName());
                    if (vp != null)
                        propertyView = vp.getView();
                }
                if (!property.getRange().getCardinality().isMany()) {
                    Element referenceEl = instanceEl.addElement("reference");
                    referenceEl.addAttribute("name", property.getName());
                    encodeEntity((Entity) value, visited, propertyView, referenceEl);
                } else {
                    Element collectionEl = instanceEl.addElement("collection");
                    collectionEl.addAttribute("name", property.getName());
                    if (value == null) {
                        encodeNull(collectionEl);
                        break;
                    }
                    for (Object childEntity : (Collection) value) {
                        encodeEntity((Entity) childEntity, visited, propertyView, collectionEl);
                    }
                }
                break;
            default:
                throw new IllegalStateException("Unknown property type");
        }
    }
}
Also used : BaseGenericIdEntity(com.haulmont.cuba.core.entity.BaseGenericIdEntity) Element(org.dom4j.Element) MetaClass(com.haulmont.chile.core.model.MetaClass) MetaProperty(com.haulmont.chile.core.model.MetaProperty)

Example 13 with MetaClass

use of com.haulmont.chile.core.model.MetaClass in project cuba by cuba-platform.

the class XMLConverter2 method processServiceMethodResult.

@Override
@Nonnull
public String processServiceMethodResult(Object result, Class resultType) throws Exception {
    Document document = DocumentHelper.createDocument();
    Element resultEl = document.addElement("result");
    if (result instanceof Entity) {
        Entity entity = (Entity) result;
        Document convertedEntity = _process(entity, null);
        resultEl.add(convertedEntity.getRootElement());
    } else if (result instanceof Collection) {
        if (!checkCollectionItemTypes((Collection) result, Entity.class))
            throw new IllegalArgumentException("Items that are not instances of Entity class found in service method result");
        ArrayList list = new ArrayList((Collection) result);
        MetaClass metaClass = null;
        if (!list.isEmpty())
            metaClass = ((Entity) list.get(0)).getMetaClass();
        Document processed = _process(list, metaClass, null);
        resultEl.add(processed.getRootElement());
    } else {
        if (result != null && resultType != Void.TYPE) {
            Datatype datatype = getDatatype(resultType);
            resultEl.setText(datatype != null ? datatype.format(result) : result.toString());
        } else {
            encodeNull(resultEl);
        }
    }
    return documentToString(document);
}
Also used : BaseGenericIdEntity(com.haulmont.cuba.core.entity.BaseGenericIdEntity) Entity(com.haulmont.cuba.core.entity.Entity) MetaClass(com.haulmont.chile.core.model.MetaClass) Element(org.dom4j.Element) Document(org.dom4j.Document) Datatype(com.haulmont.chile.core.datatypes.Datatype) Nonnull(javax.annotation.Nonnull)

Example 14 with MetaClass

use of com.haulmont.chile.core.model.MetaClass in project cuba by cuba-platform.

the class RemoveAction method checkRemovePermission.

protected boolean checkRemovePermission() {
    CollectionDatasource ds = target.getDatasource();
    if (ds instanceof PropertyDatasource) {
        PropertyDatasource propertyDatasource = (PropertyDatasource) ds;
        MetaClass parentMetaClass = propertyDatasource.getMaster().getMetaClass();
        MetaProperty metaProperty = propertyDatasource.getProperty();
        boolean modifyPermitted = security.isEntityAttrPermitted(parentMetaClass, metaProperty.getName(), EntityAttrAccess.MODIFY);
        if (!modifyPermitted) {
            return false;
        }
        if (metaProperty.getRange().getCardinality() != Range.Cardinality.MANY_TO_MANY) {
            boolean deletePermitted = security.isEntityOpPermitted(ds.getMetaClass(), EntityOp.DELETE);
            if (!deletePermitted) {
                return false;
            }
        }
    } else {
        boolean entityOpPermitted = security.isEntityOpPermitted(ds.getMetaClass(), EntityOp.DELETE);
        if (!entityOpPermitted) {
            return false;
        }
    }
    return true;
}
Also used : MetaClass(com.haulmont.chile.core.model.MetaClass) PropertyDatasource(com.haulmont.cuba.gui.data.PropertyDatasource) CollectionDatasource(com.haulmont.cuba.gui.data.CollectionDatasource) MetaProperty(com.haulmont.chile.core.model.MetaProperty)

Example 15 with MetaClass

use of com.haulmont.chile.core.model.MetaClass in project cuba by cuba-platform.

the class Param method loadEntityList.

protected List<Entity> loadEntityList(String[] ids) {
    Metadata metadata = AppBeans.get(Metadata.class);
    MetaClass metaClass = metadata.getSession().getClassNN(javaClass);
    LoadContext ctx = new LoadContext(javaClass);
    LoadContext.Query query = ctx.setQueryString("select e from " + metaClass.getName() + " e where e.id in :ids");
    query.setParameter("ids", Arrays.asList(ids));
    DataManager dataManager = AppBeans.get(DataManager.class);
    return dataManager.loadList(ctx);
}
Also used : MetaClass(com.haulmont.chile.core.model.MetaClass)

Aggregations

MetaClass (com.haulmont.chile.core.model.MetaClass)302 MetaProperty (com.haulmont.chile.core.model.MetaProperty)103 Entity (com.haulmont.cuba.core.entity.Entity)54 MetaPropertyPath (com.haulmont.chile.core.model.MetaPropertyPath)36 Nullable (javax.annotation.Nullable)25 BaseGenericIdEntity (com.haulmont.cuba.core.entity.BaseGenericIdEntity)24 Element (org.dom4j.Element)21 java.util (java.util)18 Inject (javax.inject.Inject)17 CollectionDatasource (com.haulmont.cuba.gui.data.CollectionDatasource)16 Test (org.junit.Test)15 EntityManager (com.haulmont.cuba.core.EntityManager)14 CategoryAttribute (com.haulmont.cuba.core.entity.CategoryAttribute)14 com.haulmont.cuba.core.global (com.haulmont.cuba.core.global)13 Metadata (com.haulmont.cuba.core.global.Metadata)13 Logger (org.slf4j.Logger)13 LoggerFactory (org.slf4j.LoggerFactory)13 MetadataTools (com.haulmont.cuba.core.global.MetadataTools)12 Collectors (java.util.stream.Collectors)11 Range (com.haulmont.chile.core.model.Range)10