Skip to content

Commit

Permalink
Provide API for sorting included tab
Browse files Browse the repository at this point in the history
  • Loading branch information
teosarca committed May 23, 2017
1 parent 41d7664 commit 7e6a612
Show file tree
Hide file tree
Showing 11 changed files with 160 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import de.metas.ui.web.window.model.IDocumentChangesCollector;
import de.metas.ui.web.window.model.IDocumentEvaluatee;
import de.metas.ui.web.window.model.IDocumentFieldView;
import de.metas.ui.web.window.model.OrderedDocumentsList;
import de.metas.ui.web.window.model.lookup.LookupValueByIdSupplier;

/*
Expand Down Expand Up @@ -62,7 +63,7 @@ private ADProcessParametersRepository()
}

@Override
public List<Document> retrieveDocuments(final DocumentQuery query, final IDocumentChangesCollector changesCollector)
public OrderedDocumentsList retrieveDocuments(final DocumentQuery query, final IDocumentChangesCollector changesCollector)
{
throw new UnsupportedOperationException();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
import de.metas.ui.web.window.exceptions.InvalidDocumentPathException;
import de.metas.ui.web.window.model.Document;
import de.metas.ui.web.window.model.DocumentCollection;
import de.metas.ui.web.window.model.DocumentQueryOrderBy;
import de.metas.ui.web.window.model.DocumentReference;
import de.metas.ui.web.window.model.DocumentReferencesService;
import de.metas.ui.web.window.model.IDocumentChangesCollector;
Expand Down Expand Up @@ -171,29 +172,30 @@ public ResponseEntity<JSONDocumentLayout> getLayout(

@GetMapping("/{windowId}/{documentId}")
public List<JSONDocument> getData(
@PathVariable("windowId") final String windowIdStr //
, @PathVariable("documentId") final String documentIdStr //
, @RequestParam(name = PARAM_FieldsList, required = false) @ApiParam("comma separated field names") final String fieldsListStr //
, @RequestParam(name = PARAM_Advanced, required = false, defaultValue = PARAM_Advanced_DefaultValue) final boolean advanced //
)
@PathVariable("windowId") final String windowIdStr,
@PathVariable("documentId") final String documentIdStr,
@RequestParam(name = PARAM_FieldsList, required = false) @ApiParam("comma separated field names") final String fieldsListStr,
@RequestParam(name = PARAM_Advanced, required = false, defaultValue = PARAM_Advanced_DefaultValue) final boolean advanced)
{
final WindowId windowId = WindowId.fromJson(windowIdStr);
final DocumentPath documentPath = DocumentPath.rootDocumentPath(windowId, documentIdStr);
return getData(documentPath, fieldsListStr, advanced);
final List<DocumentQueryOrderBy> orderBys = ImmutableList.of();
return getData(documentPath, fieldsListStr, advanced, orderBys);
}

@GetMapping("/{windowId}/{documentId}/{tabId}")
public List<JSONDocument> getData(
@PathVariable("windowId") final String windowIdStr //
, @PathVariable("documentId") final String documentIdStr //
, @PathVariable("tabId") final String tabIdStr //
, @RequestParam(name = PARAM_FieldsList, required = false) @ApiParam("comma separated field names") final String fieldsListStr //
, @RequestParam(name = PARAM_Advanced, required = false, defaultValue = PARAM_Advanced_DefaultValue) final boolean advanced //
)
@PathVariable("windowId") final String windowIdStr,
@PathVariable("documentId") final String documentIdStr,
@PathVariable("tabId") final String tabIdStr,
@RequestParam(name = PARAM_FieldsList, required = false) @ApiParam("comma separated field names") final String fieldsListStr,
@RequestParam(name = PARAM_Advanced, required = false, defaultValue = PARAM_Advanced_DefaultValue) final boolean advanced,
@RequestParam(name = "orderBy", required = false) final String orderBysListStr)
{
final WindowId windowId = WindowId.fromJson(windowIdStr);
final DocumentPath documentPath = DocumentPath.includedDocumentPath(windowId, documentIdStr, tabIdStr);
return getData(documentPath, fieldsListStr, advanced);
final List<DocumentQueryOrderBy> orderBys = DocumentQueryOrderBy.parseOrderBysList(orderBysListStr);
return getData(documentPath, fieldsListStr, advanced, orderBys);
}

@GetMapping("/{windowId}/{documentId}/{tabId}/{rowId}")
Expand All @@ -208,10 +210,11 @@ public List<JSONDocument> getData(
{
final WindowId windowId = WindowId.fromJson(windowIdStr);
final DocumentPath documentPath = DocumentPath.includedDocumentPath(windowId, documentIdStr, tabIdStr, rowIdStr);
return getData(documentPath, fieldsListStr, advanced);
final List<DocumentQueryOrderBy> orderBys = ImmutableList.of();
return getData(documentPath, fieldsListStr, advanced, orderBys);
}

private List<JSONDocument> getData(final DocumentPath documentPath, final String fieldsListStr, final boolean advanced)
private List<JSONDocument> getData(final DocumentPath documentPath, final String fieldsListStr, final boolean advanced, final List<DocumentQueryOrderBy> orderBys)
{
userSession.assertLoggedIn();

Expand All @@ -229,7 +232,7 @@ private List<JSONDocument> getData(final DocumentPath documentPath, final String
}
else if (documentPath.isAnyIncludedDocument())
{
documents = rootDocument.getIncludedDocuments(documentPath.getDetailId());
documents = rootDocument.getIncludedDocuments(documentPath.getDetailId(), orderBys).toList();
}
else if (documentPath.isSingleIncludedDocument())
{
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/de/metas/ui/web/window/model/Document.java
Original file line number Diff line number Diff line change
Expand Up @@ -1410,10 +1410,10 @@ public Document getIncludedDocument(final DetailId detailId, final DocumentId ro
return includedDocuments.getDocumentById(rowId);
}

public List<Document> getIncludedDocuments(final DetailId detailId)
public OrderedDocumentsList getIncludedDocuments(final DetailId detailId, final List<DocumentQueryOrderBy> orderBys)
{
final IIncludedDocumentsCollection includedDocuments = getIncludedDocumentsCollection(detailId);
return includedDocuments.getDocuments();
return includedDocuments.getDocuments(orderBys);
}

public void assertNewDocumentAllowed(final DetailId detailId)
Expand Down
15 changes: 14 additions & 1 deletion src/main/java/de/metas/ui/web/window/model/DocumentQuery.java
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ public Document retriveDocumentOrNull()
return documentsRepository.retrieveDocument(query, changesCollector);
}

public List<Document> retriveDocuments()
public OrderedDocumentsList retriveDocuments()
{
final DocumentQuery query = build();
final DocumentsRepository documentsRepository = getDocumentsRepository();
Expand Down Expand Up @@ -291,6 +291,19 @@ public Builder addOrderBy(final DocumentQueryOrderBy orderBy)
_orderBys.add(orderBy);
return this;
}

public Builder setOrderBys(final List<DocumentQueryOrderBy> orderBys)
{
if(orderBys == null || orderBys.isEmpty())
{
_orderBys = null;
}
else
{
_orderBys = new ArrayList<>(orderBys);
}
return this;
}

private List<DocumentQueryOrderBy> getOrderBysEffective()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package de.metas.ui.web.window.model;

import java.util.List;

import de.metas.ui.web.window.datatypes.DocumentId;
import de.metas.ui.web.window.descriptor.DocumentEntityDescriptor;

Expand Down Expand Up @@ -29,7 +27,7 @@

public interface DocumentsRepository
{
List<Document> retrieveDocuments(DocumentQuery query, IDocumentChangesCollector changesCollector);
OrderedDocumentsList retrieveDocuments(DocumentQuery query, IDocumentChangesCollector changesCollector);

/** @return document or null */
Document retrieveDocument(DocumentQuery query, IDocumentChangesCollector changesCollector);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import org.slf4j.Logger;

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

