Skip to content

Commit

Permalink
[#2] Metadata template development - added saveTemplateToSystemMetada…
Browse files Browse the repository at this point in the history
…taOnObject and tests
  • Loading branch information
rskarbez committed Feb 8, 2017
1 parent de9d709 commit f371a12
Show file tree
Hide file tree
Showing 3 changed files with 332 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@

import org.irods.jargon.core.connection.IRODSAccount;
import org.irods.jargon.core.exception.JargonException;
import org.irods.jargon.core.pub.DataObjectAO;
import org.irods.jargon.core.pub.CollectionAO;
import org.irods.jargon.core.pub.FileCatalogObjectAO;
import org.irods.jargon.core.pub.IRODSAccessObjectFactory;
import org.irods.jargon.core.pub.domain.AvuData;
import org.irods.jargon.core.pub.domain.Collection;
Expand Down Expand Up @@ -830,6 +833,72 @@ public boolean deleteTemplateByFqName(String fqName) {
return inFile.delete();
}

/**
* Save the values in the MetadataTemplate onto the object in the system
* metadata table.
*
*
* @param metadataTemplate
* @param pathToObject
*
* @throws FileNotFoundException
* @throws JargonException
*
*/
public void saveTemplateToSystemMetadataOnObject(
MetadataTemplate metadataTemplate, String pathToObject)
throws FileNotFoundException, JargonException {
log.info("saveTemplateToSystemMetadataOnObject()");

if (pathToObject == null || pathToObject.isEmpty()) {
throw new IllegalArgumentException("pathToObject is null or empty");
}

if (metadataTemplate == null) {
throw new IllegalArgumentException("metadataTemplate is null");
}

IRODSFile irodsObject = irodsAccessObjectFactory.getIRODSFileFactory(
irodsAccount).instanceIRODSFile(pathToObject);

if (!irodsObject.exists()) {
throw new FileNotFoundException(
"pathToObject does not resolve to an iRODS object");
}

FileCatalogObjectAO objectAO = null;

if (irodsObject.isFile()) {
objectAO = irodsAccessObjectFactory.getDataObjectAO(irodsAccount);
} else if (irodsObject.isDirectory()) {
objectAO = irodsAccessObjectFactory.getCollectionAO(irodsAccount);
} else {
throw new IllegalArgumentException(
"object at "
+ pathToObject
+ " is neither a data object nor a collection - the JargonMetadataResolver currently only supports these types of objects");
}

if (metadataTemplate.getType() == TemplateTypeEnum.FORM_BASED) {
for (MetadataElement me : ((FormBasedMetadataTemplate) metadataTemplate)
.getElements()) {
if (!me.getCurrentValue().isEmpty()) {
AvuData avuData = AvuData.instance(me.getName(),
me.getCurrentValue(),
JargonMetadataTemplateConstants.AVU_UNIT_PREFIX
+ metadataTemplate.getUuid().toString());
if (irodsObject.isFile()) {
((DataObjectAO) objectAO).addAVUMetadata(pathToObject,
avuData);
} else if (irodsObject.isDirectory()) {
((CollectionAO) objectAO).addAVUMetadata(pathToObject,
avuData);
}
}
}
} // TODO else if for different TemplateTypeEnum types
}

/**
* Populate metadata templates from a list of AVUs
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,15 @@
import junit.framework.Assert;

import org.irods.jargon.core.connection.IRODSAccount;
import org.irods.jargon.core.exception.JargonException;
import org.irods.jargon.core.pub.DataTransferOperations;
import org.irods.jargon.core.pub.IRODSAccessObjectFactory;
import org.irods.jargon.core.pub.IRODSFileSystem;
import org.irods.jargon.core.pub.domain.AvuData;
import org.irods.jargon.core.pub.io.IRODSFile;
import org.irods.jargon.core.query.AVUQueryElement;
import org.irods.jargon.core.query.MetaDataAndDomainData;
import org.irods.jargon.core.query.QueryConditionOperators;
import org.irods.jargon.dataprofile.accessor.DataProfileAccessorServiceImpl;
import org.irods.jargon.testutils.TestingPropertiesHelper;
import org.junit.After;
Expand Down Expand Up @@ -2305,14 +2308,15 @@ public void getAndMergeTemplateWithRefIrodsQueryForFile() throws Exception {
} else if (me.getName().equalsIgnoreCase("user_type")) {
Assert.assertEquals("user.type not in currentValue",
"user.type", me.getCurrentValue());
Assert.assertEquals("user.type not populated",
"rodsuser", me.getDisplayValue());
Assert.assertEquals("user.type not populated", "rodsuser",
me.getDisplayValue());
}
}
}

@Test
public void getAndMergeTemplateWithRefIrodsQueryForCollection() throws Exception {
public void getAndMergeTemplateWithRefIrodsQueryForCollection()
throws Exception {
String testDirName1 = "getAndMergeTemplateWithRefIrodsQueryForCollectionDir1";

String targetIrodsCollection1 = testingPropertiesHelper
Expand Down Expand Up @@ -2383,5 +2387,259 @@ public void getAndMergeTemplateWithRefIrodsQueryForCollection() throws Exception
}
}

@Test
public void saveTemplateToSystemMetadataForDataObject() throws Exception {
String testDirName1 = "saveTemplateToSystemMetadataForDataObjectDir";

String targetIrodsCollection1 = testingPropertiesHelper
.buildIRODSCollectionAbsolutePathFromTestProperties(
testingProperties, IRODS_TEST_SUBDIR_PATH + '/'
+ testDirName1);

IRODSAccount irodsAccount = testingPropertiesHelper
.buildIRODSAccountFromTestProperties(testingProperties);

IRODSAccessObjectFactory accessObjectFactory = irodsFileSystem
.getIRODSAccessObjectFactory();

IRODSFile targetCollectionAsFile1 = accessObjectFactory
.getIRODSFileFactory(irodsAccount).instanceIRODSFile(
targetIrodsCollection1);

targetCollectionAsFile1.mkdirs();

JargonMetadataResolver resolver = new JargonMetadataResolver(
irodsAccount, accessObjectFactory);

String mdTemplatePath1 = resolver
.findOrCreateMetadataTemplatesCollection(targetIrodsCollection1);

DataTransferOperations dataTransferOperations = irodsFileSystem
.getIRODSAccessObjectFactory().getDataTransferOperations(
irodsAccount);

dataTransferOperations.putOperation(TEMPLATE_FILE_NAME1,
mdTemplatePath1, irodsAccount.getDefaultStorageResource(),
null, null);

String templateFqName1 = mdTemplatePath1 + '/' + TEMPLATE_NOPATH1;

UUID uuid1 = UUID.randomUUID();
AvuData avuData = AvuData.instance("test1", uuid1.toString(),
JargonMetadataTemplateConstants.MD_TEMPLATE_UNIT);
accessObjectFactory.getDataObjectAO(irodsAccount).addAVUMetadata(
templateFqName1, avuData);

// Create a file in targetIrodsCollection1
dataTransferOperations.putOperation(TEST_FILE_NAME,
targetIrodsCollection1,
irodsAccount.getDefaultStorageResource(), null, null);
String testFileNameFQ = targetIrodsCollection1 + '/' + TEST_FILE_NOPATH;

MetadataTemplate metadataTemplate = resolver
.findTemplateByFqName(templateFqName1);
FormBasedMetadataTemplate fbmt = null;

if (metadataTemplate.getType() == TemplateTypeEnum.FORM_BASED) {
fbmt = (FormBasedMetadataTemplate) metadataTemplate;
}

for (MetadataElement me : fbmt.getElements()) {
if (me.getName().equalsIgnoreCase("attribute1")) {
me.setCurrentValue("value1");
} else if (me.getName().equalsIgnoreCase("attribute2")) {
me.setCurrentValue("42");
} else if (me.getName().equalsIgnoreCase("optional1")) {
me.setCurrentValue("optional_value1");
}
}

resolver.saveTemplateToSystemMetadataOnObject(fbmt, testFileNameFQ);

List<AVUQueryElement> queryElements = new ArrayList<AVUQueryElement>();
List<MetaDataAndDomainData> queryResult = new ArrayList<MetaDataAndDomainData>();

queryElements.add(AVUQueryElement.instanceForValueQuery(
AVUQueryElement.AVUQueryPart.ATTRIBUTE,
QueryConditionOperators.EQUAL, "attribute1"));

queryResult = accessObjectFactory.getDataObjectAO(irodsAccount)
.findMetadataValuesForDataObjectUsingAVUQuery(queryElements,
testFileNameFQ);

Assert.assertTrue(
"saveTemplateToSystemMetadataOnObject did not create Attribute attribute1",
!queryResult.isEmpty());

for (MetaDataAndDomainData mdd : queryResult) {
Assert.assertEquals("attribute1 has wrong value", "value1",
mdd.getAvuValue());
}

queryElements.clear();

queryElements.add(AVUQueryElement.instanceForValueQuery(
AVUQueryElement.AVUQueryPart.ATTRIBUTE,
QueryConditionOperators.EQUAL, "attribute2"));

queryResult = accessObjectFactory.getDataObjectAO(irodsAccount)
.findMetadataValuesForDataObjectUsingAVUQuery(queryElements,
testFileNameFQ);

Assert.assertTrue(
"saveTemplateToSystemMetadataOnObject did not create Attribute attribute2",
!queryResult.isEmpty());

for (MetaDataAndDomainData mdd : queryResult) {
Assert.assertEquals("attribute2 has wrong value", "42",
mdd.getAvuValue());
}

queryElements.clear();

queryElements.add(AVUQueryElement.instanceForValueQuery(
AVUQueryElement.AVUQueryPart.ATTRIBUTE,
QueryConditionOperators.EQUAL, "optional1"));

queryResult = accessObjectFactory.getDataObjectAO(irodsAccount)
.findMetadataValuesForDataObjectUsingAVUQuery(queryElements,
testFileNameFQ);

Assert.assertTrue(
"saveTemplateToSystemMetadataOnObject did not create Attribute optional1",
!queryResult.isEmpty());

for (MetaDataAndDomainData mdd : queryResult) {
Assert.assertEquals("attribute2 has wrong value",
"optional_value1", mdd.getAvuValue());
}
}

@Test
public void saveTemplateToSystemMetadataForCollection() throws Exception {
String testDirName1 = "saveTemplateToSystemMetadataForCollectionDir";

String targetIrodsCollection1 = testingPropertiesHelper
.buildIRODSCollectionAbsolutePathFromTestProperties(
testingProperties, IRODS_TEST_SUBDIR_PATH + '/'
+ testDirName1);

IRODSAccount irodsAccount = testingPropertiesHelper
.buildIRODSAccountFromTestProperties(testingProperties);

IRODSAccessObjectFactory accessObjectFactory = irodsFileSystem
.getIRODSAccessObjectFactory();

IRODSFile targetCollectionAsFile1 = accessObjectFactory
.getIRODSFileFactory(irodsAccount).instanceIRODSFile(
targetIrodsCollection1);

targetCollectionAsFile1.mkdirs();

JargonMetadataResolver resolver = new JargonMetadataResolver(
irodsAccount, accessObjectFactory);

String mdTemplatePath1 = resolver
.findOrCreateMetadataTemplatesCollection(targetIrodsCollection1);

DataTransferOperations dataTransferOperations = irodsFileSystem
.getIRODSAccessObjectFactory().getDataTransferOperations(
irodsAccount);

dataTransferOperations.putOperation(TEMPLATE_FILE_NAME1,
mdTemplatePath1, irodsAccount.getDefaultStorageResource(),
null, null);

String templateFqName1 = mdTemplatePath1 + '/' + TEMPLATE_NOPATH1;

UUID uuid1 = UUID.randomUUID();
AvuData avuData = AvuData.instance("test1", uuid1.toString(),
JargonMetadataTemplateConstants.MD_TEMPLATE_UNIT);
accessObjectFactory.getDataObjectAO(irodsAccount).addAVUMetadata(
templateFqName1, avuData);

// // Create a file in targetIrodsCollection1
// dataTransferOperations.putOperation(TEST_FILE_NAME,
// targetIrodsCollection1,
// irodsAccount.getDefaultStorageResource(), null, null);
// String testFileNameFQ = targetIrodsCollection1 + '/' + TEST_FILE_NOPATH;

MetadataTemplate metadataTemplate = resolver
.findTemplateByFqName(templateFqName1);
FormBasedMetadataTemplate fbmt = null;

if (metadataTemplate.getType() == TemplateTypeEnum.FORM_BASED) {
fbmt = (FormBasedMetadataTemplate) metadataTemplate;
}

for (MetadataElement me : fbmt.getElements()) {
if (me.getName().equalsIgnoreCase("attribute1")) {
me.setCurrentValue("value1");
} else if (me.getName().equalsIgnoreCase("attribute2")) {
me.setCurrentValue("42");
} else if (me.getName().equalsIgnoreCase("optional1")) {
me.setCurrentValue("optional_value1");
}
}

resolver.saveTemplateToSystemMetadataOnObject(fbmt, targetIrodsCollection1);

List<AVUQueryElement> queryElements = new ArrayList<AVUQueryElement>();
List<MetaDataAndDomainData> queryResult = new ArrayList<MetaDataAndDomainData>();

queryElements.add(AVUQueryElement.instanceForValueQuery(
AVUQueryElement.AVUQueryPart.ATTRIBUTE,
QueryConditionOperators.EQUAL, "attribute1"));

queryResult = accessObjectFactory.getCollectionAO(irodsAccount)
.findMetadataValuesByMetadataQueryForCollection(queryElements,
targetIrodsCollection1);

Assert.assertTrue(
"saveTemplateToSystemMetadataOnObject did not create Attribute attribute1",
!queryResult.isEmpty());

for (MetaDataAndDomainData mdd : queryResult) {
Assert.assertEquals("attribute1 has wrong value", "value1",
mdd.getAvuValue());
}

queryElements.clear();

queryElements.add(AVUQueryElement.instanceForValueQuery(
AVUQueryElement.AVUQueryPart.ATTRIBUTE,
QueryConditionOperators.EQUAL, "attribute2"));

queryResult = accessObjectFactory.getCollectionAO(irodsAccount)
.findMetadataValuesByMetadataQueryForCollection(queryElements,
targetIrodsCollection1);

Assert.assertTrue(
"saveTemplateToSystemMetadataOnObject did not create Attribute attribute2",
!queryResult.isEmpty());

for (MetaDataAndDomainData mdd : queryResult) {
Assert.assertEquals("attribute2 has wrong value", "42",
mdd.getAvuValue());
}

queryElements.clear();

queryElements.add(AVUQueryElement.instanceForValueQuery(
AVUQueryElement.AVUQueryPart.ATTRIBUTE,
QueryConditionOperators.EQUAL, "optional1"));

queryResult = accessObjectFactory.getCollectionAO(irodsAccount)
.findMetadataValuesByMetadataQueryForCollection(queryElements,
targetIrodsCollection1);

Assert.assertTrue(
"saveTemplateToSystemMetadataOnObject did not create Attribute optional1",
!queryResult.isEmpty());

for (MetaDataAndDomainData mdd : queryResult) {
Assert.assertEquals("attribute2 has wrong value",
"optional_value1", mdd.getAvuValue());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
{
"name" : "attribute1",
"description" : "First attribute",
"required" : "true",
"required" : true,
"type" : "RAW_STRING"
},
{
Expand Down

0 comments on commit f371a12

Please sign in to comment.