Export all collections of a community recursively

This commit is contained in:
Keiji Suzuki
2013-07-07 11:07:42 +09:00
parent 28502b115c
commit 2b4633a87c
4 changed files with 124 additions and 17 deletions

View File

@@ -21,9 +21,12 @@ import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
@@ -821,16 +824,17 @@ public class ItemExport
// items
// it will be checked against the config file entry
double size = 0;
final ArrayList<Integer> items = new ArrayList<Integer>();
final HashMap<String, List<Integer>> itemsMap = new HashMap<String, List<Integer>>();
for (DSpaceObject dso : dsObjects)
{
if (dso.getType() == Constants.COMMUNITY)
{
Community community = (Community) dso;
// get all the collections in the community
Collection[] collections = community.getCollections();
Collection[] collections = community.getAllCollections();
for (Collection collection : collections)
{
ArrayList<Integer> items = new ArrayList<Integer>();
// get all the items in each collection
ItemIterator iitems = collection.getItems();
try
@@ -859,12 +863,18 @@ public class ItemExport
{
iitems.close();
}
if (items.size() > 0)
{
itemsMap.put("collection_"+collection.getID(), items);
}
}
}
}
else if (dso.getType() == Constants.COLLECTION)
{
Collection collection = (Collection) dso;
ArrayList<Integer> items = new ArrayList<Integer>();
// get all the items in the collection
ItemIterator iitems = collection.getItems();
try
@@ -893,6 +903,10 @@ public class ItemExport
{
iitems.close();
}
if (items.size() > 0)
{
itemsMap.put("collection_"+collection.getID(), items);
}
}
}
else if (dso.getType() == Constants.ITEM)
@@ -910,7 +924,9 @@ public class ItemExport
size += bit.getSize();
}
}
ArrayList<Integer> items = new ArrayList<Integer>();
items.add(item.getID());
itemsMap.put("item_"+item.getID(), items);
}
else
{
@@ -942,7 +958,7 @@ public class ItemExport
}
// if we have any items to process then kick off annonymous thread
if (items.size() > 0)
if (itemsMap.size() > 0)
{
Thread go = new Thread()
{
@@ -955,16 +971,30 @@ public class ItemExport
// create a new dspace context
context = new Context();
// ignore auths
context.setIgnoreAuthorization(true);
iitems = new ItemIterator(context, items);
context.turnOffAuthorisationSystem();
String fileName = assembleFileName("item", eperson,
new Date());
String workDir = getExportWorkDirectory()
String workParentDir = getExportWorkDirectory()
+ System.getProperty("file.separator")
+ fileName;
String downloadDir = getExportDownloadDirectory(eperson
.getID());
File dnDir = new File(downloadDir);
if (!dnDir.exists() && !dnDir.mkdirs())
{
log.error("Unable to create download directory");
}
Iterator<String> iter = itemsMap.keySet().iterator();
while(iter.hasNext())
{
String keyName = iter.next();
iitems = new ItemIterator(context, itemsMap.get(keyName));
String workDir = workParentDir
+ System.getProperty("file.separator")
+ keyName;
File wkDir = new File(workDir);
if (!wkDir.exists() && !wkDir.mkdirs())
@@ -972,23 +1002,21 @@ public class ItemExport
log.error("Unable to create working directory");
}
File dnDir = new File(downloadDir);
if (!dnDir.exists() && !dnDir.mkdirs())
{
log.error("Unable to create download directory");
}
// export the items using normal export method
exportItem(context, iitems, workDir, 1, migrate);
iitems.close();
}
// now zip up the export directory created above
zip(workDir, downloadDir
zip(workParentDir, downloadDir
+ System.getProperty("file.separator")
+ fileName + ".zip");
// email message letting user know the file is ready for
// download
emailSuccessMessage(context, eperson, fileName + ".zip");
// return to enforcing auths
context.setIgnoreAuthorization(false);
context.restoreAuthSystemState();
}
catch (Exception e1)
{
@@ -1023,6 +1051,11 @@ public class ItemExport
go.isDaemon();
go.start();
}
else
{
Locale supportedLocale = I18nUtil.getEPersonLocale(eperson);
emailErrorMessage(eperson, I18nUtil.getMessage("org.dspace.app.itemexport.no-result", supportedLocale));
}
}
/**

View File

@@ -793,6 +793,48 @@ public class Community extends DSpaceObject
return communityArray;
}
/**
* Return an array of collections of this community and its subcommunities
*
* @return an array of colections
*/
public Collection[] getAllCollections() throws SQLException
{
List<Collection> collectionList = new ArrayList<Collection>();
for (Community subcommunity : getSubcommunities())
{
addCollectionList(subcommunity, collectionList);
}
for (Collection collection : getCollections())
{
collectionList.add(collection);
}
// Put them in an array
Collection[] collectionArray = new Collection[collectionList.size()];
collectionArray = (Collection[]) collectionList.toArray(collectionArray);
return collectionArray;
}
/**
* Internal method to process subcommunities recursively
*/
private void addCollectionList(Community community, List<Collection> collectionList) throws SQLException
{
for (Community subcommunity : community.getSubcommunities())
{
addCollectionList(subcommunity, collectionList);
}
for (Collection collection : community.getCollections())
{
collectionList.add(collection);
}
}
/**
* Create a new collection within this community. The collection is created
* without any workflow groups or default submitter group.

View File

@@ -1350,6 +1350,7 @@ news-side.html = news-side.html
news-top.html = news-top.html
org.dspace.app.itemexport.no-result = The DSpaceObject that you specified has no items.
org.dspace.app.webui.jsptag.CollectionListTag.collectionName = Collection Name
org.dspace.app.webui.jsptag.CommunityListTag.communityName = Community Name
org.dspace.app.webui.jsptag.ItemListTag.authors = Authors

View File

@@ -766,6 +766,37 @@ public class CommunityTest extends AbstractDSpaceObjectTest
assertThat("testGetAllParents 4", son.getAllParents()[0], equalTo(c));
}
/**
* Test of getAllCollections method, of class Community.
*/
@Test
public void testGetAllCollections() throws Exception
{
new NonStrictExpectations()
{
AuthorizeManager authManager;
{
AuthorizeManager.authorizeAction((Context) any, (Community) any,
Constants.ADD); result = null;
AuthorizeManager.authorizeActionBoolean((Context) any, (Community) any,
Constants.ADD); result = true;
}
};
//empty by default
assertThat("testGetAllCollections 0",c.getAllCollections(), notNullValue());
assertTrue("testGetAllCollections 1", c.getAllCollections().length == 0);
//community has a collection and a subcommunity, subcommunity has a collection
Collection collOfC = c.createCollection();
Community sub = Community.create(c, context);
Collection collOfSub = sub.createCollection();
assertThat("testGetAllCollections 2",c.getAllCollections(), notNullValue());
assertTrue("testGetAllCollections 3", c.getAllCollections().length == 2);
assertThat("testGetAllCollections 4", c.getAllCollections()[0], equalTo(collOfSub));
assertThat("testGetAllCollections 5", c.getAllCollections()[1], equalTo(collOfC));
}
/**
* Test of createCollection method, of class Community.
*/