mirror of
https://github.com/DSpace/DSpace.git
synced 2025-10-07 01:54:22 +00:00

git-svn-id: http://scm.dspace.org/svn/repo/dspace/trunk@5674 9c30dcfa-912a-0410-8fc2-9e0234be79fd
143 lines
4.4 KiB
Java
143 lines
4.4 KiB
Java
/*
|
|
* The contents of this file are subject to the license and copyright
|
|
* detailed in the LICENSE and NOTICE files at the root of the source
|
|
* tree and available online at
|
|
*
|
|
* http://dspace.org/license/
|
|
*/
|
|
|
|
package org.dspace.curate;
|
|
|
|
import java.io.IOException;
|
|
import java.sql.SQLException;
|
|
import java.util.ArrayList;
|
|
import java.util.HashMap;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
|
|
import org.dspace.app.util.DCInput;
|
|
import org.dspace.app.util.DCInputSet;
|
|
import org.dspace.app.util.DCInputsReader;
|
|
import org.dspace.app.util.DCInputsReaderException;
|
|
import org.dspace.content.DCValue;
|
|
import org.dspace.content.DSpaceObject;
|
|
import org.dspace.content.Item;
|
|
import org.dspace.core.Constants;
|
|
|
|
/**
|
|
* RequiredMetadata task compares item metadata with fields
|
|
* marked as required in input-forms.xml. The task succeeds if all
|
|
* required fields are present in the item metadata, otherwise it fails.
|
|
* Primarily a curation task demonstrator.
|
|
*
|
|
* @author richardrodgers
|
|
*/
|
|
@Suspendable
|
|
public class RequiredMetadata extends AbstractCurationTask
|
|
{
|
|
// map of DCInputSets
|
|
private DCInputsReader reader = null;
|
|
// map of required fields
|
|
private Map<String, List<String>> reqMap = new HashMap<String, List<String>>();
|
|
|
|
@Override public void init(Curator curator, String taskId) throws IOException
|
|
{
|
|
super.init(curator, taskId);
|
|
try
|
|
{
|
|
reader = new DCInputsReader();
|
|
}
|
|
catch (DCInputsReaderException dcrE)
|
|
{
|
|
throw new IOException(dcrE.getMessage(), dcrE);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Perform the curation task upon passed DSO
|
|
*
|
|
* @param dso the DSpace object
|
|
* @throws IOException
|
|
*/
|
|
@Override
|
|
public int perform(DSpaceObject dso) throws IOException
|
|
{
|
|
if (dso.getType() == Constants.ITEM)
|
|
{
|
|
Item item = (Item)dso;
|
|
int count = 0;
|
|
try
|
|
{
|
|
StringBuilder sb = new StringBuilder();
|
|
String handle = item.getHandle();
|
|
if (handle == null)
|
|
{
|
|
// we are still in workflow - no handle assigned
|
|
handle = "in workflow";
|
|
}
|
|
sb.append("Item: ").append(handle);
|
|
for (String req : getReqList(item.getOwningCollection().getHandle()))
|
|
{
|
|
DCValue[] vals = item.getMetadata(req);
|
|
if (vals.length == 0)
|
|
{
|
|
sb.append(" missing required field: ").append(req);
|
|
count++;
|
|
}
|
|
}
|
|
report(sb.toString());
|
|
setResult(sb.toString());
|
|
}
|
|
catch (DCInputsReaderException dcrE)
|
|
{
|
|
throw new IOException(dcrE.getMessage(), dcrE);
|
|
}
|
|
catch (SQLException sqlE)
|
|
{
|
|
throw new IOException(sqlE.getMessage(), sqlE);
|
|
}
|
|
return (count == 0) ? Curator.CURATE_SUCCESS : Curator.CURATE_FAIL;
|
|
}
|
|
else
|
|
{
|
|
setResult("OK");
|
|
return Curator.CURATE_SKIP;
|
|
}
|
|
}
|
|
|
|
private List<String> getReqList(String handle) throws DCInputsReaderException
|
|
{
|
|
List<String> reqList = reqMap.get(handle);
|
|
if (reqList == null)
|
|
{
|
|
reqList = reqMap.get("default");
|
|
}
|
|
if (reqList == null)
|
|
{
|
|
reqList = new ArrayList<String>();
|
|
DCInputSet inputs = reader.getInputs(handle);
|
|
for (int i = 0; i < inputs.getNumberPages(); i++)
|
|
{
|
|
for (DCInput input : inputs.getPageRows(i, true, true))
|
|
{
|
|
if (input.isRequired())
|
|
{
|
|
StringBuilder sb = new StringBuilder();
|
|
sb.append(input.getSchema()).append(".");
|
|
sb.append(input.getElement()).append(".");
|
|
String qual = input.getQualifier();
|
|
if (qual == null)
|
|
{
|
|
qual = "";
|
|
}
|
|
sb.append(qual);
|
|
reqList.add(sb.toString());
|
|
}
|
|
}
|
|
}
|
|
reqMap.put(inputs.getFormName(), reqList);
|
|
}
|
|
return reqList;
|
|
}
|
|
}
|