package org.greenstone.gsdl3.service; // Greenstone classes import org.greenstone.gsdl3.util.*; // XML classes import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.NodeList; // General Java classes import java.io.File; import java.io.*; import java.util.StringTokenizer; import java.util.Vector; import java.util.Set; import java.util.Iterator; import java.lang.reflect.Array; import java.util.Arrays; import java.util.ArrayList; import java.util.LinkedList; import javax.swing.tree.DefaultMutableTreeNode; import org.apache.log4j.*; public class MapRetrieve extends ServiceRack { static Logger logger = Logger.getLogger(org.greenstone.gsdl3.service.MapRetrieve.class.getName()); // the services on offer protected static final String DOCUMENT_STRUCTURE_RETRIEVE_SERVICE = "DocumentStructureRetrieve"; protected static final String DOCUMENT_METADATA_RETRIEVE_SERVICE = "DocumentMetadataRetrieve"; protected static final String DOCUMENT_CONTENT_RETRIEVE_SERVICE = "DocumentContentRetrieve"; protected static final int DOCUMENT=1; protected ArrayList index_name_list = null; protected ArrayList index_display_list = null; protected String files_home_dir = null; protected String temp_files_dir = null; //protected String temp_image_file = null; //protected String temp_image_file2 = null; protected final String jpg_ext = ".jpg"; protected String http_image_dir = null; protected String http_temp_image_dir = null; private LinkedList namesInList = new LinkedList(); private DefaultMutableTreeNode tree; /** constructor */ public MapRetrieve() {} /** configure this service */ public boolean configure(Element info, Element extra_info) { if (!super.configure(info, extra_info)){ return false; } logger.info("Configuring MapRetrieve..."); this.config_info = info; // set up short_service_info_ - for now just has name and type Element dmr_service = doc.createElement(GSXML.SERVICE_ELEM); dmr_service.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_RETRIEVE); dmr_service.setAttribute(GSXML.NAME_ATT, DOCUMENT_METADATA_RETRIEVE_SERVICE); short_service_info.appendChild(dmr_service); Element dcr_service = doc.createElement(GSXML.SERVICE_ELEM); dcr_service.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_RETRIEVE); dcr_service.setAttribute(GSXML.NAME_ATT, DOCUMENT_CONTENT_RETRIEVE_SERVICE); short_service_info.appendChild(dcr_service); Element dsr_service = doc.createElement(GSXML.SERVICE_ELEM); dsr_service.setAttribute(GSXML.TYPE_ATT, GSXML.SERVICE_TYPE_RETRIEVE); dsr_service.setAttribute(GSXML.NAME_ATT, DOCUMENT_STRUCTURE_RETRIEVE_SERVICE); short_service_info.appendChild(dsr_service); // set the files_home variable for this collection this.files_home_dir = GSFile.collectionIndexDir(this.site_home, this.cluster_name)+ File.separator+"assoc"+File.separator; this.temp_files_dir = GSFile.collectionBaseDir(this.site_home, this.cluster_name) + File.separator + "temp"+File.separator; //this.files_home_dir + "maps" + File.separator + "temp" + File.separator; //this.temp_image_file = this.temp_files_dir+"temp_"; //this.temp_image_file2 = this.temp_files_dir+"temp2_"; this.http_image_dir = this.site_http_address + "/collect/"+this.cluster_name+"/index/assoc/maps/"; this.http_temp_image_dir = this.site_http_address + "/collect/"+this.cluster_name+"/temp/"; this.index_name_list = new ArrayList(); this.index_display_list = new ArrayList(); Element index_list = (Element)GSXML.getChildByTagName(this.config_info, GSXML.INDEX_ELEM+GSXML.LIST_MODIFIER); Element display_index_list = (Element)GSXML.getChildByTagName(extra_info, "search"); if (index_list != null && display_index_list != null) { NodeList indexes = index_list.getElementsByTagName(GSXML.INDEX_ELEM); for (int i=0; i" + link; } else{ place_data = link; coOrdinates = "``"; } coOrdinates = coOrdinates+(String)coOrdinates_list.get(q); } link = ""; thumb = ""; value = "
"+link+"

