Skip to content

Commit

Permalink
Created interface data property to define interfaces Parameter Type i…
Browse files Browse the repository at this point in the history
…mplements, replaced class names used in implementation types with interface property.
  • Loading branch information
litvinovg committed Apr 17, 2024
1 parent b5b78a8 commit ed8517d
Show file tree
Hide file tree
Showing 28 changed files with 91 additions and 107 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import com.haulmont.yarg.formatters.factory.DefaultFormatterFactory;
import com.haulmont.yarg.loaders.factory.DefaultLoaderFactory;
Expand Down Expand Up @@ -68,9 +69,13 @@ public void addTemplate(Parameter templateParam) throws InitializationException
@Property(uri = "https://vivoweb.org/ontology/vitro-dynamic-api#dataSource", minOccurs = 1)
public void addDataSource(Parameter dataSource) throws InitializationException {
if (!JsonView.isJsonNode(dataSource)) {
Class<?> className = dataSource.getType().getImplementationType().getClassName();
String intefaces = dataSource.getType().getInterfaces()
.stream()
.map(Object::toString)
.collect(Collectors
.joining(", "));
throw new InitializationException(String.format(
"Only json data sources accepted on addDataSource. Provided %s", className.getCanonicalName()));
"Only json data sources accepted on addDataSource. Provided parameter implements: %s", intefaces));
}
inputParams.add(dataSource);
dataSources.add(dataSource);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,13 @@
import java.math.BigInteger;

import edu.cornell.mannlib.vitro.webapp.dynapi.components.Parameter;
import edu.cornell.mannlib.vitro.webapp.dynapi.data.types.ImplementationType;
import edu.cornell.mannlib.vitro.webapp.dynapi.data.types.ParameterType;

