Skip to content

Commit

Permalink
WEBUI_SalesOrder_CreatePurchaseOLCands_Launcher (WIP)
Browse files Browse the repository at this point in the history
  • Loading branch information
teosarca committed Nov 10, 2017
1 parent c5ccf24 commit 5afb3ed
Show file tree
Hide file tree
Showing 7 changed files with 861 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package de.metas.ui.web.order.purchase.process;

import java.util.Set;

import org.adempiere.exceptions.AdempiereException;
import org.adempiere.util.lang.impl.TableRecordReference;
import org.compiere.model.I_C_OrderLine;

import com.google.common.collect.ImmutableSet;

import de.metas.adempiere.model.I_C_Order;
import de.metas.process.IProcessPrecondition;
import de.metas.process.IProcessPreconditionsContext;
import de.metas.process.JavaProcess;
import de.metas.process.ProcessPreconditionsResolution;
import de.metas.ui.web.order.purchase.view.SalesOrderToOLCandViewFactory;

/*
* #%L
* metasfresh-webui-api
* %%
* Copyright (C) 2017 metas GmbH
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/gpl-2.0.html>.
* #L%
*/

public class WEBUI_SalesOrder_CreatePurchaseOLCands_Launcher extends JavaProcess implements IProcessPrecondition
{
@Override
public ProcessPreconditionsResolution checkPreconditionsApplicable(final IProcessPreconditionsContext context)
{
if (!context.isSingleSelection())
{
return ProcessPreconditionsResolution.rejectWithInternalReason("one and only one order shall be selected");
}

// Only draft orders
final I_C_Order order = context.getSelectedModel(I_C_Order.class);
if (order.isProcessed())
{
return ProcessPreconditionsResolution.rejectWithInternalReason("only draft orders are allowed");
}

// Only sales orders
if (!order.isSOTrx())
{
return ProcessPreconditionsResolution.rejectWithInternalReason("only sales orders are allowed");
}

// At least one sales order line shall be selected
final Set<TableRecordReference> selectedOrderLineRefs = context.getSelectedIncludedRecords();
if (selectedOrderLineRefs.isEmpty())
{
return ProcessPreconditionsResolution.rejectBecauseNoSelection();
}

return ProcessPreconditionsResolution.accept();
}

@Override
protected String doIt()
{
final Set<TableRecordReference> salesOrderLineRefs = getSelectedIncludedRecordIds(I_C_OrderLine.class)
.stream()
.map(recordId -> TableRecordReference.of(I_C_OrderLine.Table_Name, recordId))
.collect(ImmutableSet.toImmutableSet());
if (salesOrderLineRefs.isEmpty())
{
throw new AdempiereException("@NoSelection@");
}

getResult().setRecordsToOpen(salesOrderLineRefs, SalesOrderToOLCandViewFactory.WINDOW_ID_STRING);

return MSG_OK;
}

}
142 changes: 142 additions & 0 deletions src/main/java/de/metas/ui/web/order/purchase/view/OLCandRow.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
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 com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;

import de.metas.ui.web.view.IViewRow;
import de.metas.ui.web.view.IViewRowType;
import de.metas.ui.web.view.ViewRow.DefaultRowType;
import de.metas.ui.web.view.descriptor.annotation.ViewColumn;
import de.metas.ui.web.view.descriptor.annotation.ViewColumn.ViewColumnLayout;
import de.metas.ui.web.view.descriptor.annotation.ViewColumnHelper;
import de.metas.ui.web.view.json.JSONViewDataType;
import de.metas.ui.web.window.datatypes.DocumentId;
import de.metas.ui.web.window.datatypes.DocumentPath;
import de.metas.ui.web.window.datatypes.json.JSONLookupValue;
import de.metas.ui.web.window.descriptor.DocumentFieldWidgetType;
import de.metas.ui.web.window.descriptor.ViewEditorRenderMode;
import lombok.Builder;
import lombok.NonNull;
import lombok.ToString;

/*
* #%L
* metasfresh-webui-api
* %%
* Copyright (C) 2017 metas GmbH
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/gpl-2.0.html>.
* #L%
*/

