Last active
August 29, 2015 14:16
-
-
Save terrywbrady/f39e88b39dc1a9a05ec4 to your computer and use it in GitHub Desktop.
Dump DSpace Metadata Registry As JSON
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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://www.dspace.org/license/ | |
*/ | |
package org.dspace.ctask.georgetown; | |
/** | |
* Convert metadata field registry to JSON* | |
* @author twb27 | |
*/ | |
public class Field { | |
public static String makeName(String prefix, String element, String qualifier) { | |
StringBuilder sb = new StringBuilder(prefix); | |
sb.append("."); | |
sb.append(element); | |
if (qualifier != null) { | |
sb.append("."); | |
sb.append(qualifier); | |
} | |
return sb.toString(); | |
} | |
private String name; | |
private String element; | |
private String qualifier; | |
private String description; | |
public String name() {return name;} | |
public String element() {return element;} | |
public String qualifier() {return qualifier;} | |
public String description() {return description;} | |
public void setName(String name) {this.name = name;} | |
public void setElement(String element) {this.name = element;} | |
public void setQualifier(String qualifier) {this.name = qualifier;} | |
public void setDescription(String description) {this.name = description;} | |
public Field() { | |
} | |
public Field(String name, String element, String qualifier, String description) { | |
this.name = name; | |
this.element = element; | |
this.qualifier = qualifier; | |
this.description = description; | |
} | |
public String toString() { | |
return String.format("name:%s,element:%s,qualifier:%s,description:%s", name, element, qualifier, description == null ? null : description.replaceAll("\\s+", " ")); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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://www.dspace.org/license/ | |
*/ | |
package org.dspace.ctask.georgetown; | |
import java.util.ArrayList; | |
import java.util.HashMap; | |
import java.util.List; | |
/** | |
* Convert metadata schema registry to JSON | |
* @author twb27 | |
*/ | |
public class Schema { | |
private String prefix; | |
private String namespace; | |
private List<Field> fields; | |
public String prefix() {return prefix;} | |
public String namespace() {return namespace;} | |
public List<Field> getFields() {return fields;} | |
public void setPrefix(String prefix) {this.prefix = prefix;} | |
public void setNamespace(String namespace) {this.namespace = namespace;} | |
public void setFields(List<Field> fields) {this.fields = fields;} | |
public Schema() { | |
} | |
public Schema(String prefix, String namespace) { | |
this.prefix = prefix; | |
this.namespace = namespace; | |
this.fields = new ArrayList<Field>(); | |
} | |
public void addField(String name, String element, String qualifier, String description) { | |
fields.add(new Field(name, element, qualifier, description)); | |
} | |
public String toString() { | |
StringBuilder sb = new StringBuilder(); | |
sb.append(String.format("prefix:%s,namespace:%s", prefix, namespace)); | |
for(Field f: fields) { | |
sb.append(String.format("%n\t%s",f)); | |
} | |
return sb.toString(); | |
} | |
static public HashMap<String,Field> getFields(Schema[] schemas) { | |
HashMap<String,Field> fields = new HashMap<String,Field>(); | |
for(Schema schema: schemas) { | |
for(Field field: schema.getFields()) { | |
fields.put(field.name(), field); | |
} | |
} | |
return fields; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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://www.dspace.org/license/ | |
*/ | |
package org.dspace.ctask.georgetown; | |
import java.io.IOException; | |
import java.sql.SQLException; | |
import java.util.ArrayList; | |
import java.util.List; | |
import org.dspace.authorize.AuthorizeManager; | |
import org.dspace.content.Bitstream; | |
import org.dspace.content.Bundle; | |
import org.dspace.content.Collection; | |
import org.dspace.content.Community; | |
import org.dspace.content.DSpaceObject; | |
import org.dspace.content.Item; | |
import org.dspace.content.ItemIterator; | |
import org.dspace.content.MetadataField; | |
import org.dspace.content.MetadataSchema; | |
import org.dspace.core.Constants; | |
import org.dspace.core.Context; | |
import org.dspace.curate.AbstractCurationTask; | |
import org.dspace.curate.Curator; | |
import org.dspace.curate.Distributive; | |
import com.google.gson.Gson; | |
/** | |
* Dump the metadata registry to a JSON file | |
* /opt/dspace/bin/dspace curate -i all -t metadatareg -r - > metadatareg.json | |
* @author twb27 | |
*/ | |
@Distributive | |
public class SchemaReport extends AbstractCurationTask | |
{ | |
@Override | |
public int perform(DSpaceObject dso) throws IOException | |
{ | |
try { | |
return perform(Curator.curationContext(),""); | |
} catch (SQLException e) { | |
return Curator.CURATE_FAIL; | |
} | |
} | |
@Override | |
public int perform(Context ctx, String id) throws IOException | |
{ | |
try { | |
List<Schema> mySchemas = new ArrayList<Schema>(); | |
MetadataSchema[] schemas = MetadataSchema.findAll(Curator.curationContext()); | |
for(MetadataSchema schema: schemas) { | |
Schema mySchema = new Schema(schema.getName(), schema.getNamespace()); | |
mySchemas.add(mySchema); | |
MetadataField[] fields = MetadataField.findAllInSchema(Curator.curationContext(), schema.getSchemaID()); | |
for(MetadataField field: fields){ | |
String fname = Field.makeName(mySchema.prefix(), field.getElement(), field.getQualifier()); | |
mySchema.addField(fname, field.getElement(), field.getQualifier(), field.getScopeNote()); | |
} | |
} | |
Gson gson = new Gson(); | |
String s = gson.toJson(mySchemas); | |
report(s); | |
setResult(s); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
return Curator.CURATE_SUCCESS; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment