Skip to content

Commit

Permalink
Changed type compatibility check to interface comparison instead of p…
Browse files Browse the repository at this point in the history
…arameter type comparison, refactored ConversionConfiguration initialization
  • Loading branch information
litvinovg committed Apr 18, 2024
1 parent 9f21fda commit 59a7a95
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

package edu.cornell.mannlib.vitro.webapp.dynapi.components.operations;

import java.util.Set;

import edu.cornell.mannlib.vitro.webapp.dynapi.components.OperationResult;
import edu.cornell.mannlib.vitro.webapp.dynapi.components.Parameter;
import edu.cornell.mannlib.vitro.webapp.dynapi.data.Data;
import edu.cornell.mannlib.vitro.webapp.dynapi.data.DataStore;
import edu.cornell.mannlib.vitro.webapp.dynapi.data.types.ParameterType;
import edu.cornell.mannlib.vitro.webapp.utils.configuration.Property;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
Expand All @@ -15,7 +16,6 @@ public class AssignOperation extends AbstractOperation {
private static final Log log = LogFactory.getLog(AssignOperation.class);
private Parameter targetParam;
private Parameter assignableParam;
private String key;

@Property(uri = "https://vivoweb.org/ontology/vitro-dynamic-api#assignableParameter", minOccurs = 1, maxOccurs = 1)
public void addInputParameter(Parameter param) {
Expand Down Expand Up @@ -58,9 +58,9 @@ public boolean isValid() {
log.error("target parameter is not set");
return false;
}
ParameterType assignableType = assignableParam.getType();
ParameterType targetType = targetParam.getType();
if (!assignableType.equals(targetType)) {
Set<Class<?>> assignableInterfaces = assignableParam.getType().getInterfaces();
Set<Class<?>> targetInterfaces = targetParam.getType().getInterfaces();
if (!targetInterfaces.containsAll(assignableInterfaces)) {
log.error(String.format("assignable '%s' and target '%s' parameters are not compatible", assignableParam
.getName(), targetParam.getName()));
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package edu.cornell.mannlib.vitro.webapp.dynapi.data.types;

import edu.cornell.mannlib.vitro.webapp.dynapi.data.conversion.ConversionMethod;
import edu.cornell.mannlib.vitro.webapp.dynapi.data.conversion.InitializationException;
import edu.cornell.mannlib.vitro.webapp.utils.configuration.Property;
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
Expand Down Expand Up @@ -63,10 +64,11 @@ public void setStaticMethod(boolean isStaticMethod) {
}

public boolean isMethodInitialized() {
if (conversionMethod == null) {
return false;
}
return true;
return conversionMethod != null;
}

public void initialize() throws InitializationException {
conversionMethod = new ConversionMethod(this);
}

public ConversionMethod getConversionMethod() {
Expand All @@ -91,7 +93,6 @@ public boolean equals(Object object) {
.append(conversionClass, compared.conversionClass)
.append(methodName, compared.methodName)
.append(methodArguments, compared.methodArguments)
.append(conversionMethod, compared.conversionMethod)
.append(staticMethod, compared.staticMethod)
.isEquals();
}
Expand All @@ -102,7 +103,6 @@ public int hashCode() {
.append(conversionClass)
.append(methodName)
.append(methodArguments)
.append(conversionMethod)
.append(staticMethod)
.toHashCode();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
package edu.cornell.mannlib.vitro.webapp.dynapi.data.types;

import edu.cornell.mannlib.vitro.webapp.dynapi.data.conversion.ConversionException;
import edu.cornell.mannlib.vitro.webapp.dynapi.data.conversion.ConversionMethod;
import edu.cornell.mannlib.vitro.webapp.dynapi.data.conversion.InitializationException;
import edu.cornell.mannlib.vitro.webapp.utils.configuration.Property;
import org.apache.commons.lang3.builder.EqualsBuilder;
Expand All @@ -27,7 +26,7 @@ public ConversionConfiguration getDeserializationConfig() {
return deserializationConfig;
}

@Property(uri = "https://vivoweb.org/ontology/vitro-dynamic-api#defaultValue", minOccurs = 0, maxOccurs = 1)
@Property(uri = "https://vivoweb.org/ontology/vitro-dynamic-api#defaultValue", maxOccurs = 1)
public void setDefaultValue(String defaultValue) {
this.defaultValue = defaultValue;
}
Expand All @@ -36,43 +35,37 @@ public String getDefaultValue() {
return defaultValue;
}

@Property(uri = "https://vivoweb.org/ontology/vitro-dynamic-api#serializationConfig", minOccurs = 1, maxOccurs = 1)
@Property(uri = "https://vivoweb.org/ontology/vitro-dynamic-api#serializationConfig", maxOccurs = 1)
public void setSerializationConfig(ConversionConfiguration serializationConfig) {
this.serializationConfig = serializationConfig;
}

@Property(uri = "https://vivoweb.org/ontology/vitro-dynamic-api#deserializationConfig", minOccurs = 1,
maxOccurs = 1)
@Property(uri = "https://vivoweb.org/ontology/vitro-dynamic-api#deserializationConfig", maxOccurs = 1)
public void setDeserializationConfig(ConversionConfiguration deserializationConfig) {
this.deserializationConfig = deserializationConfig;
}

public Object serialize(ParameterType type, Object input) {
final ConversionConfiguration config = serializationConfig;
if (!config.isMethodInitialized()) {
initializeMethod(config);
}
return invoke(config, type, input);
return invoke(serializationConfig, type, input);
}

private void initializeMethod(ConversionConfiguration config) {
try {
config.setConversionMethod(new ConversionMethod(config));
config.initialize();
} catch (InitializationException e) {
e.printStackTrace();
log.error(e, e);
throw new RuntimeException(e.getLocalizedMessage());
}
}

public Object deserialize(ParameterType type, Object input) {
final ConversionConfiguration config = deserializationConfig;
if (!config.isMethodInitialized()) {
initializeMethod(config);
}
return invoke(config, type, input);
return invoke(deserializationConfig, type, input);
}

private Object invoke(ConversionConfiguration config, ParameterType type, Object input) {
if (!config.isMethodInitialized()) {
initializeMethod(config);
}
try {
return config.getConversionMethod().invoke(type, input);
} catch (ConversionException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

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;
import edu.cornell.mannlib.vitro.webapp.dynapi.data.conversion.InitializationException;
import edu.cornell.mannlib.vitro.webapp.utils.configuration.Property;
import org.apache.commons.lang3.builder.EqualsBuilder;
Expand Down Expand Up @@ -133,15 +132,6 @@ public void initialize() throws InitializationException {
if (getDefaultFormat() == null) {
throw new InitializationException("Default format is null");
}
ConversionConfiguration deserializationConfig = getDefaultFormat().getDeserializationConfig();
if (deserializationConfig != null) {
deserializationConfig.setConversionMethod(new ConversionMethod(deserializationConfig));
}
ConversionConfiguration serializationConfig = getDefaultFormat().getSerializationConfig();
if (serializationConfig != null) {
serializationConfig.setConversionMethod(new ConversionMethod(serializationConfig));
}

}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public void testEquality() throws InitializationException {
// Default value is not important for logical equality
param1.setDefaultValue("1");
assertEquals(param1, param2);
// internal attributeis not important for logical equality
// internal attributes not important for logical equality
param1.setInternal(true);
assertEquals(param1, param2);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,6 @@ public void testEquality() throws ClassNotFoundException, InitializationExceptio
type1.addFormat(defaultFormat1);
defaultFormat1.setSerializationConfig(config1);
type1.addFormat(defaultFormat1);
ConversionMethod cm1 = new ConversionMethod(config1);

config1.setConversionMethod(cm1);
assertNotEquals(config1, config2);

ParameterType type2 = new ParameterType();
DataFormat defaultFormat2 = new DefaultFormat();
Expand Down

0 comments on commit 59a7a95

Please sign in to comment.