import de.metas.logging.LogManager;
Expand Down Expand Up @@ -160,23 +159,21 @@ public IIncludedDocumentsCollection copy(final Document parentDocumentCopy, fina
}

@Override
public List<Document> getDocuments()
public OrderedDocumentsList getDocuments(final List<DocumentQueryOrderBy> orderBys)
{
final Map<DocumentId, Document> documentsWithChanges = new LinkedHashMap<>(getInnerDocumentsWithChanges());
List<Document> documents = DocumentQuery.builder(entityDescriptor)
OrderedDocumentsList documents = DocumentQuery.builder(entityDescriptor)
.setParentDocument(parentDocument)
.setExistingDocumentsSupplier(documentsWithChanges::remove)
.setChangesCollector(NullDocumentChangesCollector.instance)
.setOrderBys(orderBys)
.retriveDocuments();

// Add the remaining documents with changes if any
// i.e. those documents which are new and never saved in database.
if (!documentsWithChanges.isEmpty())
{
documents = ImmutableList.<Document> builder()
.addAll(documents)
.addAll(documentsWithChanges.values())
.build();
documents.addDocuments(documentsWithChanges.values());
}

staled = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,12 @@ public DetailId getDetailId()
}

@Override
public List<Document> getDocuments()
public OrderedDocumentsList getDocuments(final List<DocumentQueryOrderBy> orderBys)
{
return DocumentQuery.builder(entityDescriptor)
.setParentDocument(parentDocument)
.setChangesCollector(NullDocumentChangesCollector.instance)
.setOrderBys(orderBys)
.retriveDocuments();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public interface IIncludedDocumentsCollection

IIncludedDocumentsCollection copy(Document parentDocumentCopy, CopyMode copyMode);

List<Document> getDocuments();
OrderedDocumentsList getDocuments(List<DocumentQueryOrderBy> orderBys);

Document getDocumentById(DocumentId documentId);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -271,9 +271,10 @@ private void refreshStaleDocumentIfPossible(final Document document)
}

