use of io.jmix.data.exception.UniqueConstraintViolationException in project jmix by jmix-framework.
the class JpaDataStore method beforeSaveTransactionCommit.
@Override
protected void beforeSaveTransactionCommit(SaveContext context, Collection<Object> savedEntities, Collection<Object> removedEntities) {
if (context.isJoinTransaction()) {
List<Object> entities = new ArrayList<>(savedEntities);
entities.addAll(removedEntities);
List<EntityChangedEventInfo> eventsInfo;
EntityManager em = storeAwareLocator.getEntityManager(storeName);
boolean softDeletionBefore = PersistenceHints.isSoftDeletion(em);
try {
em.setProperty(PersistenceHints.SOFT_DELETION, context.getHints().get(PersistenceHints.SOFT_DELETION));
persistenceSupport.processFlush(em, false);
eventsInfo = entityChangedEventManager.collect(persistenceSupport.getInstances(em));
((EntityManager) em.getDelegate()).flush();
} catch (PersistenceException e) {
Pattern pattern = getUniqueConstraintViolationPattern();
Matcher matcher = pattern.matcher(e.toString());
if (matcher.find()) {
throw new UniqueConstraintViolationException(e.getMessage(), resolveConstraintName(matcher), e);
}
throw e;
} finally {
em.setProperty(PersistenceHints.SOFT_DELETION, softDeletionBefore);
}
// noinspection rawtypes
List<EntityChangedEvent> events = new ArrayList<>(eventsInfo.size());
for (EntityChangedEventInfo info : eventsInfo) {
events.add(new EntityChangedEvent<>(info.getSource(), Id.of(info.getEntity()), info.getType(), info.getChanges(), info.getOriginalMetaClass()));
}
for (Object entity : entities) {
detachEntity(em, entity, context.getFetchPlans().get(entity), true);
}
entityChangedEventManager.publish(events);
}
}
use of io.jmix.data.exception.UniqueConstraintViolationException in project jmix by jmix-framework.
the class ReportsPersistenceImpl method saveReport.
@NotNull
protected Report saveReport(Report report) {
ReportTemplate defaultTemplate = report.getDefaultTemplate();
List<ReportTemplate> loadedTemplates = report.getTemplates();
List<ReportTemplate> savedTemplates = new ArrayList<>();
report.setDefaultTemplate(null);
report.setTemplates(null);
ReportGroup group = report.getGroup();
if (group != null) {
FetchPlan fetchPlan = fetchPlanRepository.getFetchPlan(ReportGroup.class, FetchPlan.LOCAL);
ReportGroup existingGroup;
List<ReportGroup> existingGroups = em.createQuery("select g from report_ReportGroup g where g.title = :title", ReportGroup.class).setParameter("title", group.getTitle()).setHint(PersistenceHints.FETCH_PLAN, fetchPlan).getResultList();
if (CollectionUtils.isEmpty(existingGroups)) {
em.setProperty(PersistenceHints.SOFT_DELETION, false);
existingGroup = em.find(ReportGroup.class, report.getGroup().getId(), PersistenceHints.builder().withFetchPlan(fetchPlan).build());
em.setProperty(PersistenceHints.SOFT_DELETION, true);
} else {
existingGroup = existingGroups.get(0);
}
if (existingGroup != null) {
if (!entityStates.isDeleted(existingGroup)) {
report.setGroup(existingGroup);
} else {
group = dataManager.create(ReportGroup.class);
UUID newId = group.getId();
group = metadataTools.copy(existingGroup);
group.setVersion(0);
group.setDeleteTs(null);
group.setDeletedBy(null);
group.setId(newId);
report.setGroup(group);
}
} else {
em.persist(group);
}
}
em.setProperty(PersistenceHints.SOFT_DELETION, false);
Report existingReport;
List<ReportTemplate> existingTemplates = null;
try {
FetchPlan fetchPlan = fetchPlanRepository.getFetchPlan(Report.class, "report.withTemplates");
existingReport = em.find(Report.class, report.getId(), PersistenceHints.builder().withFetchPlan(fetchPlan).build());
storeIndexFields(report);
if (existingReport != null) {
report.setVersion(existingReport.getVersion());
report = em.merge(report);
if (existingReport.getTemplates() != null) {
existingTemplates = existingReport.getTemplates();
}
if (existingReport.getDeleteTs() != null) {
existingReport.setDeleteTs(null);
existingReport.setDeletedBy(null);
}
report.setDefaultTemplate(null);
report.setTemplates(null);
} else {
report.setVersion(0);
report = em.merge(report);
}
if (loadedTemplates != null) {
if (existingTemplates != null) {
for (ReportTemplate template : existingTemplates) {
if (!loadedTemplates.contains(template)) {
em.remove(template);
}
}
}
for (ReportTemplate loadedTemplate : loadedTemplates) {
ReportTemplate existingTemplate = em.find(ReportTemplate.class, loadedTemplate.getId());
if (existingTemplate != null) {
loadedTemplate.setVersion(existingTemplate.getVersion());
if (entityStates.isNew(loadedTemplate)) {
entityStates.makeDetached(loadedTemplate);
}
} else {
loadedTemplate.setVersion(0);
}
loadedTemplate.setReport(report);
savedTemplates.add(em.merge(loadedTemplate));
}
}
em.flush();
} catch (PersistenceException e) {
Pattern pattern = getUniqueConstraintViolationPattern();
Matcher matcher = pattern.matcher(e.toString());
if (matcher.find()) {
throw new UniqueConstraintViolationException(e.getMessage(), resolveConstraintName(matcher), e);
}
throw e;
} finally {
em.setProperty(PersistenceHints.SOFT_DELETION, true);
}
for (ReportTemplate savedTemplate : savedTemplates) {
if (savedTemplate.equals(defaultTemplate)) {
defaultTemplate = savedTemplate;
break;
}
}
report.setDefaultTemplate(defaultTemplate);
report.setTemplates(savedTemplates);
return report;
}
Aggregations