Package org.dspace.app.itemimport
Class ItemImportServiceImpl
- java.lang.Object
-
- org.dspace.app.itemimport.ItemImportServiceImpl
-
- All Implemented Interfaces:
ItemImportService,org.springframework.beans.factory.InitializingBean
public class ItemImportServiceImpl extends Object implements ItemImportService, org.springframework.beans.factory.InitializingBean
Import items into DSpace. The conventional use is upload files by copying them. DSpace writes the item's bitstreams into its assetstore. Metadata is also loaded to the DSpace database.A second use assumes the bitstream files already exist in a storage resource accessible to DSpace. In this case the bitstreams are 'registered'. That is, the metadata is loaded to the DSpace database and DSpace is given the location of the file which is subsumed into DSpace.
The distinction is controlled by the format of lines in the 'contents' file. See comments in processContentsFile() below.
Modified by David Little, UCSD Libraries 12/21/04 to allow the registration of files (bitstreams) into DSpace.
-
-
Field Summary
-
Constructor Summary
Constructors Modifier Constructor Description protectedItemImportServiceImpl()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected voidaddDCValue(Context c, Item i, String schema, Node n)protected ItemaddItem(Context c, List<Collection> mycollections, String path, String itemname, PrintWriter mapOut, boolean template)item? try and add it to the archive.voidaddItems(Context c, List<Collection> mycollections, String sourceDir, String mapFile, boolean template)Add itemsvoidaddItemsAtomic(Context c, List<Collection> mycollections, String sourceDir, String mapFile, boolean template)protected voidaddRelationship(Context c, Item item, Item relationItem, String relationshipType)Add relationship.protected voidaddRelationships(Context c, String sourceDir)Add relationships from a 'relationships' manifest file.voidafterPropertiesSet()voidcleanupZipTemp()CleanupvoiddeleteBatchUpload(Context c, String uploadId)Delete a batch by IDprotected booleandeleteDirectory(File path)Delete a directory and its child files and directoriesprotected voiddeleteItem(Context c, String myhandle)protected voiddeleteItem(Context c, Item myitem)voiddeleteItems(Context c, String mapFile)Delete items via mapfilevoidemailErrorMessage(EPerson eperson, String error)If a batch import is done in a new thread we are unable to communicate with calling method about success or failure.voidemailSuccessMessage(Context context, EPerson eperson, String fileName)If a batch import is done in a new thread we are unable to communicate with calling method about success or failure.protected ItemfindItemByMetaValue(Context c, String metaKey, String metaValue)Lookup an item by a (unique) meta value.protected StringgenerateRandomFilename(boolean hidden)Generate a random filename based on current timeprotected StringgetEntityType(Item item)Get the item's entity type from meta.List<BatchUpload>getImportsAvailable(EPerson eperson)Get imports available for a personStringgetImportUploadableDirectory(EPerson ePerson)Get import upload directoryprotected StringgetStringValue(Node node)Return the String value of a Node.StringgetTempWorkDir()Get temporary work directoryFilegetTempWorkDirFile()Get temporary work directory (as File)protected voidloadDublinCore(Context c, Item myitem, String filename)protected voidloadMetadata(Context c, Item myitem, String path)protected DocumentloadXML(String filename)Load in the XML from file.protected List<Collection>processCollectionFile(Context c, String path, String filename)Read the collections file inside the item directory.protected voidprocessContentFileEntry(Context c, Item i, String path, String fileName, String bundleName, boolean primary)each entry represents a bitstream....protected List<String>processContentsFile(Context c, Item i, String path, String filename)Given a contents file and an item, stuffing it with bitstreams from the contents file Returns a List of Strings with lines from the contents file that request non-default bitstream permissionprotected StringprocessHandleFile(Context c, Item i, String path, String filename)Read in the handle file contents or return null if empty or doesn't existprotected voidprocessOptions(Context c, Item myItem, List<String> options)Process the Options to apply to the Item.protected Map<String,List<String>>processRelationshipFile(String path, String filename)Read the relationship manifest file.voidprocessUIImport(String filepath, Collection owningCollection, String[] otherCollections, String resumeDir, String inputType, Context context, boolean template)Given a local file or public URL to a zip file that has the Simple Archive Format, this method imports the contents to DSpaceprotected Map<String,String>readMapFile(String filename)protected voidregisterBitstream(Context c, Item i, int assetstore, String bitstreamPath, String bundleName, String description)Register the bitstream file into DSpacevoidreplaceItems(Context c, List<Collection> mycollections, String sourceDir, String mapFile, boolean template)Replace itemsprotected ItemresolveItem(Context c, String itemIdentifier)Resolve an item identifier.protected ItemresolveRelatedItem(Context c, String itemIdentifier)Resolve an item identifier referred to in the relationships manifest file.voidsetExcludeContent(boolean isExcludeContent)Set exclude-content flag.voidsetHandler(DSpaceRunnableHandler handler)Set the DSpace Runnable Handlerprotected voidsetPermission(Context c, Group g, int actionID, Bitstream bs)Set the Permission on a Bitstream.voidsetQuiet(boolean isQuiet)Set quiet flagvoidsetResume(boolean isResume)Set resume flagvoidsetTest(boolean isTest)Set test flagvoidsetUseWorkflow(boolean useWorkflow)Set use workflowvoidsetUseWorkflowSendEmail(boolean useWorkflowSendEmail)Stringunzip(File zipfile)Unzip a fileStringunzip(File zipfile, String destDir)Unzip a file to a destinationStringunzip(String sourcedir, String zipfilename)Unzip a file in a specific source directory
-
-
-
Field Detail
-
authorizeService
@Autowired(required=true) protected AuthorizeService authorizeService
-
bitstreamService
@Autowired(required=true) protected BitstreamService bitstreamService
-
bitstreamFormatService
@Autowired(required=true) protected BitstreamFormatService bitstreamFormatService
-
bundleService
@Autowired(required=true) protected BundleService bundleService
-
collectionService
@Autowired(required=true) protected CollectionService collectionService
-
ePersonService
@Autowired(required=true) protected EPersonService ePersonService
-
handleService
@Autowired(required=true) protected HandleService handleService
-
itemService
@Autowired(required=true) protected ItemService itemService
-
installItemService
@Autowired(required=true) protected InstallItemService installItemService
-
groupService
@Autowired(required=true) protected GroupService groupService
-
metadataFieldService
@Autowired(required=true) protected MetadataFieldService metadataFieldService
-
metadataSchemaService
@Autowired(required=true) protected MetadataSchemaService metadataSchemaService
-
resourcePolicyService
@Autowired(required=true) protected ResourcePolicyService resourcePolicyService
-
workspaceItemService
@Autowired(required=true) protected WorkspaceItemService workspaceItemService
-
workflowService
@Autowired(required=true) protected WorkflowService workflowService
-
configurationService
@Autowired(required=true) protected ConfigurationService configurationService
-
relationshipService
@Autowired(required=true) protected RelationshipService relationshipService
-
relationshipTypeService
@Autowired(required=true) protected RelationshipTypeService relationshipTypeService
-
metadataValueService
@Autowired(required=true) protected MetadataValueService metadataValueService
-
tempWorkDir
protected String tempWorkDir
-
isTest
protected boolean isTest
-
isExcludeContent
protected boolean isExcludeContent
-
isResume
protected boolean isResume
-
useWorkflow
protected boolean useWorkflow
-
useWorkflowSendEmail
protected boolean useWorkflowSendEmail
-
isQuiet
protected boolean isQuiet
-
metadataFileFilter
protected FilenameFilter metadataFileFilter
-
directoryFilter
protected FilenameFilter directoryFilter
-
-
Method Detail
-
afterPropertiesSet
public void afterPropertiesSet() throws Exception- Specified by:
afterPropertiesSetin interfaceorg.springframework.beans.factory.InitializingBean- Throws:
Exception
-
addItemsAtomic
public void addItemsAtomic(Context c, List<Collection> mycollections, String sourceDir, String mapFile, boolean template) throws Exception
- Specified by:
addItemsAtomicin interfaceItemImportService- Parameters:
c- DSpace Contextmycollections- List of CollectionssourceDir- source locationmapFile- map filetemplate- whether to use template item- Throws:
Exception- if error
-
addItems
public void addItems(Context c, List<Collection> mycollections, String sourceDir, String mapFile, boolean template) throws Exception
Description copied from interface:ItemImportServiceAdd items- Specified by:
addItemsin interfaceItemImportService- Parameters:
c- DSpace Contextmycollections- List of CollectionssourceDir- source locationmapFile- map filetemplate- whether to use template item- Throws:
Exception- if error
-
addRelationships
protected void addRelationships(Context c, String sourceDir) throws Exception
Add relationships from a 'relationships' manifest file.- Parameters:
c- ContextsourceDir- The parent import source directory- Throws:
Exception
-
addRelationship
protected void addRelationship(Context c, Item item, Item relationItem, String relationshipType) throws SQLException, AuthorizeException
Add relationship.- Parameters:
c- the contextitem- the itemrelationItem- the related itemrelationshipType- the relation type name- Throws:
SQLExceptionAuthorizeException
-
getEntityType
protected String getEntityType(Item item)
Get the item's entity type from meta.- Parameters:
item-- Returns:
-
processRelationshipFile
protected Map<String,List<String>> processRelationshipFile(String path, String filename) throws Exception
Read the relationship manifest file. Each line in the file contains a relationship type id and an item identifier in the following format:relation.<relation_key> <handle|uuid|folderName:import_item_folder|schema.element[.qualifier]:value>The
input_item_foldershould refer the folder name of another item in this import batch.- Parameters:
path- The main import folder path.filename- The name of the manifest file to check ('relationships')- Returns:
- Map of found relationships
- Throws:
Exception
-
resolveRelatedItem
protected Item resolveRelatedItem(Context c, String itemIdentifier) throws Exception
Resolve an item identifier referred to in the relationships manifest file. The import item map will be checked first to see if the identifier refers to an item folder that was just imported. Next it will try to find the item by handle or UUID, or by a unique meta value.- Parameters:
c- ContextitemIdentifier- The identifier string found in the import manifest (handle, uuid, or import subfolder)- Returns:
- Item if found, or null.
- Throws:
Exception
-
resolveItem
protected Item resolveItem(Context c, String itemIdentifier) throws IllegalStateException, SQLException
Resolve an item identifier.- Parameters:
c- ContextitemIdentifier- The identifier string found in the import file (handle or UUID)- Returns:
- Item if found, or null.
- Throws:
SQLExceptionIllegalStateExceptionException
-
findItemByMetaValue
protected Item findItemByMetaValue(Context c, String metaKey, String metaValue) throws Exception
Lookup an item by a (unique) meta value.- Parameters:
c- current DSpace session.metaKey- name of the metadata field to match.metaValue- value to be matched.- Returns:
- the matching Item.
- Throws:
Exception- if single item not found.
-
replaceItems
public void replaceItems(Context c, List<Collection> mycollections, String sourceDir, String mapFile, boolean template) throws Exception
Description copied from interface:ItemImportServiceReplace items- Specified by:
replaceItemsin interfaceItemImportService- Parameters:
c- DSpace Contextmycollections- List of CollectionssourceDir- source directorymapFile- map filetemplate- whether to use template item- Throws:
Exception- if error
-
deleteItems
public void deleteItems(Context c, String mapFile) throws Exception
Description copied from interface:ItemImportServiceDelete items via mapfile- Specified by:
deleteItemsin interfaceItemImportService- Parameters:
c- DSpace ContextmapFile- map file- Throws:
Exception- if error
-
addItem
protected Item addItem(Context c, List<Collection> mycollections, String path, String itemname, PrintWriter mapOut, boolean template) throws Exception
item? try and add it to the archive.- Parameters:
c- current Contextmycollections- - add item to these Collections.path- - directory containing the item directories.itemname- handle - non-null means we have a pre-defined handle alreadymapOut- - mapfile we're writingtemplate- whether to use collection template item as starting point- Returns:
- Item
- Throws:
Exception- if error occurs
-
deleteItem
protected void deleteItem(Context c, String myhandle) throws Exception
- Throws:
Exception
-
readMapFile
protected Map<String,String> readMapFile(String filename) throws Exception
- Throws:
Exception
-
loadMetadata
protected void loadMetadata(Context c, Item myitem, String path) throws SQLException, IOException, ParserConfigurationException, SAXException, TransformerException, AuthorizeException, XPathExpressionException
-
loadDublinCore
protected void loadDublinCore(Context c, Item myitem, String filename) throws SQLException, IOException, ParserConfigurationException, SAXException, TransformerException, AuthorizeException, XPathExpressionException
-
addDCValue
protected void addDCValue(Context c, Item i, String schema, Node n) throws TransformerException, SQLException, AuthorizeException
-
processCollectionFile
protected List<Collection> processCollectionFile(Context c, String path, String filename) throws IOException, SQLException
Read the collections file inside the item directory. If there is one and it is not empty return a list of collections in which the item should be inserted. If it does not exist or it is empty return null.- Parameters:
c- The contextpath- The path to the data directory for this itemfilename- The collections file filename. Should be "collections"- Returns:
- A list of collections in which to insert the item or null
- Throws:
IOException- if IO errorSQLException- if database error
-
processHandleFile
protected String processHandleFile(Context c, Item i, String path, String filename)
Read in the handle file contents or return null if empty or doesn't exist- Parameters:
c- DSpace contexti- DSpace itempath- path to handle filefilename- name of file- Returns:
- handle file contents or null if doesn't exist
-
processContentsFile
protected List<String> processContentsFile(Context c, Item i, String path, String filename) throws SQLException, IOException, AuthorizeException
Given a contents file and an item, stuffing it with bitstreams from the contents file Returns a List of Strings with lines from the contents file that request non-default bitstream permission- Parameters:
c- DSpace Contexti- DSpace itempath- path as stringfilename- file name- Returns:
- List of Strings
- Throws:
SQLException- if database errorIOException- if IO errorAuthorizeException- if authorization error
-
processContentFileEntry
protected void processContentFileEntry(Context c, Item i, String path, String fileName, String bundleName, boolean primary) throws SQLException, IOException, AuthorizeException
each entry represents a bitstream....- Parameters:
c- DSpace Contexti- Dspace Itempath- path to filefileName- file namebundleName- bundle nameprimary- if primary bitstream- Throws:
SQLException- if database errorIOException- if IO errorAuthorizeException- if authorization error
-
registerBitstream
protected void registerBitstream(Context c, Item i, int assetstore, String bitstreamPath, String bundleName, String description) throws SQLException, IOException, AuthorizeException
Register the bitstream file into DSpace- Parameters:
c- DSpace Contexti- DSpace Itemassetstore- assetstore numberbitstreamPath- the full filepath expressed in the contents filebundleName- bundle namedescription- bitstream description- Throws:
SQLException- if database errorIOException- if IO errorAuthorizeException- if authorization error
-
processOptions
protected void processOptions(Context c, Item myItem, List<String> options) throws SQLException, AuthorizeException
Process the Options to apply to the Item. The options are tab delimited Options:48217870-MIT.pdf permissions: -r 'MIT Users' description: Full printable version (MIT only) permissions:[r|w]-['group name'] description: 'the description of the file'where:[r|w] (meaning: read|write) ['MIT Users'] (the group name)- Parameters:
c- DSpace ContextmyItem- DSpace Itemoptions- List of option strings- Throws:
SQLException- if database errorAuthorizeException- if authorization error
-
setPermission
protected void setPermission(Context c, Group g, int actionID, Bitstream bs) throws SQLException, AuthorizeException
Set the Permission on a Bitstream.- Parameters:
c- DSpace Contextg- Dspace GroupactionID- action identifierbs- Bitstream- Throws:
SQLException- if database errorAuthorizeException- if authorization error- See Also:
Constants
-
getStringValue
protected String getStringValue(Node node)
Return the String value of a Node.- Parameters:
node- node- Returns:
- string value
-
loadXML
protected Document loadXML(String filename) throws IOException, ParserConfigurationException, SAXException
Load in the XML from file.- Parameters:
filename- the filename to load from- Returns:
- the DOM representation of the XML file
- Throws:
IOException- if IO errorParserConfigurationException- if config errorSAXException- if XML error
-
deleteDirectory
protected boolean deleteDirectory(File path)
Delete a directory and its child files and directories- Parameters:
path- The directory to delete- Returns:
- Whether the deletion was successful or not
-
unzip
public String unzip(File zipfile) throws IOException
Description copied from interface:ItemImportServiceUnzip a file- Specified by:
unzipin interfaceItemImportService- Parameters:
zipfile- file- Returns:
- unzip location
- Throws:
IOException- if error
-
unzip
public String unzip(File zipfile, String destDir) throws IOException
Description copied from interface:ItemImportServiceUnzip a file to a destination- Specified by:
unzipin interfaceItemImportService- Parameters:
zipfile- filedestDir- destination directory- Returns:
- unzip location
- Throws:
IOException- if error
-
unzip
public String unzip(String sourcedir, String zipfilename) throws IOException
Description copied from interface:ItemImportServiceUnzip a file in a specific source directory- Specified by:
unzipin interfaceItemImportService- Parameters:
sourcedir- source directoryzipfilename- file name- Returns:
- unzip location
- Throws:
IOException- if error
-
generateRandomFilename
protected String generateRandomFilename(boolean hidden)
Generate a random filename based on current time- Parameters:
hidden- set to add . as a prefix to make the file hidden- Returns:
- the filename
-
processUIImport
public void processUIImport(String filepath, Collection owningCollection, String[] otherCollections, String resumeDir, String inputType, Context context, boolean template) throws Exception
Given a local file or public URL to a zip file that has the Simple Archive Format, this method imports the contents to DSpace- Specified by:
processUIImportin interfaceItemImportService- Parameters:
filepath- The filepath to local file or the public URL of the zip fileowningCollection- The owning collection the items will belong tootherCollections- The collections the created items will be inserted to, apart from the owning oneresumeDir- In case of a resume request, the directory that containsthe old mapfile and datainputType- The input type of the data (bibtex, csv, etc.), in case of local filecontext- The contexttemplate- whether to use template item- Throws:
Exception- if error
-
emailSuccessMessage
public void emailSuccessMessage(Context context, EPerson eperson, String fileName) throws javax.mail.MessagingException
Description copied from interface:ItemImportServiceIf a batch import is done in a new thread we are unable to communicate with calling method about success or failure. We accomplish this communication with email instead. Send a success email once the batch import is complete- Specified by:
emailSuccessMessagein interfaceItemImportService- Parameters:
context- - the current Contexteperson- - eperson to send the email tofileName- - the filepath to the mapfile created by the batch import- Throws:
javax.mail.MessagingException- if error
-
emailErrorMessage
public void emailErrorMessage(EPerson eperson, String error) throws javax.mail.MessagingException
Description copied from interface:ItemImportServiceIf a batch import is done in a new thread we are unable to communicate with calling method about success or failure. We accomplis this communication with email instead. Send an error email if the batch import fails- Specified by:
emailErrorMessagein interfaceItemImportService- Parameters:
eperson- - EPerson to send the error message toerror- - the error message- Throws:
javax.mail.MessagingException- if error
-
getImportsAvailable
public List<BatchUpload> getImportsAvailable(EPerson eperson) throws Exception
Description copied from interface:ItemImportServiceGet imports available for a person- Specified by:
getImportsAvailablein interfaceItemImportService- Parameters:
eperson- EPerson object- Returns:
- List of batch uploads
- Throws:
Exception- if error
-
getImportUploadableDirectory
public String getImportUploadableDirectory(EPerson ePerson) throws Exception
Description copied from interface:ItemImportServiceGet import upload directory- Specified by:
getImportUploadableDirectoryin interfaceItemImportService- Parameters:
ePerson- EPerson object- Returns:
- directory
- Throws:
Exception- if error
-
deleteBatchUpload
public void deleteBatchUpload(Context c, String uploadId) throws Exception
Description copied from interface:ItemImportServiceDelete a batch by ID- Specified by:
deleteBatchUploadin interfaceItemImportService- Parameters:
c- DSpace ContextuploadId- identifier- Throws:
Exception- if error
-
getTempWorkDir
public String getTempWorkDir()
Description copied from interface:ItemImportServiceGet temporary work directory- Specified by:
getTempWorkDirin interfaceItemImportService- Returns:
- directory as string
-
getTempWorkDirFile
public File getTempWorkDirFile() throws IOException
Description copied from interface:ItemImportServiceGet temporary work directory (as File)- Specified by:
getTempWorkDirFilein interfaceItemImportService- Returns:
- directory as File
- Throws:
IOException- if the directory cannot be created.
-
cleanupZipTemp
public void cleanupZipTemp()
Description copied from interface:ItemImportServiceCleanup- Specified by:
cleanupZipTempin interfaceItemImportService
-
setTest
public void setTest(boolean isTest)
Description copied from interface:ItemImportServiceSet test flag- Specified by:
setTestin interfaceItemImportService- Parameters:
isTest- true or false
-
setExcludeContent
public void setExcludeContent(boolean isExcludeContent)
Description copied from interface:ItemImportServiceSet exclude-content flag.- Specified by:
setExcludeContentin interfaceItemImportService- Parameters:
isExcludeContent- true or false
-
setResume
public void setResume(boolean isResume)
Description copied from interface:ItemImportServiceSet resume flag- Specified by:
setResumein interfaceItemImportService- Parameters:
isResume- true or false
-
setUseWorkflow
public void setUseWorkflow(boolean useWorkflow)
Description copied from interface:ItemImportServiceSet use workflow- Specified by:
setUseWorkflowin interfaceItemImportService- Parameters:
useWorkflow- whether to enable workflow
-
setUseWorkflowSendEmail
public void setUseWorkflowSendEmail(boolean useWorkflowSendEmail)
- Specified by:
setUseWorkflowSendEmailin interfaceItemImportService- Parameters:
useWorkflowSendEmail- whether to send mail
-
setQuiet
public void setQuiet(boolean isQuiet)
Description copied from interface:ItemImportServiceSet quiet flag- Specified by:
setQuietin interfaceItemImportService- Parameters:
isQuiet- true or false
-
setHandler
public void setHandler(DSpaceRunnableHandler handler)
Description copied from interface:ItemImportServiceSet the DSpace Runnable Handler- Specified by:
setHandlerin interfaceItemImportService
-
-