Search in sources :

Example 36 with Entity

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

the class EntityRestoreServiceBean method restoreDetails.

protected void restoreDetails(Entity entity, Date deleteTs, String storeName) {
    EntityManager em = persistence.getEntityManager(storeName);
    MetaClass metaClass = metadata.getClassNN(entity.getClass());
    List<MetaProperty> properties = new ArrayList<>();
    fillProperties(metaClass, properties, OnDelete.class.getName());
    for (MetaProperty property : properties) {
        OnDelete annotation = property.getAnnotatedElement().getAnnotation(OnDelete.class);
        DeletePolicy deletePolicy = annotation.value();
        if (deletePolicy == DeletePolicy.CASCADE) {
            MetaClass detailMetaClass = property.getRange().asClass();
            if (!storeName.equals(metadata.getTools().getStoreName(detailMetaClass))) {
                log.debug("Cannot restore " + property.getRange().asClass() + " because it is from different data store");
                continue;
            }
            if (!SoftDelete.class.isAssignableFrom(detailMetaClass.getJavaClass())) {
                log.debug("Cannot restore " + property.getRange().asClass() + " because it is hard deleted");
                continue;
            }
            MetaProperty inverseProp = property.getInverse();
            if (inverseProp == null) {
                log.debug("Cannot restore " + property.getRange().asClass() + " because it has no inverse property for " + metaClass);
                continue;
            }
            String jpql = "select e from " + detailMetaClass + " e where e." + inverseProp.getName() + ".id = ?1 " + "and e.deleteTs >= ?2 and e.deleteTs <= ?3";
            Query query = em.createQuery(jpql);
            query.setParameter(1, entity.getId());
            query.setParameter(2, DateUtils.addMilliseconds(deleteTs, -100));
            query.setParameter(3, DateUtils.addMilliseconds(deleteTs, 1000));
            // noinspection unchecked
            List<Entity> list = query.getResultList();
            for (Entity detailEntity : list) {
                if (entity instanceof SoftDelete) {
                    restoreEntity(detailEntity, storeName);
                }
            }
        }
    }
    properties = new ArrayList<>();
    fillProperties(metaClass, properties, OnDeleteInverse.class.getName());
    for (MetaProperty property : properties) {
        OnDeleteInverse annotation = property.getAnnotatedElement().getAnnotation(OnDeleteInverse.class);
        DeletePolicy deletePolicy = annotation.value();
        if (deletePolicy == DeletePolicy.CASCADE) {
            MetaClass detailMetaClass = property.getDomain();
            if (!storeName.equals(metadata.getTools().getStoreName(detailMetaClass))) {
                log.debug("Cannot restore " + property.getRange().asClass() + " because it is from different data store");
                continue;
            }
            if (!SoftDelete.class.isAssignableFrom(detailMetaClass.getJavaClass())) {
                log.debug("Cannot restore " + property.getRange().asClass() + " because it is hard deleted");
                continue;
            }
            List<MetaClass> metClassesToRestore = new ArrayList<>();
            metClassesToRestore.add(detailMetaClass);
            metClassesToRestore.addAll(detailMetaClass.getDescendants());
            for (MetaClass metaClassToRestore : metClassesToRestore) {
                if (!metadata.getTools().isPersistent(metaClassToRestore))
                    continue;
                String jpql = "select e from " + metaClassToRestore.getName() + " e where e." + property.getName() + ".id = ?1 and e.deleteTs >= ?2 and e.deleteTs <= ?3";
                Query query = em.createQuery(jpql);
                query.setParameter(1, entity.getId());
                query.setParameter(2, DateUtils.addMilliseconds(deleteTs, -100));
                query.setParameter(3, DateUtils.addMilliseconds(deleteTs, 1000));
                // noinspection unchecked
                List<Entity> list = query.getResultList();
                for (Entity detailEntity : list) {
                    if (entity instanceof SoftDelete) {
                        restoreEntity(detailEntity, storeName);
                    }
                }
            }
        }
    }
}
Also used : Entity(com.haulmont.cuba.core.entity.Entity) SoftDelete(com.haulmont.cuba.core.entity.SoftDelete) Query(com.haulmont.cuba.core.Query) DeletePolicy(com.haulmont.cuba.core.global.DeletePolicy) EntityManager(com.haulmont.cuba.core.EntityManager) MetaClass(com.haulmont.chile.core.model.MetaClass) OnDeleteInverse(com.haulmont.cuba.core.entity.annotation.OnDeleteInverse) MetaProperty(com.haulmont.chile.core.model.MetaProperty) OnDelete(com.haulmont.cuba.core.entity.annotation.OnDelete)

Example 37 with Entity

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

the class EntityRestoreServiceBean method restoreEntity.

protected void restoreEntity(Entity entity, String storeName) {
    EntityManager em = persistence.getEntityManager(storeName);
    Entity reloadedEntity = em.find(entity.getClass(), entity.getId());
    if (reloadedEntity != null && ((SoftDelete) reloadedEntity).isDeleted()) {
        log.info("Restoring deleted entity " + entity);
        Date deleteTs = ((SoftDelete) reloadedEntity).getDeleteTs();
        ((SoftDelete) reloadedEntity).setDeleteTs(null);
        em.merge(reloadedEntity);
        restoreDetails(reloadedEntity, deleteTs, storeName);
    }
}
Also used : Entity(com.haulmont.cuba.core.entity.Entity) EntityManager(com.haulmont.cuba.core.EntityManager) SoftDelete(com.haulmont.cuba.core.entity.SoftDelete)

