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 NumberIdWorker method getResult.
protected long getResult(String entityName, String sqlScript, long startValue, long increment) {
Transaction tx = persistence.getTransaction(getDataStore(entityName));
try {
checkSequenceExists(entityName, startValue, increment);
Object value = executeScript(entityName, sqlScript);
tx.commit();
if (value instanceof Long)
return (Long) value;
else if (value instanceof BigDecimal)
return ((BigDecimal) value).longValue();
else if (value instanceof String)
return Long.parseLong((String) value);
else if (value == null)
throw new IllegalStateException("No value returned");
else
throw new IllegalStateException("Unsupported value type: " + value.getClass());
} finally {
tx.end();
}
}
use of com.haulmont.cuba.core.Transaction in project cuba by cuba-platform.
the class NumberIdWorker method checkSequenceExists.
protected void checkSequenceExists(String entityName, long startValue, long increment) {
String seqName = getSequenceName(entityName);
if (existingSequences.contains(seqName))
return;
// Create sequence in separate transaction because it's name is cached and we want to be sure it is created
// regardless of possible errors in the invoking code
Transaction tx = persistence.createTransaction(getDataStore(entityName));
try {
EntityManager em = persistence.getEntityManager(getDataStore(entityName));
SequenceSupport sequenceSupport = getSequenceSupport(entityName);
Query query = em.createNativeQuery(sequenceSupport.sequenceExistsSql(seqName));
List list = query.getResultList();
if (list.isEmpty()) {
query = em.createNativeQuery(sequenceSupport.createSequenceSql(seqName, startValue, increment));
query.executeUpdate();
}
existingSequences.add(seqName);
tx.commit();
} finally {
tx.end();
}
}
use of com.haulmont.cuba.core.Transaction in project cuba by cuba-platform.
the class UniqueNumbers method checkSequenceExists.
protected void checkSequenceExists(String domain) {
String seqName = getSequenceName(domain);
if (containsSequence(seqName))
return;
// Create sequence in separate transaction because it's name is cached and we want to be sure it is created
// regardless of possible errors in the invoking code
Transaction tx = persistence.createTransaction(getDataStore(domain));
try {
lock.readLock().unlock();
lock.writeLock().lock();
EntityManager em = persistence.getEntityManager(getDataStore(domain));
Query query = em.createNativeQuery(getSequenceSupport(domain).sequenceExistsSql(seqName));
List list = query.getResultList();
if (list.isEmpty()) {
query = em.createNativeQuery(getSequenceSupport(domain).createSequenceSql(seqName, 1, 1));
query.executeUpdate();
}
tx.commit();
existingSequences.add(seqName);
} finally {
lock.readLock().lock();
lock.writeLock().unlock();
tx.end();
}
}
use of com.haulmont.cuba.core.Transaction 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");
}
}
Aggregations