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:
Alan Orth 2020-08-02 15:51:18 +03:00
parent a6d3653c9e
commit e5d45e62be
Signed by: alanorth
GPG Key ID: 0FB860CC9C45B1B9

View File

@ -37,68 +37,100 @@ import java.util.List;
public class CountryCodeTagger extends AbstractCurationTask public class CountryCodeTagger extends AbstractCurationTask
{ {
private int status = Curator.CURATE_UNSET; public class CountryCodeTaggerConfig {
private String result = null; private final String isocodesJsonPath = "/org/cgiar/cgspace/ctasks/iso_3166-1.json";
private final String cgspaceCountriesJsonPath = "/org/cgiar/cgspace/ctasks/cgspace-countries.json";
private static String isocodesJsonPath; private final String iso3166Field = taskProperty("iso3166.field");
private static String cgspaceCountriesJsonPath; private final String iso3166Alpha2Field = taskProperty("iso3166-alpha2.field");
private static String iso3166Field; private final boolean forceupdate = taskBooleanProperty("forceupdate", false);
private static String iso3166Alpha2Field;
private static boolean forceupdate;
private List<String> results = new ArrayList<String>(); 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 @Override
public int perform(DSpaceObject dso) throws IOException public int perform(DSpaceObject dso) throws IOException
{ {
// Load configuration // gotta define this here so we can access it after the if context...
isocodesJsonPath = "/org/cgiar/cgspace/ctasks/iso_3166-1.json"; CountryCodeTaggerResult alpha2Result = new CountryCodeTaggerResult();
cgspaceCountriesJsonPath = "/org/cgiar/cgspace/ctasks/cgspace-countries.json";
iso3166Field = taskProperty("iso3166.field");
iso3166Alpha2Field = taskProperty("iso3166-alpha2.field");
forceupdate = taskBooleanProperty("forceupdate", false);
if (dso.getType() == Constants.ITEM) if (dso.getType() == Constants.ITEM)
{ {
// Load configuration
CountryCodeTaggerConfig config = new CountryCodeTaggerConfig();
Item item = (Item)dso; 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(); String itemHandle = item.getHandle();
Metadatum[] itemCountries = item.getMetadataByMetadataString(iso3166Field); Metadatum[] itemCountries = item.getMetadataByMetadataString(config.iso3166Field);
// skip items that don't have country metadata // skip items that don't have country metadata
if (itemCountries.length == 0) { if (itemCountries.length == 0) {
result = itemHandle + ": no countries, skipping."; alpha2Result.setResult(itemHandle + ": no countries, skipping.");
status = Curator.CURATE_SKIP; alpha2Result.setStatus(Curator.CURATE_SKIP);
} else { } else {
Gson gson = new Gson(); Gson gson = new Gson();
// TODO: convert to try: https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html // 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); ISO3166CountriesVocabulary isocodesCountriesJson = gson.fromJson(reader, ISO3166CountriesVocabulary.class);
reader.close(); 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); CGSpaceCountriesVocabulary cgspaceCountriesJson = gson.fromJson(reader, CGSpaceCountriesVocabulary.class);
reader.close(); reader.close();
//System.out.println(itemHandle + ": " + itemCountries.length + " countries possibly need tagging"); //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() // 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); item.clearMetadata(iso3166Alpha2FieldParts[0], iso3166Alpha2FieldParts[1], iso3166Alpha2FieldParts[2], Item.ANY);
} }
// check the item's country codes, if any // check the item's country codes, if any
Metadatum[] itemAlpha2CountryCodes = item.getMetadataByMetadataString(iso3166Alpha2Field); Metadatum[] itemAlpha2CountryCodes = item.getMetadataByMetadataString(config.iso3166Alpha2Field);
if (itemAlpha2CountryCodes.length == 0) { if (itemAlpha2CountryCodes.length == 0) {
//System.out.println(itemHandle + ": Should add codes for " + itemCountries.length + " countries."); //System.out.println(itemHandle + ": Should add codes for " + itemCountries.length + " countries.");
Integer addedCodeCount = 0; int addedCodeCount = 0;
for (Metadatum itemCountry : itemCountries) { for (Metadatum itemCountry : itemCountries) {
//check ISO 3166-1 countries //check ISO 3166-1 countries
for (CountriesVocabulary.Country country : isocodesCountriesJson.countries) { for (CountriesVocabulary.Country country : isocodesCountriesJson.countries) {
@ -111,12 +143,12 @@ public class CountryCodeTagger extends AbstractCurationTask
addedCodeCount++; addedCodeCount++;
result = itemHandle + ": added " + addedCodeCount + " country code(s)"; alpha2Result.setResult(itemHandle + ": added " + addedCodeCount + " country code(s)");
status = Curator.CURATE_SUCCESS; alpha2Result.setStatus(Curator.CURATE_SUCCESS);
} catch (SQLException | AuthorizeException sqle) { } catch (SQLException | AuthorizeException sqle) {
log.debug(sqle.getMessage()); config.log.debug(sqle.getMessage());
result = itemHandle + ": error"; alpha2Result.setResult(itemHandle + ": error");
status = Curator.CURATE_ERROR; alpha2Result.setStatus(Curator.CURATE_ERROR);
} }
} }
} }
@ -132,26 +164,22 @@ public class CountryCodeTagger extends AbstractCurationTask
addedCodeCount++; addedCodeCount++;
result = itemHandle + ": added " + addedCodeCount + " country code(s)"; alpha2Result.setResult(itemHandle + ": added " + addedCodeCount + " country code(s)");
status = Curator.CURATE_SUCCESS; alpha2Result.setStatus(Curator.CURATE_SUCCESS);
} catch (SQLException | AuthorizeException sqle) { } catch (SQLException | AuthorizeException sqle) {
log.debug(sqle.getMessage()); config.log.debug(sqle.getMessage());
result = itemHandle + ": error"; alpha2Result.setResult(itemHandle + ": error");
status = Curator.CURATE_ERROR; alpha2Result.setStatus(Curator.CURATE_ERROR);
} }
} }
} }
} }
} else { } else {
result = itemHandle + ": item has country codes, skipping"; alpha2Result.setResult(itemHandle + ": item has country codes, skipping");
status = Curator.CURATE_SKIP; alpha2Result.setStatus(Curator.CURATE_SKIP);
} }
} }
setResult(result); return alpha2Result;
report(result);
}
return status;
} }
} }