Skip to content

Commit

Permalink
Added event mock objects and some new data model object and services
Browse files Browse the repository at this point in the history
  • Loading branch information
sergehuber committed May 4, 2017
1 parent 85a8196 commit 32a2419
Show file tree
Hide file tree
Showing 7 changed files with 404 additions and 7 deletions.
4 changes: 4 additions & 0 deletions java/buildAndRun.sh
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -1,53 +1,160 @@
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<Object,Object> 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<Object,Object> 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<Map.Entry<Object,Object>>(event.getProperties().entrySet());
}
})
)
.build();

@Reference
public void setEventService(EventService eventService) {
this.eventService = eventService;
}

public Collection<GraphQLFieldDefinition> getQueries() {
List<GraphQLFieldDefinition> fieldDefinitions = new ArrayList<GraphQLFieldDefinition>();
fieldDefinitions.add(
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;
}


}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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<Event> getEvents(long offset, long pageSize);
}
Loading

0 comments on commit 32a2419

Please sign in to comment.