Skip to content

Commit

Permalink
{viewId}/export/excel?selectedIds=...
Browse files Browse the repository at this point in the history
  • Loading branch information
teosarca committed Sep 13, 2017
1 parent 5a614fd commit 98f3a4c
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 11 deletions.
57 changes: 47 additions & 10 deletions src/main/java/de/metas/ui/web/view/ViewExcelExporter.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@
import java.util.List;
import java.util.Set;

import org.adempiere.exceptions.AdempiereException;
import org.adempiere.impexp.AbstractExcelExporter;

import com.google.common.collect.ImmutableList;

import de.metas.ui.web.view.descriptor.ViewLayout;
import de.metas.ui.web.window.datatypes.DocumentId;
import de.metas.ui.web.window.datatypes.DocumentIdsSelection;
import de.metas.ui.web.window.datatypes.json.JSONDate;
import de.metas.ui.web.window.datatypes.json.JSONLookupValue;
import de.metas.ui.web.window.descriptor.DocumentFieldWidgetType;
Expand Down Expand Up @@ -43,31 +46,58 @@
private final IView view;
private final ViewLayout layout;
private final String adLanguage;
/** Selected rowIds. Might be null in case ALL rows shall be exported. */
private final List<DocumentId> rowIds;

@Builder
private ViewExcelExporter(@NonNull final IView view, @NonNull final ViewLayout layout, @NonNull final String adLanguage)
private ViewExcelExporter(
@NonNull final IView view,
@NonNull final DocumentIdsSelection rowIds,
@NonNull final ViewLayout layout,
@NonNull final String adLanguage)
{
this.view = view;
this.layout = layout;
this.adLanguage = adLanguage;

if (rowIds.isAll())
{
this.rowIds = null;
}
else if (rowIds.isEmpty())
{
throw new AdempiereException("@NoSelection@");
}
else
{
this.rowIds = ImmutableList.copyOf(rowIds.toSet());
}

setFreezePane(0, 1);
}

private IViewRow getRow(final int rowIndex)
{
final int firstRow = rowIndex;
final int pageLength = 1;
final List<DocumentQueryOrderBy> orderBys = ImmutableList.of(); // default
final ViewResult page = view.getPage(firstRow, pageLength, orderBys);
final List<IViewRow> rows = page.getPage();
if (rows.isEmpty())
if (rowIds == null) // All rows
{
return null;
final int firstRow = rowIndex;
final int pageLength = 1;
final List<DocumentQueryOrderBy> orderBys = ImmutableList.of(); // default
final ViewResult page = view.getPage(firstRow, pageLength, orderBys);
final List<IViewRow> rows = page.getPage();
if (rows.isEmpty())
{
return null;
}
else
{
return rows.get(0);
}
}
else
{
return rows.get(0);
final DocumentId rowId = rowIds.get(rowIndex);
return view.getById(rowId);
}
}

Expand Down Expand Up @@ -97,7 +127,14 @@ public int getColumnCount()
@Override
public int getRowCount()
{
return (int)view.size();
if (rowIds == null)
{
return (int)view.size();
}
else
{
return rowIds.size();
}
}

@Override
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/de/metas/ui/web/view/ViewRestController.java
Original file line number Diff line number Diff line change
Expand Up @@ -356,14 +356,17 @@ public JSONZoomInto getRowFieldZoomInto(
@GetMapping("/{viewId}/export/excel")
public ResponseEntity<Resource> exportToExcel(
@PathVariable("windowId") final String windowIdStr,
@PathVariable("viewId") final String viewIdStr) throws Exception
@PathVariable("viewId") final String viewIdStr,
@RequestParam(name = "selectedIds", required = false) @ApiParam("comma separated IDs") final String selectedIdsListStr)
throws Exception
{
userSession.assertLoggedIn();

final ViewId viewId = ViewId.ofViewIdString(viewIdStr, WindowId.fromJson(windowIdStr));

final ViewExcelExporter viewExporter = ViewExcelExporter.builder()
.view(viewsRepo.getView(viewId))
.rowIds(DocumentIdsSelection.ofCommaSeparatedString(selectedIdsListStr))
.layout(viewsRepo.getViewLayout(viewId.getWindowId(), JSONViewDataType.grid))
.adLanguage(userSession.getAD_Language())
.build();
Expand Down

0 comments on commit 98f3a4c

Please sign in to comment.