Search in sources :

Example 1 with UniqueConstraintViolationException

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);
    }
}
Also used : EntityChangedEventInfo(io.jmix.data.impl.EntityChangedEventInfo) Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) EntityChangedEvent(io.jmix.core.event.EntityChangedEvent) UniqueConstraintViolationException(io.jmix.data.exception.UniqueConstraintViolationException) EntityManager(javax.persistence.EntityManager) PersistenceException(javax.persistence.PersistenceException)

Example 2 with UniqueConstraintViolationException

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;
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) ArrayList(java.util.ArrayList) PersistenceException(javax.persistence.PersistenceException) UUID(java.util.UUID) UniqueConstraintViolationException(io.jmix.data.exception.UniqueConstraintViolationException) NotNull(javax.validation.constraints.NotNull)

Aggregations

UniqueConstraintViolationException (io.jmix.data.exception.UniqueConstraintViolationException)2 Matcher (java.util.regex.Matcher)2 Pattern (java.util.regex.Pattern)2 PersistenceException (javax.persistence.PersistenceException)2 EntityChangedEvent (io.jmix.core.event.EntityChangedEvent)1 EntityChangedEventInfo (io.jmix.data.impl.EntityChangedEventInfo)1 ArrayList (java.util.ArrayList)1 UUID (java.util.UUID)1 EntityManager (javax.persistence.EntityManager)1 NotNull (javax.validation.constraints.NotNull)1