changeset 8:9ce7979fd037

Implementation Data Provider
author Jorge Urzua <jurzua@mpiwg-berlin.mpg.de>
date Wed, 24 Apr 2013 10:34:23 +0200
parents bc57f2660b0f
children 2bc783262919
files src/main/java/de/mpiwg/indexmeta/AnnotateIndexMeta.java src/main/java/de/mpiwg/indexmeta/IndexMetaParser.java src/main/java/de/mpiwg/indexmeta/bo/Contextualization.java src/main/java/de/mpiwg/indexmeta/services/DataProvider.java src/main/java/de/mpiwg/indexmeta/web/beans/ApplicationBean.java src/main/java/de/mpiwg/indexmeta/web/servlet/AbstractServlet.java src/main/java/de/mpiwg/indexmeta/web/servlet/methods/AbstractServletMethod.java src/main/webapp/WEB-INF/web.xml
diffstat 8 files changed, 188 insertions(+), 22 deletions(-) [+]
line wrap: on
line diff
--- a/src/main/java/de/mpiwg/indexmeta/AnnotateIndexMeta.java	Fri Apr 12 17:48:42 2013 +0200
+++ b/src/main/java/de/mpiwg/indexmeta/AnnotateIndexMeta.java	Wed Apr 24 10:34:23 2013 +0200
@@ -23,16 +23,22 @@
 import org.w3c.dom.NodeList;
 import org.xml.sax.SAXException;
 
