<file_sep>package com.qunb.geosearch.geoObject;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.jclouds.aws.s3.blobstore.*;
import org.jclouds.blobstore.BlobMap;
import org.jclouds.blobstore.domain.Blob;
import org.jclouds.blobstore.domain.StorageMetadata;
import org.jclouds.blobstore.domain.StorageType;
import org.jclouds.blobstore.options.ListContainerOptions;
import org.json.simple.JSONObject;
import org.json.simple.JSONValue;


import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.PreparedQuery;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.datastore.Query.FilterOperator;
import com.qunb.fuzzymatch.LetterSimilarity;
import com.qunb.fuzzymatch.LetterSimilarity.CouplingLevel;

public class QunbTextSearch {
	private volatile DatastoreService datastore;
	private volatile String input;
	private volatile String type;
	private volatile String country;
	private volatile String lang;
	private volatile List<JSONObject> result;
	
	public QunbTextSearch(String input,String type,String country,String lang) throws Exception{
		if(input.contains(",")){
			
			this.input =input.substring(0, input.indexOf(","));
		}
		else{
			this.input=input;
		}
		if(lang!=null&&!lang.isEmpty()){
			this.lang=lang;
		}
		else{
			this.lang="en";
		}
		this.type = type;
		this.country = country;
		this.datastore=  DatastoreServiceFactory.getDatastoreService();
		this.result = this.constructResult();
	}

	
	public List<JSONObject> constructResult() throws Exception{
		List<JSONObject> mydata = new ArrayList<JSONObject>();
		mydata = this.getData();
		if(mydata.isEmpty()){
			return null;
		}
		return mydata;
	}

