From 32a24191d53613832214a4cad7ce6dde0088b5d1 Mon Sep 17 00:00:00 2001 From: Serge Huber Date: Thu, 4 May 2017 11:20:10 +0200 Subject: [PATCH] Added event mock objects and some new data model object and services --- java/buildAndRun.sh | 4 + .../graphql/CXSEventGraphQLProvider.java | 119 ++++++++++++- .../contextserver/graphql/Event.java | 77 ++++++++ .../contextserver/graphql/EventService.java | 10 ++ .../contextserver/graphql/PartialList.java | 167 ++++++++++++++++++ .../internal/MockEventServiceImpl.java | 32 ++++ java/pom.xml | 2 +- 7 files changed, 404 insertions(+), 7 deletions(-) create mode 100644 java/bundles/graphql-impl/src/main/java/org/oasis_open/contextserver/graphql/Event.java create mode 100644 java/bundles/graphql-impl/src/main/java/org/oasis_open/contextserver/graphql/EventService.java create mode 100644 java/bundles/graphql-impl/src/main/java/org/oasis_open/contextserver/graphql/PartialList.java create mode 100644 java/bundles/graphql-impl/src/main/java/org/oasis_open/contextserver/graphql/internal/MockEventServiceImpl.java diff --git a/java/buildAndRun.sh b/java/buildAndRun.sh index 9208af9..5176a1c 100755 --- a/java/buildAndRun.sh +++ b/java/buildAndRun.sh @@ -1,5 +1,9 @@ #!/usr/bin/env bash mvn clean install +if [ $? -ne 0 ] +then + exit 1; +fi pushd package/target tar zxvf cxs-graphql-api-package-1.0-SNAPSHOT.tar.gz cd cxs-graphql-api-package-1.0-SNAPSHOT/bin diff --git a/java/bundles/graphql-impl/src/main/java/org/oasis_open/contextserver/graphql/CXSEventGraphQLProvider.java b/java/bundles/graphql-impl/src/main/java/org/oasis_open/contextserver/graphql/CXSEventGraphQLProvider.java index a6dc368..c926abc 100644 --- a/java/bundles/graphql-impl/src/main/java/org/oasis_open/contextserver/graphql/CXSEventGraphQLProvider.java +++ b/java/bundles/graphql-impl/src/main/java/org/oasis_open/contextserver/graphql/CXSEventGraphQLProvider.java @@ -1,40 +1,134 @@ package org.oasis_open.contextserver.graphql; +import graphql.Scalars; import graphql.schema.*; import graphql.servlet.GraphQLQueryProvider; import org.osgi.service.component.annotations.Component; +import org.osgi.service.component.annotations.Reference; import java.util.ArrayList; import java.util.Collection; import java.util.List; +import java.util.Map; import static graphql.Scalars.GraphQLID; +import static graphql.Scalars.GraphQLLong; import static graphql.Scalars.GraphQLString; import static graphql.schema.GraphQLFieldDefinition.newFieldDefinition; import static graphql.schema.GraphQLObjectType.newObject; +import static graphql.schema.GraphQLArgument.newArgument; /** * Created by loom on 20.04.17. */ @Component( - name="CXSEventGraphQLProvider", - immediate=true + name = "CXSEventGraphQLProvider", + immediate = true ) public class CXSEventGraphQLProvider implements GraphQLQueryProvider { + private EventService eventService; + + private static GraphQLObjectType CXSProperties = newObject() + .name("Properties") + .description("Generic key-value (string,string) properties") + .field(newFieldDefinition() + .type(GraphQLID) + .name("key") + .description("A unique identifier for the property") + .dataFetcher(new DataFetcher() { + public Object get(DataFetchingEnvironment environment) { + Map.Entry propertyEntry = environment.getSource(); + return propertyEntry.getKey(); + } + }) + ) + .field(newFieldDefinition() + .type(GraphQLID) + .name("value") + .description("A value for the property") + .dataFetcher(new DataFetcher() { + public Object get(DataFetchingEnvironment environment) { + Map.Entry propertyEntry = environment.getSource(); + return propertyEntry.getValue(); + } + }) + ) + .build(); + private static GraphQLObjectType CXSGraphQLEvent = newObject() .name("Event") .description("An event is generated by user interacting with the Context Server") .field(newFieldDefinition() .type(GraphQLID) .name("id") - .staticValue("eventId") + .description("A unique identifier for the event") + .dataFetcher(new DataFetcher() { + public Object get(DataFetchingEnvironment environment) { + Event event = environment.getSource(); + return event.getId(); + } + }) ) .field(newFieldDefinition() .type(GraphQLString) .name("eventType") - .staticValue("pageView") - ).build(); + .description("An identifier for the event type") + .dataFetcher(new DataFetcher() { + public Object get(DataFetchingEnvironment environment) { + Event event = environment.getSource(); + return event.getEventType(); + } + }) + ) + .field(newFieldDefinition() + .type(GraphQLLong) + .name("timestamp") + .description("The difference, measured in milliseconds, between the current time and midnight, January 1, 1970 UTC.") + .dataFetcher(new DataFetcher() { + public Object get(DataFetchingEnvironment environment) { + Event event = environment.getSource(); + return event.getTimestamp(); + } + })) + .field(newFieldDefinition() + .type(GraphQLString) + .name("subject") + .description("The entity that has fired the event (using the profile)") + .dataFetcher(new DataFetcher() { + public Object get(DataFetchingEnvironment environment) { + Event event = environment.getSource(); + return event.getSubject(); + } + })) + .field(newFieldDefinition() + .type(GraphQLString) + .name("object") + .description("The object on which the event was fired.") + .dataFetcher(new DataFetcher() { + public Object get(DataFetchingEnvironment environment) { + Event event = environment.getSource(); + return event.getObject(); + } + }) + ) + .field(newFieldDefinition() + .type(new GraphQLList(CXSProperties)) + .name("properties") + .description("Generic properties for the event") + .dataFetcher(new DataFetcher() { + public Object get(DataFetchingEnvironment environment) { + Event event = environment.getSource(); + return new ArrayList>(event.getProperties().entrySet()); + } + }) + ) + .build(); + + @Reference + public void setEventService(EventService eventService) { + this.eventService = eventService; + } public Collection getQueries() { List fieldDefinitions = new ArrayList(); @@ -42,12 +136,25 @@ public Collection getQueries() { newFieldDefinition() .name("events") .type(new GraphQLList(CXSGraphQLEvent)) + .argument(newArgument().name("offset").type(Scalars.GraphQLLong).build()) + .argument(newArgument().name("pageSize").type(Scalars.GraphQLLong).build()) .dataFetcher(new DataFetcher() { public Object get(DataFetchingEnvironment environment) { - return null; + Long offset = environment.getArgument("offset"); + if (offset == null) { + offset = 0L; + } + ; + Long pageSize = environment.getArgument("pageSize"); + if (pageSize == null) { + pageSize = 50L; + } + return eventService.getEvents(offset, pageSize); } }) .build()); return fieldDefinitions; } + + } diff --git a/java/bundles/graphql-impl/src/main/java/org/oasis_open/contextserver/graphql/Event.java b/java/bundles/graphql-impl/src/main/java/org/oasis_open/contextserver/graphql/Event.java new file mode 100644 index 0000000..0e72815 --- /dev/null +++ b/java/bundles/graphql-impl/src/main/java/org/oasis_open/contextserver/graphql/Event.java @@ -0,0 +1,77 @@ +package org.oasis_open.contextserver.graphql; + +import java.util.Properties; + +/** + * Created by loom on 04.05.17. + */ +public class Event { + private String id; + private Long timestamp; + private String eventType; + + private String subject; // e.g. profileId + private String object; // e.g. page + + private Properties properties; + + public Event() { + } + + public Event(String id, Long timestamp, String eventType, String subject, String object, Properties properties) { + this.id = id; + this.timestamp = timestamp; + this.eventType = eventType; + this.subject = subject; + this.object = object; + this.properties = properties; + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public Long getTimestamp() { + return timestamp; + } + + public void setTimestamp(Long timestamp) { + this.timestamp = timestamp; + } + + public String getEventType() { + return eventType; + } + + public void setEventType(String eventType) { + this.eventType = eventType; + } + + public String getSubject() { + return subject; + } + + public void setSubject(String subject) { + this.subject = subject; + } + + public String getObject() { + return object; + } + + public void setObject(String object) { + this.object = object; + } + + public Properties getProperties() { + return properties; + } + + public void setProperties(Properties properties) { + this.properties = properties; + } +} diff --git a/java/bundles/graphql-impl/src/main/java/org/oasis_open/contextserver/graphql/EventService.java b/java/bundles/graphql-impl/src/main/java/org/oasis_open/contextserver/graphql/EventService.java new file mode 100644 index 0000000..2f63b3f --- /dev/null +++ b/java/bundles/graphql-impl/src/main/java/org/oasis_open/contextserver/graphql/EventService.java @@ -0,0 +1,10 @@ +package org.oasis_open.contextserver.graphql; + +import java.util.List; + +/** + * Created by loom on 04.05.17. + */ +public interface EventService { + public List getEvents(long offset, long pageSize); +} diff --git a/java/bundles/graphql-impl/src/main/java/org/oasis_open/contextserver/graphql/PartialList.java b/java/bundles/graphql-impl/src/main/java/org/oasis_open/contextserver/graphql/PartialList.java new file mode 100644 index 0000000..32f1565 --- /dev/null +++ b/java/bundles/graphql-impl/src/main/java/org/oasis_open/contextserver/graphql/PartialList.java @@ -0,0 +1,167 @@ +package org.oasis_open.contextserver.graphql; + +import javax.xml.bind.annotation.XmlRootElement; +import javax.xml.bind.annotation.XmlTransient; +import java.io.Serializable; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; +import java.util.Spliterator; +import java.util.function.Consumer; + +/** + * A list of elements representing a limited view of a larger list, starting from a given element (offset from the first) and showing only a given number of elements, instead of + * showing all of them. This is useful to retrieve "pages" of large element collections. + * + * @param the generic type of contained elements + */ +@XmlRootElement +public class PartialList implements Iterable, Serializable { + + private static final long serialVersionUID = 2661946814840468260L; + private List list; + private long offset; + private long pageSize; + private long totalSize; + private String scrollIdentifier = null; + private String scrollTimeValidity = null; + + /** + * Instantiates a new PartialList. + */ + public PartialList() { + list = new ArrayList(); + offset = 0; + pageSize = 0; + totalSize = 0; + } + + /** + * Instantiates a new PartialList. + * + * @param list the limited view into the bigger List this PartialList is representing + * @param offset the offset of the first element in the view + * @param pageSize the number of elements this PartialList contains + * @param totalSize the total size of elements in the original List + */ + public PartialList(List list, long offset, long pageSize, long totalSize) { + this.list = list; + this.offset = offset; + this.pageSize = pageSize; + this.totalSize = totalSize; + } + + /** + * Retrieves the limited list view. + * + * @return a List of the {@code size} elements starting from the {@code offset}-th one from the original, larger list + */ + public List getList() { + return list; + } + + /** + * Sets the view list. + * + * @param list the view list into the bigger List this PartialList is representing + */ + public void setList(List list) { + this.list = list; + } + + /** + * Retrieves the offset of the first element of the view. + * + * @return the offset of the first element of the view + */ + public long getOffset() { + return offset; + } + + public void setOffset(long offset) { + this.offset = offset; + } + + /** + * Retrieves the number of elements this PartialList contains. + * + * @return the number of elements this PartialList contains + */ + public long getPageSize() { + return pageSize; + } + + public void setPageSize(long pageSize) { + this.pageSize = pageSize; + } + + /** + * Retrieves the total size of elements in the original List. + * + * @return the total size of elements in the original List + */ + public long getTotalSize() { + return totalSize; + } + + public void setTotalSize(long totalSize) { + this.totalSize = totalSize; + } + + /** + * Retrieves the size of this PartialList. Should equal {@link #getPageSize()}. + * + * @return the size of this PartialList + */ + @XmlTransient + public int size() { + return list.size(); + } + + /** + * Retrieves the element at the specified index + * + * @param index the index of the element to retrieve + * @return the element at the specified index + */ + @XmlTransient + public T get(int index) { + return list.get(index); + } + + /** + * Retrieve the scroll identifier to make it possible to continue a scrolling list query + * @return a string containing the scroll identifier, to be sent back in an subsequent request + */ + public String getScrollIdentifier() { + return scrollIdentifier; + } + + public void setScrollIdentifier(String scrollIdentifier) { + this.scrollIdentifier = scrollIdentifier; + } + + /** + * Retrieve the value of the scroll time validity to make it possible to continue a scrolling list query + * @return a string containing a time value for the scroll validity, to be sent back in a subsequent request + */ + public String getScrollTimeValidity() { + return scrollTimeValidity; + } + + public void setScrollTimeValidity(String scrollTimeValidity) { + this.scrollTimeValidity = scrollTimeValidity; + } + + public Iterator iterator() { + return list.iterator(); + } + + public void forEach(Consumer action) { + list.forEach(action); + } + + public Spliterator spliterator() { + return list.spliterator(); + } +} diff --git a/java/bundles/graphql-impl/src/main/java/org/oasis_open/contextserver/graphql/internal/MockEventServiceImpl.java b/java/bundles/graphql-impl/src/main/java/org/oasis_open/contextserver/graphql/internal/MockEventServiceImpl.java new file mode 100644 index 0000000..feaaafd --- /dev/null +++ b/java/bundles/graphql-impl/src/main/java/org/oasis_open/contextserver/graphql/internal/MockEventServiceImpl.java @@ -0,0 +1,32 @@ +package org.oasis_open.contextserver.graphql.internal; + +import org.oasis_open.contextserver.graphql.Event; +import org.oasis_open.contextserver.graphql.EventService; +import org.oasis_open.contextserver.graphql.PartialList; +import org.osgi.service.component.annotations.Component; + +import java.util.*; + +/** + * Created by loom on 04.05.17. + */ +@Component( + name="MockEventServiceImpl", + immediate=true +) +public class MockEventServiceImpl implements EventService { + + private Map mockEventDatabase = new HashMap(); + + public MockEventServiceImpl() { + UUID.randomUUID().toString(); + Event pageViewEvent = new Event(UUID.randomUUID().toString(), System.currentTimeMillis(), "pageView", "thomas-profile-id", "/cars/modelx", new Properties()); + Event buttonClickEvent = new Event(UUID.randomUUID().toString(), System.currentTimeMillis(), "buttonClick", "thomas-profile-id", "/cars/modelx#buy", new Properties()); + mockEventDatabase.put(pageViewEvent.getId(), pageViewEvent); + mockEventDatabase.put(buttonClickEvent.getId(), buttonClickEvent); + } + + public List getEvents(long offset, long pageSize) { + return new ArrayList(mockEventDatabase.values()); + } +} diff --git a/java/pom.xml b/java/pom.xml index 2dfaeab..0cccf96 100644 --- a/java/pom.xml +++ b/java/pom.xml @@ -11,7 +11,7 @@ 4.1.1 - 3.0.0 + 3.0.1 2.4.0