usergrid-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From g...@apache.org
Subject [1/2] incubator-usergrid git commit: Refactored the RevisedApiResponse class to only live within the rest tier. Tore out generics and just used a default Entity class that will model the information returned. Current framework hostile towards generics im
Date Thu, 04 Dec 2014 00:58:52 GMT
Repository: incubator-usergrid
Updated Branches:
  refs/heads/USERGRID-256-ApiResponseImplementation d11974d4a -> d1e06f7f7


Refactored the RevisedApiResponse class to only live within the rest tier.
Tore out generics and just used a default Entity class that will model the information returned. Current framework hostile towards generics implementation. Will have to find easier way later.


Project: http://git-wip-us.apache.org/repos/asf/incubator-usergrid/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-usergrid/commit/0a8c0412
Tree: http://git-wip-us.apache.org/repos/asf/incubator-usergrid/tree/0a8c0412
Diff: http://git-wip-us.apache.org/repos/asf/incubator-usergrid/diff/0a8c0412

Branch: refs/heads/USERGRID-256-ApiResponseImplementation
Commit: 0a8c041252f5b327a19ab1b9668b4659701ce3ce
Parents: d11974d
Author: grey <greyes@apigee.com>
Authored: Wed Dec 3 16:57:38 2014 -0800
Committer: grey <greyes@apigee.com>
Committed: Wed Dec 3 16:57:38 2014 -0800

----------------------------------------------------------------------
 .../usergrid/rest/RevisedApiResponse.java       | 651 -------------------
 .../collection/CollectionsResourceIT.java       |  32 +-
 .../rest/test/resource/NamedResource.java       |   3 +-
 .../rest/test/resource/RevisedApiResponse.java  | 651 +++++++++++++++++++
 .../rest/test/resource/ValueResource.java       |  12 +-
 .../resource/app/ApiResponseCollection.java     |  16 +
 .../rest/test/resource/app/Collection.java      |  11 +-
 .../rest/test/resource/app/model/Entity.java    | 369 +++++++++++
 8 files changed, 1076 insertions(+), 669 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/0a8c0412/stack/rest/src/main/java/org/apache/usergrid/rest/RevisedApiResponse.java
