Skip to content

Commit

Permalink
refactor objectType
Browse files Browse the repository at this point in the history
  • Loading branch information
Selindek committed Aug 7, 2023
1 parent 20225be commit fb3e23b
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
@EqualsAndHashCode(exclude = "definition")
public class Activity implements StatementObject, SubStatementObject {

private ObjectType objectType;

/**
* An identifier for a single unique Activity.
*/
Expand All @@ -54,7 +56,7 @@ public class Activity implements StatementObject, SubStatementObject {
* @param id The identifier of the Activity.
*/
public Activity(String id) {

this.objectType = null;
this.id = URI.create(id);
this.definition = null;
}
Expand Down
4 changes: 1 addition & 3 deletions xapi-model/src/main/java/dev/learning/xapi/model/Actor.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

package dev.learning.xapi.model;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.annotation.JsonProperty;
Expand Down Expand Up @@ -36,12 +35,11 @@
@NoArgsConstructor
@EqualsAndHashCode(exclude = "name")
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "objectType", defaultImpl = Agent.class,
include = As.PROPERTY)
visible = true, include = As.EXISTING_PROPERTY)
@JsonSubTypes({@JsonSubTypes.Type(value = Agent.class, name = "Agent"),
@JsonSubTypes.Type(value = Agent.class, name = "Person"),
@JsonSubTypes.Type(value = Group.class, name = "Group")})
@JsonInclude(Include.NON_EMPTY)
@JsonIgnoreProperties("objectType")
public abstract class Actor implements StatementObject, SubStatementObject {

/**
Expand Down
2 changes: 2 additions & 0 deletions xapi-model/src/main/java/dev/learning/xapi/model/Agent.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
@JsonIgnoreProperties(value = {"firstName", "lastName"})
public class Agent extends Actor {

private ObjectType objectType;

// **Warning** do not add fields that are not required by the xAPI specification.

/**
Expand Down
2 changes: 2 additions & 0 deletions xapi-model/src/main/java/dev/learning/xapi/model/Group.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
@ToString(callSuper = true)
public class Group extends Actor {

private final ObjectType objectType = ObjectType.GROUP;

/**
* The members of this Group.
*/
Expand Down
21 changes: 21 additions & 0 deletions xapi-model/src/main/java/dev/learning/xapi/model/Statement.java
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,27 @@ public Builder agentAuthority(Consumer<Agent.Builder<?, ?>> authority) {
return authority(builder.build());
}

/**
* Sets the object. <b> This custom setter makes sure that if the object is an Agent then its
* objectType property was set properly. </b>
*
* @param object The object of the Statement.
*
* @return This builder.
*/
public Builder object(StatementObject object) {

if (object instanceof final Agent agent && ObjectType.AGENT != agent.getObjectType()) {
this.object = Agent.builder().objectType(ObjectType.AGENT).name(agent.getName())
.account(agent.getAccount()).mbox(agent.getMbox()).mboxSha1sum(agent.getMboxSha1sum())
.openid(agent.getOpenid()).build();
} else {
this.object = object;
}

return this;
}

/**
* Consumer Builder for activity object.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

package dev.learning.xapi.model;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.annotation.JsonSubTypes;
Expand All @@ -21,13 +20,14 @@
*/
@JsonInclude(Include.NON_EMPTY)
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "objectType", defaultImpl = Activity.class,
include = As.PROPERTY)
visible = true, include = As.EXISTING_PROPERTY)
@JsonSubTypes({@JsonSubTypes.Type(value = Activity.class, name = "Activity"),
@JsonSubTypes.Type(value = Agent.class, name = "Agent"),
@JsonSubTypes.Type(value = Group.class, name = "Group"),
@JsonSubTypes.Type(value = SubStatement.class, name = "SubStatement"),
@JsonSubTypes.Type(value = StatementReference.class, name = "StatementRef")})
@JsonIgnoreProperties("objectType")
public interface StatementObject {

ObjectType getObjectType();

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
@Builder
public class StatementReference implements StatementObject, SubStatementObject {

private final ObjectType objectType = ObjectType.STATEMENTREF;

/**
* The UUID of a Statement.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
@EqualsAndHashCode(exclude = {"timestamp", "attachments"})
public class SubStatement implements StatementObject, CoreStatement {

private final ObjectType objectType = ObjectType.SUBSTATEMENT;

/**
* {@inheritDoc}
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

package dev.learning.xapi.model;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.annotation.JsonSubTypes;
Expand All @@ -24,12 +23,13 @@
*/
@JsonInclude(Include.NON_EMPTY)
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "objectType", defaultImpl = Activity.class,
include = As.PROPERTY)
visible = true, include = As.EXISTING_PROPERTY)
@JsonSubTypes({@JsonSubTypes.Type(value = Activity.class, name = "Activity"),
@JsonSubTypes.Type(value = Agent.class, name = "Agent"),
@JsonSubTypes.Type(value = Group.class, name = "Group"),
@JsonSubTypes.Type(value = StatementReference.class, name = "StatementRef")})
@JsonIgnoreProperties("objectType")
public interface SubStatementObject {

ObjectType getObjectType();

}

0 comments on commit fb3e23b

Please sign in to comment.