List<Statement> stToAdd = new ArrayList<Statement>();
List<Statement> stToRemove = new ArrayList<Statement>();
Map<Resource,String> toChange = new HashMap<Resource,String>();
ModelSet model = DbFace.getModel();
URI type = model.createURI(RDF.type.toString());
URI webres = model.createURI(S3B_SSCF.WebResource);
URI seeAlso = RDFS.seeAlso;
//- find all uris
ClosableIterator<Statement> it = model.findStatements(null,null, type,webres);
while (it.hasNext())
{
Statement st = it.next();
Matcher resource = Pattern.compile("^(.+)#([a-zA-Z0-9]{40})$").matcher(st.getSubject().toString());
if(resource.matches())
{
if(resource.group(1)!=null)
{
ClosableIterator<Statement> it2 = null;
try
{
it2 = model.findStatements(null,st.getSubject(), seeAlso, null);
while(it2.hasNext())
if(it2.next().getObject().toString().equals(resource.group(1)))
{
String uriStart = resource.group(1);
if(uriStart.endsWith("/")) uriStart = uriStart.substring(0,uriStart.length()-1);
String newUri = uriStart+"__"+resource.group(2);
toChange.put(st.getSubject(),newUri);
}
}
catch (Exception e)
{
//
}
finally {
if(it2!=null)
it2.close();
}
}
}
}
it.close();
for(Resource res:toChange.keySet())
{
try
{
stToAdd.clear();
stToRemove.clear();
//1. get all triples with our subject
ClosableIterator<Statement> it3 = model.findStatements(null,res, null, null);
//create new uri
URI newUri = model.createURI(toChange.get(res));
sb.append("* Old URI - ");
sb.append(res.toString());
sb.append("<br>* New URI - ");
sb.append(newUri.toString());
sb.append("<br>= statements changed: ");
while(it3.hasNext())
{
Statement st = it3.next();
stToAdd.add(new StatementImpl(null,newUri, st.getPredicate(), st.getObject()));
stToRemove.add(st);
uriStetements++;
}
it3.close();
//adding and removing inside the statement iterator
model.addAll(stToAdd.iterator());
for(Statement st:stToRemove)
model.removeStatement(st);
sb.append(uriStetements+"<br>");
statementsChanged+=uriStetements;