Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

closes #168 #212

Merged
merged 1 commit into from
Jan 2, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@

import org.robbins.flashcards.dto.BatchLoadingReceiptDto;
import org.robbins.flashcards.exceptions.FlashcardsException;
import org.springframework.data.domain.Pageable;

import java.util.List;
import java.util.Optional;
import java.util.Set;

public interface GenericCrudFacade<D, ID> {

List<D> list() throws FlashcardsException;
List<D> list(Integer page, Integer size, String sort, String direction)
throws FlashcardsException;
List<D> list(Integer page, Integer size, String sort, String direction,
Set<String> fields) throws FlashcardsException;
List<D> list(final Optional<Pageable> page) throws FlashcardsException;
List<D> list(final Optional<Pageable> page, Set<String> fields) throws FlashcardsException;
Long count();
D findOne(final ID id) throws FlashcardsException;
D findOne(final ID id, Set<String> fields) throws FlashcardsException;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
package org.robbins.flashcards.client;

import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;

import org.robbins.flashcards.dto.AbstractPersistableDto;
import org.robbins.flashcards.dto.BatchLoadingReceiptDto;
import org.robbins.flashcards.exceptions.FlashcardsException;
import org.robbins.flashcards.exceptions.ServiceException;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.HttpClientErrorException;

import java.util.*;

public abstract class AbstractCrudClient<E extends AbstractPersistableDto, ID> extends AbstractClient implements GenericRestCrudFacade<E, ID> {

/**
Expand Down Expand Up @@ -86,11 +93,11 @@ public abstract class AbstractCrudClient<E extends AbstractPersistableDto, ID> e

@Override
public List<E> list() {
return list(null, null, null, null, null);
return list(Optional.empty(), null);
}

@Override
public List<E> list(final Integer page, final Integer size, final String sort, final String direction, final Set<String> fields) {
public List<E> list(final Optional<Pageable> page, final Set<String> fields) {
// set the Authentication header
@SuppressWarnings({ "unchecked", "rawtypes" })
final HttpEntity httpEntity = new HttpEntity(getAuthHeaders());
Expand All @@ -103,8 +110,8 @@ public List<E> list(final Integer page, final Integer size, final String sort, f
}

@Override
public List<E> list(final Integer page, final Integer size, final String sort, final String direction) {
return list(page, size, sort, direction, null);
public List<E> list(final Optional<Pageable> page) {
return list(page, null);
}

@Override
Expand Down
5 changes: 5 additions & 0 deletions FlashCards_DTO/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@
<developerConnection>scm:git:git@github.com:justinhrobbins/FlashCards_App.git</developerConnection>
</scm>
<dependencies>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>

<!-- Spring data for Auditable and Persistable -->
<dependency>
<groupId>org.springframework.data</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* [y] hybris Platform
*
* Copyright (c) 2000-2013 hybris AG
* All rights reserved.
*
* This software is the confidential and proprietary information of hybris
* ("Confidential Information"). You shall not disclose such Confidential
* Information and shall use it only in accordance with the terms of the
* license agreement you entered into with hybris.
*/
package org.robbins.flashcards.dto.util;

import java.util.Optional;

import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;

public class PagingUtils
{
public static Optional<Pageable> getPageRequest(final Integer page, final Integer size,
final String sortOrder, final String sortDirection)
{

if (page == null || size == null)
{
return Optional.empty();
}

if (!StringUtils.isEmpty(sortOrder))
{
Sort sort = getSort(sortOrder, sortDirection);
return Optional.of(new PageRequest(page, size, sort));
}
else
{
return Optional.of(new PageRequest(page, size));
}
}

public static Sort getSort(final String sort, final String order)
{

if ((StringUtils.isEmpty(order)) || (order.equals("asc")))
{
return new Sort(Sort.Direction.ASC, order);
}
else if (order.equals("desc"))
{
return new Sort(Sort.Direction.DESC, order);
}
else
{
throw new IllegalArgumentException("Sort order must be 'asc' or 'desc'. '"
+ order + "' is not an acceptable sort order");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@
package org.robbins.flashcards.cassandra.repository.facade.base;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.UUID;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.NotImplementedException;
Expand All @@ -14,6 +13,7 @@
import org.robbins.flashcards.exceptions.FlashcardsException;
import org.robbins.flashcards.facade.base.GenericCrudFacade;
import org.robbins.flashcards.repository.facade.RepositoryFacade;
import org.springframework.data.domain.Pageable;

import com.datastax.driver.core.utils.UUIDs;
import com.google.common.collect.Lists;
Expand All @@ -23,18 +23,18 @@ public abstract class AbstractCrudRepositoryFacadeImpl<D, E extends AbstractPers
{
@Override
public List<D> list() throws FlashcardsException {
return list(null, null, null, null);
final List<E> results = Lists.newArrayList(getRepository().findAll());
return getConverter().getDtos(results);
}

@Override
public List<D> list(final Integer page, final Integer size, final String sort, final String direction) throws FlashcardsException {
return list(null, null, null, null, null);
public List<D> list(final Optional<Pageable> page) throws FlashcardsException {
throw new NotImplementedException("method not yet implemented in Cassandra repository");
}

@Override
public List<D> list(final Integer page, final Integer size, final String sort, final String direction, final Set<String> fields) throws FlashcardsException {
final List<E> results = Lists.newArrayList(getRepository().findAll());
return getConverter().getDtos(results);
public List<D> list(final Optional<Pageable> page, final Set<String> fields) throws FlashcardsException {
throw new NotImplementedException("method not yet implemented in Cassandra repository");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@

import java.io.Serializable;
import java.util.List;
import java.util.Optional;
import java.util.Set;

import javax.inject.Inject;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.robbins.flashcards.dto.BatchLoadingReceiptDto;
import org.robbins.flashcards.exceptions.FlashcardsException;
import org.robbins.flashcards.exceptions.RepositoryException;
Expand All @@ -20,9 +20,7 @@
import org.robbins.flashcards.repository.util.FieldInitializerUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.domain.Pageable;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

Expand Down Expand Up @@ -66,41 +64,30 @@ public BatchLoadingReceiptDto save(final List<D> dtos) throws FlashcardsExceptio

@Override
public List<D> list() throws RepositoryException {
return list(null, null, null, null);
return list(Optional.empty());
}

@Override
public List<D> list(final Integer page, final Integer size, final String sort,
final String direction)
public List<D> list(final Optional<Pageable> page)
throws RepositoryException {
return this.list(page, size, sort, direction, null);
return this.list(page, null);
}

@Override
public List<D> list(final Integer page, final Integer size, final String sort,
final String direction, final Set<String> fields) throws RepositoryException {
public List<D> list(final Optional<Pageable> page, final Set<String> fields) throws RepositoryException {

List<E> entities = null;
List<E> entities;

// are we trying to use Pagination or Sorting?
// if not then go ahead and return findAll()
if ((page == null) && (StringUtils.isEmpty(sort))) {
if (page.isPresent()) {
entities = getRepository().findAll(page.get()).getContent();
}
else {
entities = getRepository().findAll();
} // should we Page
else if (page != null) {
PageRequest pageRequest = getPageRequest(page, size, sort, direction);
entities = getRepository().findAll(pageRequest).getContent();
} // should we just Sort the list?
else if (!StringUtils.isEmpty(sort)) {
// get a sorted list
Sort entitySort = getSort(sort, direction);
entities = getRepository().findAll(entitySort);
}

if (CollectionUtils.isEmpty(entities)) {
return null;
}

return convertAndInitializeEntities(entities, fields);
}

Expand Down Expand Up @@ -135,28 +122,6 @@ public List<D> findByCreatedBy(final ID userId, final Set<String> fields) throws
return convertAndInitializeEntities(results, fields);
}

protected PageRequest getPageRequest(final Integer page, final Integer size,
final String sortOrder, final String sortDirection) {
// are we Sorting too?
if (!StringUtils.isEmpty(sortOrder)) {
Sort sort = getSort(sortOrder, sortDirection);
return new PageRequest(page, size, sort);
} else {
return new PageRequest(page, size);
}
}

protected Sort getSort(final String sort, final String order) {
if ((StringUtils.isEmpty(order)) || (order.equals("asc"))) {
return new Sort(Direction.ASC, order);
} else if (order.equals("desc")) {
return new Sort(Direction.DESC, order);
} else {
throw new IllegalArgumentException("Sort order must be 'asc' or 'desc'. '"
+ order + "' is not an acceptable sort order");
}
}

protected List<D> convertAndInitializeEntities(final List<E> entities) throws RepositoryException {
return convertAndInitializeEntities(entities, null);
}
Expand Down
Loading