Example 38 with Entity

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

the class EntitySnapshotManager method getSnapshots.

@Override
public List<EntitySnapshot> getSnapshots(MetaClass metaClass, Object id) {
    metaClass = extendedEntities.getOriginalOrThisMetaClass(metaClass);
    Entity entity = dataManager.load(new LoadContext<>(metaClass).setId(id).setView(View.LOCAL));
    checkCompositePrimaryKey(entity);
    List<EntitySnapshot> resultList = null;
    View view = metadata.getViewRepository().getView(EntitySnapshot.class, "entitySnapshot.browse");
    Transaction tx = persistence.createTransaction();
    try {
        EntityManager em = persistence.getEntityManager();
        TypedQuery<EntitySnapshot> query = em.createQuery(format("select s from sys$EntitySnapshot s where s.entity.%s = :entityId and s.entityMetaClass = :metaClass " + "order by s.snapshotDate desc", referenceToEntitySupport.getReferenceIdPropertyName(metaClass)), EntitySnapshot.class);
        query.setParameter("entityId", referenceToEntitySupport.getReferenceId(entity));
        query.setParameter("metaClass", metaClass.getName());
        query.setView(view);
        resultList = query.getResultList();
        tx.commit();
    } finally {
        tx.end();
    }
    return resultList;
}
Also used : Entity(com.haulmont.cuba.core.entity.Entity) EntityManager(com.haulmont.cuba.core.EntityManager) Transaction(com.haulmont.cuba.core.Transaction)

Example 39 with Entity

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

the class RelatedEntitiesServiceBean method getRelatedIds.

@SuppressWarnings("unchecked")
@Override
public List<Object> getRelatedIds(List<Object> parentIds, String parentMetaClass, String relationProperty) {
    checkNotNullArgument(parentIds, "parents argument is null");
    checkNotNullArgument(parentMetaClass, "parentMetaClass argument is null");
    checkNotNullArgument(relationProperty, "relationProperty argument is null");
    MetaClass metaClass = extendedEntities.getEffectiveMetaClass(metadata.getClassNN(parentMetaClass));
    Class parentClass = metaClass.getJavaClass();
    MetaProperty metaProperty = metaClass.getPropertyNN(relationProperty);
    // return empty list only after all argument checks
    if (parentIds.isEmpty()) {
        return Collections.emptyList();
    }
    MetaClass propertyMetaClass = extendedEntities.getEffectiveMetaClass(metaProperty.getRange().asClass());
    Class propertyClass = propertyMetaClass.getJavaClass();
    List<Object> relatedIds = new ArrayList<>();
    Transaction tx = persistence.createTransaction();
    try {
        EntityManager em = persistence.getEntityManager();
        String parentPrimaryKey = metadata.getTools().getPrimaryKeyName(metaClass);
        String queryString = "select x from " + parentMetaClass + " x where x." + parentPrimaryKey + " in :ids";
        Query query = em.createQuery(queryString);
        String relatedPrimaryKey = metadata.getTools().getPrimaryKeyName(propertyMetaClass);
        View view = new View(parentClass);
        view.addProperty(relationProperty, new View(propertyClass).addProperty(relatedPrimaryKey));
        query.setView(view);
        query.setParameter("ids", parentIds);
        List<Entity> resultList = query.getResultList();
        for (Entity e : resultList) {
            Object value = e.getValue(relationProperty);
            if (value instanceof Entity) {
                relatedIds.add(((Entity) value).getId());
            } else if (value instanceof Collection) {
                for (Object collectionItem : (Collection) value) {
                    relatedIds.add(((Entity) collectionItem).getId());
                }
            }
        }
        tx.commit();
    } finally {
        tx.end();
    }
    return relatedIds;
}
Also used : Entity(com.haulmont.cuba.core.entity.Entity) View(com.haulmont.cuba.core.global.View) MetaClass(com.haulmont.chile.core.model.MetaClass) MetaClass(com.haulmont.chile.core.model.MetaClass) MetaProperty(com.haulmont.chile.core.model.MetaProperty)

Example 40 with Entity

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

the class StandardCacheLoader method updateData.

@Override
public void updateData(CacheSet cacheSet, Map<String, Object> params) throws CacheException {
    if (configuration.getConfig(GlobalConfig.class).getPerformanceTestMode())
        return;
    Collection<Object> items = cacheSet.getItems();
    List updateItems = (List) params.get("items");
    if ((updateItems != null) && (updateItems.size() > 0)) {
        MetaClass metaClass = metadata.getSession().getClass(metaClassName);
        View view = metadata.getViewRepository().getView(metaClass, viewName);
        Transaction tx = persistence.createTransaction();
        try {
            EntityManager em = persistence.getEntityManager();
            for (Object item : updateItems) {
                Entity entity = (Entity) item;
                entity = em.find(entity.getClass(), entity.getId(), view);
                items.remove(item);
                if (entity != null)
                    items.add(entity);
            }
            tx.commit();
        } catch (Exception e) {
            throw new CacheException(e);
        } finally {
            tx.end();
        }
    } else {
        log.debug("Nothing to update");
    }
}
Also used : Entity(com.haulmont.cuba.core.entity.Entity) EntityManager(com.haulmont.cuba.core.EntityManager) MetaClass(com.haulmont.chile.core.model.MetaClass) Transaction(com.haulmont.cuba.core.Transaction) List(java.util.List)

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