Skip to content

Instantly share code, notes, and snippets.

@alanorth
Created July 29, 2020 05:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alanorth/6a31af592b3467f7b63ac8aea7c75d52 to your computer and use it in GitHub Desktop.
Save alanorth/6a31af592b3467f7b63ac8aea7c75d52 to your computer and use it in GitHub Desktop.
Jython-based curation task to update countryCodes based on countries
import json
from org.dspace.core import ConfigurationManager
from org.dspace.curate import ScriptedTask
class MyTask(ScriptedTask):
def init(self, curator, taskName):
print "Initializing MyTask with Jython"
def performDso(self, dso):
# I don't think we can use isinstance() with Java types...
if dso.getClass().getSimpleName() == "Item":
iso3166_1_json_path = ConfigurationManager.getProperty(
"mytask", "iso3166_1_json_path"
)
iso3166_1_country_field = ConfigurationManager.getProperty(
"mytask", "iso3166_1_country_field"
)
iso3166_1_alpha2_country_field = ConfigurationManager.getProperty(
"mytask", "iso3166_1_alpha2_country_field"
)
with open(iso3166_1_json_path) as fh:
iso3166_1_json = json.load(fh)
print(len(iso3166_1_json['3166-1']))
# get a list of countries the item has (in any lang)
countries = dso.getMetadataByMetadataString(iso3166_1_country_field)
countryCodesAlpha2 = dso.getMetadataByMetadataString(iso3166_1_alpha2_country_field)
# return immediately if the item doesn't have any countries
if len(countries) == 0:
return 0
# get a list of country codes in the item's metadata
countryCodesAlpha2 = [countryCode.value for countryCode in dso.getMetadataByMetadataString(iso3166_1_alpha2_country_field)]
# see dspace-api/src/main/java/org/dspace/content/DSpaceObject.java
for country in countries:
print ("Checking " + country.value)
if "CO" not in countryCodesAlpha2:
print ("Missing CO country code")
dso.addMetadata("cg", "coverage", "iso3166-alpha2", "en_US", "CO")
dso.update()
### SHIT THAT WORKS BUT I DON'T NEED ###
# print(dso.getName())
return 0
def performId(self, context, id):
print "perform on id %s" % (id)
return 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment