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);
}
}
}
}
}
}
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();
}
}
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;
}
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();
}
}
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());
}
Aggregations