All Downloads are FREE. Search and download functionalities are using the official Maven repository.

it.uniroma2.art.sheet2rdf.header.AdvancedGraphApplication Maven / Gradle / Ivy

There is a newer version: 6.0.6
Show newest version
package it.uniroma2.art.sheet2rdf.header;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import com.fasterxml.jackson.databind.node.ObjectNode;

import it.uniroma2.art.sheet2rdf.utils.JsonConstants;

public class AdvancedGraphApplication extends GraphApplication {
	
	private List nodeIds; //ids of the nodes referenced in the graph pattern
	private Map prefixMapping;
	private String pattern; 
	
	public AdvancedGraphApplication() {
		super();
		nodeIds = new ArrayList();
		prefixMapping = new HashMap();
	}
	
	public List getNodeIds() {
		return nodeIds;
	}

	public void setNodeIds(List nodeIds) {
		this.nodeIds = nodeIds;
	}
	
	@Override
	public void removeNode(String nodeId) {
		nodeIds.remove(nodeId);
	}
	
	public String getPattern() {
		return pattern;
	}

	public void setPattern(String pattern) {
		this.pattern = pattern;
	}

	public Map getPrefixMapping() {
		return prefixMapping;
	}

	public void setPrefixMapping(Map prefixMapping) {
		this.prefixMapping = prefixMapping;
	}
	
	@Override
	public JsonNode toJson() {
		JsonNodeFactory jsonFactory = JsonNodeFactory.instance;
		ObjectNode graphJson = jsonFactory.objectNode();
		graphJson.set(JsonConstants.ADVANCED_GRAPH_APPLICATION_ID, jsonFactory.textNode(getId()));
		graphJson.set(JsonConstants.ADVANCED_GRAPH_APPLICATION_PATTERN, jsonFactory.textNode(pattern));
		ArrayNode nodesJson = jsonFactory.arrayNode();
		for (String id: nodeIds) {
			nodesJson.add(id);
		}
		graphJson.set(JsonConstants.ADVANCED_GRAPH_APPLICATION_NODE_IDS, nodesJson);
		graphJson.set(JsonConstants.ADVANCED_GRAPH_APPLICATION_PREFIX_MAPPING, new ObjectMapper().valueToTree(prefixMapping));
		return graphJson;
	}
	
	@Override
	public void fromJson(JsonNode jsonNode) {
		ObjectMapper mapper = new ObjectMapper();
		this.id = jsonNode.get(JsonConstants.ADVANCED_GRAPH_APPLICATION_ID).asText();
		this.pattern = jsonNode.get(JsonConstants.ADVANCED_GRAPH_APPLICATION_PATTERN).asText();
		this.nodeIds = mapper.convertValue(jsonNode.get(JsonConstants.ADVANCED_GRAPH_APPLICATION_NODE_IDS), new TypeReference>(){});
		this.prefixMapping = mapper.convertValue(jsonNode.get(JsonConstants.ADVANCED_GRAPH_APPLICATION_PREFIX_MAPPING), new TypeReference>(){});
	}
	
	@Override
	public String toString() {
		String s = "";
		s += "id:\t\t" + this.id + "\n";
		s += "pattern:\t" + this.pattern + "\n";
		s += "nodeIds:\t" + this.nodeIds + "\n";
		s += "prefNs:\t\t" + this.prefixMapping;
		return s;
	}
	
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy