Skip to content

Commit

Permalink
more methods for manipulating ViewRowIdsOrderedSelection
Browse files Browse the repository at this point in the history
  • Loading branch information
teosarca committed May 3, 2017
1 parent 988d0ee commit 7ed0bdd
Show file tree
Hide file tree
Showing 7 changed files with 207 additions and 11 deletions.
30 changes: 30 additions & 0 deletions src/main/java/de/metas/ui/web/handlingunits/HUEditorRow.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.function.Supplier;

import javax.annotation.Nullable;
Expand All @@ -20,6 +22,7 @@

import de.metas.adempiere.model.I_M_Product;
import de.metas.handlingunits.model.I_M_HU;
import de.metas.handlingunits.model.I_M_HU_Storage;
import de.metas.handlingunits.model.X_M_HU;
import de.metas.handlingunits.storage.IHUProductStorage;
import de.metas.ui.web.exceptions.EntityNotFoundException;
Expand Down Expand Up @@ -71,6 +74,33 @@ public static final HUEditorRow cast(final IViewRow viewRow)
{
return (HUEditorRow)viewRow;
}

public static DocumentId rowIdFromM_HU_ID(final int huId)
{
return DocumentId.of(huId);
}

public static Set<DocumentId> rowIdsFromM_HU_IDs(final Collection<Integer> huIds)
{
return DocumentId.ofIntSet(huIds);
}


public static DocumentId rowIdFromM_HU_Storage(final int huId, final int productId)
{
return DocumentId.ofString(I_M_HU_Storage.Table_Name + "_HU" + huId + "_P" + productId);
}

public static int rowIdToM_HU_ID(final DocumentId rowId)
{
return rowId == null ? -1 : rowId.toInt();
}

public static Set<Integer> rowIdsToM_HU_IDs(final Collection<DocumentId> rowIds)
{
return DocumentId.toIntSet(rowIds);
}


private final DocumentPath documentPath;
private final DocumentId rowId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import de.metas.handlingunits.IHandlingUnitsDAO;
import de.metas.handlingunits.exceptions.HUException;
import de.metas.handlingunits.model.I_M_HU;
import de.metas.handlingunits.model.I_M_HU_Storage;
import de.metas.handlingunits.model.X_M_HU;
import de.metas.handlingunits.model.X_M_HU_PI_Version;
import de.metas.handlingunits.storage.IHUProductStorage;
Expand All @@ -31,7 +30,6 @@
import de.metas.logging.LogManager;
import de.metas.ui.web.handlingunits.util.HUPackingInfoFormatter;
import de.metas.ui.web.handlingunits.util.HUPackingInfos;
import de.metas.ui.web.window.datatypes.DocumentId;
import de.metas.ui.web.window.datatypes.WindowId;
import de.metas.ui.web.window.datatypes.json.JSONLookupValue;
import lombok.Builder;
Expand Down Expand Up @@ -148,7 +146,7 @@ private HUEditorRow createHUEditorRow(final I_M_HU hu)
final int huId = hu.getM_HU_ID();

