Index: src/java/org/fluidproject/services/CouchdbUtility.java =================================================================== --- src/java/org/fluidproject/services/CouchdbUtility.java (revision 9141) +++ src/java/org/fluidproject/services/CouchdbUtility.java (working copy) @@ -1,42 +1,125 @@ package org.fluidproject.services; +import java.io.BufferedReader; +import java.io.ByteArrayInputStream; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.net.URL; import java.util.ArrayList; +import java.util.List; +import java.util.Map; import net.sf.json.JSONArray; import org.fluidproject.services.EngageDocument; import org.jcouchdb.db.Database; import org.jcouchdb.document.Document; +import uk.org.ponder.json.support.JSONProvider; +import uk.org.ponder.saxalizer.SAXalizerMappingContext; + public class CouchdbUtility { -// public static final String[] files = { -// "2009-07-23__star_trek_spock_report.csv", -// "2009-07-23__comic_book__report.csv" -// }; + public static final String dir = "C:\\Users\\Fluid\\workspace\\services-sketches\\src\\java\\org\\fluidproject\\services\\"; + + public InputStream getResource(String name) throws FileNotFoundException { + return new FileInputStream(dir + name); + } + + public InputStream readResource(URL url) throws IOException { + String response = ""; + BufferedReader in = + new BufferedReader(new InputStreamReader(url.openStream())); + String str; + while ((str = in.readLine()) != null) { + response = response.concat(str); + } + in.close(); + if (response.equalsIgnoreCase("")) { + return null; + } + return new ByteArrayInputStream(response.getBytes("UTF-8")); + } + + public Map prepareImport(String urlString, String configFile) throws Exception { + URL url = new URL(urlString); + JSONProvider provider = new JSONProvider(); + + provider.setMappingContext(new SAXalizerMappingContext()); + InputStream config = this.getResource(configFile); + List patterns = (List) provider.readObject(new ArrayList(), config); + config.close(); + + XPPJSONGenerator gen = new XPPJSONGenerator(patterns); + InputStream fis = this.readResource(url); + if (fis == null) { + return null; + } + gen.parseStream(fis); + fis.close(); + + return gen.root; + } + + public String buildExhibitURL(String id, String lang) { + return "http://www.mccord-museum.qc.ca/fluidengage/xml/exhibit.php?exhibitID=" + id + "&Lang=" + lang; + } + + public String buildArtifactURL(String id, String lang) { + return "http://www.mccord-museum.qc.ca/fluidengage/xml/artifact.php?Lang=" + lang + "&accessnumber=" + id; + } - public static final String[] files = { "Cartoon.xml", "Headdress.xml", - "Photograph.xml", "Snuffbox.xml" }; - - public static final String dir = "/home/yura/scratchpad/services-sketches/mccord/"; - - public static void main(String[] args) { - long time = System.currentTimeMillis(); + public static void main(String[] args) { try { - Database db = new Database("titan.atrc.utoronto.ca", "mccord"); - Converter c = ConverterFactory.createConverter(ConverterFactory.ConverterType.XML); + + CouchdbUtility cdbu = new CouchdbUtility(); + + Database db = new Database("142.150.154.59", "test1"); + Database art_db = new Database("142.150.154.59", "test2"); + ArrayList docs = new ArrayList(); - for (int i = 0; i < files.length; i++) { - String filename = dir + files[i]; - JSONArray json = c.convertToJSON(filename); - for (int j = 0; j < json.size(); j++) { - docs.add(new EngageDocument(json.get(j))); - } + + int[] ids = {55, 53, 4, 60, 57}; + + for (int i = 0; i < ids.length; i++) { + ArrayList art_docs = new ArrayList(); + for (int lang = 1; lang < 3; lang++) { + String docURL = cdbu.buildExhibitURL(String.valueOf(ids[i]), String.valueOf(lang)); + Map root = cdbu.prepareImport(docURL, "config.json"); + JSONArray json = JSONArray.fromObject(root); + if (root.get("exhibit") != null && ((Map)root.get("exhibit")).get("artifacts") != null && + ((Map)((Map)root.get("exhibit")).get("artifacts")).get("artifact") != null) { + for (Map artifact : (ArrayList)((Map)((Map)root.get("exhibit")).get("artifacts")).get("artifact")) { + String anumber = (String)artifact.get("accessnumber"); + String artdocURL = cdbu.buildArtifactURL(anumber, String.valueOf(lang)); + try { + Map artRoot = cdbu.prepareImport(artdocURL, "config2.json"); + if (artRoot != null) { + JSONArray artJson = JSONArray.fromObject(artRoot); + for (int y = 0; y < artJson.size(); y++) { + art_docs.add(new EngageDocument(artJson.get(y))); + } + } + } + catch (Exception e) { + System.err.println(anumber); + e.printStackTrace(System.err); + } + } + } + for (int k = 0; k < json.size(); k++) { + docs.add(new EngageDocument(json.get(k))); + } + } + art_db.bulkCreateDocuments(art_docs); } db.bulkCreateDocuments(docs); + System.out.println("FINISHED"); } catch (Exception e) { e.printStackTrace(System.err); } - System.out.println("Template parsed in " + (System.currentTimeMillis() - time) + "ms"); } }