From f371a12ed738fe3853cb85c4b841154f415065f3 Mon Sep 17 00:00:00 2001 From: rskarbez Date: Wed, 8 Feb 2017 16:17:03 -0500 Subject: [PATCH] [#2] Metadata template development - added saveTemplateToSystemMetadataOnObject and tests --- .../JargonMetadataResolver.java | 69 +++++ .../JargonMetadataResolverTest.java | 266 +++++++++++++++++- .../test/resources/templates/test1.mdtemplate | 2 +- 3 files changed, 332 insertions(+), 5 deletions(-) diff --git a/metadata-templates/src/main/java/org/irods/jargon/metadatatemplate/JargonMetadataResolver.java b/metadata-templates/src/main/java/org/irods/jargon/metadatatemplate/JargonMetadataResolver.java index 9ce67ee..6dff1ab 100644 --- a/metadata-templates/src/main/java/org/irods/jargon/metadatatemplate/JargonMetadataResolver.java +++ b/metadata-templates/src/main/java/org/irods/jargon/metadatatemplate/JargonMetadataResolver.java @@ -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; @@ -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 * diff --git a/metadata-templates/src/test/java/org/irods/jargon/metadatatemplate/JargonMetadataResolverTest.java b/metadata-templates/src/test/java/org/irods/jargon/metadatatemplate/JargonMetadataResolverTest.java index 4b997aa..9624f8b 100644 --- a/metadata-templates/src/test/java/org/irods/jargon/metadatatemplate/JargonMetadataResolverTest.java +++ b/metadata-templates/src/test/java/org/irods/jargon/metadatatemplate/JargonMetadataResolverTest.java @@ -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; @@ -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 @@ -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 queryElements = new ArrayList(); + List queryResult = new ArrayList(); + 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 queryElements = new ArrayList(); + List queryResult = new ArrayList(); + + 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()); + } + } } diff --git a/metadata-templates/src/test/resources/templates/test1.mdtemplate b/metadata-templates/src/test/resources/templates/test1.mdtemplate index 54fe775..6e9e244 100644 --- a/metadata-templates/src/test/resources/templates/test1.mdtemplate +++ b/metadata-templates/src/test/resources/templates/test1.mdtemplate @@ -7,7 +7,7 @@ { "name" : "attribute1", "description" : "First attribute", - "required" : "true", + "required" : true, "type" : "RAW_STRING" }, {