Search in sources :

Example 56 with Entity

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

the class EntityLoadInfoBuilder method parse.

/**
 * Parse an info from the string.
 *
 * @param str string representation of the info. See {@link EntityLoadInfo} for formats.
 * @return info instance or null if the string can not be parsed. Any exception is silently swallowed.
 */
@Nullable
public EntityLoadInfo parse(String str) {
    boolean isNew = false;
    if (str.startsWith(EntityLoadInfo.NEW_PREFIX)) {
        str = str.substring("NEW-".length());
        isNew = true;
    }
    int idDashPos = str.indexOf('-');
    if (idDashPos == -1) {
        if (isNew) {
            MetaClass metaClass = metadata.getSession().getClass(str);
            if (metaClass == null) {
                return null;
            }
            Entity entity = metadata.create(metaClass);
            MetaProperty primaryKeyProp = metadata.getTools().getPrimaryKeyProperty(metaClass);
            boolean stringKey = primaryKeyProp != null && primaryKeyProp.getJavaType().equals(String.class);
            return new EntityLoadInfo(entity.getId(), metaClass, null, stringKey, true);
        }
        return null;
    }
    String entityName = str.substring(0, idDashPos);
    MetaClass metaClass = metadata.getSession().getClass(entityName);
    if (metaClass == null) {
        return null;
    }
    Object id;
    String viewName;
    boolean stringKey = false;
    MetaProperty primaryKeyProp = metadata.getTools().getPrimaryKeyProperty(metaClass);
    if (primaryKeyProp == null)
        return null;
    if (primaryKeyProp.getJavaType().equals(UUID.class)) {
        int viewDashPos = -1;
        int dashCount = StringUtils.countMatches(str, "-");
        if (dashCount < 5) {
            return null;
        }
        if (dashCount >= 6) {
            int i = 0;
            while (i < 6) {
                viewDashPos = str.indexOf('-', viewDashPos + 1);
                i++;
            }
            viewName = str.substring(viewDashPos + 1);
        } else {
            viewDashPos = str.length();
            viewName = null;
        }
        String entityIdStr = str.substring(idDashPos + 1, viewDashPos);
        try {
            id = UuidProvider.fromString(entityIdStr);
        } catch (Exception e) {
            return null;
        }
    } else {
        String entityIdStr;
        if (primaryKeyProp.getJavaType().equals(String.class)) {
            stringKey = true;
            int viewDashPos = str.indexOf("}-", idDashPos + 2);
            if (viewDashPos > -1) {
                viewName = str.substring(viewDashPos + 2);
            } else {
                viewDashPos = str.length() - 1;
                viewName = null;
            }
            entityIdStr = str.substring(idDashPos + 2, viewDashPos);
        } else {
            int viewDashPos = str.indexOf('-', idDashPos + 1);
            if (viewDashPos > -1) {
                viewName = str.substring(viewDashPos + 1);
            } else {
                viewDashPos = str.length();
                viewName = null;
            }
            entityIdStr = str.substring(idDashPos + 1, viewDashPos);
        }
        try {
            if (primaryKeyProp.getJavaType().equals(Long.class)) {
                id = Long.valueOf(entityIdStr);
            } else if (primaryKeyProp.getJavaType().equals(Integer.class)) {
                id = Integer.valueOf(entityIdStr);
            } else {
                id = entityIdStr;
            }
        } catch (Exception e) {
            return null;
        }
    }
    return new EntityLoadInfo(id, metaClass, viewName, stringKey, isNew);
}
Also used : Entity(com.haulmont.cuba.core.entity.Entity) MetaClass(com.haulmont.chile.core.model.MetaClass) MetaProperty(com.haulmont.chile.core.model.MetaProperty) Nullable(javax.annotation.Nullable)

Example 57 with Entity

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

the class MetadataTools method deepCopy.

/**
 * Copies all property values from source to destination excluding null values.
 */
public void deepCopy(Entity source, Entity destination, EntitiesHolder entitiesHolder) {
    for (MetaProperty srcProperty : source.getMetaClass().getProperties()) {
        String name = srcProperty.getName();
        if (srcProperty.isReadOnly() || !persistentAttributesLoadChecker.isLoaded(source, name)) {
            continue;
        }
        Object value = source.getValue(name);
        if (value == null) {
            continue;
        }
        if (srcProperty.getRange().isClass()) {
            Class refClass = srcProperty.getRange().asClass().getJavaClass();
            if (!isPersistent(refClass)) {
                continue;
            }
            if (srcProperty.getRange().getCardinality().isMany()) {
                // noinspection unchecked
                Collection<Entity> srcCollection = (Collection) value;
                Collection<Entity> dstCollection = value instanceof List ? new ArrayList<>() : new LinkedHashSet<>();
                for (Entity srcRef : srcCollection) {
                    Entity reloadedRef = entitiesHolder.find(srcRef.getId());
                    if (reloadedRef == null) {
                        reloadedRef = entitiesHolder.create(srcRef.getClass(), srcRef.getId());
                        deepCopy(srcRef, reloadedRef, entitiesHolder);
                    }
                    dstCollection.add(reloadedRef);
                }
                destination.setValue(name, dstCollection);
            } else {
                Entity srcRef = (Entity) value;
                Entity reloadedRef = entitiesHolder.find(srcRef.getId());
                if (reloadedRef == null) {
                    reloadedRef = entitiesHolder.create(srcRef.getClass(), srcRef.getId());
                    deepCopy(srcRef, reloadedRef, entitiesHolder);
                }
                destination.setValue(name, reloadedRef);
            }
        } else {
            destination.setValue(name, value);
        }
    }
    if (source instanceof BaseGenericIdEntity && destination instanceof BaseGenericIdEntity) {
        ((BaseGenericIdEntity) destination).setDynamicAttributes(((BaseGenericIdEntity<?>) source).getDynamicAttributes());
    }
}
Also used : Entity(com.haulmont.cuba.core.entity.Entity) EnumClass(com.haulmont.chile.core.datatypes.impl.EnumClass) ImmutableList(com.google.common.collect.ImmutableList)

Example 58 with Entity

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

the class MetadataTools method deepCopy.

/**
 * Make deep copy of the source entity: all referred entities ant collections will be copied as well
 */
public <T extends Entity> T deepCopy(T source) {
    CachingEntitiesHolder entityFinder = new CachingEntitiesHolder();
    Entity destination = entityFinder.create(source.getClass(), source.getId());
    deepCopy(source, destination, entityFinder);
    return (T) destination;
}
Also used : Entity(com.haulmont.cuba.core.entity.Entity)

Example 59 with Entity

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

the class MetadataTools method getEntityName.

/**
 * @param entityClass entity class
 * @return entity name as defined in {@link javax.persistence.Entity} annotation
 */
public String getEntityName(Class<?> entityClass) {
    Annotation annotation = entityClass.getAnnotation(javax.persistence.Entity.class);
    if (annotation == null)
        throw new IllegalArgumentException("Class " + entityClass + " is not a persistent entity");
    String name = ((javax.persistence.Entity) annotation).name();
    if (!StringUtils.isEmpty(name))
        return name;
    else
        return entityClass.getSimpleName();
}
Also used : Entity(com.haulmont.cuba.core.entity.Entity) javax.persistence(javax.persistence) Annotation(java.lang.annotation.Annotation)

Example 60 with Entity

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

the class AbstractEditor method postCommit.

/**
 * Hook to be implemented in subclasses. Called by the framework after committing datasources.
 * The default implementation notifies about commit and calls {@link #postInit()} if the window is not closing.
 * @param committed whether any data were actually changed and committed
 * @param close     whether the window is going to be closed
 * @return  true to continue, false to abort
 */
protected boolean postCommit(boolean committed, boolean close) {
    if (committed && !close) {
        if (showSaveNotification) {
            Entity entity = ((Editor) frame).getItem();
            frame.showNotification(messages.formatMessage(AppConfig.getMessagesPack(), "info.EntitySave", messages.getTools().getEntityCaption(entity.getMetaClass()), entity.getInstanceName()), NotificationType.TRAY);
        }
        postInit();
    }
    return true;
}
Also used : Entity(com.haulmont.cuba.core.entity.Entity)

Aggregations

Entity (com.haulmont.cuba.core.entity.Entity)203 MetaClass (com.haulmont.chile.core.model.MetaClass)51 MetaProperty (com.haulmont.chile.core.model.MetaProperty)44 BaseGenericIdEntity (com.haulmont.cuba.core.entity.BaseGenericIdEntity)40 CollectionDatasource (com.haulmont.cuba.gui.data.CollectionDatasource)18 Test (org.junit.Test)15 Inject (javax.inject.Inject)14 java.util (java.util)12 EntityManager (com.haulmont.cuba.core.EntityManager)11 ParseException (java.text.ParseException)11 Element (org.dom4j.Element)11 Logger (org.slf4j.Logger)11 com.haulmont.cuba.core.global (com.haulmont.cuba.core.global)10 MetaPropertyPath (com.haulmont.chile.core.model.MetaPropertyPath)9 LoggerFactory (org.slf4j.LoggerFactory)9 Query (com.haulmont.cuba.core.Query)8 Server (com.haulmont.cuba.core.entity.Server)8 Transaction (com.haulmont.cuba.core.Transaction)7 Datasource (com.haulmont.cuba.gui.data.Datasource)7 Collectors (java.util.stream.Collectors)7