public class BigIntegerView {

public static boolean isBigInteger(Parameter param) {
ParameterType paramType = param.getType();
ImplementationType implType = paramType.getImplementationType();
return implType.getClassName().equals(BigInteger.class);
return paramType.hasInterface(BigInteger.class);
}

public static BigInteger getBigInteger(Data data) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

import edu.cornell.mannlib.vitro.webapp.dynapi.components.Parameter;
import edu.cornell.mannlib.vitro.webapp.dynapi.data.implementation.ByteArray;
import edu.cornell.mannlib.vitro.webapp.dynapi.data.types.ImplementationType;
import edu.cornell.mannlib.vitro.webapp.dynapi.data.types.ParameterType;

public class BinaryView {
Expand All @@ -17,9 +16,7 @@ public static boolean isByteArray(Data data) {

public static boolean isByteArray(Parameter param) {
ParameterType type = param.getType();
ImplementationType implType = type.getImplementationType();
String className = implType.getClassName().getCanonicalName();
if (ByteArray.class.getCanonicalName().equals(className)) {
if (type.hasInterface(ByteArray.class)) {
return true;
}
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,13 @@
package edu.cornell.mannlib.vitro.webapp.dynapi.data;

import edu.cornell.mannlib.vitro.webapp.dynapi.components.Parameter;
import edu.cornell.mannlib.vitro.webapp.dynapi.data.types.ImplementationType;
import edu.cornell.mannlib.vitro.webapp.dynapi.data.types.ParameterType;

public class IntegerView {

public static boolean isInteger(Parameter param) {
ParameterType paramType = param.getType();
ImplementationType implType = paramType.getImplementationType();
return implType.getClassName().equals(Integer.class);
return paramType.hasInterface(Integer.class);
}

public static int getInteger(Data data) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,7 @@ public static boolean isJsonContainer(Parameter param) {
}

public static boolean isJsonContainer(ParameterType type) {
String canonicalName = type.getImplementationType().getClassName().getCanonicalName();
if (JsonContainer.class.getCanonicalName().equals(canonicalName)) {
return true;
}
return false;
return type.hasInterface(JsonContainer.class);
}

public static Map<String, JsonArray> getJsonArrays(Parameters params, DataStore dataStore) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import com.fasterxml.jackson.databind.JsonNode;
import edu.cornell.mannlib.vitro.webapp.dynapi.components.Parameter;
import edu.cornell.mannlib.vitro.webapp.dynapi.components.Parameters;
import edu.cornell.mannlib.vitro.webapp.dynapi.data.types.ImplementationType;
import edu.cornell.mannlib.vitro.webapp.dynapi.data.types.ParameterType;
import org.apache.jena.query.ResultSet;
import org.apache.jena.query.ResultSetFormatter;
Expand All @@ -18,9 +17,7 @@ public class JsonView {

public static boolean isJsonNode(Parameter param) {
ParameterType type = param.getType();
ImplementationType implType = type.getImplementationType();
String className = implType.getClassName().getCanonicalName();
if (JsonNode.class.getCanonicalName().equals(className)) {
if (type.hasInterface(JsonNode.class)) {
return true;
}
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,11 @@
import edu.cornell.mannlib.vitro.webapp.dynapi.components.Parameter;
import edu.cornell.mannlib.vitro.webapp.dynapi.components.Parameters;
import edu.cornell.mannlib.vitro.webapp.dynapi.data.implementation.DynapiInMemoryOntModel;
import edu.cornell.mannlib.vitro.webapp.dynapi.data.types.ImplementationType;
import edu.cornell.mannlib.vitro.webapp.dynapi.data.types.ParameterType;
import org.apache.jena.rdf.model.Model;

public class ModelView {

private static final String MODEL_CANONICAL_NAME = Model.class.getCanonicalName();

public static Model getModel(DataStore dataStore, Parameter param) {
String name = param.getName();
Data data = dataStore.getData(name);
Expand All @@ -39,9 +36,7 @@ public static boolean isModel(Data object) {

public static boolean isModel(Parameter param) {
final ParameterType type = param.getType();
final ImplementationType implementationType = type.getImplementationType();
String name = implementationType.getClassName().getCanonicalName();
return MODEL_CANONICAL_NAME.equals(name);
return type.hasInterface(Model.class);
}

public static boolean hasModel(DataStore input, Parameter param) {
Expand All @@ -50,11 +45,7 @@ public static boolean hasModel(DataStore input, Parameter param) {
return false;
}
Data data = input.getData(name);
String className = data.getParam().getType().getImplementationType().getClassName().getCanonicalName();
if (className.equals(MODEL_CANONICAL_NAME)) {
return true;
}
return false;
return data.getParam().getType().hasInterface(Model.class);
}

public static List<Model> getExistingModels(Parameters params, DataStore dataStore) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ public class ImplementationType {

private static final Log log = LogFactory.getLog(ImplementationType.class);

private Class<?> className;
private ConversionConfiguration serializationConfig;
private ConversionConfiguration deserializationConfig;
private String defaultValue = "";
Expand All @@ -28,15 +27,6 @@ public ConversionConfiguration getDeserializationConfig() {
return deserializationConfig;
}

@Property(uri = "https://vivoweb.org/ontology/vitro-dynamic-api#className", minOccurs = 1, maxOccurs = 1)
public void setClassName(String className) throws ClassNotFoundException {
this.className = Class.forName(className);
}

public Class<?> getClassName() {
return className;
}

@Property(uri = "https://vivoweb.org/ontology/vitro-dynamic-api#defaultValue", minOccurs = 0, maxOccurs = 1)
public void setDefaultValue(String defaultValue) {
this.defaultValue = defaultValue;
Expand Down Expand Up @@ -104,7 +94,6 @@ public boolean equals(Object object) {
ImplementationType compared = (ImplementationType) object;

return new EqualsBuilder()
.append(className, compared.className)
.append(serializationConfig, compared.serializationConfig)
.append(deserializationConfig, compared.deserializationConfig)
.isEquals();
Expand All @@ -113,7 +102,6 @@ public boolean equals(Object object) {
@Override
public int hashCode() {
return new HashCodeBuilder(59, 103)
.append(className)
.append(serializationConfig)
.append(deserializationConfig)
.toHashCode();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

package edu.cornell.mannlib.vitro.webapp.dynapi.data.types;

import java.util.HashSet;
import java.util.Set;

import edu.cornell.mannlib.vitro.webapp.dynapi.components.Removable;
import edu.cornell.mannlib.vitro.webapp.dynapi.components.serialization.SerializationType;
import edu.cornell.mannlib.vitro.webapp.dynapi.data.conversion.ConversionMethod;
Expand All @@ -18,6 +21,7 @@ public class ParameterType implements Removable {
private ImplementationType implementationType;
protected ParameterType nestedType = NullParameterType.getInstance();
private boolean isInternal = false;
private Set<Class<?>> interfaces = new HashSet<Class<?>>();

public String getName() {
return name;
Expand Down Expand Up @@ -61,6 +65,19 @@ public ParameterType getNestedType() {
return nestedType;
}

@Property(uri = "https://vivoweb.org/ontology/vitro-dynamic-api#interface", minOccurs = 1)
public void addInterface(String className) throws ClassNotFoundException {
interfaces.add(Class.forName(className));
}

public boolean hasInterface(Class<?> clazz) {
return interfaces.contains(clazz);
}

public Set<Class<?>> getInterfaces() {
return new HashSet<Class<?>>(interfaces);
}

public boolean isLiteral() {
if (!isRdfType()) {
return false;
Expand Down Expand Up @@ -95,11 +112,11 @@ public void dereference() {
}

public boolean isString() {
return getImplementationType().getClassName().getCanonicalName().equals(String.class.getCanonicalName());
return hasInterface(String.class);
}

public boolean isBoolean() {
return getImplementationType().getClassName().getCanonicalName().equals(Boolean.class.getCanonicalName());
return hasInterface(Boolean.class);
}

public boolean isPlainString() {
Expand Down Expand Up @@ -141,6 +158,7 @@ public boolean equals(Object object) {
.append(getImplementationType(), compared.getImplementationType())
.append(getRdfType(), compared.getRdfType())
.append(getNestedType(), compared.getNestedType())
.append(getInterfaces(), compared.getInterfaces())
.isEquals();
}

Expand All @@ -152,6 +170,7 @@ public int hashCode() {
.append(getImplementationType())
.append(getRdfType())
.append(getNestedType())
.append(getInterfaces())
.toHashCode();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public BigIntegerParam(String var) {
type.setImplementationType(implType);
implType.setSerializationConfig(getSerializationConfig());
implType.setDeserializationConfig(getDeserializationConfig());
implType.setClassName(BigInteger.class.getCanonicalName());
type.addInterface(BigInteger.class.getCanonicalName());
this.setType(type);
} catch (Exception e) {
log.error(e, e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public BooleanParam(String var) {
type.setImplementationType(implType);
implType.setSerializationConfig(getSerializationConfig());
implType.setDeserializationConfig(getDeserializationConfig());
implType.setClassName(Boolean.class.getCanonicalName());
type.addInterface(Boolean.class.getCanonicalName());
this.setType(type);
} catch (Exception e) {
log.error(e, e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public IntegerLiteralParam(String var) {
type.setSerializationType(createSerializationType());
implType.setSerializationConfig(getSerializationConfig());
implType.setDeserializationConfig(getDeserializationConfig());
implType.setClassName(Integer.class.getCanonicalName());
type.addInterface(Integer.class.getCanonicalName());
RDFType rdfType = new RDFType();
rdfType.setName("integer");
type.setRdfType(rdfType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public IntegerParam(String var) {
type.setSerializationType(createSerializationType());
implType.setSerializationConfig(getSerializationConfig());
implType.setDeserializationConfig(getDeserializationConfig());
implType.setClassName(Integer.class.getCanonicalName());
type.addInterface(Integer.class.getCanonicalName());
this.setType(type);
} catch (Exception e) {
log.error(e, e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public JsonContainerParam(String var) {
type.setImplementationType(implType);
implType.setSerializationConfig(getSerializationConfig());
implType.setDeserializationConfig(getDeserializationConfig());
implType.setClassName(JsonContainer.class.getCanonicalName());
type.addInterface(JsonContainer.class.getCanonicalName());
this.setType(type);
this.setDefaultValue(getContainerDefaultValue());
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public LangStringLiteralParam(String var) {
type.setImplementationType(implType);
implType.setSerializationConfig(getSerializationConfig());
implType.setDeserializationConfig(getDeserializationConfig());
implType.setClassName(Literal.class.getCanonicalName());
type.addInterface(Literal.class.getCanonicalName());
RDFType rdfType = new RDFType();
rdfType.setName(RDFType.LANG_STRING);
type.setRdfType(rdfType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public ModelParam(String name, boolean internal, boolean autocreate) {
type.setSerializationType(serializationType);
implType.setSerializationConfig(getSerializationConfig());
implType.setDeserializationConfig(getDeserializationConfig(internal));
implType.setClassName(Model.class.getCanonicalName());
type.addInterface(Model.class.getCanonicalName());
if (internal) {
this.setDefaultValue(name);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public StringParam(String var) {
type.setImplementationType(implType);
implType.setSerializationConfig(getSerializationConfig());
implType.setDeserializationConfig(getDeserializationConfig());
implType.setClassName(String.class.getCanonicalName());
type.addInterface(String.class.getCanonicalName());
this.setType(type);
} catch (Exception e) {
log.error(e, e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public static ParameterType getPlainStringLiteralType() {
try {
implType.setSerializationConfig(getSerializationConfig());
implType.setDeserializationConfig(getDeserializationConfig());
implType.setClassName(Literal.class.getCanonicalName());
type.addInterface(Literal.class.getCanonicalName());
RDFType rdfType = new RDFType();
rdfType.setName("string");
type.setRdfType(rdfType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public static ParameterType getUriResourceType() throws ClassNotFoundException {
ConversionConfiguration deserializationConfig = getDeserializationConfig();
implType.setSerializationConfig(serializationConfig);
implType.setDeserializationConfig(deserializationConfig);
implType.setClassName(Resource.class.getCanonicalName());
type.addInterface(Resource.class.getCanonicalName());
RDFType rdfType = new RDFType();
rdfType.setName(RDFType.ANY_URI);
type.setRdfType(rdfType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public static Parameter createModelParameter(String name) throws Exception {
Parameter uriParam = new Parameter();
ParameterType paramType = new ParameterType();
ImplementationType implType = new ImplementationType();
implType.setClassName(Model.class.getCanonicalName());
paramType.addInterface(Model.class.getCanonicalName());
paramType.setImplementationType(implType);

ConversionConfiguration serConfig = createConfig(ModelWriterTest.MODEL_CONVERSION_CLASS, "input", "serializeN3",
Expand Down Expand Up @@ -86,7 +86,7 @@ public static Parameter createUriParameter(String name) throws Exception {
deserialization.setMethodName("toString");
deserialization.setStaticMethod(false);
uri1ImplType.setSerializationConfig(deserialization);
uri1ImplType.setClassName(String.class.getCanonicalName());
uriParamType.addInterface(String.class.getCanonicalName());

RDFType rdfType = new RDFType();
rdfType.setName("anyURI");
Expand All @@ -112,7 +112,7 @@ public static Parameter createStringLiteralParameter(String name) throws Excepti
config.setStaticMethod(false);
impltype.setDeserializationConfig(config);
impltype.setSerializationConfig(config);
impltype.setClassName(String.class.getCanonicalName());
uri1ParamType.addInterface(String.class.getCanonicalName());

RDFType rdfType = new RDFType();
rdfType.setName("string");
Expand All @@ -139,7 +139,7 @@ public static Parameter createBooleanParameter(String name) throws Exception {
config.setStaticMethod(false);
uriImplType.setDeserializationConfig(config);
uriImplType.setSerializationConfig(config);
uriImplType.setClassName(String.class.getCanonicalName());
uriParamType.addInterface(String.class.getCanonicalName());

RDFType rdfType = new RDFType();
rdfType.setName("boolean");
Expand All @@ -159,7 +159,7 @@ public static Parameter createJsonParameter(String allVar) throws Exception {

jsonImplType.setDeserializationConfig(getJsonDeserializationConfig());
jsonImplType.setSerializationConfig(getJsonSerializationConfig());
jsonImplType.setClassName(JsonNode.class.getCanonicalName());
jsonParamType.addInterface(JsonNode.class.getCanonicalName());
jsonImplType.setDefaultValue("{ }");
jsonParamType.setImplementationType(jsonImplType);
jsonParam.setType(jsonParamType);
Expand Down Expand Up @@ -194,7 +194,7 @@ public static Parameter createByteArrayParameter(String name) throws Exception {

implType.setDeserializationConfig(getByteArrayDeserializationConfig());
implType.setSerializationConfig(getByteArraySerializationConfig());
implType.setClassName(ByteArray.class.getCanonicalName());
paramType.addInterface(ByteArray.class.getCanonicalName());
implType.setDefaultValue("");
paramType.setImplementationType(implType);
param.setType(paramType);
Expand Down
Loading

0 comments on commit ed8517d

Please sign in to comment.