	@SuppressWarnings("deprecation")
	public  synchronized List<JSONObject> getData() throws IOException{
		List<JSONObject> output = new ArrayList<JSONObject>();
		List<Entity> entities = new ArrayList<Entity>();
		if (this.input.length() == 2) {
			entities = GeoDataStore.searchCountryCode(this.input);
		}
		if (entities.size() == 0) {
			entities = GeoDataStore.searchName(this.input, this.country, this.type);
		}
		for(Entity geo:entities){
			JSONObject mygeo = new JSONObject();
			mygeo = GeoNameOperation.EntitytoJson(geo);
			output.add(mygeo);
		}
		if(output.size()==0){
			System.out.println("---Result Not Found at Qunb---");
		}
		return output;
	}
	public List<Map<String,Object>> getResult(){
		List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
		if(this.result!=null){
			for(int i = 0;i<this.result.size();i++){
				Map<String,Object> tmpmap = new HashMap<String,Object>();
				tmpmap = GeoNameOperation.JsonToMap(this.result.get(i));
				list.add(tmpmap);
			}
		}
		return list;	
	}
	public static List<Map<String,Object>> classResult(List<Map<String,Object>> mylist){
		for(int i =0;i<mylist.size()-1;i++){
			Map<String,Object> tmpmap_j = null;
			Map<String,Object> tmpmap_j_1 = null;
			for(int j=mylist.size()-1;j>i;j--){
				if(Integer.valueOf(mylist.get(j).get("qunb:population").toString())!=0 &&Integer.valueOf(mylist.get(j-1).get("qunb:population").toString())!=0){
					if(Integer.valueOf(mylist.get(j).get("qunb:population").toString())>Integer.valueOf(mylist.get(j-1).get("qunb:population").toString())){
						tmpmap_j=mylist.get(j);
						tmpmap_j_1=mylist.get(j-1);
						mylist.set(j, tmpmap_j_1);
						mylist.set(j-1,tmpmap_j);
					}
					else if(Integer.valueOf(mylist.get(j).get("qunb:population").toString())==Integer.valueOf(mylist.get(j-1).get("qunb:population").toString())){
						if(getfclordre(mylist.get(j))>getfclordre(mylist.get(j-1))){
							tmpmap_j=mylist.get(j);
							tmpmap_j_1=mylist.get(j-1);
							mylist.set(j, tmpmap_j_1);
							mylist.set(j-1,tmpmap_j);
						}
						else{
							if(getfclordre(mylist.get(j))==0&&getfclordre(mylist.get(j-1))==0){
								if(getfcodeorder_A(mylist.get(j))>getfcodeorder_A(mylist.get(j-1))){
									tmpmap_j=mylist.get(j);
									tmpmap_j_1=mylist.get(j-1);
									mylist.set(j, tmpmap_j_1);
									mylist.set(j-1,tmpmap_j);
								}
							}
							else if(getfclordre(mylist.get(j))==1&&getfclordre(mylist.get(j-1))==1){
								if(getfcodeorder_P(mylist.get(j))>getfcodeorder_P(mylist.get(j-1))){
									tmpmap_j=mylist.get(j);
									tmpmap_j_1=mylist.get(j-1);
									mylist.set(j, tmpmap_j_1);
									mylist.set(j-1,tmpmap_j);
								}
							}
						}
					}
				}
				else{ 
						if(getfclordre(mylist.get(j))>getfclordre(mylist.get(j-1))){
							tmpmap_j=mylist.get(j);
							tmpmap_j_1=mylist.get(j-1);
							mylist.set(j, tmpmap_j_1);
							mylist.set(j-1,tmpmap_j);
						}
						else{
							if(getfclordre(mylist.get(j))==0&&getfclordre(mylist.get(j-1))==0){
								if(getfcodeorder_A(mylist.get(j))>getfcodeorder_A(mylist.get(j-1))){
									tmpmap_j=mylist.get(j);
									tmpmap_j_1=mylist.get(j-1);
									mylist.set(j, tmpmap_j_1);
									mylist.set(j-1,tmpmap_j);
								}
							}
							else if(getfclordre(mylist.get(j))==1&&getfclordre(mylist.get(j-1))==1){
								if(getfcodeorder_P(mylist.get(j))>getfcodeorder_P(mylist.get(j-1))){
									tmpmap_j=mylist.get(j);
									tmpmap_j_1=mylist.get(j-1);
									mylist.set(j, tmpmap_j_1);
									mylist.set(j-1,tmpmap_j);
								}
							}
						}
				}
			}
		}
		return mylist;
	}
	public static int getfclordre(Map<String,Object> map){
		if(map.get("qunb:fclcode").equals("A")){
			return 2;
		}
		else if(map.get("qunb:fclcode").equals("P")){
			return 1;
		}
		else {
			return 0;
		}
	}
	public static int getfcodeorder_P(Map<String,Object> map){
		if(map.get("qunb:fcode").equals("PPLC")){
			return 5;
		}
		else if(map.get("qunb:fcode").equals("PPLA")){
			return 4;
		}
		else if(map.get("qunb:fcode").equals("PPLA2")){
			return 3;
		}
		else if(map.get("qunb:fcode").equals("PPLA3")){
			return 2;
		}
		else if(map.get("qunb:fcode").equals("PPLA4")){
			return 1;
		}
		else {
			return 0;
		}
	}
	public static int getfcodeorder_A(Map<String,Object> map){
		if(map.get("qunb:fcode").equals("PCL")){
			return 8;
		}
		else if(map.get("qunb:fcode").equals("PCLD")){
			return 7;
		}
		else if(map.get("qunb:fcode").equals("PCLF")){
			return 6;
		}
		else if(map.get("qunb:fcode").equals("ADMD")){
			return 5;
		}
		else if(map.get("qunb:fcode").equals("ADM1")){
			return 4;
		}
		else if(map.get("qunb:fcode").equals("ADM2")){
			return 3;
		}
		else if(map.get("qunb:fcode").equals("ADM3")){
			return 2;
		}
		else if(map.get("qunb:fcode").equals("ADM4")){
			return 1;
		}
		else {
			return 0;
		}
	}
	

}
<file_sep>package com.qunb.geosearch.geoObject;

import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import org.json.JSONException;
import org.json.simple.JSONObject;



import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;

public class GeoName {
	private String name;
	private double lat;
	private double lng;
	private String geonameId;
	private String countryCode;
	private String countryName;
	private String fcl;
	private String fcode;
	private String alternateNames;
	private String population;
	private String fclName;
	
