}
public Entity getEntity(Model model, Resource resource, Class<?> clazz)
throws Exception {
//Or Entity.class or clazz.class
Entity entity = (Entity) getResourceFromCache(resource, Entity.class);
if(entity != null) {
return entity;
}
if(clazz == null) {
StmtIterator stmtI1 = model.listStatements(resource, RDF.type, (RDFNode)null);
while(stmtI1.hasNext()) {
Statement typeStatement = stmtI1.nextStatement();
for(Resource resourceType : foafAgentClasses) {
if(typeStatement.getObject().asResource().getURI().equals(
resourceType.getURI())) {
return getFoafAgent(model, resource);
}
}
}
//The default option:Entity class
entity = new Entity();
entity.setResource(resource);
addResourceInstanceToCache(resource,entity);
} else {
entity = (Entity) clazz.newInstance(); //new Entity();
entity.setResource(resource);
addResourceInstanceToCache(resource,entity);
}
// Specific Attributes and Properties of Entity Class //
// identifier //
Property identifier = ResourceFactory.createProperty(
riNamespace + "identifier");
StmtIterator stmtI1 = model.listStatements(resource,
identifier, (RDFNode)null);
while(stmtI1.hasNext()) {
Statement statement = stmtI1.nextStatement();
// validate identifier property //
if(!statement.getObject().isLiteral()) {
if(!ModelException.throwException(ModelException.ENTITY,
"identifier property of Entity resource:"+
resource.getURI()+" is not a literal")) {
return null;
}
} else {
entity.setUniqueIdentificator(statement.getObject(
).asLiteral().getString());
}
}
// hasReputation //
Property hasReputation = ResourceFactory.createProperty(
riNamespace + "hasMetric");
stmtI1 = model.listStatements(resource,
hasReputation, (RDFNode)null);
while(stmtI1.hasNext()) {
Statement statement = stmtI1.nextStatement();
// validate hasReputation property //
if(!statement.getObject().isResource()) {
if(!ModelException.throwException(ModelException.ENTITY,
"hasReputation property of Entity resource:"+
resource.getURI()+" is not a resource")) {
return null;
}
} else {
ReputationObject repObj = (ReputationObject) getResourceFromCache(
statement.getObject().asResource(), ReputationObject.class);
if(repObj == null) {
repObj = getReputationObject(model,
statement.getObject().asResource());
}
entity.addHasReputation(repObj);
}
}
// hasValue //
Property hasValue = ResourceFactory.createProperty(
riNamespace + "hasValue");
stmtI1 = model.listStatements(resource,
hasValue, (RDFNode)null);
while(stmtI1.hasNext()) {
Statement statement = stmtI1.nextStatement();
// validate hasValue property //
if(!statement.getObject().isResource()) {
if(!ModelException.throwException(ModelException.ENTITY,
"hasValue property of Entity resource:"+
resource.getURI()+" is not a resource")) {
return null;
}
} else {
ReputationValue repVal = (ReputationValue) getResourceFromCache(
statement.getObject().asResource(), ReputationValue.class);
if(repVal == null) {
repVal = getReputationValue(model,
statement.getObject().asResource());
}
entity.addHasValue(repVal);
}
}
// hasEvaluation //
Property hasEvaluation = ResourceFactory.createProperty(
riNamespace + "hasEvaluation");
stmtI1 = model.listStatements(resource,
hasEvaluation, (RDFNode)null);
while(stmtI1.hasNext()) {
Statement statement = stmtI1.nextStatement();
// validate hasEvaluation property //
if(!statement.getObject().isResource()) {
if(!ModelException.throwException(ModelException.ENTITY,
"hasEvaluation property of Entity resource:"+
resource.getURI()+" is not a resource")) {
return null;
}
} else {
ReputationEvaluation repEva = (ReputationEvaluation) getResourceFromCache(
statement.getObject().asResource(), ReputationEvaluation.class);
if(repEva == null) {
repEva = getReputationEvaluation(model,
statement.getObject().asResource());
}
entity.addHasEvaluation(repEva);
}
}
// foafAccount //
Property foafAccount = ResourceFactory.createProperty(
foafNamespace + "account");
stmtI1 = model.listStatements(resource,
foafAccount, (RDFNode)null);
while(stmtI1.hasNext()) {
Statement statement = stmtI1.nextStatement();
// validate foafAccount property //
if(!statement.getObject().isResource()) {
if(!ModelException.throwException(ModelException.ENTITY,
"foafAccount property of Entity resource:"+
resource.getURI()+" is not a resource")) {
return null;
}
} else {
entity.addOnlineAccount(getFoafOnlineAccount(model,
statement.getObject().asResource()));
}
}
// foafHoldsAccount //
Property foafHoldsAccount = ResourceFactory.createProperty(
foafNamespace + "holdsAccount");
stmtI1 = model.listStatements(resource,
foafHoldsAccount, (RDFNode)null);
while(stmtI1.hasNext()) {
Statement statement = stmtI1.nextStatement();
// validate foafHoldsAccount property //
if(!statement.getObject().isResource()) {
if(!ModelException.throwException(ModelException.ENTITY,
"foafHoldsAccount property of Entity resource:"+
resource.getURI()+" is not a resource")) {
return null;
}
} else {
entity.addOnlineAccount(getFoafOnlineAccount(model,
statement.getObject().asResource()));
}
}
return entity;
}