use of de.metas.ui.web.window.descriptor.sql.SqlDocumentEntityDataBindingDescriptor in project metasfresh-webui-api by metasfresh.
the class SqlDocumentsRepository method save.
@Override
public void save(final Document document) {
Services.get(ITrxManager.class).assertThreadInheritedTrxExists();
assertThisRepository(document.getEntityDescriptor());
DocumentPermissionsHelper.assertCanEdit(document);
// Runnables to be executed after the PO is saved
final List<Runnable> afterSaveRunnables = new ArrayList<>();
//
// Load the PO / Create new PO instance
final PO po = retrieveOrCreatePO(document);
//
// Set values to PO
final boolean isNew = document.isNew();
boolean changes = false;
for (final IDocumentFieldView documentField : document.getFieldViews()) {
if (!isNew && !documentField.hasChangesToSave()) {
logger.trace("Skip setting PO value because document field has no changes: {}", documentField);
continue;
}
if (DocumentFieldWidgetType.Labels == documentField.getWidgetType()) {
// save labels after PO is saved because we want to make sure it's not new (so we can link to it)
afterSaveRunnables.add(() -> saveLabels(document, documentField));
}
if (setPOValue(po, documentField)) {
changes = true;
}
}
//
// Save the PO
boolean needsRefresh = false;
if (changes) {
//
// Actual save
// TODO: advice the PO to not reload after save.
InterfaceWrapperHelper.save(po);
document.markAsNotNew();
needsRefresh = true;
} else {
logger.trace("Skip saving {} because there was no actual change", po);
}
// Execute after save runnables
if (!afterSaveRunnables.isEmpty()) {
afterSaveRunnables.forEach(r -> r.run());
needsRefresh = true;
}
// Reload the document
if (needsRefresh) {
final SqlDocumentEntityDataBindingDescriptor dataBinding = document.getEntityDescriptor().getDataBinding(SqlDocumentEntityDataBindingDescriptor.class);
final DocumentId idNew = extractDocumentId(po, dataBinding);
refresh(document, idNew);
}
// Notify the parent document that one of it's children were saved
if (!document.isRootDocument()) {
document.getParentDocument().onChildSaved(document);
}
}
use of de.metas.ui.web.window.descriptor.sql.SqlDocumentEntityDataBindingDescriptor in project metasfresh-webui-api by metasfresh.
the class SqlDocumentsRepository method retrieveNextDocumentId.
private static DocumentId retrieveNextDocumentId(final DocumentEntityDescriptor entityDescriptor) {
final SqlDocumentEntityDataBindingDescriptor dataBinding = SqlDocumentEntityDataBindingDescriptor.cast(entityDescriptor.getDataBinding());
final int adClientId = UserSession.getCurrent().getAD_Client_ID();
final String tableName = dataBinding.getTableName();
final int nextId = DB.getNextID(adClientId, tableName, ITrx.TRXNAME_ThreadInherited);
if (nextId <= 0) {
throw new DBException("Cannot retrieve next ID from database for " + entityDescriptor);
}
logger.trace("Acquired next ID={} for {}", nextId, entityDescriptor);
return DocumentId.of(nextId);
}
Aggregations