use of com.haulmont.cuba.core.Transaction 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.Transaction 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.Transaction 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.cuba.core.Transaction 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;
}
use of com.haulmont.cuba.core.Transaction 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();
}
}
Aggregations