@ToString(exclude = "_fieldNameAndJsonValues")
public class OLCandRow implements IViewRow
{
private final DocumentId rowId;

@ViewColumn(captionKey = "M_Product_ID", widgetType = DocumentFieldWidgetType.Lookup, layouts = {
@ViewColumnLayout(when = JSONViewDataType.grid, seqNo = 10),
@ViewColumnLayout(when = JSONViewDataType.includedView, seqNo = 10)
})
private final JSONLookupValue product;
@ViewColumn(captionKey = "Vendor_ID", widgetType = DocumentFieldWidgetType.Lookup, layouts = {
@ViewColumnLayout(when = JSONViewDataType.grid, seqNo = 20),
@ViewColumnLayout(when = JSONViewDataType.includedView, seqNo = 20)
})
private final JSONLookupValue vendorBPartner;

@ViewColumn(captionKey = "QtyToDeliver", widgetType = DocumentFieldWidgetType.Quantity, layouts = {
@ViewColumnLayout(when = JSONViewDataType.grid, seqNo = 30),
@ViewColumnLayout(when = JSONViewDataType.includedView, seqNo = 30)
})
private final BigDecimal qtyToDeliver;

@ViewColumn(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 = {
@ViewColumnLayout(when = JSONViewDataType.grid, seqNo = 50),
@ViewColumnLayout(when = JSONViewDataType.includedView, seqNo = 50)
})
private final Date datePromised;

private transient ImmutableMap<String, Object> _fieldNameAndJsonValues;

@Builder
public OLCandRow(
@NonNull final DocumentId rowId,
@NonNull final JSONLookupValue product,
@NonNull final JSONLookupValue vendorBPartner,
@NonNull final BigDecimal qtyToDeliver,
@NonNull final BigDecimal qtyToPurchase,
@NonNull final Date datePromised)
{
this.rowId = rowId;
this.product = product;
this.vendorBPartner = vendorBPartner;
this.qtyToDeliver = qtyToDeliver;
this.qtyToPurchase = qtyToPurchase;
this.datePromised = datePromised;
}

@Override
public DocumentId getId()
{
return rowId;
}

@Override
public IViewRowType getType()
{
return DefaultRowType.Row;
}

@Override
public boolean isProcessed()
{
return false;
}

@Override
public DocumentPath getDocumentPath()
{
// TODO Auto-generated method stub
return null;
}

@Override
public Map<String, Object> getFieldNameAndJsonValues()
{
if (_fieldNameAndJsonValues == null)
{
_fieldNameAndJsonValues = ViewColumnHelper.extractJsonMap(this);
}
return _fieldNameAndJsonValues;
}

@Override
public List<? extends IViewRow> getIncludedRows()
{
return ImmutableList.of();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
package de.metas.ui.web.order.purchase.view;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Stream;

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

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.view.IViewRow;
import de.metas.ui.web.window.datatypes.DocumentId;
import de.metas.ui.web.window.datatypes.DocumentIdsSelection;
import lombok.NonNull;

/*
* #%L
* metasfresh-webui-api
* %%
* Copyright (C) 2017 metas GmbH
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/gpl-2.0.html>.
* #L%
*/

class OLCandRowsCollection
{
private static final int DEFAULT_PAGE_LENGTH = 30;

public static final OLCandRowsCollection ofSupplier(final OLCandRowsSupplier rowsSupplier)
{
return new OLCandRowsCollection(rowsSupplier);
}

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

private OLCandRowsCollection(@NonNull final OLCandRowsSupplier rowsSupplier)
{
this.rowsSupplier = ExtendedMemorizingSupplier.of(() -> Maps.uniqueIndex(rowsSupplier.retrieveRows(), OLCandRow::getId));
}

@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();
}

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

public List<OLCandRow> getPage(final int firstRow, final int pageLength)
{
return getRowsMap().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);
if (row == null)
{
throw new EntityNotFoundException("Row not found").setParameter("rowId", rowId);
}
return row;
}

public Stream<? extends IViewRow> streamByIds(final DocumentIdsSelection rowIds)
{
if (rowIds.isAll())
{
return getRowsMap().values().stream();
}
else
{
return rowIds.stream().map(this::getById);
}
}

public boolean notifyRecordsChanged(Set<TableRecordReference> recordRefs)
{
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
{
return false;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package de.metas.ui.web.order.purchase.view;

import java.util.List;

/*
* #%L
* metasfresh-webui-api
* %%
* Copyright (C) 2017 metas GmbH
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/gpl-2.0.html>.
* #L%
*/

@FunctionalInterface
public interface OLCandRowsSupplier
{
List<OLCandRow> retrieveRows();
}
Loading

0 comments on commit 5afb3ed

Please sign in to comment.