----------------------------------------------------------------------
diff --git a/stack/rest/src/main/java/org/apache/usergrid/rest/RevisedApiResponse.java b/stack/rest/src/main/java/org/apache/usergrid/rest/RevisedApiResponse.java
deleted file mode 100644
index ddf14c8..0000000
--- a/stack/rest/src/main/java/org/apache/usergrid/rest/RevisedApiResponse.java
+++ /dev/null
@@ -1,651 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements.  See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License.  You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.apache.usergrid.rest;
-
-
-import com.fasterxml.jackson.annotation.JsonAnyGetter;
-import com.fasterxml.jackson.annotation.JsonAnySetter;
-import com.fasterxml.jackson.annotation.JsonCreator;
-import com.fasterxml.jackson.annotation.JsonProperty;
-import com.fasterxml.jackson.annotation.JsonPropertyOrder;
-import com.fasterxml.jackson.databind.annotation.JsonSerialize;
-import com.fasterxml.jackson.databind.annotation.JsonSerialize.Inclusion;
-import java.util.ArrayList;
-import java.util.LinkedHashMap;
-import java.util.List;
-import java.util.Map;
-import java.util.TreeMap;
-import java.util.UUID;
-
-import javax.xml.bind.annotation.XmlAnyElement;
-import javax.xml.bind.annotation.XmlRootElement;
-import org.springframework.beans.factory.annotation.Autowired;
-import org.apache.usergrid.persistence.AggregateCounterSet;
-import org.apache.usergrid.persistence.Entity;
-import org.apache.usergrid.persistence.entities.Application;
-import org.apache.usergrid.security.oauth.ClientCredentialsInfo;
-import org.apache.usergrid.services.ServiceRequest;
-import org.apache.usergrid.services.ServiceResults;
-import org.apache.usergrid.utils.InflectionUtils;
-
-import org.apache.commons.lang.ClassUtils;
-import org.apache.commons.lang.StringUtils;
-
-import static org.apache.usergrid.utils.InflectionUtils.pluralize;
-
-//TODO: Move this into the testing framework
-@JsonPropertyOrder( {
-        "action", "application", "params", "path", "query", "uri", "status", "error", "applications", "entity",
-        "entities", "list", "data", "next", "timestamp", "duration"
-} )
-@XmlRootElement
-public class RevisedApiResponse<T>  {
-
-    private ServiceRequest esp;
-
-    private String error;
-    private String errorDescription;
-    private String errorUri;
-    private String exception;
-    private String callback;
-
-    private String path;
-    private String uri;
-    private String status;
-    private long timestamp;
-    private String organization;
-    private String applicationName;
-    private UUID application;
-    private List<T> entities;
-    private UUID next;
-    private String cursor;
-    private Integer count;
-    private String action;
-    private List<Object> list;
-    private Object data;
-    private Map<String, UUID> applications;
-    private Map<String, Object> metadata;
-    private Map<String, List<String>> params;
-    private List<AggregateCounterSet> counters;
-    private ClientCredentialsInfo credentials;
-
-    protected Map<String, Object> properties = new TreeMap<String, Object>( String.CASE_INSENSITIVE_ORDER );
-
-    @Autowired
-    protected ServerEnvironmentProperties serverEnvironmentProperties;
-
-
-    public RevisedApiResponse() {
-        timestamp = System.currentTimeMillis();
-    }
-
-
-    public RevisedApiResponse( ServerEnvironmentProperties properties ) {
-        this.serverEnvironmentProperties = properties;
-        timestamp = System.currentTimeMillis();
-    }
-
-
-    @JsonSerialize( include = Inclusion.NON_NULL )
-    public String getCallback() {
-        return callback;
-    }
-
-
-    public void setCallback( String callback ) {
-        this.callback = callback;
-    }
-
-
-    @JsonSerialize( include = Inclusion.NON_NULL )
-    public String getError() {
-        return error;
-    }
-
-
-    @JsonProperty("error")
-    public void setError( String code ) {
-        error = code;
-    }
-
-
-    public static String exceptionToErrorCode( Throwable e ) {
-        if ( e == null ) {
-            return "service_error";
-        }
-        String s = ClassUtils.getShortClassName( e.getClass() );
-        s = StringUtils.removeEnd( s, "Exception" );
-        s = InflectionUtils.underscore( s ).toLowerCase();
-        return s;
-    }
-
-
-    public RevisedApiResponse withError( String code ) {
-        return withError( code, null, null );
-    }
-
-
-    public void setError( Throwable e ) {
-        setError( null, null, e );
-    }
-
-
-    public RevisedApiResponse withError( Throwable e ) {
-        return withError( null, null, e );
-    }
-
-
-    public void setError( String description, Throwable e ) {
-        setError( null, description, e );
-    }
-
-
-    public RevisedApiResponse withError( String description, Throwable e ) {
-        return withError( null, description, e );
-    }
-
-
-
-    public void setError( String code, String description, Throwable e ) {
-        if ( code == null ) {
-            code = exceptionToErrorCode( e );
-        }
-        error = code;
-        errorDescription = description;
-        if ( e != null ) {
-            if ( description == null ) {
-                errorDescription = e.getMessage();
-            }
-            exception = e.getClass().getName();
-        }
-    }
-
-
-    public RevisedApiResponse withError( String code, String description, Throwable e ) {
-        setError( code, description, e );
-        return this;
-    }
-
-
-    @JsonSerialize( include = Inclusion.NON_NULL )
-    @JsonProperty( "error_description" )
-    public String getErrorDescription() {
-        return errorDescription;
-    }
-
-
-    @JsonProperty( "error_description" )
-    public void setErrorDescription( String errorDescription ) {
-        this.errorDescription = errorDescription;
-    }
-
-
-    @JsonSerialize( include = Inclusion.NON_NULL )
-    @JsonProperty( "error_uri" )
-    public String getErrorUri() {
-        return errorUri;
-    }
-
-
-    @JsonProperty( "error_uri" )
-    public void setErrorUri( String errorUri ) {
-        this.errorUri = errorUri;
-    }
-
-
-    @JsonSerialize( include = Inclusion.NON_NULL )
-    public String getException() {
-        return exception;
-    }
-
-
-    public void setException( String exception ) {
-        this.exception = exception;
-    }
-
-
-    @JsonSerialize( include = Inclusion.NON_NULL )
-    public String getPath() {
-        return path;
-    }
-
-
-    public void setPath( String path ) {
-        if ( path == null ) {
-            this.path = null;
-            uri = null;
-        }
-        this.path = path;
-        //uri = createPath( path );
-    }
-
-
-    @JsonSerialize( include = Inclusion.NON_NULL )
-    public String getUri() {
-        return uri;
-    }
-
-    public void setUri(String uri) {
-        //not entirely sure this works
-//        if( uri == null){
-//            uri = createPath( getPath() );
-//        }
-        this.uri = uri;
-    }
-
-
-    public void setServiceRequest( ServiceRequest p ) {
-        esp = p;
-        if ( p != null ) {
-            path = p.getPath();
-            uri = createPath( path );
-        }
-    }
-
-
-    public RevisedApiResponse withServiceRequest( ServiceRequest p ) {
-        setServiceRequest( p );
-        return this;
-    }
-
-
-    @JsonSerialize( include = Inclusion.NON_NULL )
-    public String getStatus() {
-        return status;
-    }
-
-
-    public void setSuccess() {
-        status = "ok";
-    }
-
-
-    public RevisedApiResponse withSuccess() {
-        status = "ok";
-        return this;
-    }
-
-
-    @JsonSerialize( include = Inclusion.NON_NULL )
-    public long getDuration() {
-        return System.currentTimeMillis() - timestamp;
-    }
-
-
-    public void setTimestamp( long timestamp ) {
-        this.timestamp = timestamp;
-    }
-
-
-    public RevisedApiResponse withTimestamp( long timestamp ) {
-        this.timestamp = timestamp;
-        return this;
-    }
-
-
-    @JsonSerialize( include = Inclusion.NON_NULL )
-    public long getTimestamp() {
-        return timestamp;
-    }
-
-
-    @JsonSerialize( include = Inclusion.NON_NULL )
-    public String getAction() {
-        return action;
-    }
-
-
-    public void setAction( String action ) {
-        this.action = action;
-    }
-
-
-    public RevisedApiResponse withAction( String action ) {
-        this.action = action;
-        return this;
-    }
-
-
-    @JsonSerialize( include = Inclusion.NON_NULL )
-    public UUID getApplication() {
-        return application;
-    }
-
-    @JsonSerialize( include = Inclusion.NON_NULL )
-    public String applicationName() {
-        return applicationName;
-    }
-
-
-    /** @return the orgId */
-
-    @JsonSerialize( include = Inclusion.NON_NULL )
-    public String getOrganization() {
-        return organization;
-    }
-
-
-
-    public void setApplication( Application app) {
-        this.application = app.getUuid();
-    }
-
-    /** Set the application and organization information */
-//    public void setApplication( Application app ) {
-//        this.organization = app.getOrganizationName();
-//        this.applicationName = app.getApplicationName();
-//        this.application = app.getUuid();
-//
-//        if ( esp != null ) {
-//            uri = createPath( esp.toString() );
-//        }
-//    }
-
-
-    @JsonSerialize( include = Inclusion.NON_NULL )
-    @XmlAnyElement
-    public List<T> getEntities() {
-        return entities;
-    }
-
-
-    public void setEntities( List<T> entities ) {
-        if ( entities != null ) {
-            this.entities = entities;
-        }
-        else {
-            this.entities = new ArrayList<T>();
-        }
-    }
-
-
-    public RevisedApiResponse withEntities( List<T> entities ) {
-        setEntities( entities );
-        return this;
-    }
-
-
-    public void setResults( ServiceResults results ) {
-        if ( results != null ) {
-            setPath( results.getPath() );
-            //entities = results.getEntities();
-            next = results.getNextResult();
-            cursor = results.getCursor();
-            counters = results.getCounters();
-        }
-        else {
-            entities = new ArrayList<T>();
-        }
-    }
-
-
-    public RevisedApiResponse withResults( ServiceResults results ) {
-        setResults( results );
-        return this;
-    }
-
-
-    public RevisedApiResponse withResultsCount( ServiceResults results ) {
-        setResults( results );
-        if ( results != null ) {
-            count = results.size();
-        }
-        return this;
-    }
-
-
-    @JsonSerialize( include = Inclusion.NON_NULL )
-    public UUID getNext() {
-        return next;
-    }
-
-
-    public void setNext( UUID next ) {
-        this.next = next;
-    }
-
-
-    @JsonSerialize( include = Inclusion.NON_NULL )
-    public String getCursor() {
-        return cursor;
-    }
-
-
-    public void setCursor( String cursor ) {
-        this.cursor = cursor;
-    }
-
-
-    @JsonSerialize( include = Inclusion.NON_NULL )
-    public Integer getCount() {
-        return count;
-    }
-
-
-    public void setCount( Integer count ) {
-        this.count = count;
-    }
-
-
-    public RevisedApiResponse withEntity( T entity ) {
-        entities = new ArrayList<T>();
-        entities.add( entity );
-        return this;
-    }
-
-
-    @JsonSerialize( include = Inclusion.NON_NULL )
-    public List<Object> getList() {
-        return list;
-    }
-
-
-    public void setList( List<Object> list ) {
-        if ( list != null ) {
-            this.list = list;
-        }
-        else {
-            this.list = new ArrayList<Object>();
-        }
-    }
-
-
-    public RevisedApiResponse withList( List<Object> list ) {
-        setList( list );
-        return this;
-    }
-
-
-    public RevisedApiResponse withListCount( List<Object> list ) {
-        setList( list );
-        if ( !list.isEmpty() ) {
-            this.count = list.size();
-        }
-        return this;
-    }
-
-
-    @JsonSerialize( include = Inclusion.NON_NULL )
-    public Object getData() {
-        return data;
-    }
-
-
-    public void setData( Object data ) {
-        if ( data != null ) {
-            this.data = data;
-        }
-        else {
-            this.data = new LinkedHashMap<String, Object>();
-        }
-    }
-
-
-    public RevisedApiResponse withData( Object data ) {
-        setData( data );
-        return this;
-    }
-
-
-    @JsonSerialize( include = Inclusion.NON_NULL )
-    public List<AggregateCounterSet> getCounters() {
-        return counters;
-    }
-
-
-    public void setCounters( List<AggregateCounterSet> counters ) {
-        this.counters = counters;
-    }
-
-
-    @JsonSerialize( include = Inclusion.NON_NULL )
-    public Map<String, UUID> getApplications() {
-        return applications;
-    }
-
-
-    public void setApplications( Map<String, UUID> applications ) {
-        this.applications = applications;
-    }
-
-
-    public RevisedApiResponse withApplications( Map<String, UUID> applications ) {
-        this.applications = applications;
-        return this;
-    }
-
-
-    @JsonSerialize( include = Inclusion.NON_NULL )
-    public ClientCredentialsInfo getCredentials() {
-        return credentials;
-    }
-
-
-    public void setCredentials( ClientCredentialsInfo credentials ) {
-        this.credentials = credentials;
-    }
-
-
-    public RevisedApiResponse withCredentials( ClientCredentialsInfo credentials ) {
-        this.credentials = credentials;
-        return this;
-    }
-
-
-    @JsonSerialize( include = Inclusion.NON_NULL )
-    public Map<String, List<String>> getParams() {
-        return params;
-    }
-
-
-    public void setParams( Map<String, List<String>> params ) {
-        Map<String, List<String>> q = new LinkedHashMap<String, List<String>>();
-        for ( String k : params.keySet() ) {
-            List<String> v = params.get( k );
-            if ( v != null ) {
-                q.put( k, new ArrayList<String>( v ) );
-            }
-        }
-        this.params = q;
-    }
-
-
-    @JsonSerialize( include = Inclusion.NON_NULL )
-    public Map<String, Object> getMetadata() {
-        return metadata;
-    }
-
-
-    public void setMetadata( Map<String, Object> metadata ) {
-        this.metadata = metadata;
-    }
-
-
-    public String getEntityPath( String url_base, Entity entity ) {
-        String entity_uri = null;
-        if ( !Application.ENTITY_TYPE.equals( entity.getType() ) ) {
-            entity_uri = createPath( pluralize( entity.getType() ), entity.getUuid().toString() );
-        }
-        else {
-            entity_uri = createPath();
-        }
-        return entity_uri;
-    }
-
-
-//    public void prepareEntities() {
-//        if ( uri != null ) {
-//            String url_base = serverEnvironmentProperties.getApiBase();
-//            if ( entities != null ) {
-//                for ( Entity entity : entities ) {
-//                    String entity_uri = getEntityPath( url_base, entity );
-//                    entity.setMetadata( "uri", entity_uri );
-//                    entity.setMetadata( "path", path + "/" + entity.getUuid() );
-//                }
-//            }
-//        }
-//    }
-
-
-    @JsonAnyGetter
-    public Map<String, Object> getProperties() {
-        return properties;
-    }
-
-
-    @JsonAnySetter
-    public void setProperty( String key, Object value ) {
-        properties.put( key, value );
-    }
-
-
-    /**
-     * Create a path
-     *
-     * @return `
-     */
-    private String createPath( String... suffixes ) {
-
-        StringBuilder builder = new StringBuilder();
-
-        builder.append( serverEnvironmentProperties.getApiBase() );
-        if ( !serverEnvironmentProperties.getApiBase().endsWith( "/" ) ) {
-            builder.append( "/" );
-        }
-        builder.append( organization );
-        builder.append( "/" );
-        builder.append( applicationName );
-
-        if ( suffixes.length == 0 ) {
-            return builder.toString();
-        }
-
-
-        for ( String current : suffixes ) {
-            if ( current == null ) {
-                continue;
-            }
-
-            if ( !current.startsWith( "/" ) ) {
-                builder.append( "/" );
-            }
-            builder.append( current );
-        }
-
-        return builder.toString();
-    }
-}

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/0a8c0412/stack/rest/src/test/java/org/apache/usergrid/rest/applications/collection/CollectionsResourceIT.java
----------------------------------------------------------------------
diff --git a/stack/rest/src/test/java/org/apache/usergrid/rest/applications/collection/CollectionsResourceIT.java b/stack/rest/src/test/java/org/apache/usergrid/rest/applications/collection/CollectionsResourceIT.java
index 540a8e1..7c035bf 100644
--- a/stack/rest/src/test/java/org/apache/usergrid/rest/applications/collection/CollectionsResourceIT.java
+++ b/stack/rest/src/test/java/org/apache/usergrid/rest/applications/collection/CollectionsResourceIT.java
@@ -36,7 +36,9 @@ import org.apache.usergrid.rest.RevisedApiResponse;
 import org.apache.usergrid.rest.TestContextSetup;
 import org.apache.usergrid.rest.test.resource.app.ApiResponseCollection;
 import org.apache.usergrid.rest.test.resource.app.Collection;