@Override
public synchronized List<Document> getDocuments()
public synchronized OrderedDocumentsList getDocuments(final List<DocumentQueryOrderBy> orderBys)
{
return ImmutableList.copyOf(getInnerDocumentsFullyLoaded());
// TODO: use orderBys
return OrderedDocumentsList.of(getInnerDocumentsFullyLoaded(), ImmutableList.of());
}

/**
Expand Down Expand Up @@ -364,7 +365,7 @@ private final void loadAll()
{
//
// Retrieve the documents from repository
final List<Document> documentsNew = DocumentQuery.builder(entityDescriptor)
final OrderedDocumentsList documentsNew = DocumentQuery.builder(entityDescriptor)
.setParentDocument(parentDocument)
.retriveDocuments();

Expand All @@ -391,7 +392,7 @@ private final void loadAll()

//
// Put the new documents(from repository) into our documents map
for (final Document document : documentsNew)
for (final Document document : documentsNew.toList())
{
final DocumentId documentId = document.getDocumentId();
final Document documentExisting = documents.put(documentId, document);
Expand Down
103 changes: 103 additions & 0 deletions src/main/java/de/metas/ui/web/window/model/OrderedDocumentsList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package de.metas.ui.web.window.model;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import com.google.common.collect.ImmutableList;

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%
*/

/**
* Mutable ordered documents list.
*
* It also contains {@link #getOrderBys()}.
*
* @author metas-dev <dev@metasfresh.com>
*
*/
@ToString
public final class OrderedDocumentsList
{
public static final OrderedDocumentsList of(final Collection<Document> documents, final List<DocumentQueryOrderBy> orderBys)
{
return new OrderedDocumentsList(documents, orderBys);
}

public static final OrderedDocumentsList newEmpty(final List<DocumentQueryOrderBy> orderBys)
{
return new OrderedDocumentsList(ImmutableList.of(), orderBys);
}

private final List<Document> documents;
private final List<DocumentQueryOrderBy> orderBys;

private OrderedDocumentsList(final Collection<Document> documents, final List<DocumentQueryOrderBy> orderBys)
{
this.documents = documents == null ? new ArrayList<>() : new ArrayList<>(documents);
this.orderBys = ImmutableList.copyOf(orderBys);
}

public List<Document> toList()
{
return documents;
}

public void addDocument(@NonNull final Document document)
{
documents.add(document);
}

public void addDocuments(@NonNull final Collection<Document> documents)
{
if (documents.isEmpty())
{
return;
}

documents.forEach(this::addDocument);
}

public int size()
{
return documents.size();
}

public boolean isEmpty()
{
return documents.isEmpty();
}

public Document get(final int index)
{
return documents.get(index);
}

public List<DocumentQueryOrderBy> getOrderBys()
{
return orderBys;
}
}
Loading

0 comments on commit 7e6a612

Please sign in to comment.