+import de.mpiwg.indexmeta.bo.Contextualization;
+import de.mpiwg.indexmeta.services.DataProvider;
+
 public class AnnotateIndexMeta {
 
     public static void main(String argv[])  {
         System.out.println("in main");
 
         // Methodenaufruf
-        String filepath = "/Users/kthoden/eclipse/workspace/IndexMetaContextualization/data/index.meta/index.meta_FQPFR8XP";
+		String filepath = "/Users/kthoden/eclipse/workspace/IndexMetaContextualization/data/index.meta/index.meta_FQPFR8XP";
         // this is a list of all the elements we want to contextualize
-        List<String> contextualizableList = Arrays.asList(new String[]{"author","editor","publisher","city","holding-library","keywords"});
-        try {xmlParse(filepath,contextualizableList);
+        //List<String> contextualizableList = Arrays.asList(new String[]{"author","editor","publisher","city","holding-library","keywords"});
+        try {
+        	
+        	DataProvider dp = new DataProvider();
+        	xmlParse(filepath, dp);
         }
         catch (Exception e) {
             e.printStackTrace();
@@ -47,10 +53,20 @@
      * @throws Exception which means that in the source index.meta file there are already markers for contextualization.
      * 
      */
-    public static void xmlParse(String filepath, List<String> contextualizableList) throws Exception {
+    public static File xmlParse(String filepath, DataProvider dp) throws Exception {
+    	/*
+    	Contextualization ctx = new Contextualization();
+    	ctx.setElementId("context-id value");
+    	ctx.setIndexMetaId("ABABABA");
+    	ctx.setContent("KLaus");
+    	ctx.setType(Contextualization.AUTHOR);
+    	ctx.setRemoteId("");
+    	dp.saveCtx(ctx);
+    	*/
+    	File file = null;
         try {
             // this is how the outputfile will be called
-            String outfilepath = filepath + "-annot";
+            String outfilepath = filepath + ".annot";
             // open the file and parse it
             DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
             DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
@@ -58,7 +74,7 @@
 
             // iterate through the document
             Integer count = 0; 
-            for(String contextElement : contextualizableList){
+            for(String contextElement : Contextualization.contextualizableList){
                 NodeList nodeList = doc.getElementsByTagName(contextElement);
                 for(int i=0; i < nodeList.getLength(); i++){
                     Node iter2 = nodeList.item(i);
@@ -87,7 +103,8 @@
             TransformerFactory transformerFactory = TransformerFactory.newInstance();
             Transformer transformer = transformerFactory.newTransformer();
             DOMSource source = new DOMSource(doc);
-            StreamResult result = new StreamResult(new File(outfilepath));
+            file = new File(outfilepath);
+            StreamResult result = new StreamResult(file);
             transformer.transform(source, result);
             /*
              * should these really go inside this method?
@@ -101,6 +118,7 @@
         } catch (SAXException sae) {
             sae.printStackTrace();
         }
+        return file;
     }
 
     /**
--- a/src/main/java/de/mpiwg/indexmeta/IndexMetaParser.java	Fri Apr 12 17:48:42 2013 +0200
+++ b/src/main/java/de/mpiwg/indexmeta/IndexMetaParser.java	Wed Apr 24 10:34:23 2013 +0200
@@ -1,6 +1,7 @@
 package de.mpiwg.indexmeta;
 
 import java.io.File;
+import java.io.FilenameFilter;
 import java.util.ArrayList;
 import java.util.List;
 
@@ -11,6 +12,7 @@
 import javax.xml.xpath.XPathExpression;
 import javax.xml.xpath.XPathFactory;
 
+import org.apache.commons.lang.StringUtils;
 import org.w3c.dom.Document;
 import org.w3c.dom.Element;
 import org.w3c.dom.Node;
@@ -20,20 +22,116 @@
 
 public class IndexMetaParser {
 
+	public static String indexMetaFile = "index.meta";
+	public static String annotatedIndexMetaFile = "index.meta.annot";
 	
+	/*
 	public static List<Contextualization> getCtxItems(String filePath){
 		List<Contextualization> rs = new ArrayList<Contextualization>();
 		
 		try {
 			File file = new File("/Users/jurzua/Projects/workspace/contextualization/data/index.meta/01index.meta.anno.xml");
 			
+			if(file.isFile()){
+				rs = getCtxItemsFromFile(file, rs);
+			}else if(file.isDirectory()){
+				rs = getCtxItemsFromDirectory(file, rs);
+			}
+	        
+		} catch (Exception e) {
+			e.printStackTrace();
+		}
+		
+		return rs;
+	}
+	*/
+	
+	public static List<Contextualization> getCtxFromDirectory(String directory){
+		List<Contextualization> rs = new ArrayList<Contextualization>();
+		
+		try {
+			File file = new File(directory);
+			rs = getCtxItemsFromDirectory(file, rs);				        
+		} catch (Exception e) {
+			e.printStackTrace();
+		}
+		
+		return rs;
+	}
+	
+	public static List<Contextualization> getCtxFromResource(String indexMetaResource){
+		List<Contextualization> rs = new ArrayList<Contextualization>();
+		
+		try {
+			File file = new File(indexMetaResource);
+			rs = getCtxItemsFromIndexMetaResource(file, rs);				        
+		} catch (Exception e) {
+			e.printStackTrace();
+		}
+		
+		return rs;
+	}
+	
+	
+	
+	private static List<Contextualization> getCtxItemsFromDirectory(File directory, List<Contextualization> rs) throws Exception{
+		String canonicalPath = directory.getCanonicalPath();
+		System.out.println(canonicalPath);
+		
+		File[] files = directory.listFiles();
+		
+		for(File indexMetaResource : files){
+			
+			if(indexMetaResource.isDirectory()){
+				rs = getCtxItemsFromIndexMetaResource(indexMetaResource, rs);
+			}
+		}
+		return rs;
+	}
+	
+	private static List<Contextualization> getCtxItemsFromIndexMetaResource(File indexMetaResource, List<Contextualization> rs){
+		
+		System.out.print("Working on: " + indexMetaResource.getName());
+		
+		File[] list0 = indexMetaResource.listFiles(indexMetaFilter);
+		File annotatedFile = null;
+		
+		if(list0.length != 0){
+			System.out.print("\tFound: " + indexMetaFile);
+			File[] list1 = indexMetaResource.listFiles(annotatedIndexMetaFilter);
+			if(list1.length == 0){
+				//create annotated file
+				System.out.print("\tAnnotated no found");
+				try{
+					annotatedFile = AnnotateIndexMeta.xmlParse(list0[0].getAbsolutePath(), null);
+				}catch (Exception e) {
+					e.printStackTrace();
+				}
+				
+			}else{
+				annotatedFile = list1[0];
+			}
+		}
+		rs = getCtxItemsFromFile(annotatedFile, rs, indexMetaResource.getName());
+		
+		System.out.println();
+		
+		return rs;
+	}
+	
+	private static List<Contextualization> getCtxItemsFromFile(File file, List<Contextualization> rs, String indexMetaId){
+		
+		try {
 			DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
 			DocumentBuilder builder = factory.newDocumentBuilder();
 			Document doc = builder.parse(file);
 			
+			
+			for(String ctxElement : Contextualization.contextualizableList){}
+			
 			List<Node> nodes = getNodeList(doc, Contextualization.AUTHOR);
 			for(Node node : nodes){
-				Contextualization ctx = getCtx(node, "XX");
+				Contextualization ctx = getCtx(node, indexMetaId);
 				if(ctx != null){
 					rs.add(ctx);
 				}				
@@ -41,22 +139,19 @@
 
 			nodes = getNodeList(doc, Contextualization.CITY);
 			for(Node node : nodes){
-				Contextualization ctx = getCtx(node, "XX");
+				Contextualization ctx = getCtx(node, indexMetaId);
 				if(ctx != null){
 					rs.add(ctx);
 				}
 			}
-			
-			for(Contextualization ctx : rs){
-				System.out.println(ctx.toString());
-			}			
-	        
 		} catch (Exception e) {
 			e.printStackTrace();
 		}
 		
+		
 		return rs;
-	} 
+	}
+	
 	
 	public static List<Node> getNodeList(Document doc, String tagName){
 		List<Node> rs = new ArrayList<Node>();
@@ -106,7 +201,15 @@
 	
 	public static void main(String[] args){
 
-		getCtxItems("/Users/jurzua/Projects/workspace/contextualization/data/index.meta/01index.meta");
+		//--/Volumes/online_permanent/library
+		//getCtxItems("/Users/jurzua/Projects/workspace/contextualization/data/index.meta/01index.meta");
+		List<Contextualization> rs = getCtxFromDirectory("/Users/jurzua/Projects/max-planck/index_meta/library");
+		//List<Contextualization> rs = getCtxFromResource("/Users/jurzua/Projects/max-planck/index_meta/library/BB1RH90M");
+		
+		for(Contextualization ctx : rs){
+			System.out.println(ctx.toString());
+		}
+		
 	}
 	
     public static void printXpathResult(Object result){
@@ -124,4 +227,16 @@
         	}
         }
     }
+    
+    private static FilenameFilter indexMetaFilter = new FilenameFilter() {
+		public boolean accept(File directory, String fileName) {
+            return StringUtils.equals(fileName, indexMetaFile);
+        }
+	};
+	
+	private static FilenameFilter annotatedIndexMetaFilter = new FilenameFilter() {
+		public boolean accept(File directory, String fileName) {
+            return StringUtils.equals(fileName, annotatedIndexMetaFile);
+        }
+	};
 }
--- a/src/main/java/de/mpiwg/indexmeta/bo/Contextualization.java	Fri Apr 12 17:48:42 2013 +0200
+++ b/src/main/java/de/mpiwg/indexmeta/bo/Contextualization.java	Wed Apr 24 10:34:23 2013 +0200
@@ -1,5 +1,8 @@
 	package de.mpiwg.indexmeta.bo;
 
+import java.util.Arrays;
+import java.util.List;
+
 import javax.persistence.Entity;
 import javax.persistence.GeneratedValue;
 import javax.persistence.Id;
@@ -25,8 +28,20 @@
 	public static String CITY = "city";
 	public static String HOLDING_LIBRARY = "holding-library";
 	public static String AUTHOR = "author";
+	public static String KEYWORDS = "keywords";
+	public static String EDITOR = "editor";
 	
-	//"publisher","city","holding-library", "author"
+	public static List<String> contextualizableList = Arrays.asList(
+			new String[]{
+					AUTHOR,
+					EDITOR,
+					PUBLISHER,
+					CITY,
+					HOLDING_LIBRARY,
+					KEYWORDS});
+	
+	
+	//"publisher","city","holding-library", "author", "keywords", "editor"
 	private String type;
 	
 	public String getIndexMetaId() {
--- a/src/main/java/de/mpiwg/indexmeta/services/DataProvider.java	Fri Apr 12 17:48:42 2013 +0200
+++ b/src/main/java/de/mpiwg/indexmeta/services/DataProvider.java	Wed Apr 24 10:34:23 2013 +0200
@@ -32,7 +32,7 @@
 		return ps.getCtxMap().getValuesByThirdKey(remoteId);
 	}
 	
-	public List<Contextualization> getCtx(String indexMetaId, String elementId){
+	public Contextualization getCtx(String indexMetaId, String elementId) throws Exception{
 		List<Contextualization> rs = new ArrayList<Contextualization>();
 		
 		for(Contextualization ctx : ps.getCtxMap().getValuesByFirstKey(indexMetaId)){
@@ -40,7 +40,12 @@
 				rs.add(ctx);
 			}
 		}
-		return rs;
+		if(rs.isEmpty())
+			return null;
+		else if(rs.size() == 1)
+			return rs.get(0);
+		else
+			throw new Exception("For indexMetaId=" + indexMetaId + " and elementId=" + elementId + " there are more than one element in DB.");
 	}
 	
 	public Contextualization getCtx(Long id){
--- a/src/main/java/de/mpiwg/indexmeta/web/beans/ApplicationBean.java	Fri Apr 12 17:48:42 2013 +0200
+++ b/src/main/java/de/mpiwg/indexmeta/web/beans/ApplicationBean.java	Wed Apr 24 10:34:23 2013 +0200
@@ -44,4 +44,7 @@
 		}
 	}
 	
+	public DataProvider getDp(){
+		return this.dp;
+	}
 }
--- a/src/main/java/de/mpiwg/indexmeta/web/servlet/AbstractServlet.java	Fri Apr 12 17:48:42 2013 +0200
+++ b/src/main/java/de/mpiwg/indexmeta/web/servlet/AbstractServlet.java	Wed Apr 24 10:34:23 2013 +0200
@@ -10,6 +10,7 @@
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 
+import de.mpiwg.indexmeta.services.DataProvider;
 import de.mpiwg.indexmeta.web.beans.AbstractBean;
 import de.mpiwg.indexmeta.web.beans.ApplicationBean;
 
@@ -19,11 +20,12 @@
 	
 	public static String METHOD = "mt";
 	public static String getCtxFromDB = "getCtxFromDB";
+	public static String getCtxFromIndexMeta = "getCtxFromIndexMeta";
+	public static String getSaveCtx = "getSaveCtx";
 	
 	public ApplicationBean getAppBean(HttpServletRequest request, HttpServletResponse response) {
 		ApplicationBean appBean = (ApplicationBean)getApplicationBean(request, response, AbstractBean.BEAN_APP);
-		
-		
+				
 		if(appBean == null){
 			appBean = new ApplicationBean();
 			getFacesContext(request, response).getCurrentInstance().getExternalContext().getApplicationMap().put(AbstractBean.BEAN_APP, appBean);			
@@ -31,6 +33,10 @@
 		return appBean; 
 	}
 	
+	public DataProvider getDp(HttpServletRequest request, HttpServletResponse response) {
+		return getAppBean(request, response).getDp();
+	}
+	
 	public Object getApplicationBean(HttpServletRequest request, HttpServletResponse response, String bean) {
 		return getFacesContext(request, response).getExternalContext().getApplicationMap().get(bean);
 	}
--- a/src/main/java/de/mpiwg/indexmeta/web/servlet/methods/AbstractServletMethod.java	Fri Apr 12 17:48:42 2013 +0200
+++ b/src/main/java/de/mpiwg/indexmeta/web/servlet/methods/AbstractServletMethod.java	Wed Apr 24 10:34:23 2013 +0200
@@ -15,6 +15,10 @@
 	public static String p_type = "type";
 	public static String p_remoteId = "remoteId";
 	public static String p_elementId = "elementId";
+	public static String p_comment = "comment";
+	public static String p_content = "content";
+	public static String p_state = "state";
+	
 	
 	protected static String RUNTIME = "runtime";
 	
--- a/src/main/webapp/WEB-INF/web.xml	Fri Apr 12 17:48:42 2013 +0200
+++ b/src/main/webapp/WEB-INF/web.xml	Wed Apr 24 10:34:23 2013 +0200
@@ -20,7 +20,7 @@
 	
     <servlet>
         <servlet-name>ws</servlet-name>
-        <servlet-class>de.mpiwg.indexmeta.web.servlet.JSONInterface</servlet-class>
+        <servlet-class>de.mpiwg.indexmeta.web.servlet.JSONServlet</servlet-class>
     </servlet>
     <servlet-mapping>
         <servlet-name>ws</servlet-name>