use of com.haulmont.cuba.core.EntityManager in project cuba by cuba-platform.
the class Emailer method migrateEmailsToFileStorage.
@Override
public void migrateEmailsToFileStorage(List<SendingMessage> messages) {
try (Transaction tx = persistence.createTransaction()) {
EntityManager em = persistence.getEntityManager();
for (SendingMessage msg : messages) {
migrateMessage(em, msg);
}
tx.commit();
}
}
use of com.haulmont.cuba.core.EntityManager 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);
}
}
}
}
}
}
use of com.haulmont.cuba.core.EntityManager 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);
}
}
use of com.haulmont.cuba.core.EntityManager 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();
}
}
use of com.haulmont.cuba.core.EntityManager 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;
}
Aggregations