+import org.apache.usergrid.rest.test.resource.app.User;
 import org.apache.usergrid.utils.UUIDUtils;
+import org.apache.usergrid.rest.test.resource.app.model.Entity;
 
 import com.sun.jersey.api.client.UniformInterfaceException;
 import java.io.IOException;
@@ -91,13 +93,17 @@ public class CollectionsResourceIT extends AbstractRestIT {
 
     @Test
     public void postToEmptyCollectionApiResponse() throws IOException {
-        Map<String, String> payload = new HashMap<String, String>();
+        //Map<String, String> payload = new HashMap<String, String>();
+        Map<String, String> payload = hashMap( "name", "Austin" ).map( "state", "TX" );
 
-        ApiResponseCollection node = context.collection( "cities" ).postResponse( payload );
 
-        assertFalse( node.hasNext() );
+        ApiResponseCollection<Entity> node = context.collection( "cities" ).postResponse( payload );
+//TODO: code = node.getHTTPResponseCode();
+//        assertFalse( node.hasNext() );
 
-        ApiResponseCollection<Collection> collection = context.collection( "cities" ).getCollectionResponse();
+        Entity entity = node.getResponse().getEntities().get( 0 );
+
+        ApiResponseCollection<Entity> collection = context.collection( "cities" ).getCollectionResponse();
 
         assertEquals( ( Object ) 0, collection.getResponse().getCount() );
     }
@@ -154,21 +160,27 @@ public class CollectionsResourceIT extends AbstractRestIT {
 
     @Test
     public void stringWithSpaces() throws IOException {
-        Map<String, String> payload = hashMap( "summaryOverview", "My Summary" ).map( "caltype", "personal" );
 
-        JsonNode node = mapper.readTree( resource().path( "/test-organization/test-app/calendarlists" )
-                .queryParam( "access_token", access_token ).accept( MediaType.APPLICATION_JSON )
-                .type( MediaType.APPLICATION_JSON_TYPE ).post( String.class, payload ));
 
+        String collectionName = "calendarlists";
+        Map<String, String> payload = hashMap( "summaryOverview", "My Summary" ).map( "caltype", "personal" );
+
+//        JsonNode node = mapper.readTree( resource().path( "/test-organization/test-app/calendarlists" )
+//                .queryParam( "access_token", access_token ).accept( MediaType.APPLICATION_JSON )
+//                .type( MediaType.APPLICATION_JSON_TYPE ).post( String.class, payload ));
+//this need to be decoupled so that it only requires client side models ( in this case we need an entity class.
+        ApiResponseCollection<User> arc = context.<User>collection( collectionName ).postResponse( payload );
 
-        UUID id = getEntityId( node, 0 );
+//TODO:broken, plz fix
+//        org.apache.usergrid.persistence.Entity entity = arc.next().toTypedEntity();
+        UUID id = null;//arc.next().getUuid();// entity.getUuid();
 
         //post a second entity
 
 
         payload = hashMap( "summaryOverview", "Your Summary" ).map( "caltype", "personal" );
 
-        node = mapper.readTree( resource().path( "/test-organization/test-app/calendarlists" ).queryParam( "access_token", access_token )
+        JsonNode node = mapper.readTree( resource().path( "/test-organization/test-app/calendarlists" ).queryParam( "access_token", access_token )
                 .accept( MediaType.APPLICATION_JSON ).type( MediaType.APPLICATION_JSON_TYPE )
                 .post( String.class, payload ));
 

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/0a8c0412/stack/rest/src/test/java/org/apache/usergrid/rest/test/resource/NamedResource.java
----------------------------------------------------------------------
diff --git a/stack/rest/src/test/java/org/apache/usergrid/rest/test/resource/NamedResource.java b/stack/rest/src/test/java/org/apache/usergrid/rest/test/resource/NamedResource.java
index 262c962..1c3c4b9 100644
--- a/stack/rest/src/test/java/org/apache/usergrid/rest/test/resource/NamedResource.java
+++ b/stack/rest/src/test/java/org/apache/usergrid/rest/test/resource/NamedResource.java
@@ -127,7 +127,8 @@ public abstract class NamedResource {
         return resource;
     }
 
-
+//TODO: change get Entity to return generic type, then when called in specific CollectionResource classes ( i.e userscollection,rolesCollectgion,GroupsCollection)
+    //it would defin
     /**
      * Get the entity from the entity array in the response
      */

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/0a8c0412/stack/rest/src/test/java/org/apache/usergrid/rest/test/resource/RevisedApiResponse.java
----------------------------------------------------------------------
diff --git a/stack/rest/src/test/java/org/apache/usergrid/rest/test/resource/RevisedApiResponse.java b/stack/rest/src/test/java/org/apache/usergrid/rest/test/resource/RevisedApiResponse.java
new file mode 100644
index 0000000..b64a9d0
--- /dev/null
+++ b/stack/rest/src/test/java/org/apache/usergrid/rest/test/resource/RevisedApiResponse.java
@@ -0,0 +1,651 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.usergrid.rest.test.resource;
+
+
+import com.fasterxml.jackson.annotation.JsonAnyGetter;
+import com.fasterxml.jackson.annotation.JsonAnySetter;
+import com.fasterxml.jackson.annotation.JsonProperty;
+import com.fasterxml.jackson.annotation.JsonPropertyOrder;
+import com.fasterxml.jackson.databind.annotation.JsonSerialize;
+import com.fasterxml.jackson.databind.annotation.JsonSerialize.Inclusion;
+import java.util.ArrayList;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.TreeMap;
+import java.util.UUID;
+
+import javax.xml.bind.annotation.XmlAnyElement;
+import javax.xml.bind.annotation.XmlRootElement;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.apache.usergrid.persistence.AggregateCounterSet;
+import org.apache.usergrid.persistence.entities.Application;
+import org.apache.usergrid.rest.ServerEnvironmentProperties;
+import org.apache.usergrid.security.oauth.ClientCredentialsInfo;
+import org.apache.usergrid.services.ServiceRequest;
+import org.apache.usergrid.services.ServiceResults;
+import org.apache.usergrid.utils.InflectionUtils;
+
+import org.apache.commons.lang.ClassUtils;
+import org.apache.commons.lang.StringUtils;
+
+import static org.apache.usergrid.utils.InflectionUtils.pluralize;
+
+
+//TODO: Move this into the testing framework
+@JsonPropertyOrder( {
+        "action", "application", "params", "path", "query", "uri", "status", "error", "applications", "entity",
+        "entities", "list", "data", "next", "timestamp", "duration"
+} )
+@XmlRootElement
+public class RevisedApiResponse  {
+
+    private ServiceRequest esp;
+//TODO: investigate that errors are all properly set/ http responsecode, error description, The error Code thrown form the stack.
+    private String error;
+    private String errorDescription;
+    private String errorUri;
+    private String exception; //illegal_argument
+    private String callback;
+
+    private String path;
+    private String uri;
+    private String status;
+    private long timestamp;
+    private String organization;
+    private String applicationName;
+    private UUID application;
+    private List<org.apache.usergrid.persistence.Entity> entities;
+    private UUID next;
+    private String cursor;
+    private Integer count;
+    private String action;
+    private List<Object> list;
+    private Object data;
+    private Map<String, UUID> applications;
+    private Map<String, Object> metadata;
+    private Map<String, List<String>> params;
+    private List<AggregateCounterSet> counters;
+    private ClientCredentialsInfo credentials;
+
+    protected Map<String, Object> properties = new TreeMap<String, Object>( String.CASE_INSENSITIVE_ORDER );
+
+    @Autowired
+    protected ServerEnvironmentProperties serverEnvironmentProperties;
+
+
+    public RevisedApiResponse() {
+        timestamp = System.currentTimeMillis();
+    }
+
+
+    public RevisedApiResponse( ServerEnvironmentProperties properties ) {
+        this.serverEnvironmentProperties = properties;
+        timestamp = System.currentTimeMillis();
+    }
+
+
+    @JsonSerialize( include = Inclusion.NON_NULL )
+    public String getCallback() {
+        return callback;
+    }
+
+
+    public void setCallback( String callback ) {
+        this.callback = callback;
+    }
+
+
+    @JsonSerialize( include = Inclusion.NON_NULL )
+    public String getError() {
+        return error;
+    }
+
+
+    @JsonProperty("error")
+    public void setError( String code ) {
+        error = code;
+    }
+
+
+    public static String exceptionToErrorCode( Throwable e ) {
+        if ( e == null ) {
+            return "service_error";
+        }
+        String s = ClassUtils.getShortClassName( e.getClass() );
+        s = StringUtils.removeEnd( s, "Exception" );
+        s = InflectionUtils.underscore( s ).toLowerCase();
+        return s;
+    }
+
+
+    public RevisedApiResponse withError( String code ) {
+        return withError( code, null, null );
+    }
+
+
+    public void setError( Throwable e ) {
+        setError( null, null, e );
+    }
+
+
+    public RevisedApiResponse withError( Throwable e ) {
+        return withError( null, null, e );
+    }
+
+
+    public void setError( String description, Throwable e ) {
+        setError( null, description, e );
+    }
+
+
+    public RevisedApiResponse withError( String description, Throwable e ) {
+        return withError( null, description, e );
+    }
+
+
+
+    public void setError( String code, String description, Throwable e ) {
+        if ( code == null ) {
+            code = exceptionToErrorCode( e );
+        }
+        error = code;
+        errorDescription = description;
+        if ( e != null ) {
+            if ( description == null ) {
+                errorDescription = e.getMessage();
+            }
+            exception = e.getClass().getName();
+        }
+    }
+
+
+    public RevisedApiResponse withError( String code, String description, Throwable e ) {
+        setError( code, description, e );
+        return this;
+    }
+
+
+    @JsonSerialize( include = Inclusion.NON_NULL )
+    @JsonProperty( "error_description" )
+    public String getErrorDescription() {
+        return errorDescription;
+    }
+
+
+    @JsonProperty( "error_description" )
+    public void setErrorDescription( String errorDescription ) {
+        this.errorDescription = errorDescription;
+    }
+
+
+    @JsonSerialize( include = Inclusion.NON_NULL )
+    @JsonProperty( "error_uri" )
+    public String getErrorUri() {
+        return errorUri;
+    }
+
+
+    @JsonProperty( "error_uri" )
+    public void setErrorUri( String errorUri ) {
+        this.errorUri = errorUri;
+    }
+
+
+    @JsonSerialize( include = Inclusion.NON_NULL )
+    public String getException() {
+        return exception;
+    }
+
+
+    public void setException( String exception ) {
+        this.exception = exception;
+    }
+
+
+    @JsonSerialize( include = Inclusion.NON_NULL )
+    public String getPath() {
+        return path;
+    }
+
+
+    public void setPath( String path ) {
+        if ( path == null ) {
+            this.path = null;
+            uri = null;
+        }
+        this.path = path;
+        //uri = createPath( path );
+    }
+
+
+    @JsonSerialize( include = Inclusion.NON_NULL )
+    public String getUri() {
+        return uri;
+    }
+
+    public void setUri(String uri) {
+        //not entirely sure this works
+//        if( uri == null){
+//            uri = createPath( getPath() );
+//        }
+        this.uri = uri;
+    }
+
+
+    public void setServiceRequest( ServiceRequest p ) {
+        esp = p;
+        if ( p != null ) {
+            path = p.getPath();
+            uri = createPath( path );
+        }
+    }
+
+
+    public RevisedApiResponse withServiceRequest( ServiceRequest p ) {
+        setServiceRequest( p );
+        return this;
+    }
+
+
+    @JsonSerialize( include = Inclusion.NON_NULL )
+    public String getStatus() {
+        return status;
+    }
+
+
+    public void setSuccess() {
+        status = "ok";
+    }
+
+
+    public RevisedApiResponse withSuccess() {
+        status = "ok";
+        return this;
+    }
+
+
+    @JsonSerialize( include = Inclusion.NON_NULL )
+    public long getDuration() {
+        return System.currentTimeMillis() - timestamp;
+    }
+
+
+    public void setTimestamp( long timestamp ) {
+        this.timestamp = timestamp;
+    }
+
+
+    public RevisedApiResponse withTimestamp( long timestamp ) {
+        this.timestamp = timestamp;
+        return this;
+    }
+
+
+    @JsonSerialize( include = Inclusion.NON_NULL )
+    public long getTimestamp() {
+        return timestamp;
+    }
+
+
+    @JsonSerialize( include = Inclusion.NON_NULL )
+    public String getAction() {
+        return action;
+    }
+
+
+    public void setAction( String action ) {
+        this.action = action;
+    }
+
+
+    public RevisedApiResponse withAction( String action ) {
+        this.action = action;
+        return this;
+    }
+
+
+    @JsonSerialize( include = Inclusion.NON_NULL )
+    public UUID getApplication() {
+        return application;
+    }
+
+    @JsonSerialize( include = Inclusion.NON_NULL )
+    public String applicationName() {
+        return applicationName;
+    }
+
+
+    /** @return the orgId */
+
+    @JsonSerialize( include = Inclusion.NON_NULL )
+    public String getOrganization() {
+        return organization;
+    }
+
+
+
+    public void setApplication( Application app) {
+        this.application = app.getUuid();
+    }
+
+    /** Set the application and organization information */
+//    public void setApplication( Application app ) {
+//        this.organization = app.getOrganizationName();
+//        this.applicationName = app.getApplicationName();
+//        this.application = app.getUuid();
+//
+//        if ( esp != null ) {
+//            uri = createPath( esp.toString() );
+//        }
+//    }
+
+
+    @JsonSerialize( include = Inclusion.NON_NULL )
+    @XmlAnyElement
+    public List<T> getEntities() {
+        return entities;
+    }
+
+
+    public void setEntities( List<T> entities ) {
+        if ( entities != null ) {
+            this.entities = entities;
+        }
+        else {
+            this.entities = new ArrayList<T>();
+        }
+    }
+
+
+    public RevisedApiResponse withEntities( List<T> entities ) {
+        setEntities( entities );
+        return this;
+    }
+
+
+    public void setResults( ServiceResults results ) {
+        if ( results != null ) {
+            setPath( results.getPath() );
+            //entities = results.getEntities();
+            next = results.getNextResult();
+            cursor = results.getCursor();
+            counters = results.getCounters();
+        }
+        else {
+            entities = new ArrayList<T>();
+        }
+    }
+
+
+    public RevisedApiResponse withResults( ServiceResults results ) {
+        setResults( results );
+        return this;
+    }
+
+
+    public RevisedApiResponse withResultsCount( ServiceResults results ) {
+        setResults( results );
+        if ( results != null ) {
+            count = results.size();
+        }
+        return this;
+    }
+
+
+    @JsonSerialize( include = Inclusion.NON_NULL )
+    public UUID getNext() {
+        return next;
+    }
+
+
+    public void setNext( UUID next ) {
+        this.next = next;
+    }
+
+
+    @JsonSerialize( include = Inclusion.NON_NULL )
+    public String getCursor() {
+        return cursor;
+    }
+
+
+    public void setCursor( String cursor ) {
+        this.cursor = cursor;
+    }
+
+
+    @JsonSerialize( include = Inclusion.NON_NULL )
+    public Integer getCount() {
+        return count;
+    }
+
+
+    public void setCount( Integer count ) {
+        this.count = count;
+    }
+
+
+    public RevisedApiResponse withEntity( T entity ) {
+        entities = new ArrayList<T>();
+        entities.add( entity );
+        return this;
+    }
+
+
+    @JsonSerialize( include = Inclusion.NON_NULL )
+    public List<Object> getList() {
+        return list;
+    }
+
+
+    public void setList( List<Object> list ) {
+        if ( list != null ) {
+            this.list = list;
+        }
+        else {
+            this.list = new ArrayList<Object>();
+        }
+    }
+
+
+    public RevisedApiResponse withList( List<Object> list ) {
+        setList( list );
+        return this;
+    }
+
+
+    public RevisedApiResponse withListCount( List<Object> list ) {
+        setList( list );
+        if ( !list.isEmpty() ) {
+            this.count = list.size();
+        }
+        return this;
+    }
+
+
+    @JsonSerialize( include = Inclusion.NON_NULL )
+    public Object getData() {
+        return data;
+    }
+
+
+    public void setData( Object data ) {
+        if ( data != null ) {
+            this.data = data;
+        }
+        else {
+            this.data = new LinkedHashMap<String, Object>();
+        }
+    }
+
+
+    public RevisedApiResponse withData( Object data ) {
+        setData( data );
+        return this;
+    }
+
+
+    @JsonSerialize( include = Inclusion.NON_NULL )
+    public List<AggregateCounterSet> getCounters() {
+        return counters;
+    }
+
+
+    public void setCounters( List<AggregateCounterSet> counters ) {
+        this.counters = counters;
+    }
+
+
+    @JsonSerialize( include = Inclusion.NON_NULL )
+    public Map<String, UUID> getApplications() {
+        return applications;
+    }
+
+
+    public void setApplications( Map<String, UUID> applications ) {
+        this.applications = applications;
+    }
+
+
+    public RevisedApiResponse withApplications( Map<String, UUID> applications ) {
+        this.applications = applications;
+        return this;
+    }
+
+
+    @JsonSerialize( include = Inclusion.NON_NULL )
+    public ClientCredentialsInfo getCredentials() {
+        return credentials;
+    }
+
+
+    public void setCredentials( ClientCredentialsInfo credentials ) {
+        this.credentials = credentials;
+    }
+
+
+    public RevisedApiResponse withCredentials( ClientCredentialsInfo credentials ) {
+        this.credentials = credentials;
+        return this;
+    }
+
+
+    @JsonSerialize( include = Inclusion.NON_NULL )
+    public Map<String, List<String>> getParams() {
+        return params;
+    }
+
+
+    public void setParams( Map<String, List<String>> params ) {
+        Map<String, List<String>> q = new LinkedHashMap<String, List<String>>();
+        for ( String k : params.keySet() ) {
+            List<String> v = params.get( k );
+            if ( v != null ) {
+                q.put( k, new ArrayList<String>( v ) );
+            }
+        }
+        this.params = q;
+    }
+
+
+    @JsonSerialize( include = Inclusion.NON_NULL )
+    public Map<String, Object> getMetadata() {
+        return metadata;
+    }
+
+
+    public void setMetadata( Map<String, Object> metadata ) {
+        this.metadata = metadata;
+    }
+
+
+    public String getEntityPath( String url_base, org.apache.usergrid.persistence.Entity entity ) {
+        String entity_uri = null;
+        if ( !Application.ENTITY_TYPE.equals( entity.getType() ) ) {
+            entity_uri = createPath( pluralize( entity.getType() ), entity.getUuid().toString() );
+        }
+        else {
+            entity_uri = createPath();
+        }
+        return entity_uri;
+    }
+
+//TODO: figure out what this does.
+//    public void prepareEntities() {
+//        if ( uri != null ) {
+//            String url_base = serverEnvironmentProperties.getApiBase();
+//            if ( entities != null ) {
+//                for ( Entity entity : entities ) {
+//                    String entity_uri = getEntityPath( url_base, entity );
+//                    entity.setMetadata( "uri", entity_uri );
+//                    entity.setMetadata( "path", path + "/" + entity.getUuid() );
+//                }
+//            }
+//        }
+//    }
+
+
+    @JsonAnyGetter
+    public Map<String, Object> getProperties() {
+        return properties;
+    }
+
+
+    @JsonAnySetter
+    public void setProperty( String key, Object value ) {
+        properties.put( key, value );
+    }
+
+
+    /**
+     * Create a path
+     *
+     * @return `
+     */
+    private String createPath( String... suffixes ) {
+
+        StringBuilder builder = new StringBuilder();
+
+        builder.append( serverEnvironmentProperties.getApiBase() );
+        if ( !serverEnvironmentProperties.getApiBase().endsWith( "/" ) ) {
+            builder.append( "/" );
+        }
+        builder.append( organization );
+        builder.append( "/" );
+        builder.append( applicationName );
+
+        if ( suffixes.length == 0 ) {
+            return builder.toString();
+        }
+
+
+        for ( String current : suffixes ) {
+            if ( current == null ) {
+                continue;
+            }
+
+            if ( !current.startsWith( "/" ) ) {
+                builder.append( "/" );
+            }
+            builder.append( current );
+        }
+
+        return builder.toString();
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/0a8c0412/stack/rest/src/test/java/org/apache/usergrid/rest/test/resource/ValueResource.java
----------------------------------------------------------------------
diff --git a/stack/rest/src/test/java/org/apache/usergrid/rest/test/resource/ValueResource.java b/stack/rest/src/test/java/org/apache/usergrid/rest/test/resource/ValueResource.java
index 09a412b..ce5b33e 100644
--- a/stack/rest/src/test/java/org/apache/usergrid/rest/test/resource/ValueResource.java
+++ b/stack/rest/src/test/java/org/apache/usergrid/rest/test/resource/ValueResource.java
@@ -22,8 +22,12 @@ import java.util.Map;
 import java.util.Map.Entry;
 import java.util.UUID;
 
+import com.fasterxml.jackson.databind.JavaType;
 import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.core.type.TypeReference;
 
+
+import com.sun.jersey.api.client.GenericType;
 import com.sun.jersey.api.client.WebResource;
 import java.io.IOException;
 
@@ -31,10 +35,12 @@ import static org.junit.Assert.assertEquals;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import org.apache.usergrid.persistence.Entity;
 import org.apache.usergrid.rest.ApiResponse;
 import org.apache.usergrid.rest.RevisedApiResponse;
 
 
+
 /** @author tnine */
 public abstract class ValueResource extends NamedResource {
 
@@ -104,9 +110,11 @@ public abstract class ValueResource extends NamedResource {
     }
 
     //TODO: eventually we need to merge this and make it the offical postInternal. this was not done to keep tests compiling.
-    protected RevisedApiResponse postInternalResponse( Map<String, ?> entity ) throws IOException {
+    protected <T> RevisedApiResponse<T> postInternalResponse( Map<String, ?> entity ) throws IOException {
 
-        return jsonMedia( withParams( withToken( resource() ) ) ).post( RevisedApiResponse.class, entity );
+        return mapper.readValue( jsonMedia( withParams( withToken( resource() ) ) ).post( String.class, entity ),
+                new TypeReference<RevisedApiResponse<T>>() {} );
+        //jsonMedia( withParams( withToken( resource() ) ) ).post( RevisedApiResponse.class, entity );
     }
 
 

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/0a8c0412/stack/rest/src/test/java/org/apache/usergrid/rest/test/resource/app/ApiResponseCollection.java
----------------------------------------------------------------------
diff --git a/stack/rest/src/test/java/org/apache/usergrid/rest/test/resource/app/ApiResponseCollection.java b/stack/rest/src/test/java/org/apache/usergrid/rest/test/resource/app/ApiResponseCollection.java
index 74b2238..bec4224 100644
--- a/stack/rest/src/test/java/org/apache/usergrid/rest/test/resource/app/ApiResponseCollection.java
+++ b/stack/rest/src/test/java/org/apache/usergrid/rest/test/resource/app/ApiResponseCollection.java
@@ -12,6 +12,22 @@ import org.apache.usergrid.rest.test.resource.CollectionResource;
 /**
  * A stateful iterable collection response.  This is a "collection" of entities from our response that are easier
  * to work with. The Generic means that we can type cast the iterator
+ *
+ * Keep generics? Maybe just use entities for now
+ * 1.) Primary key
+ * 2.) Default data-> default data is different from type to type. (Groups would need path and title, Activities require actors...etc)
+ * 3.) Things that you can do with them-> Groups create connections or something else. Adding users to a group. ( this can be boiled down to creating a connection )
+ *
+ * Two connecting builder patterns
+ * 1. POST /collection/entity/verb (e.g. likes or following)/collection/entity  //connect any two entities
+ *  - POST /users/fred/following/users/barney
+ * 2. POST /collection/entity/collection/entity //for built in collections e.g. add user to group, add role to group, etc
+ *  - POST users/fred/groups/funlovincriminals
+ *
+ * Two similar builder patterns for getting connected entities
+ * 1. GET /users/fred/following
+ * 2. GET /users/fred/groups
+ *
  */
 public class ApiResponseCollection<T> implements Iterable<T>, Iterator<T> {
 

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/0a8c0412/stack/rest/src/test/java/org/apache/usergrid/rest/test/resource/app/Collection.java
----------------------------------------------------------------------
diff --git a/stack/rest/src/test/java/org/apache/usergrid/rest/test/resource/app/Collection.java b/stack/rest/src/test/java/org/apache/usergrid/rest/test/resource/app/Collection.java
index 4925d19..2be60a1 100644
--- a/stack/rest/src/test/java/org/apache/usergrid/rest/test/resource/app/Collection.java
+++ b/stack/rest/src/test/java/org/apache/usergrid/rest/test/resource/app/Collection.java
@@ -20,6 +20,7 @@ package org.apache.usergrid.rest.test.resource.app;
 import java.io.IOException;
 import java.util.Map;
 
+import org.apache.usergrid.persistence.Entity;
 import org.apache.usergrid.rest.ApiResponse;
 import org.apache.usergrid.rest.RevisedApiResponse;
 import org.apache.usergrid.rest.test.resource.CollectionResource;
@@ -83,11 +84,11 @@ public class Collection extends SetResource {
         return getEntity( response, 0 );
     }
 
-    public ApiResponseCollection<Collection> postResponse(Map entityData) throws IOException{
+    public <T> ApiResponseCollection<T> postResponse(Map entityData) throws IOException{
 
         CollectionResource collectionResource = new CollectionResource( this.getName(),this.getParent() );
-        RevisedApiResponse response = this.postInternalResponse( entityData );
-        ApiResponseCollection<Collection> collectionRevisedApiResponse = new ApiResponseCollection<>(collectionResource,response );
+        RevisedApiResponse<T> response = this.<T>postInternalResponse( entityData );
+        ApiResponseCollection<T> collectionRevisedApiResponse = new ApiResponseCollection<T>(collectionResource,response );
 
         return collectionRevisedApiResponse;
     }
@@ -98,9 +99,9 @@ public class Collection extends SetResource {
      * @return
      * @throws IOException
      */
-    public ApiResponseCollection<Collection> getCollectionResponse() throws IOException  {
+    public <T> ApiResponseCollection<T> getCollectionResponse() throws IOException  {
         CollectionResource collectionResource = new CollectionResource( this.getName(),this.getParent() );
-        ApiResponseCollection<Collection> collectionRevisedApiResponse = new ApiResponseCollection<>(collectionResource,this.getResponse() );
+        ApiResponseCollection<T> collectionRevisedApiResponse = new ApiResponseCollection<T>(collectionResource,this.getResponse() );
         return collectionRevisedApiResponse;
     }
 

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/0a8c0412/stack/rest/src/test/java/org/apache/usergrid/rest/test/resource/app/model/Entity.java
----------------------------------------------------------------------
diff --git a/stack/rest/src/test/java/org/apache/usergrid/rest/test/resource/app/model/Entity.java b/stack/rest/src/test/java/org/apache/usergrid/rest/test/resource/app/model/Entity.java
new file mode 100644
index 0000000..da83dcf
--- /dev/null
+++ b/stack/rest/src/test/java/org/apache/usergrid/rest/test/resource/app/model/Entity.java
@@ -0,0 +1,369 @@
+<<<<<<< Updated upstream
+package org.apache.usergrid.rest.test.resource.app.model;
+
+
+/**
+ * Created by ApigeeCorporation on 12/2/14.
+ */
+public class Entity {}
+=======
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.usergrid.rest.test.resource.app.model;
+
+
+
+import java.io.Serializable;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.TreeMap;
+import java.util.UUID;
+
+import javax.xml.bind.annotation.XmlRootElement;
+
+import org.apache.usergrid.persistence.EntityFactory;
+import org.apache.usergrid.persistence.Schema;
+import org.apache.usergrid.persistence.annotations.EntityProperty;
+
+import com.fasterxml.jackson.annotation.JsonAnyGetter;
+import com.fasterxml.jackson.annotation.JsonAnySetter;
+import com.fasterxml.jackson.annotation.JsonIgnore;
+import com.fasterxml.jackson.annotation.JsonPropertyOrder;
+import com.fasterxml.jackson.databind.annotation.JsonSerialize;
+
+import static org.apache.usergrid.persistence.Schema.PROPERTY_NAME;
+import static org.apache.usergrid.persistence.Schema.PROPERTY_TYPE;
+import static org.apache.usergrid.persistence.Schema.PROPERTY_URI;
+import static org.apache.usergrid.persistence.Schema.PROPERTY_UUID;
+
+
+/** Entities are the base object type in the service. */
+
+//@JsonPropertyOrder({ PROPERTY_UUID, PROPERTY_TYPE, PROPERTY_URI, PROPERTY_NAME })
+@XmlRootElement
+public class Entity implements Serializable {
+
+    protected UUID uuid;
+
+    protected Long created;
+
+    protected Long modified;
+
+    protected String type;
+
+    protected Map<String, Object> dynamic_properties = new TreeMap<String, Object>( String.CASE_INSENSITIVE_ORDER );
+
+    protected Map<String, Set<Object>> dynamic_sets = new TreeMap<String, Set<Object>>( String.CASE_INSENSITIVE_ORDER );
+
+    @EntityProperty(required = true, mutable = false, basic = true, indexed = false)
+    @JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)
+    public UUID getUuid() {
+        return uuid;
+    }
+
+
+    public void setUuid( UUID uuid ) {
+        this.uuid = uuid;
+    }
+
+
+    @EntityProperty(required = true, mutable = false, basic = true, indexed = false)
+    public String getType() {
+        return type;
+        //return Schema.getDefaultSchema().getEntityType( this.getClass() );
+    }
+
+
+    public void setType( String type ) {
+        this.type = type;
+    }
+
+
+    
+    @EntityProperty(indexed = true, required = true, mutable = false)
+    @JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)
+    public Long getCreated() {
+        return created;
+    }
+
+
+    
+    public void setCreated( Long created ) {
+        if ( created == null ) {
+            created = System.currentTimeMillis();
+        }
+        this.created = created;
+    }
+
+
+    
+    @EntityProperty(indexed = true, required = true, mutable = true)
+    @JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)
+    public Long getModified() {
+        return modified;
+    }
+
+
+    
+    public void setModified( Long modified ) {
+        if ( modified == null ) {
+            modified = System.currentTimeMillis();
+        }
+        this.modified = modified;
+    }
+
+
+    
+    @JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)
+    public String getName() {
+        Object value = getProperty( PROPERTY_NAME );
+
+        if ( value instanceof UUID ) {
+            // fixes existing data that uses UUID in USERGRID-2099
+            return value.toString();
+        }
+
+        return ( String ) value;
+    }
+
+
+    
+    @JsonIgnore
+    public Map<String, Object> getProperties() {
+        return dynamic_properties;
+    }
+
+
+    
+    public final Object getProperty( String propertyName ) {
+        return dynamic_properties.get( propertyName );
+    }
+
+
+    
+    public final void setProperty( String propertyName, Object propertyValue ) {
+        if (propertyValue == null || propertyValue.equals("")) {
+            if (dynamic_properties.containsKey(propertyName)) {
+                dynamic_properties.remove(propertyName);
+            }
+        } else {
+            dynamic_properties.put(propertyName, propertyValue);
+        }
+    }
+
+
+    
+    public void setProperties( Map<String, Object> properties ) {
+        dynamic_properties = new TreeMap<String, Object>( String.CASE_INSENSITIVE_ORDER );
+        addProperties( properties );
+    }
+
+
+    
+    public void addProperties( Map<String, Object> properties ) {
+        if ( properties == null ) {
+            return;
+        }
+        for ( Map.Entry<String, Object> entry : properties.entrySet() ) {
+            setProperty( entry.getKey(), entry.getValue() );
+        }
+    }
+
+
+    
+    @JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)
+    public Object getMetadata( String key ) {
+        return getDataset( "metadata", key );
+    }
+
+
+    
+    public void setMetadata( String key, Object value ) {
+        setDataset( "metadata", key, value );
+    }
+
+
+    
+    public void mergeMetadata( Map<String, Object> new_metadata ) {
+        mergeDataset( "metadata", new_metadata );
+    }
+
+
+    
+    public void clearMetadata() {
+        clearDataset( "metadata" );
+    }
+
+
+    public <T> T getDataset( String property, String key ) {
+        Object md = dynamic_properties.get( property );
+        if ( md == null ) {
+            return null;
+        }
+        if ( !( md instanceof Map<?, ?> ) ) {
+            return null;
+        }
+        @SuppressWarnings("unchecked") Map<String, T> metadata = ( Map<String, T> ) md;
+        return metadata.get( key );
+    }
+
+
+    public <T> void setDataset( String property, String key, T value ) {
+        if ( key == null ) {
+            return;
+        }
+        Object md = dynamic_properties.get( property );
+        if ( !( md instanceof Map<?, ?> ) ) {
+            md = new HashMap<String, T>();
+            dynamic_properties.put( property, md );
+        }
+        @SuppressWarnings("unchecked") Map<String, T> metadata = ( Map<String, T> ) md;
+        metadata.put( key, value );
+    }
+
+
+    public <T> void mergeDataset( String property, Map<String, T> new_metadata ) {
+        Object md = dynamic_properties.get( property );
+        if ( !( md instanceof Map<?, ?> ) ) {
+            md = new HashMap<String, T>();
+            dynamic_properties.put( property, md );
+        }
+        @SuppressWarnings("unchecked") Map<String, T> metadata = ( Map<String, T> ) md;
+        metadata.putAll( new_metadata );
+    }
+
+
+    public void clearDataset( String property ) {
+        dynamic_properties.remove( property );
+    }
+
+
+    
+    public List<org.apache.usergrid.persistence.Entity> getCollections( String key ) {
+        return getDataset( "collections", key );
+    }
+
+
+    
+    public void setCollections( String key, List<org.apache.usergrid.persistence.Entity> results ) {
+        setDataset( "collections", key, results );
+    }
+
+
+    
+    public List<org.apache.usergrid.persistence.Entity> getConnections( String key ) {
+        return getDataset( "connections", key );
+    }
+
+
+    
+    public void setConnections( String key, List<org.apache.usergrid.persistence.Entity> results ) {
+        setDataset( "connections", key, results );
+    }
+
+
+    
+    public String toString() {
+        return "Entity(" + getProperties() + ")";
+    }
+
+
+    
+    @JsonAnySetter
+    public void setDynamicProperty( String key, Object value ) {
+        if (value == null || value.equals("")) {
+            if (dynamic_properties.containsKey(key)) {
+                dynamic_properties.remove(key);
+            }
+        } else {
+            dynamic_properties.put(key, value);
+        }
+    }
+
+
+    
+    @JsonAnyGetter
+    public Map<String, Object> getDynamicProperties() {
+        return dynamic_properties;
+    }
+
+
+    
+    public final int compareTo( org.apache.usergrid.persistence.Entity o ) {
+        if ( o == null ) {
+            return 1;
+        }
+        try {
+            long t1 = getUuid().timestamp();
+            long t2 = o.getUuid().timestamp();
+            return ( t1 < t2 ) ? -1 : ( t1 == t2 ) ? 0 : 1;
+        }
+        catch ( UnsupportedOperationException e ) {
+        }
+        return getUuid().compareTo( o.getUuid() );
+    }
+
+
+    
+    public org.apache.usergrid.persistence.Entity toTypedEntity() {
+        org.apache.usergrid.persistence.Entity entity = EntityFactory.newEntity( getUuid(), getType() );
+        entity.setProperties( getProperties() );
+        return entity;
+    }
+
+
+    /* (non-Javadoc)
+     * @see java.lang.Object#hashCode()
+     */
+    
+    public int hashCode() {
+        final int prime = 31;
+        int result = 1;
+        result = prime * result + ( ( uuid == null ) ? 0 : uuid.hashCode() );
+        return result;
+    }
+
+
+    /* (non-Javadoc)
+     * @see java.lang.Object#equals(java.lang.Object)
+     */
+    
+//    public boolean equals( Object obj ) {
+//        if ( this == obj ) {
+//            return true;
+//        }
+//        if ( obj == null ) {
+//            return false;
+//        }
+//        if ( getClass() != obj.getClass() ) {
+//            return false;
+//        }
+//        AbstractEntity other = ( AbstractEntity ) obj;
+//        if ( uuid == null ) {
+//            if ( other.uuid != null ) {
+//                return false;
+//            }
+//        }
+//        else if ( !uuid.equals( other.uuid ) ) {
+//            return false;
+//        }
+//        return true;
+//    }
+}
+>>>>>>> Stashed changes


Mime
View raw message