Skip to content

Commit

Permalink
OLCandView: implement editable support
Browse files Browse the repository at this point in the history
  • Loading branch information
teosarca committed Nov 13, 2017
1 parent 0a2489a commit 8123035
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,21 +68,23 @@ public class OLCandRow implements IViewRow
})
private final BigDecimal qtyToDeliver;

@ViewColumn(captionKey = "QtyToPurchase", widgetType = DocumentFieldWidgetType.Quantity, editor = ViewEditorRenderMode.ALWAYS, layouts = {
public static final String FIELDNAME_QtyToPurchase = "qtyToPurchase";
@ViewColumn(fieldName = FIELDNAME_QtyToPurchase, captionKey = "QtyToPurchase", widgetType = DocumentFieldWidgetType.Quantity, editor = ViewEditorRenderMode.ALWAYS, layouts = {
@ViewColumnLayout(when = JSONViewDataType.grid, seqNo = 40),
@ViewColumnLayout(when = JSONViewDataType.includedView, seqNo = 40)
})
private final BigDecimal qtyToPurchase;

@ViewColumn(captionKey = "DatePromised", widgetType = DocumentFieldWidgetType.DateTime, editor = ViewEditorRenderMode.ALWAYS, layouts = {
public static final String FIELDNAME_DatePromised = "datePromised";
@ViewColumn(fieldName = FIELDNAME_DatePromised, captionKey = "DatePromised", widgetType = DocumentFieldWidgetType.DateTime, editor = ViewEditorRenderMode.ALWAYS, layouts = {
@ViewColumnLayout(when = JSONViewDataType.grid, seqNo = 50),
@ViewColumnLayout(when = JSONViewDataType.includedView, seqNo = 50)
})
private final Date datePromised;

private transient ImmutableMap<String, Object> _fieldNameAndJsonValues;

@Builder
@Builder(toBuilder = true)
public OLCandRow(
@NonNull final DocumentId rowId,
@NonNull final JSONLookupValue product,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
package de.metas.ui.web.order.purchase.view;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentMap;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.adempiere.util.lang.ExtendedMemorizingSupplier;
import org.adempiere.util.lang.impl.TableRecordReference;
import org.adempiere.exceptions.AdempiereException;

import com.google.common.base.MoreObjects;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Maps;

import de.metas.handlingunits.model.I_C_OLCand;
import de.metas.ui.web.exceptions.EntityNotFoundException;
import de.metas.ui.web.order.purchase.view.OLCandRow.OLCandRowBuilder;
import de.metas.ui.web.view.IViewRow;
import de.metas.ui.web.window.datatypes.DocumentId;
import de.metas.ui.web.window.datatypes.DocumentIdsSelection;
import de.metas.ui.web.window.datatypes.json.JSONDocumentChangedEvent;
import lombok.NonNull;

/*
Expand Down Expand Up @@ -50,45 +52,37 @@ public static final OLCandRowsCollection ofSupplier(final OLCandRowsSupplier row
return new OLCandRowsCollection(rowsSupplier);
}

private final ExtendedMemorizingSupplier<Map<DocumentId, OLCandRow>> rowsSupplier;
private final ConcurrentMap<DocumentId, OLCandRow> rowsById;

private OLCandRowsCollection(@NonNull final OLCandRowsSupplier rowsSupplier)
{
this.rowsSupplier = ExtendedMemorizingSupplier.of(() -> Maps.uniqueIndex(rowsSupplier.retrieveRows(), OLCandRow::getId));
rowsById = rowsSupplier.retrieveRows()
.stream()
.collect(Collectors.toConcurrentMap(OLCandRow::getId, Function.identity()));
}

@Override
public String toString()
{
return MoreObjects.toStringHelper(this).addValue(rowsSupplier).toString();
}

public void invalidateAll()
{
rowsSupplier.forget();
}

private final Map<DocumentId, OLCandRow> getRowsMap()
{
return rowsSupplier.get();
return MoreObjects.toStringHelper(this).addValue(rowsById).toString();
}

public long size()
{
return getRowsMap().size();
return rowsById.size();
}

public List<OLCandRow> getPage(final int firstRow, final int pageLength)
{
return getRowsMap().values().stream()
return rowsById.values().stream()
.skip(firstRow >= 0 ? firstRow : 0)
.limit(pageLength > 0 ? pageLength : DEFAULT_PAGE_LENGTH)
.collect(ImmutableList.toImmutableList());
}

public OLCandRow getById(@NonNull final DocumentId rowId) throws EntityNotFoundException
{
final OLCandRow row = getRowsMap().get(rowId);
final OLCandRow row = rowsById.get(rowId);
if (row == null)
{
throw new EntityNotFoundException("Row not found").setParameter("rowId", rowId);
Expand All @@ -100,31 +94,52 @@ public Stream<? extends IViewRow> streamByIds(final DocumentIdsSelection rowIds)
{
if (rowIds.isAll())
{
return getRowsMap().values().stream();
return rowsById.values().stream();
}
else
{
return rowIds.stream().map(this::getById);
}
}

public boolean notifyRecordsChanged(Set<TableRecordReference> recordRefs)
private void updateRow(@NonNull final DocumentId rowId, @NonNull final Function<OLCandRow, OLCandRow> mapper)
{
final Set<DocumentId> rowIds = getRowsMap().keySet();

final boolean matches = recordRefs.stream()
.filter(record -> I_C_OLCand.Table_Name.equals(record.getTableName()))
.map(record -> DocumentId.of(record.getRecord_ID()))
.anyMatch(rowIds::contains);

if(matches)
{
invalidateAll();
return true;
}
else
rowsById.compute(rowId, (k, existingRow) -> {
if (existingRow == null)
{
throw new EntityNotFoundException("Row not found").setParameter("rowId", rowId);
}
return mapper.apply(existingRow);
});
}

public void patchRow(final DocumentId rowId, final List<JSONDocumentChangedEvent> fieldChangeRequests)
{
updateRow(rowId, row -> patchRow(row, fieldChangeRequests));
}

private OLCandRow patchRow(final OLCandRow row, final List<JSONDocumentChangedEvent> fieldChangeRequests)
{
final OLCandRowBuilder newRowBuilder = row.toBuilder();
for (final JSONDocumentChangedEvent fieldChangeRequest : fieldChangeRequests)
{
return false;
final String fieldName = fieldChangeRequest.getPath();
if (OLCandRow.FIELDNAME_QtyToPurchase.equals(fieldName))
{
final BigDecimal qtyToPurchase = fieldChangeRequest.getValueAsBigDecimal();
newRowBuilder.qtyToPurchase(qtyToPurchase);
}
else if (OLCandRow.FIELDNAME_DatePromised.equals(fieldName))
{
final Date datePromised = fieldChangeRequest.getValueAsDateTime();
newRowBuilder.datePromised(datePromised);
}
else
{
throw new AdempiereException("Field " + fieldName + " is not editable").setParameter("row", row);
}
}

return newRowBuilder.build();
}
}
36 changes: 23 additions & 13 deletions src/main/java/de/metas/ui/web/order/purchase/view/OLCandView.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,17 @@
import de.metas.ordercandidate.model.I_C_OLCand;
import de.metas.ui.web.document.filter.DocumentFilter;
import de.metas.ui.web.exceptions.EntityNotFoundException;
import de.metas.ui.web.view.IEditableView;
import de.metas.ui.web.view.IView;
import de.metas.ui.web.view.IViewRow;
import de.metas.ui.web.view.ViewId;
import de.metas.ui.web.view.ViewResult;
import de.metas.ui.web.view.event.ViewChangesCollector;
import de.metas.ui.web.view.json.JSONViewDataType;
import de.metas.ui.web.window.datatypes.DocumentId;
import de.metas.ui.web.window.datatypes.DocumentIdsSelection;
import de.metas.ui.web.window.datatypes.DocumentPath;
import de.metas.ui.web.window.datatypes.LookupValuesList;
import de.metas.ui.web.window.datatypes.json.JSONDocumentChangedEvent;
import de.metas.ui.web.window.model.DocumentQueryOrderBy;
import de.metas.ui.web.window.model.sql.SqlOptions;
import lombok.Builder;
Expand Down Expand Up @@ -51,9 +52,9 @@
* #L%
*/

public class OLCandView implements IView
public class OLCandView implements IEditableView
{
public static OLCandView cast(IView view)
public static OLCandView cast(final IView view)
{
return (OLCandView)view;
}
Expand Down Expand Up @@ -132,8 +133,6 @@ public boolean isQueryLimitHit()
@Override
public void invalidateAll()
{
rows.invalidateAll();
ViewChangesCollector.getCurrentOrAutoflush().collectFullyChanged(this);
}

@Override
Expand Down Expand Up @@ -186,8 +185,7 @@ public List<DocumentQueryOrderBy> getDefaultOrderBys()
@Override
public String getSqlWhereClause(final DocumentIdsSelection rowIds, final SqlOptions sqlOpts)
{
// TODO Auto-generated method stub
return null;
throw new UnsupportedOperationException();
}

@Override
Expand All @@ -205,11 +203,23 @@ public Stream<? extends IViewRow> streamByIds(final DocumentIdsSelection rowIds)
@Override
public void notifyRecordsChanged(final Set<TableRecordReference> recordRefs)
{
if (rows.notifyRecordsChanged(recordRefs))
{
// Collect event
// TODO: check which rowIds are contained in this view and fire events only for those
ViewChangesCollector.getCurrentOrAutoflush().collectFullyChanged(this);
}
}

@Override
public void patchViewRow(final RowEditingContext ctx, final List<JSONDocumentChangedEvent> fieldChangeRequests)
{
rows.patchRow(ctx.getRowId(), fieldChangeRequests);
}

@Override
public LookupValuesList getFieldTypeahead(final RowEditingContext ctx, final String fieldName, final String query)
{
throw new UnsupportedOperationException();
}

@Override
public LookupValuesList getFieldDropdown(final RowEditingContext ctx, final String fieldName)
{
throw new UnsupportedOperationException();
}
}

0 comments on commit 8123035

Please sign in to comment.