	public void setName(String name) {
		this.name = name;
	}
	public String getName() {
		return name;
	}
	public void setLat(String lat) {
		this.lat = Double.valueOf(lat);
	}
	public double getLat() {
		return this.lat;
	}
	public void setLng(String lng) {
		this.lng = Double.valueOf(lng);
	}
	public double getLng() {
		return this.lng;
	}
	public void setGeonameId(String geonameId) {
		this.geonameId = geonameId;
	}
	public String getGeonameId() {
		return this.geonameId;
	}
	public void setCountryName(String countryName) {
		this.countryName = countryName;
	}
	public String getCountryName() {
		return countryName;
	}
	public void setCountryCode(String countryCode) {
		this.countryCode = countryCode;
	}
	public String getCountryCode() {
		return countryCode;
	}
	public void setFcode(String fcode) {
		this.fcode = fcode;
	}
	public String getFcode() {
		return fcode;
	}
	public void setFcl(String fcl) {
		this.fcl = fcl;
	}
	public String getFcl() {
		return fcl;
	}
	public void setPopulation(String ppl) {
		this.population = ppl;
	}
	public String getPopulation() {
		return this.population;
	}
	public String getFclName(){
		return this.fclName;
	}
	public void setFclName(String fclname){
		this.fclName = fclname;
	}
	
	public Entity toEntity(){
		Key geo_key = KeyFactory.createKey("GeonameId", this.getGeonameId().toString());
		Entity geo = new Entity("GeoName",geo_key);
		geo.setProperty("geonameId", this.getGeonameId());
		geo.setProperty("name", this.getName());
		geo.setProperty("countryName", this.getCountryName());
		geo.setProperty("countryCode", this.getCountryCode());
		geo.setProperty("Lat", this.getLat());
		geo.setProperty("Lng", this.getLng());
		geo.setProperty("Fcl", this.getFcl());
		geo.setProperty("Fcode", this.getFcode());
		geo.setProperty("Population", this.getPopulation());
		return geo;
	}
	
	public  org.json.JSONObject toJson() throws JSONException{
		org.json.JSONObject tmp = new org.json.JSONObject();
		tmp.put("qunb:geoId", this.getGeonameId());
		tmp.put("qunb:geoName",this.getName());
		tmp.put("qunb:geoLat",this.getLat());
		tmp.put("qunb:geoLng",this.getLng());
		tmp.put("qunb:geoFcode",this.getFcode());
		return tmp;
	}
	public String getAlternateNames() {
		return alternateNames;
	}
	public void setAlternateNames(String alternateNames) {
		this.alternateNames = alternateNames;
	}
}
<file_sep>package com.qunb.rest.dimension;
import org.restlet.Application;
import org.restlet.Restlet;
import org.restlet.routing.Router;

import com.qunb.rest.unit.QunbUnitResource;

public class QunbDimensionWSApplication extends Application {
	@Override
	public Restlet createRoot() {
	 Router router = new Router(getContext());
	 router.attachDefault(QunbDimensionResource.class);
     return router;
    }

}
<file_sep>package com.qunb.geosearch.LatLngSearch;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.ws.rs.Path;

import org.geonames.WebService;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.restlet.resource.Get;
import org.restlet.resource.ServerResource;


import com.qunb.geosearch.TextSearch.QunbGeoTextSearchResource;
import com.qunb.geosearch.geoObject.GeoName;
import com.qunb.geosearch.geoObject.GeoNameOperation;
import com.qunb.geosearch.geoObject.GeoNamesLatLngSearch;
import com.qunb.geosearch.geoObject.QunbLatLogSearch;

