diff --git a/src/main/java/biz/nynja/account/grid/ag/AdminServiceImpl.java b/src/main/java/biz/nynja/account/grid/ag/AdminServiceImpl.java new file mode 100644 index 0000000000000000000000000000000000000000..211b1fcbcadbe174ed03f458994b36e3deffc77b --- /dev/null +++ b/src/main/java/biz/nynja/account/grid/ag/AdminServiceImpl.java @@ -0,0 +1,48 @@ +package biz.nynja.account.grid.ag; + +import org.lognet.springboot.grpc.GRpcService; +import org.springframework.beans.factory.annotation.Autowired; + +import biz.nynja.account.admin.grpc.AccountsAdminResponse; +import biz.nynja.account.admin.grpc.AccountsCount; +import biz.nynja.account.admin.grpc.AdminAccountServiceGrpc; +import biz.nynja.account.admin.grpc.EmptyRequest; +import biz.nynja.account.admin.grpc.GetAllAccountsRequest; +import biz.nynja.account.repositories.AccountRepository; +import io.grpc.stub.StreamObserver; + +/** + * gRPC Admin service implementation.
+ * The service extends the protobuf generated class and overrides the needed methods. It also saves/retrieves the admin + * information. + */ +@GRpcService +public class AdminServiceImpl extends AdminAccountServiceGrpc.AdminAccountServiceImplBase { + + private AgGridService agGridService; + + private AccountRepository accountRepository; + + @Autowired + public AdminServiceImpl(AgGridService agGridService, AccountRepository accountRepository) { + this.agGridService = agGridService; + this.accountRepository = accountRepository; + } + + @Override + public void getAllAccounts(GetAllAccountsRequest request, StreamObserver responseObserver) { + + AccountsAdminResponse response = agGridService.getData(request.getEndRow(), request.getStartRow()); + + responseObserver.onNext(response); + responseObserver.onCompleted(); + return; + } + + @Override + public void getCountOfAllAccounts(EmptyRequest request, StreamObserver responseObserver) { + long count = accountRepository.count(); + responseObserver.onNext(AccountsCount.newBuilder().setCount(Math.toIntExact(count)).build()); + responseObserver.onCompleted(); + } +} diff --git a/src/main/java/biz/nynja/account/grid/ag/AgGridController.java b/src/main/java/biz/nynja/account/grid/ag/AgGridController.java new file mode 100644 index 0000000000000000000000000000000000000000..f7a14db4b288859231f58a369ba06ef9ebb51f55 --- /dev/null +++ b/src/main/java/biz/nynja/account/grid/ag/AgGridController.java @@ -0,0 +1,39 @@ +package biz.nynja.account.grid.ag; + +import java.util.HashMap; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RestController; + +import biz.nynja.account.grid.ag.request.GetRowsRequest; +import biz.nynja.account.repositories.AccountRepository; + +@RestController +public class AgGridController { + + private AgGridService agGridService; + + private AccountRepository accountRepository; + + @Autowired + public AgGridController(AgGridService accountDao, AccountRepository accountRepository) { + this.agGridService = accountDao; + this.accountRepository = accountRepository; + } + + /* @RequestMapping(method = RequestMethod.POST, value = "/getRows") + public ResponseEntity getRows(@RequestBody GetRowsRequest request) { + return ResponseEntity.ok(agGridService.getData(request)); + }*/ + + @RequestMapping(method = RequestMethod.GET, value = "/getRowsCount") + public ResponseEntity> getCountOfRows() { + HashMap map = new HashMap<>(); + map.put("lastRow", accountRepository.count()); + return ResponseEntity.ok(map); + } +} diff --git a/src/main/java/biz/nynja/account/grid/ag/AgGridService.java b/src/main/java/biz/nynja/account/grid/ag/AgGridService.java new file mode 100644 index 0000000000000000000000000000000000000000..a09d1686039be00edef87687bf7510030713fc8e --- /dev/null +++ b/src/main/java/biz/nynja/account/grid/ag/AgGridService.java @@ -0,0 +1,54 @@ +package biz.nynja.account.grid.ag; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.cassandra.core.query.CassandraPageRequest; +import org.springframework.data.domain.Pageable; +import org.springframework.data.domain.Slice; +import org.springframework.stereotype.Service; + +import biz.nynja.account.admin.grpc.AccountAdminResponse; +import biz.nynja.account.admin.grpc.AccountsAdminResponse; +import biz.nynja.account.grpc.AccountDetails; +import biz.nynja.account.models.Account; +import biz.nynja.account.repositories.AccountRepository; + +@Service +public class AgGridService { + + private AccountRepository accountRepository; + + private Slice accounts = null; + + @Autowired + public AgGridService(AccountRepository accountRepository) { + this.accountRepository = accountRepository; + } + + public AccountsAdminResponse getData(int endRow, int startRow) { + + Map> pivotValues = new HashMap>(); + + // Sort sort = new Sort(new Sort.Order(Direction.ASC, "type")); + + Pageable pageable = CassandraPageRequest.of(0, endRow); + + accounts = accountRepository.findAll(pageable); + List rows = new ArrayList<>(); + accounts.getContent().subList(startRow - 1, accounts.getNumberOfElements()).forEach(account -> { + AccountDetails accountDetails = account.toProto(); + rows.add(accountDetails); + }); + + // create response with our results + + AccountsAdminResponse response = AccountsAdminResponse.newBuilder() + .setAccountsResponse(AccountAdminResponse.newBuilder().addAllAccountDetails(rows).build()).build(); + return response; + } + +} diff --git a/src/main/java/biz/nynja/account/grid/ag/GetRowsResponse.java b/src/main/java/biz/nynja/account/grid/ag/GetRowsResponse.java new file mode 100644 index 0000000000000000000000000000000000000000..164390213ee4509d6420d0a319df666d664d9c3d --- /dev/null +++ b/src/main/java/biz/nynja/account/grid/ag/GetRowsResponse.java @@ -0,0 +1,44 @@ +package biz.nynja.account.grid.ag; + +import java.util.List; +import java.util.Map; + +public class GetRowsResponse { + + private List> data; + private int lastRow; + private List secondaryColumnFields; + + public GetRowsResponse() { + } + + public GetRowsResponse(List> data, int lastRow, List secondaryColumnFields) { + this.data = data; + this.lastRow = lastRow; + this.secondaryColumnFields = secondaryColumnFields; + } + + public List> getData() { + return data; + } + + public void setData(List> data) { + this.data = data; + } + + public int getLastRow() { + return lastRow; + } + + public void setLastRow(int lastRow) { + this.lastRow = lastRow; + } + + public List getSecondaryColumnFields() { + return secondaryColumnFields; + } + + public void setSecondaryColumns(List secondaryColumnFields) { + this.secondaryColumnFields = secondaryColumnFields; + } +} diff --git a/src/main/java/biz/nynja/account/grid/ag/ResponseBuilder.java b/src/main/java/biz/nynja/account/grid/ag/ResponseBuilder.java new file mode 100644 index 0000000000000000000000000000000000000000..4191f03eb77041854c1d7dc4e7a8696fa86b5dc6 --- /dev/null +++ b/src/main/java/biz/nynja/account/grid/ag/ResponseBuilder.java @@ -0,0 +1,56 @@ +package biz.nynja.account.grid.ag; + +import static java.util.stream.Collectors.joining; +import static java.util.stream.Collectors.toCollection; +import static java.util.stream.Collectors.toList; + +import java.util.ArrayList; +import java.util.LinkedHashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import org.apache.commons.lang3.tuple.Pair; + +import com.google.common.collect.Sets; + +import biz.nynja.account.grid.ag.request.ColumnVO; + +public class ResponseBuilder { + public static GetRowsResponse createResponse(int endRow, int startRow, + List> rows, + Map> pivotValues) { + + int currentLastRow = startRow + rows.size(); + int lastRow = currentLastRow <= endRow ? currentLastRow : -1; + + List valueColumns = new ArrayList<>(); + + return new GetRowsResponse(rows, lastRow, getSecondaryColumns(pivotValues, valueColumns)); + } + + private static List getSecondaryColumns(Map> pivotValues, List valueColumns) { + + // create pairs of pivot col and pivot value i.e. (DEALTYPE,Financial), (BIDTYPE,Sell)... + List>> pivotPairs = pivotValues.entrySet().stream() + .map(e -> e.getValue().stream() + .map(pivotValue -> Pair.of(e.getKey(), pivotValue)) + .collect(toCollection(LinkedHashSet::new))) + .collect(toList()); + + // create cartesian product of pivot and value columns i.e. Financial_Sell_CURRENTVALUE, Physical_Buy_CURRENTVALUE... + return Sets.cartesianProduct(pivotPairs) + .stream() + .flatMap(pairs -> { + // collect pivot cols, i.e. Financial_Sell + String pivotCol = pairs.stream() + .map(Pair::getRight) + .collect(joining("_")); + + // append value cols, i.e. Financial_Sell_CURRENTVALUE, Financial_Sell_PREVIOUSVALUE + return valueColumns.stream() + .map(valueCol -> pivotCol + "_" + valueCol.getField()); + }) + .collect(toList()); + } +} diff --git a/src/main/java/biz/nynja/account/grid/ag/filter/ColumnFilter.java b/src/main/java/biz/nynja/account/grid/ag/filter/ColumnFilter.java new file mode 100644 index 0000000000000000000000000000000000000000..2dfc17f47bcb49523b04c81b89965487202823bd --- /dev/null +++ b/src/main/java/biz/nynja/account/grid/ag/filter/ColumnFilter.java @@ -0,0 +1,11 @@ +package biz.nynja.account.grid.ag.filter; + +import com.fasterxml.jackson.annotation.JsonSubTypes; +import com.fasterxml.jackson.annotation.JsonTypeInfo; + +@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "filterType") +@JsonSubTypes({ @JsonSubTypes.Type(value = NumberColumnFilter.class, name = "number"), + @JsonSubTypes.Type(value = SetColumnFilter.class, name = "set") }) +public abstract class ColumnFilter { + String filterType; +} diff --git a/src/main/java/biz/nynja/account/grid/ag/filter/NumberColumnFilter.java b/src/main/java/biz/nynja/account/grid/ag/filter/NumberColumnFilter.java new file mode 100644 index 0000000000000000000000000000000000000000..f7ddb55c44962f8c197d0938186cb72554c556e7 --- /dev/null +++ b/src/main/java/biz/nynja/account/grid/ag/filter/NumberColumnFilter.java @@ -0,0 +1,33 @@ +package biz.nynja.account.grid.ag.filter; + +public class NumberColumnFilter extends ColumnFilter { + + private String type; + private Integer filter; + private Integer filterTo; + + public NumberColumnFilter() { + } + + public NumberColumnFilter(String type, Integer filter, Integer filterTo) { + this.type = type; + this.filter = filter; + this.filterTo = filterTo; + } + + public String getFilterType() { + return filterType; + } + + public String getType() { + return type; + } + + public Integer getFilter() { + return filter; + } + + public Integer getFilterTo() { + return filterTo; + } +} diff --git a/src/main/java/biz/nynja/account/grid/ag/filter/SetColumnFilter.java b/src/main/java/biz/nynja/account/grid/ag/filter/SetColumnFilter.java new file mode 100644 index 0000000000000000000000000000000000000000..f8cef7450376284a19c75072ded01f9fc0c5f8c5 --- /dev/null +++ b/src/main/java/biz/nynja/account/grid/ag/filter/SetColumnFilter.java @@ -0,0 +1,18 @@ +package biz.nynja.account.grid.ag.filter; + +import java.util.List; + +public class SetColumnFilter extends ColumnFilter { + private List values; + + public SetColumnFilter() { + } + + public SetColumnFilter(List values) { + this.values = values; + } + + public List getValues() { + return values; + } +} diff --git a/src/main/java/biz/nynja/account/grid/ag/request/ColumnVO.java b/src/main/java/biz/nynja/account/grid/ag/request/ColumnVO.java new file mode 100644 index 0000000000000000000000000000000000000000..79f1ea36da24a6e48bb3cddfbd7673cbabff1386 --- /dev/null +++ b/src/main/java/biz/nynja/account/grid/ag/request/ColumnVO.java @@ -0,0 +1,69 @@ +package biz.nynja.account.grid.ag.request; + +import java.util.Objects; + +public class ColumnVO { + + private String id; + private String displayName; + private String field; + private String aggFunc; + + public ColumnVO() { + } + + public ColumnVO(String id, String displayName, String field, String aggFunc) { + this.id = id; + this.displayName = displayName; + this.field = field; + this.aggFunc = aggFunc; + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getDisplayName() { + return displayName; + } + + public void setDisplayName(String displayName) { + this.displayName = displayName; + } + + public String getField() { + return field; + } + + public void setField(String field) { + this.field = field; + } + + public String getAggFunc() { + return aggFunc; + } + + public void setAggFunc(String aggFunc) { + this.aggFunc = aggFunc; + } + + @Override + public boolean equals(Object o) { + if (this == o) + return true; + if (o == null || getClass() != o.getClass()) + return false; + ColumnVO columnVO = (ColumnVO) o; + return Objects.equals(id, columnVO.id) && Objects.equals(displayName, columnVO.displayName) + && Objects.equals(field, columnVO.field) && Objects.equals(aggFunc, columnVO.aggFunc); + } + + @Override + public int hashCode() { + return Objects.hash(id, displayName, field, aggFunc); + } +} diff --git a/src/main/java/biz/nynja/account/grid/ag/request/FilterRequest.java b/src/main/java/biz/nynja/account/grid/ag/request/FilterRequest.java new file mode 100644 index 0000000000000000000000000000000000000000..e87d451ff2694ed1761a675e09482ec2d7e40e1b --- /dev/null +++ b/src/main/java/biz/nynja/account/grid/ag/request/FilterRequest.java @@ -0,0 +1,30 @@ +package biz.nynja.account.grid.ag.request; + +import java.util.Map; + +import biz.nynja.account.grid.ag.filter.ColumnFilter; + +public class FilterRequest { + + private Map filterModel; + + public FilterRequest() { + } + + public FilterRequest(Map filterModel) { + this.filterModel = filterModel; + } + + public Map getFilterModel() { + return filterModel; + } + + public void setFilterModel(Map filterModel) { + this.filterModel = filterModel; + } + + @Override + public String toString() { + return "FilterRequest{" + "filterModel=" + filterModel + '}'; + } +} diff --git a/src/main/java/biz/nynja/account/grid/ag/request/GetRowsRequest.java b/src/main/java/biz/nynja/account/grid/ag/request/GetRowsRequest.java new file mode 100644 index 0000000000000000000000000000000000000000..c2328878055212aee776133029b8f79be0455764 --- /dev/null +++ b/src/main/java/biz/nynja/account/grid/ag/request/GetRowsRequest.java @@ -0,0 +1,115 @@ +package biz.nynja.account.grid.ag.request; + +import java.util.Collections; +import java.util.List; +import java.util.Map; + +import biz.nynja.account.admin.grpc.SortModel; +import biz.nynja.account.grid.ag.filter.ColumnFilter; + +public class GetRowsRequest { + + private int startRow, endRow; + + // row group columns + private List rowGroupCols; + + // value columns + private List valueCols; + + // pivot columns + private List pivotCols; + + // true if pivot mode is one, otherwise false + private boolean pivotMode; + + // what groups the user is viewing + private List groupKeys; + + // if filtering, what the filter model is + private Map filterModel; + + // if sorting, what the sort model is + private List sortModel; + + public GetRowsRequest() { + this.rowGroupCols = Collections.emptyList(); + this.valueCols = Collections.emptyList(); + this.pivotCols = Collections.emptyList(); + this.groupKeys = Collections.emptyList(); + this.filterModel = Collections.emptyMap(); + this.sortModel = Collections.emptyList(); + } + + public int getStartRow() { + return startRow; + } + + public void setStartRow(int startRow) { + this.startRow = startRow; + } + + public int getEndRow() { + return endRow; + } + + public void setEndRow(int endRow) { + this.endRow = endRow; + } + + public List getRowGroupCols() { + return rowGroupCols; + } + + public void setRowGroupCols(List rowGroupCols) { + this.rowGroupCols = rowGroupCols; + } + + public List getValueCols() { + return valueCols; + } + + public void setValueCols(List valueCols) { + this.valueCols = valueCols; + } + + public List getPivotCols() { + return pivotCols; + } + + public void setPivotCols(List pivotCols) { + this.pivotCols = pivotCols; + } + + public boolean isPivotMode() { + return pivotMode; + } + + public void setPivotMode(boolean pivotMode) { + this.pivotMode = pivotMode; + } + + public List getGroupKeys() { + return groupKeys; + } + + public void setGroupKeys(List groupKeys) { + this.groupKeys = groupKeys; + } + + public Map getFilterModel() { + return filterModel; + } + + public void setFilterModel(Map filterModel) { + this.filterModel = filterModel; + } + + public List getSortModel() { + return sortModel; + } + + public void setSortModel(List sortModel) { + this.sortModel = sortModel; + } +}