"+place_data+"
"+value+"

"+link+thumb+"
"; if(metadata.equals("Title")) if(!(place_data.equals("")) && place_data.indexOf(", , ;")==-1 && node_id.indexOf("```")==-1) GSXML.addMetadata(this.doc, node_meta_list, "Title", value);//metadata, value); else GSXML.addMetadata(this.doc, node_meta_list, metadata, ""); if(place_data.indexOf(", , ;")==-1){ if(i==0) if(!(mapFreq==0)){ outfile = new BufferedWriter(new FileWriter(this.temp_files_dir+"links"+uid)); outfile.write(""); } if(!(mapFreq==0)){ if(i%7 == 0) outfile.write(""); outfile.write(""); } if(i == request_nodes.getLength()-1){ outfile.write("
"+link+thumb+"
"+link+year+"

"); outfile.flush(); outfile.close(); } } } } }catch(IOException ioe){ioe.printStackTrace();} return result; }catch(Exception excep){excep.printStackTrace();} return null; } protected Element processDocumentContentRetrieve(Element request){ // Create a new (empty) result message Element result = doc.createElement(GSXML.RESPONSE_ELEM); result.setAttribute(GSXML.FROM_ATT, DOCUMENT_CONTENT_RETRIEVE_SERVICE); result.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_PROCESS); String uid = request.getAttribute(GSXML.USER_ID_ATT); String temp_image_file = this.temp_files_dir+"temp_"+uid+".jpg"; String temp_image_file2 = this.temp_files_dir+"temp_"+uid+"_2.jpg"; Element query_doc_list = (Element) GSXML.getChildByTagName(request, GSXML.DOC_NODE_ELEM+GSXML.LIST_MODIFIER); if (query_doc_list == null) { logger.error("DocumentContentRetrieve request specified no doc nodes.\n"); return result; } String legend_file = this.temp_files_dir+"legend_"+uid+".jpg"; String blank_file = this.files_home_dir + "blank.jpg"; Element doc_list = doc.createElement(GSXML.DOC_NODE_ELEM+GSXML.LIST_MODIFIER); result.appendChild(doc_list); // Get the documents String[] doc_ids = GSXML.getAttributeValuesFromList(query_doc_list, GSXML.NODE_ID_ATT); for (int i = 0; i < doc_ids.length; i++) { String doc_id = doc_ids[i]; // img_num is the name of the map file eg. 072.jpg String img_num = doc_id.substring(0,doc_id.indexOf('.')); // strings for inserting image in html String img_left = "0; ri++){ returnNames[ri]=(String)namesInList.getFirst(); returnNames[ri] = returnNames[ri].substring(0,returnNames[ri].indexOf('`')); namesInList.removeFirst(); } //sort the names Arrays.sort(returnNames); doc_content = "\n"; for(int nameIndex=0; nameIndex < namesInArraySize; nameIndex++){ String tempName = returnNames[nameIndex]; //convert names with spaces for hyperlinks if(returnNames[nameIndex].indexOf(' ') != -1){ returnNames[nameIndex] = returnNames[nameIndex].replaceAll(" ","+"); returnNames[nameIndex] = "%22"+returnNames[nameIndex]+"%22"; } // add the place name to the html doc_content = doc_content+""+tempName+"
"; } }catch(Exception ioexception){ioexception.printStackTrace();} } else{ try{ //file for converting image BufferedWriter bw = new BufferedWriter(new FileWriter(this.temp_files_dir + "add_x_"+uid));; Process proc; // if a new search if(doc_id.indexOf("```") != -1){ // copy requested map to temp.jpg proc = Runtime.getRuntime().exec("cp "+this.files_home_dir+"maps"+File.separator+img_num+".jpg "+temp_image_file); proc.waitFor(); } //get the image size String get_size[] = {"identify","-size","10000", temp_image_file}; proc = Runtime.getRuntime().exec(get_size); BufferedReader br = new BufferedReader(new InputStreamReader(proc.getInputStream())); img_size = br.readLine(); proc.waitFor(); img_size = img_size.substring(img_size.indexOf("JPEG")+5,img_size.indexOf(" ",img_size.indexOf("JPEG")+5)); if (img_size.indexOf("+")!=-1) { img_size = img_size.substring(0, img_size.indexOf("+")); } width = Integer.parseInt(img_size.substring(0,img_size.indexOf("x"))); height = Integer.parseInt(img_size.substring(img_size.indexOf("x")+1,img_size.length())); // if a new search if(doc_id.indexOf("```") != -1){ co_ordinates = doc_id.substring(doc_id.indexOf("```")+2, doc_id.length()); // get the number of places to mark on the map int x_number = Integer.parseInt(doc_id.substring(doc_id.lastIndexOf('`',doc_id.indexOf("```")-1)+1,doc_id.indexOf("```"))); //write the convert command bw.write("convert -font helvetica -fill red -pointsize 48 "); for(int xi=0; xi= tlx && xco < brx && yco >= tly && yco < bry){ // rotate the point around the centre according to map orientation xco=xco-xcent; yco=yco-ycent; double oldx=xco; xco=xco*Math.cos(orient)-yco*Math.sin(orient); yco=oldx*Math.sin(orient)+yco*Math.cos(orient); xco=xco+xcent; yco=yco+ycent; // get the pixels for where to place the mark xco = (xco-tlx)/(brx-tlx); yco = (yco-tly)/(bry-tly); width = (int)(xco*width)-5; height = (int)(yco*height)-5; //if this is the first point to be added if(!add_place_type){ // write the initial convert command bw.write("convert -font helvetica -fill red -pointsize 36 "); // toggle check value add_place_type = true; } // write the options for the convert command bw.write("-draw 'text "+width+","+height+" \""+leg_num+"\"' "); bw.flush(); // reset width and height variables for the image width = Integer.parseInt(img_size.substring(0,img_size.indexOf("x"))); height = Integer.parseInt(img_size.substring(img_size.indexOf("x")+1,img_size.length())); } } //if there are places to mark on the map if(add_place_type){ // finish the convert command and write command for scaling image bw.write(temp_image_file+" "+temp_image_file+";convert -scale "+((int)(ratio*width))+"x"+((int)(ratio*height))+" "+temp_image_file+" "+temp_image_file2); // open file for converting the legend command BufferedWriter buf = new BufferedWriter(new FileWriter(this.temp_files_dir+"add_l_"+uid)); if(leg_num == 1) buf.write("cp "+blank_file +" "+legend_file+";"); // write the command for adding to the legend buf.write("convert -font helvetica -fill red -pointsize 12 -draw 'text 15,"+(leg_num*15+20)+" \""+leg_num+" "+getType(restricter)+"\"' "+legend_file+" "+legend_file); buf.flush(); buf.close(); // execute the command for the legend image proc = Runtime.getRuntime().exec("sh "+this.temp_files_dir+"add_l_"+uid); proc.waitFor(); } inType.close(); } bw.flush(); bw.close(); // execute the convert commands etc. proc = Runtime.getRuntime().exec("sh "+this.temp_files_dir+"add_x_"+uid); proc.waitFor(); }catch(Exception ioe){ioe.printStackTrace();} //write the html for the document String doc_content_head = "?a=d&c="+this.cluster_name+"&dt=map&d="; doc_content = "\">"; doc_content = doc_content+"\" onclick=\"imgClickHandler(event, this, '"+img_num+"')\">"; // replace _httpimg_ with the correct address doc_content = ""+doc_content+"
"; String javascript = getJavascript(ratio, doc_id, leg_num); doc_content = "

