view src/main/java/org/mpi/openmind/scripts/DivaImportHttp.java @ 112:933d17f95016

new script MigratePrimeAliases to migrate is_prime_alias_X_of.
author Robert Casties <casties@mpiwg-berlin.mpg.de>
date Wed, 14 Aug 2019 20:48:02 +0200
parents 8013b12cecf7
children 0a8facc3d296
line wrap: on
line source

package org.mpi.openmind.scripts;

import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.apache.log4j.ConsoleAppender;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.log4j.PatternLayout;
import org.json.JSONArray;
import org.json.JSONObject;
import org.json.JSONTokener;
import org.mpi.openmind.cache.WrapperService;
import org.mpi.openmind.repository.bo.Attribute;
import org.mpi.openmind.repository.bo.Entity;
import org.mpi.openmind.repository.bo.Node;
import org.mpi.openmind.repository.services.ServiceRegistry;


/**
 * Downloads a list of Diva manifest files from the repository over HTTP 
 * and checks if each manifest has a corresponding DIGITALIZATION object. 
 * Creates missing DIGITALIZATION objects.
 * 
 * @author casties
 *
 */
public class DivaImportHttp {
	
	static{
		ConsoleAppender console = new ConsoleAppender(); //create appender
		  //configure the appender
		  String PATTERN = "%d [%p|%c|%C{1}] %m%n";
		  console.setLayout(new PatternLayout(PATTERN)); 
		  console.setThreshold(Level.INFO);
		  console.activateOptions();
		  //add appender to any Logger (here is root)
		  Logger.getRootLogger().addAppender(console);
	}

	public static String DIGITALIZATION = "DIGITALIZATION";
	public static String userName = "diva-import";
	
	/** URL for listing of Diva menifest files */
    public static String scanListUrl = "https://ismi-imageserver-cc.mpiwg-berlin.mpg.de/iiif/manifests-internal/";
	
	public static void execute() {
		ServiceRegistry services = new ServiceRegistry();
		// data model should exist by now
		//createDataModel(services.getWrapper());
		importData(services.getWrapper());
	}
	
	/**
	 * Downloads a list of Diva manifest files from the repository and checks if
	 * each manifest has a corresponding DIGITALIZATION object. Creates missing
	 * DIGITALIZATION objects.
	 * 
	 * @param omService
	 */
	private static void importData(WrapperService omService){
		try {
		    DefaultHttpClient httpclient = new DefaultHttpClient();
		    HttpGet httpGet = new HttpGet(scanListUrl);
		    try {
	            System.out.println("Reading scan manifests from "+scanListUrl);
	            // send HTTP request and read response
	            HttpResponse response = httpclient.execute(httpGet);
		        if (response.getStatusLine().getStatusCode() > 200) {
		            System.out.println("ERROR reading HTTP response: "+response.getStatusLine());
		            return;
		        }
		        HttpEntity htent = response.getEntity();
		        JSONTokener jsonReader = new JSONTokener(new InputStreamReader(htent.getContent()));
		        // parse JSON directory index of manifest files
		        JSONArray files = new JSONArray(jsonReader);
		        int numFiles = files.length();
		        // get all DIGITALIZATION entities
		        System.out.println("Loading all DIGITALIZATIONs");
		        List<Entity> digiList = omService.getEntitiesByDef(DIGITALIZATION);
		        // unpack in Map by name/ownValue
		        Map<String, Entity> digiMap = new HashMap<String, Entity>(digiList.size());
		        for (Entity digi : digiList) {
		        	String name = digi.getOwnValue();
		        	digiMap.put(name, digi);
		        }
	            List<Entity> saveList = new ArrayList<Entity>();
	            // go through all filenames in the list
		        for (int i = 0; i < numFiles; ++i) {
		        	JSONObject file = files.getJSONObject(i);
		            String filename = file.getString("name");
                    System.out.println("check: "+filename);
                    String digiName = filename.replace(".json", "");
                    if (digiMap.containsKey(digiName)) {
                        //System.out.println("  exists: "+res);
                    } else {
                        // no existing DIGITALIZATION - create new Entity
                        System.out.println(" create: "+digiName);
                        Entity digi = new Entity(Node.TYPE_ABOX, DIGITALIZATION, false);
                        digi.setOwnValue(digiName);                        
                        digi.addAttribute(new Attribute("name", "text", digiName));
                        digi.addAttribute(new Attribute("num_files", "text", "100"));
                        // add to list
                        saveList.add(digi);
                    }
		        }
		        // ensure http entity is fully consumed
		        EntityUtils.consume(htent);
		        // persist OpenMind entities
		        omService.saveEntityList(saveList, userName);
                System.out.println("Found " + numFiles + " manifests");
	            System.out.println("Created " + saveList.size() + " DIGITALIZATIONs");
	            System.out.println("END");
		        
		    } finally {
		        httpGet.releaseConnection();
		    }
		    
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	
	public static void main(String[] args){
		execute();
		System.exit(0);
	}
}