Skip to content
This repository has been archived by the owner on Dec 12, 2018. It is now read-only.

Commit

Permalink
#200 - formatting and code cleanup cont'd
Browse files Browse the repository at this point in the history
  • Loading branch information
lhazlewood committed Jun 3, 2015
1 parent 61c97fb commit 277a5b7
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 105 deletions.
143 changes: 39 additions & 104 deletions impl/src/main/java/com/stormpath/sdk/impl/ds/DefaultDataStore.java
Original file line number Diff line number Diff line change
Expand Up @@ -384,136 +384,70 @@ private <T extends Resource, R extends T> R getResource(String href, Class<T> pa
@SuppressWarnings("unchecked")
@Override
public <T extends Resource> T create(String parentHref, T resource) {

Class<T> clazz = (Class<T>) resource.getClass();

T returnValue = create(parentHref, resource, clazz);

//ensure the caller's argument is updated with what is returned from the server:
AbstractResource in = (AbstractResource) resource;
AbstractResource ret = (AbstractResource) returnValue;
LinkedHashMap<String, Object> props = toMap(ret, false);

//@since 1.0.RC3
if (!Collections.isEmpty(props) && !CollectionResource.class.isAssignableFrom(clazz) &&
props.get("href") != null) {
in.setProperties(toEnlistment(props));
} else {
in.setProperties(props);
}

return (T) in;
return (T)save(parentHref, resource, resource.getClass(), null);
}

@SuppressWarnings("unchecked")
@Override
public <T extends Resource> T create(String parentHref, T resource, Options options) {

Assert.isInstanceOf(DefaultOptions.class, options, DEFAULT_OPTIONS_MSG);

DefaultOptions defaultOptions = (DefaultOptions) options;
QueryString qs = queryStringFactory.createQueryString(parentHref, defaultOptions);

Class<T> clazz = (Class<T>) resource.getClass();

T returnValue = save(parentHref, resource, clazz, qs);

//ensure the caller's argument is updated with what is returned from the server:
AbstractResource in = (AbstractResource) resource;
AbstractResource ret = (AbstractResource) returnValue;
LinkedHashMap<String, Object> props = toMap(ret, false);

//@since 1.0.RC3
if (!Collections.isEmpty(props) && !CollectionResource.class.isAssignableFrom(clazz) &&
props.get("href") != null) {
in.setProperties(toEnlistment(props));
} else {
in.setProperties(props);
}

return (T) in;
QueryString qs = toQueryString(parentHref, options);
return (T)save(parentHref, resource, resource.getClass(), qs);
}

@SuppressWarnings("unchecked")
@Override
public <T extends Resource & Saveable> void save(T resource) {
Assert.notNull(resource, "resource argument cannot be null.");
Assert.isInstanceOf(AbstractResource.class, resource);
Assert.isInstanceOf(Saveable.class, resource);

AbstractResource aResource = (AbstractResource) resource;

String href = aResource.getHref();
Assert.hasLength(href, HREF_REQD_MSG);

Class<T> clazz = (Class<T>) resource.getClass();

T returnValue = save(href, resource, clazz);

//ensure the caller's argument is updated with what is returned from the server:
AbstractResource ret = (AbstractResource) returnValue;
LinkedHashMap<String, Object> props = toMap(ret, false);
aResource.setProperties(props);
public <T extends Resource, R extends Resource> R create(String parentHref, T resource, Class<? extends R> returnType) {
return save(parentHref, resource, returnType, null);
}

@SuppressWarnings("unchecked")
@Override
public <T extends Resource & Saveable> void save(T resource, Options options) {
Assert.notNull(resource, "resource argument cannot be null.");
Assert.isInstanceOf(AbstractResource.class, resource);
Assert.isInstanceOf(Saveable.class, resource);

Assert.isInstanceOf(DefaultOptions.class, options, DEFAULT_OPTIONS_MSG);

AbstractResource aResource = (AbstractResource) resource;

String href = aResource.getHref();
Assert.hasLength(href, HREF_REQD_MSG);

DefaultOptions defaultOptions = (DefaultOptions) options;
QueryString qs = queryStringFactory.createQueryString(href, defaultOptions);

Class<T> clazz = (Class<T>) resource.getClass();

T returnValue = save(href, resource, clazz, qs);

//ensure the caller's argument is updated with what is returned from the server:
AbstractResource ret = (AbstractResource) returnValue;
LinkedHashMap<String, Object> props = toMap(ret, false);
aResource.setProperties(props);
public <T extends Resource & Saveable> void save(T resource) {
save(resource, (Options)null);
}

@Override
public <T extends Resource, R extends Resource> R create(String parentHref, T resource,
Class<? extends R> returnType) {
return save(parentHref, resource, returnType);
public <T extends Resource & Saveable> void save(T resource, Options options) {
Assert.notNull(options, "options argument cannot be null.");
String href = resource.getHref();
Assert.hasText(href, HREF_REQD_MSG);
QueryString qs = toQueryString(href, options);
save(href, resource, resource.getClass(), qs);
}

@Override
public <T extends Resource & Saveable, R extends Resource> R save(T resource, Class<? extends R> returnType) {
return save(resource.getHref(), resource, returnType);
Assert.hasText(resource.getHref(), HREF_REQD_MSG);
return save(resource.getHref(), resource, returnType, null);
}

private <T extends Resource, R extends Resource> R save(String href, T resource, Class<? extends R> returnType) {
return save(href, resource, returnType, null);
private QueryString toQueryString(String href, Options options) {
if (options == null) {
return null;
}
Assert.isInstanceOf(DefaultOptions.class, options, DEFAULT_OPTIONS_MSG);
DefaultOptions defaultOptions = (DefaultOptions)options;
return queryStringFactory.createQueryString(href, defaultOptions);
}

private <T extends Resource, R extends Resource> R save(String href, T resource, Class<? extends R> returnType,
QueryString qs) {
@SuppressWarnings("unchecked")
private <T extends Resource, R extends Resource> R save(String href, final T resource, Class<? extends R> returnType, QueryString qs) {

Assert.hasText(href, "href argument cannot be null or empty.");
Assert.notNull(resource, "resource argument cannot be null.");
Assert.notNull(returnType, "returnType class cannot be null.");
Assert.isInstanceOf(AbstractResource.class, resource);
Assert.isTrue(!CollectionResource.class.isAssignableFrom(resource.getClass()), "Collections cannot be persisted.");

SanitizedQuery sanitized = QuerySanitizer.sanitize(href, qs);
href = sanitized.getHrefWithoutQuery();
//need to qualify the href it to ensure our cache lookups work as expected
//(cache key = fully qualified href):
href = ensureFullyQualified(href);

AbstractResource abstractResource = (AbstractResource) resource;

final AbstractResource abstractResource = (AbstractResource) resource;
LinkedHashMap<String, Object> props = toMap(abstractResource, true);

String bodyString = mapMarshaller.marshal(props);

StringInputStream body = new StringInputStream(bodyString);
long length = body.available();

QueryString filteredQs = queryStringFilterProcessor.process(returnType, qs);
Request request = new DefaultRequest(HttpMethod.POST, href, filteredQs, null, body, length);

Expand Down Expand Up @@ -543,10 +477,6 @@ private <T extends Resource, R extends Resource> R save(String href, T resource,
return null;
}

//asserts invariant given that we should have returned if the responseBody is null or empty:
//noinspection ConstantConditions
assert responseBody != null && !responseBody.isEmpty() : "Response body must be non-empty.";

//since 1.0.RC3 RC: emailVerification boolean hack. See: https://github.com/stormpath/stormpath-sdk-java/issues/60
boolean emailVerification = resource instanceof EmailVerificationToken && returnType.equals(Account.class);
//since 1.0.RC4 : fix for https://github.com/stormpath/stormpath-sdk-java/issues/140 where Account remains disabled after
Expand Down Expand Up @@ -585,10 +515,15 @@ private <T extends Resource, R extends Resource> R save(String href, T resource,
}

//@since 1.0.RC3
if (!Collections.isEmpty(returnResponseBody) && returnResponseBody.get("href") != null) {
if (isMaterialized(returnResponseBody)) {
returnResponseBody = toEnlistment(returnResponseBody);
}

//ensure the caller's argument is updated with what is returned from the server if the types are the same:
if (returnType.equals(abstractResource.getClass())) {
abstractResource.setProperties((Map<String,Object>)returnResponseBody);
}

return resourceFactory.instantiate(returnType, returnResponseBody);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ public DefaultPropertiesFilterProcessor(List<PropertiesFilter<T>> filters) {
*/
@Override
public T process(Class clazz, T properties) {

if (properties == null) {
return null;
}

T result = properties;
for (PropertiesFilter<T> filter : filters) {
result = filter.filter(clazz, result);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@
*/
public class QuerySanitizer {

public static SanitizedQuery sanitize(String href, Map<String,Object> queryParams) {


public static SanitizedQuery sanitize(String href, Map<String,?> queryParams) {
Assert.notNull(href, "href argument cannot be null.");

QueryString query = new QueryString(queryParams); //create a copy so we don't manipulate the argument
Expand Down

0 comments on commit 277a5b7

Please sign in to comment.