You are currently viewing the map at "+(int)(ratio*100)+"%
Select a radio button for a new zoom level.
"+javascript+doc_content; doc_content = doc_content.replaceAll("zoomhead", doc_content_head); doc_content = doc_content.replaceAll("placeChooserImage", doc_content_head+"imageChooser`"+doc_id+"`"+ratio); // read the file for the navigation thumbnails try{ BufferedReader infile = new BufferedReader(new FileReader(this.temp_files_dir+"links"+uid)); doc_content = doc_content + infile.readLine(); infile.close(); }catch(Exception ioexc){ioexc.printStackTrace();} } // put the html in a text node Element text_doc = doc.createElement(GSXML.DOC_NODE_ELEM); text_doc.setAttribute(GSXML.NODE_ID_ATT, doc_id); GSXML.addDocText(doc, text_doc, doc_content); doc_list.appendChild(text_doc); } return result; } // resolves location codes for places from LINZ database private String getLocation(String location){ String read; try{ BufferedReader in = new BufferedReader(new FileReader(this.files_home_dir+"files"+File.separator+"landdist.txt")); in.readLine(); while(in.ready()){ read = in.readLine(); if(read.substring(0,2).equals(location)){ in.close(); return read.substring(3, read.length()); } } }catch(Exception e){} return ""; } // resolves type codes for the LINZ database private String getType(String type){ String read; try{ BufferedReader in = new BufferedReader(new FileReader(this.files_home_dir+"files"+File.separator+"pointdes.txt")); in.readLine(); while(in.ready()){ read = in.readLine(); if(read.substring(0,read.indexOf('`')).toLowerCase().equals(type.toLowerCase())){ in.close(); return read.substring(read.indexOf('`')+1, read.indexOf(':')); } } }catch(Exception e){} return ""; } //recursive pre-order traversal of the 2-dimensional tree // x & y are co-ordinates of mouse click, xoff & yoff are the distance in degrees away from x & y to search, // d is the current node of the tree, xcomp controls whether searching is by x co-ordinate or y co-ordinate. private void findName(double x, double y, double xoff, double yoff, DefaultMutableTreeNode d, boolean xcomp){ //if a leaf node return. All leaf nodes are empty if(d.isLeaf()) return; //get coordinates of the current node double xco = Double.parseDouble(((String)d.getUserObject()).substring(((String)d.getUserObject()).length()-6,((String)d.getUserObject()).length())); double yco = Double.parseDouble(((String)d.getUserObject()).substring(((String)d.getUserObject()).length()-12,((String)d.getUserObject()).length()-7)); //if in range if((x-xoff)xco && (y-yoff)yco){ //add to list namesInList.addFirst((String)d.getUserObject()); } //if comparing the x axis if(xcomp){ //if need to search left subtree if((x-xoff)xco) findName(x,y,xoff,yoff,(DefaultMutableTreeNode)d.getChildAt(1),!xcomp); } else{ //if need to search left subtree if((y-yoff)yco) findName(x,y,xoff,yoff,(DefaultMutableTreeNode)d.getChildAt(1),!xcomp); } } // a whole lot of stuff for the html that is hidden at the bottom here because it clutters more than I already have further up! private String getJavascript(double ratio, String doc_id, int leg_num) { String javascript = "\n"; String radioButtons = "\n
"; if(ratio != 0.4) radioButtons = radioButtons + "\n\t40%"; if(ratio != 0.5) radioButtons = radioButtons + "\n\t50%"; if(ratio != 0.6) radioButtons = radioButtons + "\n\t60%"; if(ratio != 0.7) radioButtons = radioButtons + "\n\t70%"; if(ratio != 0.8) radioButtons = radioButtons + "\n\t80%"; if(ratio != 0.9) radioButtons = radioButtons + "\n\t90%"; if(ratio != 1.0) radioButtons = radioButtons + "\n\t100%"; radioButtons = radioButtons + "\n
"; leg_num = leg_num+1; String doc_link = "?a=d&c="+this.cluster_name+"&dt=map&d="+doc_id+"`"+ratio+"`"+leg_num+"`"; StringBuffer dropDownBox = new StringBuffer(); dropDownBox.append("Add Locations Of Type:
\n"); return javascript + radioButtons + dropDownBox; } }