view src/econnect/wp3_3/server/FlickrServiceImpl.java @ 45:fc9061488e75 CellTable

background and border color of CellTable for selection and hover
author Sebastian Kruse <skruse@mpiwg-berlin.mpg.de>
date Fri, 07 Dec 2012 18:57:02 +0100
parents cf06b77a8bbd
children
line wrap: on
line source

package econnect.wp3_3.server;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.URLConnection;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Locale;

import javax.xml.parsers.*;
import org.xml.sax.InputSource;
import org.w3c.dom.*;
import java.io.*;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;

import econnect.wp3_3.client.services.FlickrService;
import econnect.wp3_3.shared.DataElement;

public class FlickrServiceImpl extends RemoteServiceServlet implements FlickrService {

	private String key="3f0e901a712e4a1e4143a3be6fa8131e";
	private String server="http://api.flickr.com/services/rest/";
	private String searchMethod = "flickr.photos.search";
	private String placeIdMethod = "flickr.places.getInfo";

	private Document getXMLFromConnection(String connection){
		try {
			URLConnection uc = new URL(connection).openConnection();
			ByteArrayOutputStream result = new ByteArrayOutputStream();
			InputStream input = uc.getInputStream();
			byte[] buffer = new byte[10000];
			int amount = 0;   
			while(amount != -1){
				result.write(buffer, 0, amount);
				amount = input.read(buffer);
			}
			
			String xmlString = result.toString();
			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
	        DocumentBuilder db = dbf.newDocumentBuilder();
	        InputSource is = new InputSource();
	        is.setCharacterStream(new StringReader(xmlString));

	        return db.parse(is);
		}
		catch(Exception e){	
		}
		return null;
	}
	
	@Override
	public DataElement[] getResultsAsKmlString(String term) {

		String searchConnection = server+"?method="+searchMethod;
		searchConnection += "&api_key="+key;
		searchConnection += "&tags=";
		String[] tags = term.split(" ");
		for( int i=0; i<tags.length; i++ ){
			searchConnection += tags[i];
			if( i+1 < tags.length ){
				searchConnection += ",";
			}
		}
		searchConnection += "&has_geo=true";
		searchConnection += "&tag_mode=all";
		searchConnection += "&extras=geo,url_t,date_taken";

		String placeConnection = server+"?method="+placeIdMethod+"&api_key="+key;

		DataElement[] elements = new DataElement[4000];

		for( int j=0; j<16; j++ ){
			System.out.println(j);
			try {
				String c = searchConnection + "&page="+(j+1);
				Document doc = getXMLFromConnection(c);
		        NodeList nodes = doc.getElementsByTagName("photo");
	            		        
		        for (int i = 0; i < nodes.getLength(); i++) {
		        	try {
			            Element element = (Element) nodes.item(i);
			            String name = element.getAttribute("title");
			            double latitude = (new Double(element.getAttribute("latitude"))).doubleValue();
			            double longitude = (new Double(element.getAttribute("longitude"))).doubleValue();
			            int granularity = 0;
			            
			            String place = "";
			            String description = "<a target='_blank' href='http://flickr.com/photos/"+element.getAttribute("owner")+"/"+element.getAttribute("id")+"'><img src='"+element.getAttribute("url_t")+"'/></a>";
			            
			            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			            Date d = sdf.parse(element.getAttribute("datetaken"));
			    	    long millies = d.getTime();
						String date = (new Long(millies)).toString();
/*			          
			            String time = element.getAttribute("datetaken");
			    	    long d = Long.parseLong(time)*1000;
						String date = (new Long(d)).toString();
*/
						DataElement e = new DataElement();
			            e.setValues(name, place, latitude, longitude, granularity, date, description);
			            elements[i+250*j] = e;			            
		        	}
		        	catch( Exception e ){	        		
		        	}            
		         }
			}
			catch(Exception e){
			}
			
		}
        return elements;
		
	}	

}