mirror of
https://github.com/ilri/cgspace-java-helpers.git
synced 2024-11-22 23:05:12 +01:00
src/main/java: Refactor CountryCodeTagger.java
Now is much more modular and can easily, cleanly be extended to do ISO 3166-1 Alpha3, numeric, etc...
This commit is contained in:
parent
a6d3653c9e
commit
e5d45e62be
@ -37,68 +37,100 @@ import java.util.List;
|
||||
|
||||
public class CountryCodeTagger extends AbstractCurationTask
|
||||
{
|
||||
private int status = Curator.CURATE_UNSET;
|
||||
private String result = null;
|
||||
|
||||
private static String isocodesJsonPath;
|
||||
private static String cgspaceCountriesJsonPath;
|
||||
private static String iso3166Field;
|
||||
private static String iso3166Alpha2Field;
|
||||
private static boolean forceupdate;
|
||||
public class CountryCodeTaggerConfig {
|
||||
private final String isocodesJsonPath = "/org/cgiar/cgspace/ctasks/iso_3166-1.json";
|
||||
private final String cgspaceCountriesJsonPath = "/org/cgiar/cgspace/ctasks/cgspace-countries.json";
|
||||
private final String iso3166Field = taskProperty("iso3166.field");
|
||||
private final String iso3166Alpha2Field = taskProperty("iso3166-alpha2.field");
|
||||
private final boolean forceupdate = taskBooleanProperty("forceupdate", false);
|
||||
|
||||
private List<String> results = new ArrayList<String>();
|
||||
|
||||
private static Logger log = Logger.getLogger(CountryCodeTagger.class);
|
||||
private Logger log = Logger.getLogger(CountryCodeTagger.class);
|
||||
}
|
||||
|
||||
public class CountryCodeTaggerResult {
|
||||
private int status = Curator.CURATE_UNSET;
|
||||
private String result = null;
|
||||
|
||||
public int getStatus() {
|
||||
return status;
|
||||
}
|
||||
|
||||
public void setStatus(int status) {
|
||||
this.status = status;
|
||||
}
|
||||
|
||||
public String getResult() {
|
||||
return result;
|
||||
}
|
||||
|
||||
public void setResult(String result) {
|
||||
this.result = result;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int perform(DSpaceObject dso) throws IOException
|
||||
{
|
||||
// Load configuration
|
||||
isocodesJsonPath = "/org/cgiar/cgspace/ctasks/iso_3166-1.json";
|
||||
cgspaceCountriesJsonPath = "/org/cgiar/cgspace/ctasks/cgspace-countries.json";
|
||||
iso3166Field = taskProperty("iso3166.field");
|
||||
iso3166Alpha2Field = taskProperty("iso3166-alpha2.field");
|
||||
forceupdate = taskBooleanProperty("forceupdate", false);
|
||||
// gotta define this here so we can access it after the if context...
|
||||
CountryCodeTaggerResult alpha2Result = new CountryCodeTaggerResult();
|
||||
|
||||
if (dso.getType() == Constants.ITEM)
|
||||
{
|
||||
// Load configuration
|
||||
CountryCodeTaggerConfig config = new CountryCodeTaggerConfig();
|
||||
|
||||
Item item = (Item)dso;
|
||||
|
||||
alpha2Result = performAlpha2(item, config);
|
||||
|
||||
setResult(alpha2Result.getResult());
|
||||
report(alpha2Result.getResult());
|
||||
}
|
||||
|
||||
return alpha2Result.getStatus();
|
||||
}
|
||||
|
||||
public CountryCodeTaggerResult performAlpha2(Item item, CountryCodeTaggerConfig config) throws IOException
|
||||
{
|
||||
CountryCodeTaggerResult alpha2Result = new CountryCodeTaggerResult();
|
||||
String itemHandle = item.getHandle();
|
||||
|
||||
Metadatum[] itemCountries = item.getMetadataByMetadataString(iso3166Field);
|
||||
Metadatum[] itemCountries = item.getMetadataByMetadataString(config.iso3166Field);
|
||||
|
||||
// skip items that don't have country metadata
|
||||
if (itemCountries.length == 0) {
|
||||
result = itemHandle + ": no countries, skipping.";
|
||||
status = Curator.CURATE_SKIP;
|
||||
alpha2Result.setResult(itemHandle + ": no countries, skipping.");
|
||||
alpha2Result.setStatus(Curator.CURATE_SKIP);
|
||||
} else {
|
||||
Gson gson = new Gson();
|
||||
|
||||
// TODO: convert to try: https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html
|
||||
BufferedReader reader = new BufferedReader(new InputStreamReader(this.getClass().getResourceAsStream(isocodesJsonPath)));
|
||||
BufferedReader reader = new BufferedReader(new InputStreamReader(this.getClass().getResourceAsStream(config.isocodesJsonPath)));
|
||||
ISO3166CountriesVocabulary isocodesCountriesJson = gson.fromJson(reader, ISO3166CountriesVocabulary.class);
|
||||
reader.close();
|
||||
|
||||
reader = new BufferedReader(new InputStreamReader(this.getClass().getResourceAsStream(cgspaceCountriesJsonPath)));
|
||||
reader = new BufferedReader(new InputStreamReader(this.getClass().getResourceAsStream(config.cgspaceCountriesJsonPath)));
|
||||
CGSpaceCountriesVocabulary cgspaceCountriesJson = gson.fromJson(reader, CGSpaceCountriesVocabulary.class);
|
||||
reader.close();
|
||||
|
||||
//System.out.println(itemHandle + ": " + itemCountries.length + " countries possibly need tagging");
|
||||
|
||||
// split the alpha2 country code field into schema, element, and qualifier so we can use it with item.addMetadata()
|
||||
String[] iso3166Alpha2FieldParts = iso3166Alpha2Field.split("\\.");
|
||||
String[] iso3166Alpha2FieldParts = config.iso3166Alpha2Field.split("\\.");
|
||||
|
||||
if (forceupdate) {
|
||||
if (config.forceupdate) {
|
||||
item.clearMetadata(iso3166Alpha2FieldParts[0], iso3166Alpha2FieldParts[1], iso3166Alpha2FieldParts[2], Item.ANY);
|
||||
}
|
||||
|
||||
// check the item's country codes, if any
|
||||
Metadatum[] itemAlpha2CountryCodes = item.getMetadataByMetadataString(iso3166Alpha2Field);
|
||||
Metadatum[] itemAlpha2CountryCodes = item.getMetadataByMetadataString(config.iso3166Alpha2Field);
|
||||
|
||||
if (itemAlpha2CountryCodes.length == 0) {
|
||||
//System.out.println(itemHandle + ": Should add codes for " + itemCountries.length + " countries.");
|
||||
|
||||
Integer addedCodeCount = 0;
|
||||
int addedCodeCount = 0;
|
||||
for (Metadatum itemCountry : itemCountries) {
|
||||
//check ISO 3166-1 countries
|
||||
for (CountriesVocabulary.Country country : isocodesCountriesJson.countries) {
|
||||
@ -111,12 +143,12 @@ public class CountryCodeTagger extends AbstractCurationTask
|
||||
|
||||
addedCodeCount++;
|
||||
|
||||
result = itemHandle + ": added " + addedCodeCount + " country code(s)";
|
||||
status = Curator.CURATE_SUCCESS;
|
||||
alpha2Result.setResult(itemHandle + ": added " + addedCodeCount + " country code(s)");
|
||||
alpha2Result.setStatus(Curator.CURATE_SUCCESS);
|
||||
} catch (SQLException | AuthorizeException sqle) {
|
||||
log.debug(sqle.getMessage());
|
||||
result = itemHandle + ": error";
|
||||
status = Curator.CURATE_ERROR;
|
||||
config.log.debug(sqle.getMessage());
|
||||
alpha2Result.setResult(itemHandle + ": error");
|
||||
alpha2Result.setStatus(Curator.CURATE_ERROR);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -132,26 +164,22 @@ public class CountryCodeTagger extends AbstractCurationTask
|
||||
|
||||
addedCodeCount++;
|
||||
|
||||
result = itemHandle + ": added " + addedCodeCount + " country code(s)";
|
||||
status = Curator.CURATE_SUCCESS;
|
||||
alpha2Result.setResult(itemHandle + ": added " + addedCodeCount + " country code(s)");
|
||||
alpha2Result.setStatus(Curator.CURATE_SUCCESS);
|
||||
} catch (SQLException | AuthorizeException sqle) {
|
||||
log.debug(sqle.getMessage());
|
||||
result = itemHandle + ": error";
|
||||
status = Curator.CURATE_ERROR;
|
||||
config.log.debug(sqle.getMessage());
|
||||
alpha2Result.setResult(itemHandle + ": error");
|
||||
alpha2Result.setStatus(Curator.CURATE_ERROR);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
result = itemHandle + ": item has country codes, skipping";
|
||||
status = Curator.CURATE_SKIP;
|
||||
alpha2Result.setResult(itemHandle + ": item has country codes, skipping");
|
||||
alpha2Result.setStatus(Curator.CURATE_SKIP);
|
||||
}
|
||||
}
|
||||
|
||||
setResult(result);
|
||||
report(result);
|
||||
}
|
||||
|
||||
return status;
|
||||
return alpha2Result;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user