diff --git a/source/java/org/alfresco/repo/bulkimport/impl/AbstractNodeImporter.java b/source/java/org/alfresco/repo/bulkimport/impl/AbstractNodeImporter.java index 9f26339719..b6f7456d07 100644 --- a/source/java/org/alfresco/repo/bulkimport/impl/AbstractNodeImporter.java +++ b/source/java/org/alfresco/repo/bulkimport/impl/AbstractNodeImporter.java @@ -55,7 +55,7 @@ import org.apache.commons.logging.LogFactory; * Abstract base class for the node importer, containing helper methods for use by subclasses. * * @since 4.0 - * + * */ public abstract class AbstractNodeImporter implements NodeImporter { @@ -69,39 +69,39 @@ public abstract class AbstractNodeImporter implements NodeImporter protected BehaviourFilter behaviourFilter; public void setVersionService(VersionService versionService) - { - this.versionService = versionService; - } + { + this.versionService = versionService; + } - public void setFileFolderService(FileFolderService fileFolderService) - { - this.fileFolderService = fileFolderService; - } + public void setFileFolderService(FileFolderService fileFolderService) + { + this.fileFolderService = fileFolderService; + } - public void setNodeService(NodeService nodeService) - { - this.nodeService = nodeService; - } + public void setNodeService(NodeService nodeService) + { + this.nodeService = nodeService; + } - public void setMetadataLoader(MetadataLoader metadataLoader) - { - this.metadataLoader = metadataLoader; - } + public void setMetadataLoader(MetadataLoader metadataLoader) + { + this.metadataLoader = metadataLoader; + } - public void setImportStatus(BulkImportStatusImpl importStatus) - { - this.importStatus = importStatus; - } + public void setImportStatus(BulkImportStatusImpl importStatus) + { + this.importStatus = importStatus; + } - public void setBehaviourFilter(BehaviourFilter behaviourFilter) - { - this.behaviourFilter = behaviourFilter; - } + public void setBehaviourFilter(BehaviourFilter behaviourFilter) + { + this.behaviourFilter = behaviourFilter; + } protected abstract NodeRef importImportableItemImpl(ImportableItem importableItem, boolean replaceExisting); protected abstract void importContentAndMetadata(NodeRef nodeRef, ImportableItem.ContentAndMetadata contentAndMetadata, MetadataLoader.Metadata metadata); - /* + /* * Because commons-lang ToStringBuilder doesn't seem to like unmodifiable Maps */ protected final String mapToString(Map map) @@ -121,27 +121,27 @@ public abstract class AbstractNodeImporter implements NodeImporter result.append(String.valueOf(map.get(key))); result.append(",\n"); } - + // Delete final dangling ", " value result.delete(result.length() - 2, result.length()); } - + result.append(']'); } else { result.append("(null)"); } - + return(result.toString()); } - + /** * Returns the name of the given importable item. This is the final name of the item, as it would appear in the repository, * after metadata renames are taken into account. * * @param importableItem The importableItem with which to - * @param metadata + * @param metadata * @return the name of the given importable item */ protected final String getImportableItemName(ImportableItem importableItem, MetadataLoader.Metadata metadata) @@ -153,7 +153,7 @@ public abstract class AbstractNodeImporter implements NodeImporter { result = (String)metadata.getProperties().get(ContentModel.PROP_NAME); } - + // Step 2: attempt to get name from metadata file if (result == null && importableItem != null && @@ -169,36 +169,36 @@ public abstract class AbstractNodeImporter implements NodeImporter (MetadataLoader.METADATA_SUFFIX.length() + metadataLoader.getMetadataFileExtension().length())); } } - + return(result); } - + protected final int importImportableItemFile(NodeRef nodeRef, ImportableItem importableItem, MetadataLoader.Metadata metadata) { - int result = 0; + int result = 0; - if (importableItem.hasVersionEntries()) - { - // If cm:versionable isn't listed as one of the aspects for this node, add it - cm:versionable is required for nodes that have versions - if (!metadata.getAspects().contains(ContentModel.ASPECT_VERSIONABLE)) - { - if (logger.isWarnEnabled()) logger.warn("Metadata for file '" + getFileName(importableItem.getHeadRevision().getContentFile()) + "' was missing the cm:versionable aspect, yet it has " + importableItem.getVersionEntries().size() + " versions. Adding cm:versionable."); - metadata.addAspect(ContentModel.ASPECT_VERSIONABLE); - } + if (importableItem.hasVersionEntries()) + { + // If cm:versionable isn't listed as one of the aspects for this node, add it - cm:versionable is required for nodes that have versions + if (!metadata.getAspects().contains(ContentModel.ASPECT_VERSIONABLE)) + { + if (logger.isWarnEnabled()) logger.warn("Metadata for file '" + getFileName(importableItem.getHeadRevision().getContentFile()) + "' was missing the cm:versionable aspect, yet it has " + importableItem.getVersionEntries().size() + " versions. Adding cm:versionable."); + metadata.addAspect(ContentModel.ASPECT_VERSIONABLE); + } - result = importContentVersions(nodeRef, importableItem); - } + result = importContentVersions(nodeRef, importableItem); + } else { importContentAndMetadata(nodeRef, importableItem.getHeadRevision(), metadata); } - return(result); + return(result); } protected final int importContentVersions(NodeRef nodeRef, ImportableItem importableItem) { - int result = 0; + int result = 0; Map versionProperties = new HashMap(); // Note: PROP_VERSION_LABEL is a "reserved" property, and cannot be modified by custom code. // In other words, we can't use the version label on disk as the version label in Alfresco. :-( @@ -206,16 +206,16 @@ public abstract class AbstractNodeImporter implements NodeImporter //versionProperties.put(ContentModel.PROP_VERSION_LABEL.toPrefixString(), String.valueOf(versionEntry.getVersion())); versionProperties.put(VersionModel.PROP_VERSION_TYPE, VersionType.MAJOR); // Load every version as a major version for now - see http://code.google.com/p/alfresco-bulk-filesystem-import/issues/detail?id=84 - for (final ImportableItem.VersionedContentAndMetadata versionEntry : importableItem.getVersionEntries()) - { - MetadataLoader.Metadata metadata = loadMetadata(versionEntry); - importContentAndMetadata(nodeRef, versionEntry, metadata); + for (final ImportableItem.VersionedContentAndMetadata versionEntry : importableItem.getVersionEntries()) + { + MetadataLoader.Metadata metadata = loadMetadata(versionEntry); + importContentAndMetadata(nodeRef, versionEntry, metadata); - if (logger.isDebugEnabled()) logger.debug("Creating v" + String.valueOf(versionEntry.getVersion()) + " of node '" + nodeRef.toString() + "' (note: version label in Alfresco will not be the same - it is not currently possible to explicitly force a particular version label)."); + if (logger.isDebugEnabled()) logger.debug("Creating v" + String.valueOf(versionEntry.getVersion()) + " of node '" + nodeRef.toString() + "' (note: version label in Alfresco will not be the same - it is not currently possible to explicitly force a particular version label)."); - versionService.createVersion(nodeRef, versionProperties); - result += metadata.getProperties().size() + 4; // Add 4 for "standard" metadata properties read from filesystem - } + versionService.createVersion(nodeRef, versionProperties); + result += metadata.getProperties().size() + 4; // Add 4 for "standard" metadata properties read from filesystem + } if (logger.isDebugEnabled()) logger.debug("Creating head revision of node " + nodeRef.toString()); ImportableItem.ContentAndMetadata contentAndMetadata = importableItem.getHeadRevision(); @@ -223,163 +223,163 @@ public abstract class AbstractNodeImporter implements NodeImporter importContentAndMetadata(nodeRef, importableItem.getHeadRevision(), metadata); versionService.createVersion(nodeRef, versionProperties); - return(result); + return(result); } protected final Triple createOrFindNode(NodeRef target, ImportableItem importableItem, - boolean replaceExisting, MetadataLoader.Metadata metadata) - { - Triple result = null; - boolean isDirectory = false; - NodeState nodeState = replaceExisting ? NodeState.REPLACED : NodeState.SKIPPED; - String nodeName = getImportableItemName(importableItem, metadata); - NodeRef nodeRef = null; + boolean replaceExisting, MetadataLoader.Metadata metadata) + { + Triple result = null; + boolean isDirectory = false; + NodeState nodeState = replaceExisting ? NodeState.REPLACED : NodeState.SKIPPED; + String nodeName = getImportableItemName(importableItem, metadata); + NodeRef nodeRef = null; - //####TODO: handle this more elegantly - if (nodeName == null) - { - throw new IllegalStateException("Unable to determine node name for " + String.valueOf(importableItem)); - } + //####TODO: handle this more elegantly + if (nodeName == null) + { + throw new IllegalStateException("Unable to determine node name for " + String.valueOf(importableItem)); + } - if (logger.isDebugEnabled()) - { - logger.debug("Searching for node with name '" + nodeName + "' within node '" + target.toString() + "'."); - } + if (logger.isDebugEnabled()) + { + logger.debug("Searching for node with name '" + nodeName + "' within node '" + target.toString() + "'."); + } - nodeRef = fileFolderService.searchSimple(target, nodeName); + nodeRef = fileFolderService.searchSimple(target, nodeName); - // If we didn't find an existing item, create a new node in the repo. - if (nodeRef == null) - { - // But only if the content file exists - we don't create new nodes based on metadata-only importableItems - if (importableItem.getHeadRevision().contentFileExists()) - { - isDirectory = ImportableItem.FileType.DIRECTORY.equals(importableItem.getHeadRevision().getContentFileType()); + // If we didn't find an existing item, create a new node in the repo. + if (nodeRef == null) + { + // But only if the content file exists - we don't create new nodes based on metadata-only importableItems + if (importableItem.getHeadRevision().contentFileExists()) + { + isDirectory = ImportableItem.FileType.DIRECTORY.equals(importableItem.getHeadRevision().getContentFileType()); - try - { - if (logger.isDebugEnabled()) logger.debug("Creating new node of type '" + metadata.getType().toString() + "' with name '" + nodeName + "' within node '" + target.toString() + "'."); - nodeRef = fileFolderService.create(target, nodeName, metadata.getType()).getNodeRef(); - nodeState = NodeState.CREATED; - } - catch (final FileExistsException fee) - { - if (logger.isWarnEnabled()) logger.warn("Node with name '" + nodeName + "' within node '" + target.toString() + "' was created concurrently to the bulk import. Skipping importing it.", fee); - nodeRef = null; - nodeState = NodeState.SKIPPED; - } - } - else - { - if (logger.isDebugEnabled()) logger.debug("Skipping creation of new node '" + nodeName + "' within node '" + target.toString() + "' since it doesn't have a content file."); - nodeRef = null; - nodeState = NodeState.SKIPPED; - } - } - // We found the node in the repository. Make sure we return the NodeRef, so that recursive loading works (we need the NodeRef of all sub-spaces, even if we didn't create them). - else - { - if (replaceExisting) - { - boolean targetNodeIsSpace = fileFolderService.getFileInfo(nodeRef).isFolder(); + try + { + if (logger.isDebugEnabled()) logger.debug("Creating new node of type '" + metadata.getType().toString() + "' with name '" + nodeName + "' within node '" + target.toString() + "'."); + nodeRef = fileFolderService.create(target, nodeName, metadata.getType()).getNodeRef(); + nodeState = NodeState.CREATED; + } + catch (final FileExistsException fee) + { + if (logger.isWarnEnabled()) logger.warn("Node with name '" + nodeName + "' within node '" + target.toString() + "' was created concurrently to the bulk import. Skipping importing it.", fee); + nodeRef = null; + nodeState = NodeState.SKIPPED; + } + } + else + { + if (logger.isDebugEnabled()) logger.debug("Skipping creation of new node '" + nodeName + "' within node '" + target.toString() + "' since it doesn't have a content file."); + nodeRef = null; + nodeState = NodeState.SKIPPED; + } + } + // We found the node in the repository. Make sure we return the NodeRef, so that recursive loading works (we need the NodeRef of all sub-spaces, even if we didn't create them). + else + { + if (replaceExisting) + { + boolean targetNodeIsSpace = fileFolderService.getFileInfo(nodeRef).isFolder(); - if (importableItem.getHeadRevision().contentFileExists()) - { - // If the source file exists, ensure that the target node is of the same type (i.e. file or folder) as it. - isDirectory = ImportableItem.FileType.DIRECTORY.equals(importableItem.getHeadRevision().getContentFileType()); + if (importableItem.getHeadRevision().contentFileExists()) + { + // If the source file exists, ensure that the target node is of the same type (i.e. file or folder) as it. + isDirectory = ImportableItem.FileType.DIRECTORY.equals(importableItem.getHeadRevision().getContentFileType()); - if (isDirectory != targetNodeIsSpace) - { - if (logger.isWarnEnabled()) logger.warn("Skipping replacement of " + (isDirectory ? "Directory " : "File ") + - "'" + getFileName(importableItem.getHeadRevision().getContentFile()) + "'. " + - "The target node in the repository is a " + (targetNodeIsSpace ? "space node" : "content node") + "."); - nodeState = NodeState.SKIPPED; - } - } - else - { - isDirectory = targetNodeIsSpace; - } + if (isDirectory != targetNodeIsSpace) + { + if (logger.isWarnEnabled()) logger.warn("Skipping replacement of " + (isDirectory ? "Directory " : "File ") + + "'" + getFileName(importableItem.getHeadRevision().getContentFile()) + "'. " + + "The target node in the repository is a " + (targetNodeIsSpace ? "space node" : "content node") + "."); + nodeState = NodeState.SKIPPED; + } + } + else + { + isDirectory = targetNodeIsSpace; + } - if (nodeRef != null) - { - if (metadata.getType() != null) - { - // Finally, specialise the type. - if (logger.isDebugEnabled()) logger.debug("Specialising type of node '" + nodeRef.toString() + "' to '" + String.valueOf(metadata.getType()) + "'."); - nodeService.setType(nodeRef, metadata.getType()); - } + if (nodeRef != null) + { + if (metadata.getType() != null) + { + // Finally, specialise the type. + if (logger.isDebugEnabled()) logger.debug("Specialising type of node '" + nodeRef.toString() + "' to '" + String.valueOf(metadata.getType()) + "'."); + nodeService.setType(nodeRef, metadata.getType()); + } - nodeState = NodeState.REPLACED; - } - } - else - { - if (logger.isDebugEnabled()) logger.debug("Found content node '" + nodeRef.toString() + "', but replaceExisting=false, so skipping it."); - nodeState = NodeState.SKIPPED; - } - } + nodeState = NodeState.REPLACED; + } + } + else + { + if (logger.isDebugEnabled()) logger.debug("Found content node '" + nodeRef.toString() + "', but replaceExisting=false, so skipping it."); + nodeState = NodeState.SKIPPED; + } + } - result = new Triple(nodeRef, isDirectory, nodeState); + result = new Triple(nodeRef, isDirectory, nodeState); - return(result); - } + return(result); + } protected String getFileName(File file) { - return FileUtils.getFileName(file); + return FileUtils.getFileName(file); } - + protected final void importImportableItemMetadata(NodeRef nodeRef, File parentFile, MetadataLoader.Metadata metadata) { - // Attach aspects - if (metadata.getAspects() != null) - { - for (final QName aspect : metadata.getAspects()) - { - if (logger.isDebugEnabled()) logger.debug("Attaching aspect '" + aspect.toString() + "' to node '" + nodeRef.toString() + "'."); + // Attach aspects + if (metadata.getAspects() != null) + { + for (final QName aspect : metadata.getAspects()) + { + if (logger.isDebugEnabled()) logger.debug("Attaching aspect '" + aspect.toString() + "' to node '" + nodeRef.toString() + "'."); - nodeService.addAspect(nodeRef, aspect, null); // Note: we set the aspect's properties separately, hence null for the third parameter - } - } + nodeService.addAspect(nodeRef, aspect, null); // Note: we set the aspect's properties separately, hence null for the third parameter + } + } - // Set property values for both the type and any aspect(s) - if (metadata.getProperties() != null) - { - if (logger.isDebugEnabled()) logger.debug("Adding properties to node '" + nodeRef.toString() + "':\n" + mapToString(metadata.getProperties())); + // Set property values for both the type and any aspect(s) + if (metadata.getProperties() != null) + { + if (logger.isDebugEnabled()) logger.debug("Adding properties to node '" + nodeRef.toString() + "':\n" + mapToString(metadata.getProperties())); - try - { - nodeService.addProperties(nodeRef, metadata.getProperties()); - } - catch (final InvalidNodeRefException inre) - { - if (!nodeRef.equals(inre.getNodeRef())) - { - // Caused by an invalid NodeRef in the metadata (e.g. in an association) - throw new IllegalStateException("Invalid nodeRef found in metadata for '" + getFileName(parentFile) + "'. " + - "Probable cause: an association is being populated via metadata, but the " + - "NodeRef for the target of that association ('" + inre.getNodeRef() + "') is invalid. " + - "Please double check your metadata file and try again.", inre); - } - else - { - // Logic bug in the BFSIT. :-( - throw inre; - } - } - } + try + { + nodeService.addProperties(nodeRef, metadata.getProperties()); + } + catch (final InvalidNodeRefException inre) + { + if (!nodeRef.equals(inre.getNodeRef())) + { + // Caused by an invalid NodeRef in the metadata (e.g. in an association) + throw new IllegalStateException("Invalid nodeRef found in metadata for '" + getFileName(parentFile) + "'. " + + "Probable cause: an association is being populated via metadata, but the " + + "NodeRef for the target of that association ('" + inre.getNodeRef() + "') is invalid. " + + "Please double check your metadata file and try again.", inre); + } + else + { + // Logic bug in the BFSIT. :-( + throw inre; + } + } + } } protected final void importImportableItemDirectory(NodeRef nodeRef, ImportableItem importableItem, MetadataLoader.Metadata metadata) { - if (importableItem.hasVersionEntries()) - { - logger.warn("Skipping versions for directory '" + getFileName(importableItem.getHeadRevision().getContentFile()) + "' - Alfresco does not support versioned spaces."); - } + if (importableItem.hasVersionEntries()) + { + logger.warn("Skipping versions for directory '" + getFileName(importableItem.getHeadRevision().getContentFile()) + "' - Alfresco does not support versioned spaces."); + } - // Attach aspects and set all properties - importImportableItemMetadata(nodeRef, importableItem.getHeadRevision().getContentFile(), metadata); + // Attach aspects and set all properties + importImportableItemMetadata(nodeRef, importableItem.getHeadRevision().getContentFile(), metadata); } protected final MetadataLoader.Metadata loadMetadata(ImportableItem.ContentAndMetadata contentAndMetadata) @@ -392,19 +392,19 @@ public abstract class AbstractNodeImporter implements NodeImporter final String filename = contentAndMetadata.getContentFile().getName().trim().replaceFirst(DirectoryAnalyser.VERSION_SUFFIX_REGEX, ""); // Strip off the version suffix (if any) final Date modified = new Date(contentAndMetadata.getContentFile().lastModified()); final Date created = modified; //TODO: determine proper file creation time (awaiting JDK 1.7 NIO2 library) - + result.setType(ImportableItem.FileType.FILE.equals(contentAndMetadata.getContentFileType()) ? ContentModel.TYPE_CONTENT : ContentModel.TYPE_FOLDER); result.addProperty(ContentModel.PROP_NAME, filename); result.addProperty(ContentModel.PROP_TITLE, filename); result.addProperty(ContentModel.PROP_CREATED, created); result.addProperty(ContentModel.PROP_MODIFIED, modified); } - + if (metadataLoader != null) { metadataLoader.loadMetadata(contentAndMetadata, result); } - + return(result); } @@ -412,7 +412,7 @@ public abstract class AbstractNodeImporter implements NodeImporter { if(logger.isDebugEnabled()) { - logger.debug("Importing " + String.valueOf(importableItem)); + logger.debug("Importing " + String.valueOf(importableItem)); } NodeRef nodeRef = importImportableItemImpl(importableItem, replaceExisting); @@ -421,26 +421,26 @@ public abstract class AbstractNodeImporter implements NodeImporter //importableItem.setParent(null); // importableItem.clearParent(); - importableItem.setNodeRef(nodeRef); + importableItem.setNodeRef(nodeRef); return nodeRef; } - + protected void skipImportableDirectory(ImportableItem importableItem) { if (logger.isInfoEnabled()) - { - logger.info("Skipping '" + getFileName(importableItem.getHeadRevision().getContentFile()) + "' as it already exists in the repository and 'replace existing' is false."); - } + { + logger.info("Skipping '" + getFileName(importableItem.getHeadRevision().getContentFile()) + "' as it already exists in the repository and 'replace existing' is false."); + } importStatus.incrementImportableItemsSkipped(importableItem, true); } protected void skipImportableFile(ImportableItem importableItem) { if (logger.isInfoEnabled()) - { - logger.info("Skipping '" + getFileName(importableItem.getHeadRevision().getContentFile()) + "' as it already exists in the repository and 'replace existing' is false."); - } + { + logger.info("Skipping '" + getFileName(importableItem.getHeadRevision().getContentFile()) + "' as it already exists in the repository and 'replace existing' is false."); + } importStatus.incrementImportableItemsSkipped(importableItem, false); } } diff --git a/source/test-java/org/alfresco/repo/bulkimport/impl/AbstractBulkImportTests.java b/source/test-java/org/alfresco/repo/bulkimport/impl/AbstractBulkImportTests.java index bd2deaec8e..7c5790193d 100644 --- a/source/test-java/org/alfresco/repo/bulkimport/impl/AbstractBulkImportTests.java +++ b/source/test-java/org/alfresco/repo/bulkimport/impl/AbstractBulkImportTests.java @@ -66,214 +66,214 @@ import org.springframework.context.ApplicationContext; */ public class AbstractBulkImportTests { - protected static ApplicationContext ctx = null; + protected static ApplicationContext ctx = null; protected FileFolderService fileFolderService; protected NodeService nodeService; protected TransactionService transactionService; - protected ContentService contentService; - protected UserTransaction txn = null; - protected RuleService ruleService; + protected ContentService contentService; + protected UserTransaction txn = null; + protected RuleService ruleService; protected ActionService actionService; protected VersionService versionService; - protected MultiThreadedBulkFilesystemImporter bulkImporter; + protected MultiThreadedBulkFilesystemImporter bulkImporter; - protected NodeRef rootNodeRef; - protected FileInfo topLevelFolder; - protected NodeRef top; + protected NodeRef rootNodeRef; + protected FileInfo topLevelFolder; + protected NodeRef top; - protected static void startContext() - { - ctx = ApplicationContextHelper.getApplicationContext(); - } + protected static void startContext() + { + ctx = ApplicationContextHelper.getApplicationContext(); + } - protected static void startContext(String[] configLocations) - { - ctx = ApplicationContextHelper.getApplicationContext(configLocations); - } + protected static void startContext(String[] configLocations) + { + ctx = ApplicationContextHelper.getApplicationContext(configLocations); + } - protected static void stopContext() - { - ApplicationContextHelper.closeApplicationContext(); - } + protected static void stopContext() + { + ApplicationContextHelper.closeApplicationContext(); + } @Before - public void setup() throws SystemException, NotSupportedException - { - try - { - nodeService = (NodeService)ctx.getBean("nodeService"); - fileFolderService = (FileFolderService)ctx.getBean("fileFolderService"); - transactionService = (TransactionService)ctx.getBean("transactionService"); - bulkImporter = (MultiThreadedBulkFilesystemImporter)ctx.getBean("bulkFilesystemImporter"); - contentService = (ContentService)ctx.getBean("contentService"); - actionService = (ActionService)ctx.getBean("actionService"); - ruleService = (RuleService)ctx.getBean("ruleService"); + public void setup() throws SystemException, NotSupportedException + { + try + { + nodeService = (NodeService)ctx.getBean("nodeService"); + fileFolderService = (FileFolderService)ctx.getBean("fileFolderService"); + transactionService = (TransactionService)ctx.getBean("transactionService"); + bulkImporter = (MultiThreadedBulkFilesystemImporter)ctx.getBean("bulkFilesystemImporter"); + contentService = (ContentService)ctx.getBean("contentService"); + actionService = (ActionService)ctx.getBean("actionService"); + ruleService = (RuleService)ctx.getBean("ruleService"); versionService = (VersionService)ctx.getBean("versionService"); - AuthenticationUtil.setFullyAuthenticatedUser(AuthenticationUtil.getAdminUserName()); + AuthenticationUtil.setFullyAuthenticatedUser(AuthenticationUtil.getAdminUserName()); - String s = "BulkFilesystemImport" + System.currentTimeMillis(); - - txn = transactionService.getUserTransaction(); - txn.begin(); - - AuthenticationUtil.pushAuthentication(); - AuthenticationUtil.setFullyAuthenticatedUser(AuthenticationUtil.getAdminUserName()); + String s = "BulkFilesystemImport" + System.currentTimeMillis(); - StoreRef storeRef = nodeService.createStore(StoreRef.PROTOCOL_WORKSPACE, s); - rootNodeRef = nodeService.getRootNode(storeRef); - top = nodeService.createNode(rootNodeRef, ContentModel.ASSOC_CHILDREN, QName.createQName("{namespace}top"), ContentModel.TYPE_FOLDER).getChildRef(); - - topLevelFolder = fileFolderService.create(top, s, ContentModel.TYPE_FOLDER); + txn = transactionService.getUserTransaction(); + txn.begin(); - txn.commit(); - } - catch(Throwable e) - { - fail(e.getMessage()); - } - } + AuthenticationUtil.pushAuthentication(); + AuthenticationUtil.setFullyAuthenticatedUser(AuthenticationUtil.getAdminUserName()); + + StoreRef storeRef = nodeService.createStore(StoreRef.PROTOCOL_WORKSPACE, s); + rootNodeRef = nodeService.getRootNode(storeRef); + top = nodeService.createNode(rootNodeRef, ContentModel.ASSOC_CHILDREN, QName.createQName("{namespace}top"), ContentModel.TYPE_FOLDER).getChildRef(); + + topLevelFolder = fileFolderService.create(top, s, ContentModel.TYPE_FOLDER); + + txn.commit(); + } + catch(Throwable e) + { + fail(e.getMessage()); + } + } @After - public void teardown() throws Exception - { + public void teardown() throws Exception + { AuthenticationUtil.popAuthentication(); - if(txn != null) - { - txn.commit(); - } - } + if(txn != null) + { + txn.commit(); + } + } @AfterClass public static void afterTests() { - stopContext(); + stopContext(); } protected List getFolders(NodeRef parent, String pattern) { - PagingResults page = fileFolderService.list(parent, false, true, pattern, null, null, new PagingRequest(CannedQueryPageDetails.DEFAULT_PAGE_SIZE)); - List folders = page.getPage(); - return folders; + PagingResults page = fileFolderService.list(parent, false, true, pattern, null, null, new PagingRequest(CannedQueryPageDetails.DEFAULT_PAGE_SIZE)); + List folders = page.getPage(); + return folders; } protected List getFiles(NodeRef parent, String pattern) { - PagingResults page = fileFolderService.list(parent, true, false, pattern, null, null, new PagingRequest(CannedQueryPageDetails.DEFAULT_PAGE_SIZE)); - List files = page.getPage(); - return files; + PagingResults page = fileFolderService.list(parent, true, false, pattern, null, null, new PagingRequest(CannedQueryPageDetails.DEFAULT_PAGE_SIZE)); + List files = page.getPage(); + return files; } - + protected Map toMap(List list) { - Map map = new HashMap(list.size()); - for(FileInfo fileInfo : list) - { - map.put(fileInfo.getName(), fileInfo); - } - return map; + Map map = new HashMap(list.size()); + for(FileInfo fileInfo : list) + { + map.put(fileInfo.getName(), fileInfo); + } + return map; } protected void checkFolder(NodeRef folderNode, String childFolderName, String pattern, int numExpectedFolders, int numExpectedFiles, ExpectedFolder[] expectedFolders, ExpectedFile[] expectedFiles) { - List folders = getFolders(folderNode, childFolderName); - assertEquals("", 1, folders.size()); - NodeRef folder1 = folders.get(0).getNodeRef(); - checkFiles(folder1, pattern, numExpectedFolders, numExpectedFiles, expectedFiles, expectedFolders); + List folders = getFolders(folderNode, childFolderName); + assertEquals("", 1, folders.size()); + NodeRef folder1 = folders.get(0).getNodeRef(); + checkFiles(folder1, pattern, numExpectedFolders, numExpectedFiles, expectedFiles, expectedFolders); } protected void checkFiles(NodeRef parent, String pattern, int expectedNumFolders, int expectedNumFiles, - ExpectedFile[] expectedFiles, ExpectedFolder[] expectedFolders) + ExpectedFile[] expectedFiles, ExpectedFolder[] expectedFolders) { - Map folders = toMap(getFolders(parent, pattern)); - Map files = toMap(getFiles(parent, pattern)); - assertEquals("", expectedNumFolders, folders.size()); - assertEquals("", expectedNumFiles, files.size()); - - if(expectedFiles != null) - { - for(ExpectedFile expectedFile : expectedFiles) - { - FileInfo fileInfo = files.get(expectedFile.getName()); - assertNotNull("", fileInfo); - assertNotNull("", fileInfo.getContentData()); - assertEquals(expectedFile.getMimeType(), fileInfo.getContentData().getMimetype()); - if(fileInfo.getContentData().getMimetype() == MimetypeMap.MIMETYPE_TEXT_PLAIN - && expectedFile.getContentContains() != null) - { - ContentReader reader = contentService.getReader(fileInfo.getNodeRef(), ContentModel.PROP_CONTENT); - String contentContains = expectedFile.getContentContains(); - assertTrue("", reader.getContentString().indexOf(contentContains) != -1); - } - } - } - - if(expectedFolders != null) - { - for(ExpectedFolder expectedFolder : expectedFolders) - { - FileInfo fileInfo = folders.get(expectedFolder.getName()); - assertNotNull("", fileInfo); - } - } + Map folders = toMap(getFolders(parent, pattern)); + Map files = toMap(getFiles(parent, pattern)); + assertEquals("", expectedNumFolders, folders.size()); + assertEquals("", expectedNumFiles, files.size()); + + if(expectedFiles != null) + { + for(ExpectedFile expectedFile : expectedFiles) + { + FileInfo fileInfo = files.get(expectedFile.getName()); + assertNotNull("", fileInfo); + assertNotNull("", fileInfo.getContentData()); + assertEquals(expectedFile.getMimeType(), fileInfo.getContentData().getMimetype()); + if(fileInfo.getContentData().getMimetype() == MimetypeMap.MIMETYPE_TEXT_PLAIN + && expectedFile.getContentContains() != null) + { + ContentReader reader = contentService.getReader(fileInfo.getNodeRef(), ContentModel.PROP_CONTENT); + String contentContains = expectedFile.getContentContains(); + assertTrue("", reader.getContentString().indexOf(contentContains) != -1); + } + } + } + + if(expectedFolders != null) + { + for(ExpectedFolder expectedFolder : expectedFolders) + { + FileInfo fileInfo = folders.get(expectedFolder.getName()); + assertNotNull("", fileInfo); + } + } } protected void checkContent(FileInfo file, String name, String mimeType) { - assertEquals("", name, file.getName()); - assertEquals("", mimeType, file.getContentData().getMimetype()); + assertEquals("", name, file.getName()); + assertEquals("", mimeType, file.getContentData().getMimetype()); } - + protected static class ExpectedFolder - { - private String name; + { + private String name; - public ExpectedFolder(String name) - { - super(); - this.name = name; - } + public ExpectedFolder(String name) + { + super(); + this.name = name; + } - public String getName() - { - return name; - } - } - - protected static class ExpectedFile - { - private String name; - private String mimeType; - private String contentContains = null; - - public ExpectedFile(String name, String mimeType, String contentContains) - { - this(name, mimeType); - this.contentContains = contentContains; - } - - public ExpectedFile(String name, String mimeType) - { - super(); - this.name = name; - this.mimeType = mimeType; - } + public String getName() + { + return name; + } + } - public String getName() - { - return name; - } + protected static class ExpectedFile + { + private String name; + private String mimeType; + private String contentContains = null; + + public ExpectedFile(String name, String mimeType, String contentContains) + { + this(name, mimeType); + this.contentContains = contentContains; + } + + public ExpectedFile(String name, String mimeType) + { + super(); + this.name = name; + this.mimeType = mimeType; + } - public String getMimeType() - { - return mimeType; - } + public String getName() + { + return name; + } - public String getContentContains() - { - return contentContains; - } - } + public String getMimeType() + { + return mimeType; + } + + public String getContentContains() + { + return contentContains; + } + } } diff --git a/source/test-java/org/alfresco/repo/bulkimport/impl/BulkImportTest.java b/source/test-java/org/alfresco/repo/bulkimport/impl/BulkImportTest.java index 3f0eb3172d..4bf28cbab7 100644 --- a/source/test-java/org/alfresco/repo/bulkimport/impl/BulkImportTest.java +++ b/source/test-java/org/alfresco/repo/bulkimport/impl/BulkImportTest.java @@ -63,151 +63,151 @@ import org.springframework.util.ResourceUtils; */ public class BulkImportTest extends AbstractBulkImportTests { - private StreamingNodeImporterFactory streamingNodeImporterFactory; + private StreamingNodeImporterFactory streamingNodeImporterFactory; - @BeforeClass - public static void beforeTests() - { - startContext(); - } + @BeforeClass + public static void beforeTests() + { + startContext(); + } @Before - public void setup() throws SystemException, NotSupportedException - { - super.setup(); - streamingNodeImporterFactory = (StreamingNodeImporterFactory)ctx.getBean("streamingNodeImporterFactory"); - } + public void setup() throws SystemException, NotSupportedException + { + super.setup(); + streamingNodeImporterFactory = (StreamingNodeImporterFactory)ctx.getBean("streamingNodeImporterFactory"); + } /** * For replaceExisting = true, the title must be taken from the metadata and not overridden by the actual filename. * * @throws Throwable */ - @Test - public void testMNT8470() throws Throwable - { + @Test + public void testMNT8470() throws Throwable + { txn = transactionService.getUserTransaction(); txn.begin(); - NodeRef folderNode = topLevelFolder.getNodeRef(); + NodeRef folderNode = topLevelFolder.getNodeRef(); - try - { - NodeImporter nodeImporter = streamingNodeImporterFactory.getNodeImporter(ResourceUtils.getFile("classpath:bulkimport1")); + try + { + NodeImporter nodeImporter = streamingNodeImporterFactory.getNodeImporter(ResourceUtils.getFile("classpath:bulkimport1")); BulkImportParameters bulkImportParameters = new BulkImportParameters(); bulkImportParameters.setTarget(folderNode); bulkImportParameters.setReplaceExisting(true); bulkImportParameters.setDisableRulesService(true); bulkImportParameters.setBatchSize(40); - bulkImporter.bulkImport(bulkImportParameters, nodeImporter); - } - catch(Throwable e) - { - fail(e.getMessage()); - } + bulkImporter.bulkImport(bulkImportParameters, nodeImporter); + } + catch(Throwable e) + { + fail(e.getMessage()); + } - System.out.println(bulkImporter.getStatus()); - assertEquals(false, bulkImporter.getStatus().inProgress()); - - List folders = getFolders(folderNode, null); - assertEquals(1, folders.size()); - FileInfo folder1 = folders.get(0); - assertEquals("folder1", folder1.getName()); - // title should be taken from the metadata file - assertEquals("", folder1.getProperties().get(ContentModel.PROP_TITLE)); - } + System.out.println(bulkImporter.getStatus()); + assertEquals(false, bulkImporter.getStatus().inProgress()); + + List folders = getFolders(folderNode, null); + assertEquals(1, folders.size()); + FileInfo folder1 = folders.get(0); + assertEquals("folder1", folder1.getName()); + // title should be taken from the metadata file + assertEquals("", folder1.getProperties().get(ContentModel.PROP_TITLE)); + } - @Test - public void testCopyImportStriping() throws Throwable - { + @Test + public void testCopyImportStriping() throws Throwable + { txn = transactionService.getUserTransaction(); txn.begin(); - NodeRef folderNode = topLevelFolder.getNodeRef(); + NodeRef folderNode = topLevelFolder.getNodeRef(); - try - { - NodeImporter nodeImporter = streamingNodeImporterFactory.getNodeImporter(ResourceUtils.getFile("classpath:bulkimport")); + try + { + NodeImporter nodeImporter = streamingNodeImporterFactory.getNodeImporter(ResourceUtils.getFile("classpath:bulkimport")); BulkImportParameters bulkImportParameters = new BulkImportParameters(); bulkImportParameters.setTarget(folderNode); bulkImportParameters.setReplaceExisting(true); bulkImportParameters.setDisableRulesService(true); bulkImportParameters.setBatchSize(40); - bulkImporter.bulkImport(bulkImportParameters, nodeImporter); - } - catch(Throwable e) - { - fail(e.getMessage()); - } + bulkImporter.bulkImport(bulkImportParameters, nodeImporter); + } + catch(Throwable e) + { + fail(e.getMessage()); + } - System.out.println(bulkImporter.getStatus()); + System.out.println(bulkImporter.getStatus()); - checkFiles(folderNode, null, 2, 9, - new ExpectedFile[] - { - new ExpectedFile("quickImg1.xls", MimetypeMap.MIMETYPE_EXCEL), - new ExpectedFile("quickImg1.doc", MimetypeMap.MIMETYPE_WORD), - new ExpectedFile("quick.txt", MimetypeMap.MIMETYPE_TEXT_PLAIN, "The quick brown fox jumps over the lazy dog"), - }, - new ExpectedFolder[] - { - new ExpectedFolder("folder1"), - new ExpectedFolder("folder2") - }); - - List folders = getFolders(folderNode, "folder1"); - assertEquals("", 1, folders.size()); - NodeRef folder1 = folders.get(0).getNodeRef(); - checkFiles(folder1, null, 1, 0, null, - new ExpectedFolder[] - { - new ExpectedFolder("folder1.1") - }); + checkFiles(folderNode, null, 2, 9, + new ExpectedFile[] + { + new ExpectedFile("quickImg1.xls", MimetypeMap.MIMETYPE_EXCEL), + new ExpectedFile("quickImg1.doc", MimetypeMap.MIMETYPE_WORD), + new ExpectedFile("quick.txt", MimetypeMap.MIMETYPE_TEXT_PLAIN, "The quick brown fox jumps over the lazy dog"), + }, + new ExpectedFolder[] + { + new ExpectedFolder("folder1"), + new ExpectedFolder("folder2") + }); - folders = getFolders(folderNode, "folder2"); - assertEquals("", 1, folders.size()); - NodeRef folder2 = folders.get(0).getNodeRef(); - checkFiles(folder2, null, 1, 0, - new ExpectedFile[] - { - }, - new ExpectedFolder[] - { - new ExpectedFolder("folder2.1") - }); + List folders = getFolders(folderNode, "folder1"); + assertEquals("", 1, folders.size()); + NodeRef folder1 = folders.get(0).getNodeRef(); + checkFiles(folder1, null, 1, 0, null, + new ExpectedFolder[] + { + new ExpectedFolder("folder1.1") + }); + + folders = getFolders(folderNode, "folder2"); + assertEquals("", 1, folders.size()); + NodeRef folder2 = folders.get(0).getNodeRef(); + checkFiles(folder2, null, 1, 0, + new ExpectedFile[] + { + }, + new ExpectedFolder[] + { + new ExpectedFolder("folder2.1") + }); + + folders = getFolders(folder1, "folder1.1"); + assertEquals("", 1, folders.size()); + NodeRef folder1_1 = folders.get(0).getNodeRef(); + checkFiles(folder1_1, null, 2, 12, + new ExpectedFile[] + { + new ExpectedFile("quick.txt", MimetypeMap.MIMETYPE_TEXT_PLAIN, "The quick brown fox jumps over the lazy dog"), + new ExpectedFile("quick.sxw", MimetypeMap.MIMETYPE_OPENOFFICE1_WRITER), + new ExpectedFile("quick.tar", "application/x-gtar"), + }, + new ExpectedFolder[] + { + new ExpectedFolder("folder1.1.1"), + new ExpectedFolder("folder1.1.2") + }); + + folders = getFolders(folder2, "folder2.1"); + assertEquals("", 1, folders.size()); + NodeRef folder2_1 = folders.get(0).getNodeRef(); + + checkFiles(folder2_1, null, 0, 17, + new ExpectedFile[] + { + new ExpectedFile("quick.png", MimetypeMap.MIMETYPE_IMAGE_PNG), + new ExpectedFile("quick.pdf", MimetypeMap.MIMETYPE_PDF), + new ExpectedFile("quick.odt", MimetypeMap.MIMETYPE_OPENDOCUMENT_TEXT), + }, + new ExpectedFolder[] + { + }); + } - folders = getFolders(folder1, "folder1.1"); - assertEquals("", 1, folders.size()); - NodeRef folder1_1 = folders.get(0).getNodeRef(); - checkFiles(folder1_1, null, 2, 12, - new ExpectedFile[] - { - new ExpectedFile("quick.txt", MimetypeMap.MIMETYPE_TEXT_PLAIN, "The quick brown fox jumps over the lazy dog"), - new ExpectedFile("quick.sxw", MimetypeMap.MIMETYPE_OPENOFFICE1_WRITER), - new ExpectedFile("quick.tar", "application/x-gtar"), - }, - new ExpectedFolder[] - { - new ExpectedFolder("folder1.1.1"), - new ExpectedFolder("folder1.1.2") - }); - - folders = getFolders(folder2, "folder2.1"); - assertEquals("", 1, folders.size()); - NodeRef folder2_1 = folders.get(0).getNodeRef(); - - checkFiles(folder2_1, null, 0, 17, - new ExpectedFile[] - { - new ExpectedFile("quick.png", MimetypeMap.MIMETYPE_IMAGE_PNG), - new ExpectedFile("quick.pdf", MimetypeMap.MIMETYPE_PDF), - new ExpectedFile("quick.odt", MimetypeMap.MIMETYPE_OPENDOCUMENT_TEXT), - }, - new ExpectedFolder[] - { - }); - } - protected Rule createCopyRule(NodeRef targetNode, boolean isAppliedToChildren) { Rule rule = new Rule(); @@ -215,7 +215,7 @@ public class BulkImportTest extends AbstractBulkImportTests String title = "rule title " + System.currentTimeMillis(); rule.setTitle(title); rule.setDescription(title); - rule.applyToChildren(isAppliedToChildren); + rule.applyToChildren(isAppliedToChildren); Map params = new HashMap(1); params.put(MoveActionExecuter.PARAM_DESTINATION_FOLDER, targetNode); @@ -228,9 +228,9 @@ public class BulkImportTest extends AbstractBulkImportTests return rule; } - @Test - public void testImportWithRules() throws Throwable - { + @Test + public void testImportWithRules() throws Throwable + { NodeRef folderNode = topLevelFolder.getNodeRef(); NodeImporter nodeImporter = null; @@ -258,60 +258,60 @@ public class BulkImportTest extends AbstractBulkImportTests bulkImporter.bulkImport(bulkImportParameters, nodeImporter); System.out.println(bulkImporter.getStatus()); - + assertEquals("", 74, bulkImporter.getStatus().getNumberOfContentNodesCreated()); - checkFiles(folderNode, null, 2, 9, new ExpectedFile[] { - new ExpectedFile("quickImg1.xls", MimetypeMap.MIMETYPE_EXCEL), - new ExpectedFile("quickImg1.doc", MimetypeMap.MIMETYPE_WORD), - new ExpectedFile("quick.txt", MimetypeMap.MIMETYPE_TEXT_PLAIN, "The quick brown fox jumps over the lazy dog"), - }, - new ExpectedFolder[] { - new ExpectedFolder("folder1"), - new ExpectedFolder("folder2") - }); + checkFiles(folderNode, null, 2, 9, new ExpectedFile[] { + new ExpectedFile("quickImg1.xls", MimetypeMap.MIMETYPE_EXCEL), + new ExpectedFile("quickImg1.doc", MimetypeMap.MIMETYPE_WORD), + new ExpectedFile("quick.txt", MimetypeMap.MIMETYPE_TEXT_PLAIN, "The quick brown fox jumps over the lazy dog"), + }, + new ExpectedFolder[] { + new ExpectedFolder("folder1"), + new ExpectedFolder("folder2") + }); - List folders = getFolders(folderNode, "folder1"); - assertEquals("", 1, folders.size()); - NodeRef folder1 = folders.get(0).getNodeRef(); - checkFiles(folder1, null, 1, 0, null, new ExpectedFolder[] { - new ExpectedFolder("folder1.1") - }); + List folders = getFolders(folderNode, "folder1"); + assertEquals("", 1, folders.size()); + NodeRef folder1 = folders.get(0).getNodeRef(); + checkFiles(folder1, null, 1, 0, null, new ExpectedFolder[] { + new ExpectedFolder("folder1.1") + }); - folders = getFolders(folderNode, "folder2"); - assertEquals("", 1, folders.size()); - NodeRef folder2 = folders.get(0).getNodeRef(); - checkFiles(folder2, null, 1, 0, new ExpectedFile[] { - }, - new ExpectedFolder[] { - new ExpectedFolder("folder2.1") - }); + folders = getFolders(folderNode, "folder2"); + assertEquals("", 1, folders.size()); + NodeRef folder2 = folders.get(0).getNodeRef(); + checkFiles(folder2, null, 1, 0, new ExpectedFile[] { + }, + new ExpectedFolder[] { + new ExpectedFolder("folder2.1") + }); - folders = getFolders(folder1, "folder1.1"); - assertEquals("", 1, folders.size()); - NodeRef folder1_1 = folders.get(0).getNodeRef(); - checkFiles(folder1_1, null, 2, 12, new ExpectedFile[] { - new ExpectedFile("quick.txt", MimetypeMap.MIMETYPE_TEXT_PLAIN, "The quick brown fox jumps over the lazy dog"), - new ExpectedFile("quick.sxw", MimetypeMap.MIMETYPE_OPENOFFICE1_WRITER), - new ExpectedFile("quick.tar", "application/x-gtar"), - }, - new ExpectedFolder[] { - new ExpectedFolder("folder1.1.1"), - new ExpectedFolder("folder1.1.2") - }); + folders = getFolders(folder1, "folder1.1"); + assertEquals("", 1, folders.size()); + NodeRef folder1_1 = folders.get(0).getNodeRef(); + checkFiles(folder1_1, null, 2, 12, new ExpectedFile[] { + new ExpectedFile("quick.txt", MimetypeMap.MIMETYPE_TEXT_PLAIN, "The quick brown fox jumps over the lazy dog"), + new ExpectedFile("quick.sxw", MimetypeMap.MIMETYPE_OPENOFFICE1_WRITER), + new ExpectedFile("quick.tar", "application/x-gtar"), + }, + new ExpectedFolder[] { + new ExpectedFolder("folder1.1.1"), + new ExpectedFolder("folder1.1.2") + }); - folders = getFolders(folder2, "folder2.1"); - assertEquals("", 1, folders.size()); - NodeRef folder2_1 = folders.get(0).getNodeRef(); + folders = getFolders(folder2, "folder2.1"); + assertEquals("", 1, folders.size()); + NodeRef folder2_1 = folders.get(0).getNodeRef(); - checkFiles(folder2_1, null, 0, 17, new ExpectedFile[] { - new ExpectedFile("quick.png", MimetypeMap.MIMETYPE_IMAGE_PNG), - new ExpectedFile("quick.pdf", MimetypeMap.MIMETYPE_PDF), - new ExpectedFile("quick.odt", MimetypeMap.MIMETYPE_OPENDOCUMENT_TEXT), - }, - new ExpectedFolder[] { - }); - } + checkFiles(folder2_1, null, 0, 17, new ExpectedFile[] { + new ExpectedFile("quick.png", MimetypeMap.MIMETYPE_IMAGE_PNG), + new ExpectedFile("quick.pdf", MimetypeMap.MIMETYPE_PDF), + new ExpectedFile("quick.odt", MimetypeMap.MIMETYPE_OPENDOCUMENT_TEXT), + }, + new ExpectedFolder[] { + }); + } /** * MNT-9076: Penultimate version cannot be accessed from Share when uploading using bulkimport @@ -373,6 +373,6 @@ public class BulkImportTest extends AbstractBulkImportTests contentReader = this.contentService.getReader(versions[3].getFrozenStateNodeRef(), ContentModel.PROP_CONTENT); assertNotNull(contentReader); assertEquals("This is version 1 of fileWithVersions.txt.", contentReader.getContentString()); - } + } }