NIHVIVO-962 Merge 5536 from branch
This commit is contained in:
parent
77bba699bf
commit
6bb9980fc3
9 changed files with 147 additions and 70 deletions
|
@ -5,7 +5,6 @@ package edu.cornell.mannlib.vitro.webapp.filestorage.updater;
|
|||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
import com.hp.hpl.jena.rdf.model.ResIterator;
|
||||
import com.hp.hpl.jena.rdf.model.Resource;
|
||||
|
||||
/**
|
||||
|
@ -28,17 +27,11 @@ public class AllThumbsAdjuster extends FsuScanner {
|
|||
updateLog.section("Creating main images for thumbnails "
|
||||
+ "that have none.");
|
||||
|
||||
ResIterator haveThumb = model.listResourcesWithProperty(thumbProperty);
|
||||
try {
|
||||
while (haveThumb.hasNext()) {
|
||||
Resource resource = haveThumb.next();
|
||||
|
||||
if (resource.getProperty(imageProperty) == null) {
|
||||
createMainImageFromThumbnail(resource);
|
||||
}
|
||||
for (Resource resource : ModelWrapper.listResourcesWithProperty(model,
|
||||
thumbProperty)) {
|
||||
if (ResourceWrapper.getProperty(resource, imageProperty) == null) {
|
||||
createMainImageFromThumbnail(resource);
|
||||
}
|
||||
} finally {
|
||||
haveThumb.close();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -62,12 +55,10 @@ public class AllThumbsAdjuster extends FsuScanner {
|
|||
File mainFile = imageDirectoryWithBackup.getNewfile(mainFilename);
|
||||
mainFile = checkNameConflicts(mainFile);
|
||||
FileUtil.copyFile(thumbFile, mainFile);
|
||||
|
||||
resource.addProperty(imageProperty, mainFilename);
|
||||
ResourceWrapper.addProperty(resource, imageProperty, mainFilename);
|
||||
} catch (IOException e) {
|
||||
updateLog.error(resource, "failed to create main file '"
|
||||
+ mainFilename + "'", e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -7,7 +7,6 @@ import java.io.File;
|
|||
import com.hp.hpl.jena.rdf.model.Literal;
|
||||
import com.hp.hpl.jena.rdf.model.Property;
|
||||
import com.hp.hpl.jena.rdf.model.RDFNode;
|
||||
import com.hp.hpl.jena.rdf.model.ResIterator;
|
||||
import com.hp.hpl.jena.rdf.model.Resource;
|
||||
import com.hp.hpl.jena.rdf.model.Statement;
|
||||
|
||||
|
@ -39,14 +38,9 @@ public class DeadEndPropertyRemover extends FsuScanner {
|
|||
* Check all of the individuals that possess this property.
|
||||
*/
|
||||
private void removeDeadEndProperties(Property prop, String label) {
|
||||
ResIterator resources = model.listResourcesWithProperty(prop);
|
||||
try {
|
||||
while (resources.hasNext()) {
|
||||
Resource resource = resources.next();
|
||||
removeDeadEndPropertiesFromResource(resource, prop, label);
|
||||
}
|
||||
} finally {
|
||||
resources.close();
|
||||
for (Resource resource : ModelWrapper.listResourcesWithProperty(model,
|
||||
prop)) {
|
||||
removeDeadEndPropertiesFromResource(resource, prop, label);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -67,7 +61,8 @@ public class DeadEndPropertyRemover extends FsuScanner {
|
|||
"removing link to " + label + " '" + filename
|
||||
+ "': file does not exist at '"
|
||||
+ file.getAbsolutePath() + "'.");
|
||||
model.remove(stmt);
|
||||
|
||||
ModelWrapper.removeStatement(model, stmt);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -13,6 +13,7 @@ import com.hp.hpl.jena.rdf.model.RDFNode;
|
|||
import com.hp.hpl.jena.rdf.model.Resource;
|
||||
import com.hp.hpl.jena.rdf.model.Statement;
|
||||
import com.hp.hpl.jena.rdf.model.StmtIterator;
|
||||
import com.hp.hpl.jena.shared.Lock;
|
||||
|
||||
/**
|
||||
* Base class for the tools that scan the model. Holds some useful fields and
|
||||
|
@ -66,6 +67,8 @@ public abstract class FsuScanner {
|
|||
*/
|
||||
protected List<Statement> getStatements(Resource resource, Property property) {
|
||||
List<Statement> list = new ArrayList<Statement>();
|
||||
|
||||
resource.getModel().enterCriticalSection(Lock.READ);
|
||||
StmtIterator stmts = resource.listProperties(property);
|
||||
try {
|
||||
while (stmts.hasNext()) {
|
||||
|
@ -73,6 +76,7 @@ public abstract class FsuScanner {
|
|||
}
|
||||
} finally {
|
||||
stmts.close();
|
||||
resource.getModel().leaveCriticalSection();
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
|
|
@ -81,7 +81,7 @@ public class ImageSchemaTranslater extends FsuScanner {
|
|||
|
||||
translateMainImage(resource, mainImages.get(0));
|
||||
translated.add(mainImages.get(0));
|
||||
resource.removeAll(imageProperty);
|
||||
ResourceWrapper.removeAll(resource, imageProperty);
|
||||
|
||||
List<String> thumbnails = getValues(resource, thumbProperty);
|
||||
if (thumbnails.size() != 1) {
|
||||
|
@ -92,7 +92,7 @@ public class ImageSchemaTranslater extends FsuScanner {
|
|||
|
||||
translateThumbnail(resource, thumbnails.get(0));
|
||||
translated.add(thumbnails.get(0));
|
||||
resource.removeAll(thumbProperty);
|
||||
ResourceWrapper.removeAll(resource, thumbProperty);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -0,0 +1,45 @@
|
|||
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
|
||||
|
||||
package edu.cornell.mannlib.vitro.webapp.filestorage.updater;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
import com.hp.hpl.jena.rdf.model.Model;
|
||||
import com.hp.hpl.jena.rdf.model.Property;
|
||||
import com.hp.hpl.jena.rdf.model.ResIterator;
|
||||
import com.hp.hpl.jena.rdf.model.Resource;
|
||||
import com.hp.hpl.jena.rdf.model.Statement;
|
||||
import com.hp.hpl.jena.shared.Lock;
|
||||
|
||||
/**
|
||||
* Utility methods that operate against the Model with proper locks.
|
||||
*/
|
||||
public class ModelWrapper {
|
||||
|
||||
public static Collection<Resource> listResourcesWithProperty(Model model,
|
||||
Property property) {
|
||||
List<Resource> list = new ArrayList<Resource>();
|
||||
ResIterator iterator = model.listResourcesWithProperty(property);
|
||||
try {
|
||||
while (iterator.hasNext()) {
|
||||
Resource resource = iterator.next();
|
||||
list.add(resource);
|
||||
}
|
||||
} finally {
|
||||
iterator.close();
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
public static void removeStatement(Model model, Statement stmt) {
|
||||
model.enterCriticalSection(Lock.WRITE);
|
||||
try {
|
||||
model.remove(stmt);
|
||||
} finally {
|
||||
model.leaveCriticalSection();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -7,7 +7,6 @@ import java.util.List;
|
|||
import com.hp.hpl.jena.rdf.model.Literal;
|
||||
import com.hp.hpl.jena.rdf.model.Property;
|
||||
import com.hp.hpl.jena.rdf.model.RDFNode;
|
||||
import com.hp.hpl.jena.rdf.model.ResIterator;
|
||||
import com.hp.hpl.jena.rdf.model.Resource;
|
||||
import com.hp.hpl.jena.rdf.model.Statement;
|
||||
|
||||
|
@ -37,14 +36,9 @@ public class MultiplePropertyRemover extends FsuScanner {
|
|||
* Check each resource that has this property.
|
||||
*/
|
||||
public void removeExtraProperties(Property prop, String label) {
|
||||
ResIterator resources = model.listResourcesWithProperty(prop);
|
||||
try {
|
||||
while (resources.hasNext()) {
|
||||
Resource resource = resources.next();
|
||||
removeExtraProperties(resource, prop, label);
|
||||
}
|
||||
} finally {
|
||||
resources.close();
|
||||
for (Resource resource : ModelWrapper.listResourcesWithProperty(model,
|
||||
prop)) {
|
||||
removeExtraProperties(resource, prop, label);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -65,7 +59,7 @@ public class MultiplePropertyRemover extends FsuScanner {
|
|||
updateLog.warn(resource, "removing extra " + label
|
||||
+ " property: '" + node + "'");
|
||||
}
|
||||
model.remove(stmt);
|
||||
ModelWrapper.removeStatement(model, stmt);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -10,7 +10,6 @@ import java.io.IOException;
|
|||
|
||||
import javax.imageio.ImageIO;
|
||||
|
||||
import com.hp.hpl.jena.rdf.model.ResIterator;
|
||||
import com.hp.hpl.jena.rdf.model.Resource;
|
||||
|
||||
/**
|
||||
|
@ -32,17 +31,11 @@ public class NoThumbsAdjuster extends FsuScanner {
|
|||
public void adjust() {
|
||||
updateLog.section("Creating thumbnails to match main images.");
|
||||
|
||||
ResIterator haveImage = model.listResourcesWithProperty(imageProperty);
|
||||
try {
|
||||
while (haveImage.hasNext()) {
|
||||
Resource resource = haveImage.next();
|
||||
|
||||
if (resource.getProperty(thumbProperty) == null) {
|
||||
createThumbnailFromMainImage(resource);
|
||||
}
|
||||
for (Resource resource : ModelWrapper.listResourcesWithProperty(model,
|
||||
imageProperty)) {
|
||||
if (resource.getProperty(thumbProperty) == null) {
|
||||
createThumbnailFromMainImage(resource);
|
||||
}
|
||||
} finally {
|
||||
haveImage.close();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -67,8 +60,7 @@ public class NoThumbsAdjuster extends FsuScanner {
|
|||
generateThumbnailImage(mainFile, thumbFile,
|
||||
FileStorageUpdater.THUMBNAIL_WIDTH,
|
||||
FileStorageUpdater.THUMBNAIL_HEIGHT);
|
||||
|
||||
resource.addProperty(thumbProperty, thumbFilename);
|
||||
ResourceWrapper.addProperty(resource, thumbProperty, thumbFilename);
|
||||
} catch (IOException e) {
|
||||
updateLog.error(resource, "failed to create thumbnail file '"
|
||||
+ thumbFilename + "'", e);
|
||||
|
|
|
@ -8,10 +8,9 @@ import java.util.List;
|
|||
import com.hp.hpl.jena.rdf.model.Literal;
|
||||
import com.hp.hpl.jena.rdf.model.Property;
|
||||
import com.hp.hpl.jena.rdf.model.RDFNode;
|
||||
import com.hp.hpl.jena.rdf.model.ResIterator;
|
||||
import com.hp.hpl.jena.rdf.model.Resource;
|
||||
import com.hp.hpl.jena.rdf.model.Statement;
|
||||
import com.hp.hpl.jena.rdf.model.StmtIterator;
|
||||
import com.hp.hpl.jena.shared.Lock;
|
||||
|
||||
/**
|
||||
* All image properties should have literal values. Burn any that don't.
|
||||
|
@ -37,14 +36,9 @@ public class NonLiteralPropertyRemover extends FsuScanner {
|
|||
* Check all resources for bogus values on this property.
|
||||
*/
|
||||
private void removeNonLiterals(Property prop, String label) {
|
||||
ResIterator resources = model.listResourcesWithProperty(prop);
|
||||
try {
|
||||
while (resources.hasNext()) {
|
||||
Resource resource = resources.next();
|
||||
removeNonLiterals(resource, prop, label);
|
||||
}
|
||||
} finally {
|
||||
resources.close();
|
||||
for (Resource resource : ModelWrapper.listResourcesWithProperty(model,
|
||||
prop)) {
|
||||
removeNonLiterals(resource, prop, label);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -54,24 +48,23 @@ public class NonLiteralPropertyRemover extends FsuScanner {
|
|||
private void removeNonLiterals(Resource resource, Property prop,
|
||||
String label) {
|
||||
List<RDFNode> bogusValues = new ArrayList<RDFNode>();
|
||||
StmtIterator stmts = resource.listProperties(prop);
|
||||
try {
|
||||
while (stmts.hasNext()) {
|
||||
Statement stmt = stmts.next();
|
||||
RDFNode object = stmt.getObject();
|
||||
if (!object.isLiteral()) {
|
||||
bogusValues.add(object);
|
||||
}
|
||||
for (Statement stmt : ResourceWrapper.listProperties(resource, prop)) {
|
||||
RDFNode object = stmt.getObject();
|
||||
if (!object.isLiteral()) {
|
||||
bogusValues.add(object);
|
||||
}
|
||||
} finally {
|
||||
stmts.close();
|
||||
}
|
||||
|
||||
for (RDFNode bogusValue : bogusValues) {
|
||||
updateLog.warn(resource, "discarding " + label
|
||||
+ " property with non-literal as object: '" + bogusValue
|
||||
+ "'");
|
||||
model.createStatement(resource, prop, bogusValue).remove();
|
||||
model.enterCriticalSection(Lock.WRITE);
|
||||
try {
|
||||
model.createStatement(resource, prop, bogusValue).remove();
|
||||
} finally {
|
||||
model.leaveCriticalSection();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,63 @@
|
|||
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
|
||||
|
||||
package edu.cornell.mannlib.vitro.webapp.filestorage.updater;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
import com.hp.hpl.jena.rdf.model.Property;
|
||||
import com.hp.hpl.jena.rdf.model.Resource;
|
||||
import com.hp.hpl.jena.rdf.model.Statement;
|
||||
import com.hp.hpl.jena.rdf.model.StmtIterator;
|
||||
import com.hp.hpl.jena.shared.Lock;
|
||||
|
||||
/**
|
||||
* Utility methods that get the appropriate model locks before manipluating
|
||||
* resources.
|
||||
*/
|
||||
public class ResourceWrapper {
|
||||
|
||||
public static Statement getProperty(Resource resource, Property property) {
|
||||
resource.getModel().enterCriticalSection(Lock.READ);
|
||||
try {
|
||||
return resource.getProperty(property);
|
||||
} finally {
|
||||
resource.getModel().leaveCriticalSection();
|
||||
}
|
||||
}
|
||||
|
||||
public static void addProperty(Resource resource, Property property,
|
||||
String value) {
|
||||
resource.getModel().enterCriticalSection(Lock.WRITE);
|
||||
try {
|
||||
resource.addProperty(property, value);
|
||||
} finally {
|
||||
resource.getModel().leaveCriticalSection();
|
||||
}
|
||||
}
|
||||
|
||||
public static void removeAll(Resource resource, Property property) {
|
||||
resource.getModel().enterCriticalSection(Lock.WRITE);
|
||||
try {
|
||||
resource.removeAll(property);
|
||||
} finally {
|
||||
resource.getModel().leaveCriticalSection();
|
||||
}
|
||||
}
|
||||
|
||||
public static Collection<Statement> listProperties(Resource resource,
|
||||
Property prop) {
|
||||
List<Statement> list = new ArrayList<Statement>();
|
||||
StmtIterator stmts = resource.listProperties(prop);
|
||||
try {
|
||||
while (stmts.hasNext()) {
|
||||
list.add(stmts.next());
|
||||
}
|
||||
return list;
|
||||
} finally {
|
||||
stmts.close();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Loading…
Add table
Reference in a new issue