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.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
import com.hp.hpl.jena.rdf.model.ResIterator;
|
|
||||||
import com.hp.hpl.jena.rdf.model.Resource;
|
import com.hp.hpl.jena.rdf.model.Resource;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -28,18 +27,12 @@ public class AllThumbsAdjuster extends FsuScanner {
|
||||||
updateLog.section("Creating main images for thumbnails "
|
updateLog.section("Creating main images for thumbnails "
|
||||||
+ "that have none.");
|
+ "that have none.");
|
||||||
|
|
||||||
ResIterator haveThumb = model.listResourcesWithProperty(thumbProperty);
|
for (Resource resource : ModelWrapper.listResourcesWithProperty(model,
|
||||||
try {
|
thumbProperty)) {
|
||||||
while (haveThumb.hasNext()) {
|
if (ResourceWrapper.getProperty(resource, imageProperty) == null) {
|
||||||
Resource resource = haveThumb.next();
|
|
||||||
|
|
||||||
if (resource.getProperty(imageProperty) == null) {
|
|
||||||
createMainImageFromThumbnail(resource);
|
createMainImageFromThumbnail(resource);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} finally {
|
|
||||||
haveThumb.close();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -62,12 +55,10 @@ public class AllThumbsAdjuster extends FsuScanner {
|
||||||
File mainFile = imageDirectoryWithBackup.getNewfile(mainFilename);
|
File mainFile = imageDirectoryWithBackup.getNewfile(mainFilename);
|
||||||
mainFile = checkNameConflicts(mainFile);
|
mainFile = checkNameConflicts(mainFile);
|
||||||
FileUtil.copyFile(thumbFile, mainFile);
|
FileUtil.copyFile(thumbFile, mainFile);
|
||||||
|
ResourceWrapper.addProperty(resource, imageProperty, mainFilename);
|
||||||
resource.addProperty(imageProperty, mainFilename);
|
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
updateLog.error(resource, "failed to create main file '"
|
updateLog.error(resource, "failed to create main file '"
|
||||||
+ mainFilename + "'", e);
|
+ 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.Literal;
|
||||||
import com.hp.hpl.jena.rdf.model.Property;
|
import com.hp.hpl.jena.rdf.model.Property;
|
||||||
import com.hp.hpl.jena.rdf.model.RDFNode;
|
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.Resource;
|
||||||
import com.hp.hpl.jena.rdf.model.Statement;
|
import com.hp.hpl.jena.rdf.model.Statement;
|
||||||
|
|
||||||
|
@ -39,15 +38,10 @@ public class DeadEndPropertyRemover extends FsuScanner {
|
||||||
* Check all of the individuals that possess this property.
|
* Check all of the individuals that possess this property.
|
||||||
*/
|
*/
|
||||||
private void removeDeadEndProperties(Property prop, String label) {
|
private void removeDeadEndProperties(Property prop, String label) {
|
||||||
ResIterator resources = model.listResourcesWithProperty(prop);
|
for (Resource resource : ModelWrapper.listResourcesWithProperty(model,
|
||||||
try {
|
prop)) {
|
||||||
while (resources.hasNext()) {
|
|
||||||
Resource resource = resources.next();
|
|
||||||
removeDeadEndPropertiesFromResource(resource, prop, label);
|
removeDeadEndPropertiesFromResource(resource, prop, label);
|
||||||
}
|
}
|
||||||
} finally {
|
|
||||||
resources.close();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -67,7 +61,8 @@ public class DeadEndPropertyRemover extends FsuScanner {
|
||||||
"removing link to " + label + " '" + filename
|
"removing link to " + label + " '" + filename
|
||||||
+ "': file does not exist at '"
|
+ "': file does not exist at '"
|
||||||
+ file.getAbsolutePath() + "'.");
|
+ 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.Resource;
|
||||||
import com.hp.hpl.jena.rdf.model.Statement;
|
import com.hp.hpl.jena.rdf.model.Statement;
|
||||||
import com.hp.hpl.jena.rdf.model.StmtIterator;
|
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
|
* 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) {
|
protected List<Statement> getStatements(Resource resource, Property property) {
|
||||||
List<Statement> list = new ArrayList<Statement>();
|
List<Statement> list = new ArrayList<Statement>();
|
||||||
|
|
||||||
|
resource.getModel().enterCriticalSection(Lock.READ);
|
||||||
StmtIterator stmts = resource.listProperties(property);
|
StmtIterator stmts = resource.listProperties(property);
|
||||||
try {
|
try {
|
||||||
while (stmts.hasNext()) {
|
while (stmts.hasNext()) {
|
||||||
|
@ -73,6 +76,7 @@ public abstract class FsuScanner {
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
stmts.close();
|
stmts.close();
|
||||||
|
resource.getModel().leaveCriticalSection();
|
||||||
}
|
}
|
||||||
return list;
|
return list;
|
||||||
}
|
}
|
||||||
|
|
|
@ -81,7 +81,7 @@ public class ImageSchemaTranslater extends FsuScanner {
|
||||||
|
|
||||||
translateMainImage(resource, mainImages.get(0));
|
translateMainImage(resource, mainImages.get(0));
|
||||||
translated.add(mainImages.get(0));
|
translated.add(mainImages.get(0));
|
||||||
resource.removeAll(imageProperty);
|
ResourceWrapper.removeAll(resource, imageProperty);
|
||||||
|
|
||||||
List<String> thumbnails = getValues(resource, thumbProperty);
|
List<String> thumbnails = getValues(resource, thumbProperty);
|
||||||
if (thumbnails.size() != 1) {
|
if (thumbnails.size() != 1) {
|
||||||
|
@ -92,7 +92,7 @@ public class ImageSchemaTranslater extends FsuScanner {
|
||||||
|
|
||||||
translateThumbnail(resource, thumbnails.get(0));
|
translateThumbnail(resource, thumbnails.get(0));
|
||||||
translated.add(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.Literal;
|
||||||
import com.hp.hpl.jena.rdf.model.Property;
|
import com.hp.hpl.jena.rdf.model.Property;
|
||||||
import com.hp.hpl.jena.rdf.model.RDFNode;
|
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.Resource;
|
||||||
import com.hp.hpl.jena.rdf.model.Statement;
|
import com.hp.hpl.jena.rdf.model.Statement;
|
||||||
|
|
||||||
|
@ -37,15 +36,10 @@ public class MultiplePropertyRemover extends FsuScanner {
|
||||||
* Check each resource that has this property.
|
* Check each resource that has this property.
|
||||||
*/
|
*/
|
||||||
public void removeExtraProperties(Property prop, String label) {
|
public void removeExtraProperties(Property prop, String label) {
|
||||||
ResIterator resources = model.listResourcesWithProperty(prop);
|
for (Resource resource : ModelWrapper.listResourcesWithProperty(model,
|
||||||
try {
|
prop)) {
|
||||||
while (resources.hasNext()) {
|
|
||||||
Resource resource = resources.next();
|
|
||||||
removeExtraProperties(resource, prop, label);
|
removeExtraProperties(resource, prop, label);
|
||||||
}
|
}
|
||||||
} finally {
|
|
||||||
resources.close();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -65,7 +59,7 @@ public class MultiplePropertyRemover extends FsuScanner {
|
||||||
updateLog.warn(resource, "removing extra " + label
|
updateLog.warn(resource, "removing extra " + label
|
||||||
+ " property: '" + node + "'");
|
+ " property: '" + node + "'");
|
||||||
}
|
}
|
||||||
model.remove(stmt);
|
ModelWrapper.removeStatement(model, stmt);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,7 +10,6 @@ import java.io.IOException;
|
||||||
|
|
||||||
import javax.imageio.ImageIO;
|
import javax.imageio.ImageIO;
|
||||||
|
|
||||||
import com.hp.hpl.jena.rdf.model.ResIterator;
|
|
||||||
import com.hp.hpl.jena.rdf.model.Resource;
|
import com.hp.hpl.jena.rdf.model.Resource;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -32,18 +31,12 @@ public class NoThumbsAdjuster extends FsuScanner {
|
||||||
public void adjust() {
|
public void adjust() {
|
||||||
updateLog.section("Creating thumbnails to match main images.");
|
updateLog.section("Creating thumbnails to match main images.");
|
||||||
|
|
||||||
ResIterator haveImage = model.listResourcesWithProperty(imageProperty);
|
for (Resource resource : ModelWrapper.listResourcesWithProperty(model,
|
||||||
try {
|
imageProperty)) {
|
||||||
while (haveImage.hasNext()) {
|
|
||||||
Resource resource = haveImage.next();
|
|
||||||
|
|
||||||
if (resource.getProperty(thumbProperty) == null) {
|
if (resource.getProperty(thumbProperty) == null) {
|
||||||
createThumbnailFromMainImage(resource);
|
createThumbnailFromMainImage(resource);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} finally {
|
|
||||||
haveImage.close();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -67,8 +60,7 @@ public class NoThumbsAdjuster extends FsuScanner {
|
||||||
generateThumbnailImage(mainFile, thumbFile,
|
generateThumbnailImage(mainFile, thumbFile,
|
||||||
FileStorageUpdater.THUMBNAIL_WIDTH,
|
FileStorageUpdater.THUMBNAIL_WIDTH,
|
||||||
FileStorageUpdater.THUMBNAIL_HEIGHT);
|
FileStorageUpdater.THUMBNAIL_HEIGHT);
|
||||||
|
ResourceWrapper.addProperty(resource, thumbProperty, thumbFilename);
|
||||||
resource.addProperty(thumbProperty, thumbFilename);
|
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
updateLog.error(resource, "failed to create thumbnail file '"
|
updateLog.error(resource, "failed to create thumbnail file '"
|
||||||
+ thumbFilename + "'", e);
|
+ 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.Literal;
|
||||||
import com.hp.hpl.jena.rdf.model.Property;
|
import com.hp.hpl.jena.rdf.model.Property;
|
||||||
import com.hp.hpl.jena.rdf.model.RDFNode;
|
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.Resource;
|
||||||
import com.hp.hpl.jena.rdf.model.Statement;
|
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.
|
* All image properties should have literal values. Burn any that don't.
|
||||||
|
@ -37,15 +36,10 @@ public class NonLiteralPropertyRemover extends FsuScanner {
|
||||||
* Check all resources for bogus values on this property.
|
* Check all resources for bogus values on this property.
|
||||||
*/
|
*/
|
||||||
private void removeNonLiterals(Property prop, String label) {
|
private void removeNonLiterals(Property prop, String label) {
|
||||||
ResIterator resources = model.listResourcesWithProperty(prop);
|
for (Resource resource : ModelWrapper.listResourcesWithProperty(model,
|
||||||
try {
|
prop)) {
|
||||||
while (resources.hasNext()) {
|
|
||||||
Resource resource = resources.next();
|
|
||||||
removeNonLiterals(resource, prop, label);
|
removeNonLiterals(resource, prop, label);
|
||||||
}
|
}
|
||||||
} finally {
|
|
||||||
resources.close();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -54,24 +48,23 @@ public class NonLiteralPropertyRemover extends FsuScanner {
|
||||||
private void removeNonLiterals(Resource resource, Property prop,
|
private void removeNonLiterals(Resource resource, Property prop,
|
||||||
String label) {
|
String label) {
|
||||||
List<RDFNode> bogusValues = new ArrayList<RDFNode>();
|
List<RDFNode> bogusValues = new ArrayList<RDFNode>();
|
||||||
StmtIterator stmts = resource.listProperties(prop);
|
for (Statement stmt : ResourceWrapper.listProperties(resource, prop)) {
|
||||||
try {
|
|
||||||
while (stmts.hasNext()) {
|
|
||||||
Statement stmt = stmts.next();
|
|
||||||
RDFNode object = stmt.getObject();
|
RDFNode object = stmt.getObject();
|
||||||
if (!object.isLiteral()) {
|
if (!object.isLiteral()) {
|
||||||
bogusValues.add(object);
|
bogusValues.add(object);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} finally {
|
|
||||||
stmts.close();
|
|
||||||
}
|
|
||||||
|
|
||||||
for (RDFNode bogusValue : bogusValues) {
|
for (RDFNode bogusValue : bogusValues) {
|
||||||
updateLog.warn(resource, "discarding " + label
|
updateLog.warn(resource, "discarding " + label
|
||||||
+ " property with non-literal as object: '" + bogusValue
|
+ " property with non-literal as object: '" + bogusValue
|
||||||
+ "'");
|
+ "'");
|
||||||
|
model.enterCriticalSection(Lock.WRITE);
|
||||||
|
try {
|
||||||
model.createStatement(resource, prop, bogusValue).remove();
|
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