final HUEditorRow.Builder huEditorRow = HUEditorRow.builder(windowId)
.setRowId(DocumentId.of(huId))
.setRowId(HUEditorRow.rowIdFromM_HU_ID(huId))
.setType(huRecordType)
.setAttributesProvider(attributesProvider)
.setProcessed(processed)
Expand Down Expand Up @@ -275,7 +273,7 @@ private HUEditorRow createHUEditorRow(final int parent_HU_ID, final IHUProductSt
final HUEditorRowAttributesProvider attributesProviderEffective = huId != parent_HU_ID ? attributesProvider : null;

return HUEditorRow.builder(windowId)
.setRowId(DocumentId.ofString(I_M_HU_Storage.Table_Name + "_HU" + huId + "_P" + product.getM_Product_ID()))
.setRowId(HUEditorRow.rowIdFromM_HU_Storage(huId, product.getM_Product_ID()))
.setType(HUEditorRowType.HUStorage)
.setProcessed(processed)
.setAttributesProvider(attributesProviderEffective)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,92 @@ public ViewRowIdsOrderedSelection createOrderedSelectionFromSelection(final View
.setQueryLimit(fromSelection.getQueryLimit())
.build();
}

@Override
public ViewRowIdsOrderedSelection addRowIdsToSelection(final ViewRowIdsOrderedSelection selection, final Collection<DocumentId> rowIds)
{
if (rowIds == null || rowIds.isEmpty())
{
// nothing changed
return selection;
}

//
// Add
boolean hasChanges = false;
final String selectionId = selection.getSelectionId();
for(final DocumentId rowId : rowIds)
{
final List<Object> sqlParams = new ArrayList<>();
final String sqlAdd = newSqlViewSelectionQueryBuilder().buildSqlAddRowIdsFromSelection(sqlParams, selectionId, rowId);
final int added = DB.executeUpdateEx(sqlAdd, sqlParams.toArray(), ITrx.TRXNAME_ThreadInherited);
if (added <= 0)
{
continue;
}

hasChanges = true;
}
if(!hasChanges)
{
// nothing changed
return selection;
}

//
// Retrieve current size
// NOTE: we are querying it instead of adding how many we added to current "size" because it might be that the size is staled
final int size = retrieveSize(selectionId);

return selection.toBuilder()
.setSize(size)
.build();
}

@Override
public ViewRowIdsOrderedSelection removeRowIdsFromSelection(final ViewRowIdsOrderedSelection selection, final Collection<DocumentId> rowIds)
{
if (rowIds == null || rowIds.isEmpty())
{
// nothing changed
return selection;
}

//
// Delete
{
final String sqlDelete = newSqlViewSelectionQueryBuilder().buildSqlDeleteRowIdsFromSelection(selection.getSelectionId(), rowIds);
final int deleted = DB.executeUpdateEx(sqlDelete, ITrx.TRXNAME_ThreadInherited);
if (deleted <= 0)
{
// nothing changed
return selection;
}
}

//
// Retrieve current size
// NOTE: we are querying it instead of subtracting "deleted" from current "size" because it might be that the size is staled
final int size = retrieveSize(selection.getSelectionId());

return selection.toBuilder()
.setSize(size)
.build();
}

private final int retrieveSize(final String selectionId)
{
final List<Object> sqlParams = new ArrayList<>();
final String sqlCount = newSqlViewSelectionQueryBuilder().buildSqlRetrieveSize(sqlParams, selectionId);
final int size = DB.getSQLValueEx(ITrx.TRXNAME_ThreadInherited, sqlCount, sqlParams);
return size <= 0 ? 0 : size;
}

public boolean containsAnyOfRowIds(ViewRowIdsOrderedSelection defaultSelection, final Collection<DocumentId> rowIds)
{
final List<Object> sqlParams = new ArrayList<>();
final String sqlCount = newSqlViewSelectionQueryBuilder().buildSqlCount(sqlParams, defaultSelection.getSelectionId(), rowIds);
int count = DB.executeUpdateEx(sqlCount, sqlParams.toArray(), ITrx.TRXNAME_ThreadInherited);
return count > 0;
}
}
11 changes: 11 additions & 0 deletions src/main/java/de/metas/ui/web/view/ViewRowIdsOrderedSelection.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

import de.metas.ui.web.window.datatypes.WindowId;
import de.metas.ui.web.window.model.DocumentQueryOrderBy;
import lombok.EqualsAndHashCode;

/*
* #%L
Expand All @@ -35,6 +36,7 @@
*/

@Immutable
@EqualsAndHashCode
public final class ViewRowIdsOrderedSelection
{
public static final Builder builder()
Expand Down Expand Up @@ -70,6 +72,15 @@ public String toString()
.add("orderBys", orderBys.isEmpty() ? null : orderBys)
.toString();
}

public Builder toBuilder()
{
return builder()
.setViewId(viewId)
.setSize(size)
.setOrderBys(orderBys)
.setQueryLimit(queryLimit);
}

