Search in sources :

Example 66 with MetaClass

use of com.haulmont.chile.core.model.MetaClass 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 67 with MetaClass

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

the class EntitySnapshotManager method migrateSnapshots.

@Override
public void migrateSnapshots(MetaClass metaClass, Object id, Map<Class, Class> classMapping) {
    metaClass = extendedEntities.getOriginalOrThisMetaClass(metaClass);
    // load snapshots
    List<EntitySnapshot> snapshotList = getSnapshots(metaClass, id);
    Class javaClass = metaClass.getJavaClass();
    MetaClass mappedMetaClass = null;
    if (classMapping.containsKey(javaClass)) {
        Class mappedClass = classMapping.get(javaClass);
        mappedMetaClass = extendedEntities.getOriginalOrThisMetaClass(metadata.getClass(mappedClass));
    }
    for (EntitySnapshot snapshot : snapshotList) {
        if (mappedMetaClass != null) {
            snapshot.setEntityMetaClass(mappedMetaClass.getName());
        }
        String snapshotXml = snapshot.getSnapshotXml();
        String viewXml = snapshot.getViewXml();
        snapshot.setSnapshotXml(processSnapshotXml(snapshotXml, classMapping));
        snapshot.setViewXml(processViewXml(viewXml, classMapping));
    }
    // Save snapshots to db
    Transaction tx = persistence.createTransaction();
    try {
        EntityManager em = persistence.getEntityManager();
        for (EntitySnapshot snapshot : snapshotList) {
            em.merge(snapshot);
        }
        tx.commit();
    } finally {
        tx.end();
    }
}
Also used : EntityManager(com.haulmont.cuba.core.EntityManager) MetaClass(com.haulmont.chile.core.model.MetaClass) Transaction(com.haulmont.cuba.core.Transaction) MetaClass(com.haulmont.chile.core.model.MetaClass)

Example 68 with MetaClass

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

the class EntitySnapshotManager method createSnapshot.

@Override
public EntitySnapshot createSnapshot(Entity entity, View view, Date snapshotDate, User author) {
    Preconditions.checkNotNullArgument(entity);
    Preconditions.checkNotNullArgument(view);
    Preconditions.checkNotNullArgument(snapshotDate);
    checkCompositePrimaryKey(entity);
    Class viewEntityClass = view.getEntityClass();
    Class entityClass = entity.getClass();
    if (!viewEntityClass.isAssignableFrom(entityClass)) {
        throw new IllegalStateException("View could not be used with this propertyValue");
    }
    MetaClass metaClass = extendedEntities.getOriginalOrThisMetaClass(metadata.getClass(entity.getClass()));
    EntitySnapshot snapshot = metadata.create(EntitySnapshot.class);
    snapshot.setObjectEntityId(referenceToEntitySupport.getReferenceId(entity));
    snapshot.setEntityMetaClass(metaClass.getName());
    snapshot.setViewXml(viewSerializationAPI.toJson(view, ViewSerializationOption.COMPACT_FORMAT));
    snapshot.setSnapshotXml(entitySerializationAPI.toJson(entity));
    snapshot.setSnapshotDate(snapshotDate);
    snapshot.setAuthor(author);
    Transaction tx = persistence.createTransaction();
    try {
        EntityManager em = persistence.getEntityManager();
        em.persist(snapshot);
        tx.commit();
    } finally {
        tx.end();
    }
    return snapshot;
}
Also used : EntityManager(com.haulmont.cuba.core.EntityManager) MetaClass(com.haulmont.chile.core.model.MetaClass) Transaction(com.haulmont.cuba.core.Transaction) MetaClass(com.haulmont.chile.core.model.MetaClass)

Example 69 with MetaClass

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

the class FoldersServiceBean method loadSearchFolders.

@Override
public List<SearchFolder> loadSearchFolders() {
    log.debug("Loading SearchFolders");
    StopWatch stopWatch = new Slf4JStopWatch("SearchFolders");
    stopWatch.start();
    Transaction tx = persistence.createTransaction();
    try {
        EntityManager em = persistence.getEntityManager();
        MetaClass effectiveMetaClass = metadata.getExtendedEntities().getEffectiveMetaClass(SearchFolder.class);
        TypedQuery<SearchFolder> q = em.createQuery("select f from " + effectiveMetaClass.getName() + " f " + "left join fetch f.user u on u.id = ?1 " + "left join fetch f.presentation " + "where (u.id = ?1 or u is null) " + "order by f.sortOrder, f.name", SearchFolder.class);
        q.setParameter(1, userSessionSource.currentOrSubstitutedUserId());
        List<SearchFolder> list = q.getResultList();
        // fetch parents
        for (SearchFolder folder : list) {
            folder.getParent();
        }
        tx.commit();
        return list;
    } finally {
        tx.end();
        stopWatch.stop();
    }
}
Also used : Slf4JStopWatch(org.perf4j.slf4j.Slf4JStopWatch) EntityManager(com.haulmont.cuba.core.EntityManager) Transaction(com.haulmont.cuba.core.Transaction) MetaClass(com.haulmont.chile.core.model.MetaClass) SearchFolder(com.haulmont.cuba.security.entity.SearchFolder) Slf4JStopWatch(org.perf4j.slf4j.Slf4JStopWatch) StopWatch(org.perf4j.StopWatch)

Example 70 with MetaClass

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

the class LockManager method unlock.

@Override
public void unlock(Entity entity) {
    Preconditions.checkNotNullArgument(entity, "entity is null");
    MetaClass metaClass = metadata.getClassNN(entity.getClass());
    MetaClass originalMetaClass = metadata.getExtendedEntities().getOriginalOrThisMetaClass(metaClass);
    unlock(originalMetaClass.getName(), entity.getId().toString());
}
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