@Path("/geosearch/latlngsearch/search")
public class QunbGeoLatLngSearchResource extends ServerResource {
	@Get
	public JSONArray searchGeoID() throws Exception{
		WebService svc = new WebService();
		svc.setUserName("leipang");
		double lat = Double.parseDouble(getQuery().getValues("lat"));
		double lng = Double.parseDouble(getQuery().getValues("lng"));
		String level = getQuery().getValues("level");
		String limit = getQuery().getValues("limit");
		List<JSONObject> result = new ArrayList<JSONObject>();
		result = getResultList(lat,lng,level,limit);
		JSONArray jsonresult = getSearchResult(result);
		return jsonresult;
	}
	@SuppressWarnings("null")
	public List<JSONObject> getResultList(double lat,double lng,String level,String limit) throws Exception{
		List<JSONObject> list = new ArrayList<JSONObject>();
		QunbLatLogSearch mysearch = new QunbLatLogSearch(lat,lng);
		if(mysearch.getResult().isEmpty()){
			GeoNamesLatLngSearch mysearch2 = new GeoNamesLatLngSearch(lat,lng,level);
			mysearch2.storeData();
			GeoName geo_search = mysearch2.getResult();
			if(geo_search!=null){
				JSONObject tmp = new JSONObject();
				tmp = geo_search.toJson();
				list.add(tmp);
			}
			else{
				JSONObject tmp = new JSONObject();
				tmp.put("error 400","The level is not a fcode!");
				list.add(tmp);
			}
			
		}
		else{
			List<Map<String,Object>> qunb_search=mysearch.getResult();
			List<Map<String,Object>> qunb_search_class=QunbLatLogSearch.classResult(qunb_search);
			GeoNameOperation.toListJson(qunb_search_class, limit);
		}
		return list;
	}
	@SuppressWarnings("unchecked")
	public JSONArray getSearchResult(List<JSONObject> mylist){
		System.out.println(mylist.size());
		JSONArray output = new JSONArray();
		for(int i = 0;i<mylist.size();i++){
			output.put(mylist.get(i));
		}
		return output;
	}
	

}
<file_sep>package com.qunb.geosearch.TextSearch;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.geonames.WebService;
import org.json.*;
import org.restlet.resource.Get;
import org.restlet.resource.ServerResource;

import com.qunb.geosearch.geoObject.GeoNameOperation;
import com.qunb.geosearch.geoObject.GeoNamesTextSearch;
import com.qunb.geosearch.geoObject.QunbTextSearch;

import javax.ws.rs.Path;


@Path("/geosearch/textsearch/search")
public class QunbGeoTextSearchResource extends ServerResource{
	@SuppressWarnings("static-access")
	@Get
	public JSONArray searchGeoID() throws Exception{

		WebService svc = new WebService();
		svc.setUserName("leipang");
		String input = getQuery().getValues("q");
		String type = getQuery().getValues("type");
		String country = getQuery().getValues("country");
		String lang = getQuery().getValues("lang");
		String limit = getQuery().getValues("limit");
		List<JSONObject> result = new ArrayList<JSONObject>();
		result = getResultList(input,type,country,limit,lang);
		JSONArray jsonresult = getSearchResult(result);
		return jsonresult;

	}
	@SuppressWarnings("static-access")
	public List<JSONObject> getResultList(String input,String type,String country,String limit,String lang) throws Exception{
		List<JSONObject> list = new ArrayList<JSONObject>();
		QunbTextSearch mysearch = new QunbTextSearch(input,type,country,lang);
		if(mysearch.getResult().isEmpty()){
			GeoNamesTextSearch mysearch2 = new GeoNamesTextSearch(input,limit,type,country,lang);
			mysearch2.storeData();
			List<Map<String, Object>> mylist_result = mysearch2.getReturnResult();
			List<Map<String, Object>> mylist_result_class = QunbTextSearch.classResult(mylist_result);
			list = GeoNameOperation.toListJson(mylist_result_class,limit);
		}
		else{
			List<Map<String, Object>> mylist_result = mysearch.getResult();
			List<Map<String, Object>> mylist_result_class = QunbTextSearch.classResult(mylist_result);
			list = GeoNameOperation.toListJson(mylist_result_class,limit);
		}
		return list;
	}
	
	@SuppressWarnings("unchecked")
	public JSONArray getSearchResult(List<JSONObject> mylist){
		System.out.println(mylist.size());
		JSONArray output = new JSONArray();
		for(int i = 0;i<mylist.size();i++){
			 output.put(mylist.get(i));
		}
		return output;
	}
	
	

}