public ViewId getViewId()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ public interface ViewRowIdsOrderedSelectionFactory
ViewRowIdsOrderedSelection createOrderedSelectionFromSelection(ViewEvaluationCtx viewEvalCtx, ViewRowIdsOrderedSelection fromSelection, List<DocumentQueryOrderBy> orderBys);

String getSqlWhereClause(ViewId viewId, Collection<DocumentId> rowIds);

ViewRowIdsOrderedSelection addRowIdsToSelection(ViewRowIdsOrderedSelection selection, Collection<DocumentId> rowIds);

ViewRowIdsOrderedSelection removeRowIdsFromSelection(ViewRowIdsOrderedSelection selection, Collection<DocumentId> rowIds);


}
12 changes: 10 additions & 2 deletions src/main/java/de/metas/ui/web/view/descriptor/SqlViewBinding.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.adempiere.ad.expression.api.IExpressionEvaluator.OnVariableNotFound;
import org.adempiere.ad.expression.api.IStringExpression;
import org.adempiere.ad.expression.api.impl.CompositeStringExpression;
import org.adempiere.ad.expression.api.impl.ConstantStringExpression;
import org.adempiere.util.Check;

import com.google.common.base.Joiner;
Expand Down Expand Up @@ -286,7 +287,7 @@ public static final class Builder
{
private String _sqlTableName;
private String _tableAlias;
private IStringExpression sqlWhereClause;
private IStringExpression sqlWhereClause = IStringExpression.NULL;

private Collection<String> displayFieldNames;
private final Map<String, SqlViewRowFieldBinding> _fieldsByFieldName = new LinkedHashMap<>();
Expand Down Expand Up @@ -333,9 +334,16 @@ private String getTableAlias()

public Builder setSqlWhereClause(final IStringExpression sqlWhereClause)
{
this.sqlWhereClause = sqlWhereClause;
this.sqlWhereClause = sqlWhereClause == null ? IStringExpression.NULL : sqlWhereClause;
return this;
}

public Builder setSqlWhereClause(final String sqlWhereClause)
{
this.sqlWhereClause = ConstantStringExpression.ofNullable(sqlWhereClause);
return this;
}


private IStringExpression getSqlWhereClause()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
*
* @see I_T_WEBUI_ViewSelection
*/
// TODO: used only in one place.. consider removing it from here
public final class SqlViewSelectionQueryBuilder
{
public static final SqlViewSelectionQueryBuilder newInstance(final SqlEntityBinding entityBinding)
Expand Down Expand Up @@ -81,7 +82,7 @@ public String getKeyColumnName()
{
return entityBinding.getKeyColumnName();
}

public IStringExpression getSqlWhereClause()
{
return entityBinding.getSqlWhereClause();
Expand Down Expand Up @@ -142,7 +143,7 @@ public String buildSqlCreateSelectionFrom( //
final List<Object> sqlWhereClauseParams = new ArrayList<>();
final IStringExpression sqlWhereClause = buildSqlWhereClause(sqlWhereClauseParams, filters);

if(sqlWhereClause != null && !sqlWhereClause.isNullExpression())
if (sqlWhereClause != null && !sqlWhereClause.isNullExpression())
{
sqlBuilder.append("\n AND (\n").append(sqlWhereClause).append("\n)");
sqlParams.addAll(sqlWhereClauseParams);
Expand All @@ -162,7 +163,7 @@ public String buildSqlCreateSelectionFrom( //
final String sql = sqlBuilder.build().evaluate(viewEvalCtx.toEvaluatee(), OnVariableNotFound.Fail);
return sql;
}

private final IStringExpression buildSqlWhereClause(final List<Object> sqlParams, final List<DocumentFilter> filters)
{
final CompositeStringExpression.Builder sqlWhereClauseBuilder = IStringExpression.composer();
Expand All @@ -177,14 +178,14 @@ private final IStringExpression buildSqlWhereClause(final List<Object> sqlParams
sqlWhereClauseBuilder.append(" /* entity where clause */ (").append(entityWhereClauseExpression).append(")");
}
}

//
// Document filters
{
final String sqlFilters = SqlDocumentFiltersBuilder.newInstance(entityBinding)
.addFilters(filters)
.buildSqlWhereClause(sqlParams);
if(!Check.isEmpty(sqlFilters, true))
if (!Check.isEmpty(sqlFilters, true))
{
sqlWhereClauseBuilder.appendIfNotEmpty("\n AND ");
sqlWhereClauseBuilder.append(" /* filters */ (\n").append(sqlFilters).append(")\n");
Expand Down Expand Up @@ -254,4 +255,60 @@ public String buildSqlWhereClause(final String selectionId, final Collection<Doc

}

public String buildSqlDeleteRowIdsFromSelection(final String selectionId, final Collection<DocumentId> rowIds)
{
final Set<Integer> rowIdsAsInts = DocumentId.toIntSet(rowIds);
if (rowIdsAsInts.isEmpty())
{
return null;
}

return "DELETE FROM " + I_T_WEBUI_ViewSelection.Table_Name
+ " WHERE " + I_T_WEBUI_ViewSelection.COLUMNNAME_UUID + "=" + DB.TO_STRING(selectionId)
+ " AND " + I_T_WEBUI_ViewSelection.COLUMNNAME_Record_ID + " IN " + DB.buildSqlList(rowIdsAsInts);
}

public String buildSqlAddRowIdsFromSelection(final List<Object> sqlParams, final String selectionId, final DocumentId rowId)
{
final int rowIdInt = rowId.toInt();
final String sql = "INSERT INTO " + I_T_WEBUI_ViewSelection.Table_Name + " ("
+ " " + I_T_WEBUI_ViewSelection.COLUMNNAME_UUID
+ ", " + I_T_WEBUI_ViewSelection.COLUMNNAME_Line
+ ", " + I_T_WEBUI_ViewSelection.COLUMNNAME_Record_ID
+ ")"
+ " SELECT "
+ " ? as UUID " // UUID
+ ", (select max(Line) from T_WEBUI_ViewSelection z where z.UUID=?) as Line" // Line
+ ", ? as Record_ID" // Record_ID
+ " WHERE "
+ " NOT EXISTS(select 1 from " + I_T_WEBUI_ViewSelection.Table_Name + " z where z.UUID=? and z.Record_ID=?)";
// TODO: we should also validate if the rowId is allowed to be part of this selection (e.g. enforce entity binding's SQL where clause)

sqlParams.add(selectionId); // UUID
sqlParams.add(selectionId); // for Line
sqlParams.add(rowIdInt); // Record_ID
sqlParams.add(selectionId); // for NOT EXISTS
sqlParams.add(rowIdInt); // for NOT EXISTS

return sql;
}

public String buildSqlRetrieveSize(final List<Object> sqlParams, final String selectionId)
{
Check.assumeNotEmpty(selectionId, "selectionId is not empty");
sqlParams.add(selectionId);
return "SELECT COUNT(1) FROM " + I_T_WEBUI_ViewSelection.Table_Name + " WHERE " + I_T_WEBUI_ViewSelection.COLUMNNAME_UUID + "=?";
}

public String buildSqlCount(final List<Object> sqlParams, final String selectionId, final Collection<DocumentId> rowIds)
{
Check.assumeNotEmpty(selectionId, "selectionId is not empty");
final Set<Integer> recordIds = DocumentId.toIntSet(rowIds);
Check.assumeNotEmpty(recordIds, "recordIds is not empty");

sqlParams.add(selectionId);
return "SELECT COUNT(1) FROM " + I_T_WEBUI_ViewSelection.Table_Name + " WHERE " + I_T_WEBUI_ViewSelection.COLUMNNAME_UUID + "=?"
+ " AND " + DB.buildSqlList(I_T_WEBUI_ViewSelection.COLUMNNAME_Record_ID, rowIds, sqlParams);
}

}

0 comments on commit 7ed0bdd

Please sign in to comment.