/** * Saving records to the database. JPA handles bath writing, so parameter paging can be ignored * (vs remove). * * @param <T> POJOs must extend DefaultRecord * @param list list of records to save in the database */ public <T extends Record> void save(List<T> list) { if (list != null) { mc.transaction( () -> { list.stream() .filter((record) -> (record != null)) .forEach( (record) -> { Long recordId = record.getId(); if (recordId == null) { // persisting new record RecordWrapper wrapper = createRecordWrapper(record); indexRecord(true, record); auditor.logCreateRecord(wrapper); } else { // using reference as only SET is necesssary RecordWrapper wrapper = mc.getTransactionEntityManager() .getReference( RecordWrapper.class, recordId); // getReference creating two queries including // RecordType wrapper.setData(mc.toWrapper(record)); //// if (record.isDocumentChanged()) { //// wrapper.setDocument(record.getDocument()); //// } indexRecord(false, record); auditor.logUpdateRecord(wrapper, false); // //record.isDocumentChanged()); } }); }); } }
/** * creating a new record * * @param <T> POJOs must extend DefaultRecord * @param record record to save * @return the entity encapsulating the record in the database */ private <T extends Record> RecordWrapper createRecordWrapper(T record) { RecordWrapper wrapper = new RecordWrapper(); RecordType recordType = mc.getType(record.getClass(), false); wrapper.setRecordType(recordType.getId()); wrapper.setData(mc.toWrapper(record)); //// if (record.isDocumentChanged()) { //// wrapper.setDocument(record.getDocument()); //// } // -- tenant management Object tenant = mc.getTransactionEntityManager() .getProperties() .get(EntityManagerProperties.MULTITENANT_PROPERTY_DEFAULT); if (tenant != null) { wrapper.setTenant(tenant.toString()); } // -- getting the id from the JPA mc.getTransactionEntityManager().persist(wrapper); Long recordId = wrapper.getId(); record.setId(recordId); return wrapper; }