/alaspatial/src/main/java/org/ala/spatial/web/SpeciesAutoComplete.java
Java | 249 lines | 160 code | 53 blank | 36 comment | 44 complexity | de9e47d3fa9cc6ff4694090ac6b6bb86 MD5 | raw file
1package org.ala.spatial.web; 2 3import java.net.URLEncoder; 4import java.util.Iterator; 5import org.ala.spatial.analysis.index.OccurrencesCollection; 6import org.apache.commons.httpclient.HttpClient; 7import org.apache.commons.httpclient.methods.GetMethod; 8import org.codehaus.jackson.JsonFactory; 9import org.codehaus.jackson.JsonParser; 10import org.codehaus.jackson.JsonToken; 11import org.zkoss.zk.ui.event.InputEvent; 12import org.zkoss.zul.Combobox; 13import org.zkoss.zul.Comboitem; 14 15/** 16 * 17 * @author ajay 18 */ 19public class SpeciesAutoComplete extends Combobox { 20 21 public SpeciesAutoComplete() { 22 refresh(""); //init the child comboitems 23 } 24 25 public SpeciesAutoComplete(String value) { 26 super(value); //it invokes setValue(), which inits the child comboitems 27 } 28 29 @Override 30 public void setValue(String value) { 31 super.setValue(value); 32 refresh(value); //refresh the child comboitems 33 } 34 35 /** Listens what an user is entering. 36 */ 37 public void onChanging(InputEvent evt) { 38 if (!evt.isChangingBySelectBack()) { 39 refresh(evt.getValue()); 40 } 41 } 42 43 private void refreshJSON(String val) { 44 String snUrl = "http://data.ala.org.au/search/scientificNames/_name_*/json"; 45 String cnUrl = "http://data.ala.org.au/search/commonNames/_name_*/json"; 46 47 String urlOptionSort = "sort"; 48 String urlOptionDir = "dir"; 49 String urlOptionStart = "startIndex"; 50 String urlOptionPageSize = "results"; 51 52 try { 53 54 if (val.length() < 4) { 55 return; 56 } 57 58 String nsurl = snUrl.replace("_name_", URLEncoder.encode(val, "UTF-8")); 59 60 HttpClient client = new HttpClient(); 61 GetMethod get = new GetMethod(nsurl); 62 63 int result = client.executeMethod(get); 64 String slist = get.getResponseBodyAsString(); 65 66 System.out.println("Got JSON.slist for " + nsurl + ": \n" + slist); 67 68 69 JsonFactory f = new JsonFactory(); 70 JsonParser jp = f.createJsonParser(slist); 71 72 System.out.println("jp.init: " + jp.getCurrentName()); 73 74 jp.nextToken(); // will return JsonToken.START_OBJECT (verify?) 75 76 while (jp.nextToken() != JsonToken.END_OBJECT) { 77 String fieldname = jp.getCurrentName(); 78 JsonToken jt = jp.nextToken(); // move to value, or START_OBJECT/START_ARRAY 79 if ("result".equals(fieldname)) { // contains an object 80 /* 81 while (jp.nextToken() != JsonToken.END_OBJECT) { 82 String namefield = jp.getCurrentName(); 83 jp.nextToken(); // move to value 84 if ("scientificName".equals(namefield)) { 85 name.setFirst(jp.getText()); 86 } else if ("scientificNameUrl".equals(namefield)) { 87 name.setLast(jp.getText()); 88 } else if ("occurrenceCoordinateCount".equals(namefield)) { 89 name.setLast(jp.getText()); 90 } 91 92 //else { 93 // throw new IllegalStateException("Unrecognized field '" + fieldname + "'!"); 94 //} 95 96 97 } 98 * 99 */ 100 //user.setName(name); 101 System.out.println("Got result: " + jt.name()); 102 } else if ("recordsReturned".equals(fieldname)) { 103 //user.setGender(Gender.valueOf(jp.getText())); 104 System.out.println("pagesize: " + jp.getText()); 105 } else if ("totalRecords".equals(fieldname)) { 106 //user.setVerified(jp.getCurrentToken() == JsonToken.VALUE_TRUE); 107 System.out.println("total: " + jp.getText()); 108 } else if ("startIndex".equals(fieldname)) { 109 //user.setUserImage(jp.getBinaryValue()); 110 System.out.println("startIndex: " + jp.getText()); 111 } 112 //else { 113 // throw new IllegalStateException("Unrecognized field '" + fieldname + "'!"); 114 //} 115 } 116 jp.close(); // ensure resources get cleaned up timely and properly 117 118 119 120 121 122 123 124 } catch (Exception e) { 125 System.out.println("Oopss! something went wrong in SpeciesAutoComplete.refreshJSON"); 126 e.printStackTrace(System.out); 127 128 //new Comboitem("No species found. error.").setParent(this); 129 } 130 } 131 132 public void refresh(String val) { 133 try { 134 135 if (val.length() == 0) { 136 setDroppable("false"); 137 } else { 138 setDroppable("true"); 139 } 140 141 String[] aslist = OccurrencesCollection.findSpecies(val, 40); 142 if (aslist == null) { 143 aslist = new String[1]; 144 aslist[0] = ""; 145 } 146 System.out.print("#" + aslist.length); 147 148 Iterator it = getItems().iterator(); 149 150 if (aslist.length > 0) { 151 152 for (int i = 0; i < aslist.length; i++) { 153 String[] spVal = aslist[i].split("/"); 154 155 Comboitem myci = null; 156 if (it != null && it.hasNext()) { 157 myci = ((Comboitem) it.next()); 158 myci.setLabel(spVal[0].trim()); 159 } else { 160 it = null; 161 myci = new Comboitem(spVal[0].trim()); 162 myci.setParent(this); 163 } 164 myci.setDescription(spVal[1].trim() + " - " + spVal[2].trim()); 165 } 166 } else { 167 if (it != null && it.hasNext()) { 168 ((Comboitem) it.next()).setLabel("No species found."); 169 } else { 170 it = null; 171 new Comboitem("No species found.").setParent(this); 172 } 173 174 } 175 176 while (it != null && it.hasNext()) { 177 it.next(); 178 it.remove(); 179 } 180 181 182 } catch (Exception e) { 183 System.out.println("Species.AutoComplete error: "); 184 e.printStackTrace(System.out); 185 } 186 } 187 188 /** Refresh comboitem based on the specified value. 189 */ 190 private void refreshBIE(String val) { 191 192 String snUrl = "http://data.ala.org.au/taxonomy/taxonName/ajax/view/ajaxTaxonName?query="; 193 String cnUrl = "http://data.ala.org.au/taxonomy/taxonName/ajax/returnType/commonName/view/ajaxTaxonName?query="; 194 195 try { 196 197 String nsurl = snUrl + URLEncoder.encode(val, "UTF-8"); 198 199 HttpClient client = new HttpClient(); 200 GetMethod get = new GetMethod(nsurl); 201 202 int result = client.executeMethod(get); 203 String slist = get.getResponseBodyAsString(); 204 205 System.out.println("Response status code: " + result); 206 System.out.println("Response: \n" + slist); 207 208 String[] aslist = slist.split("\n"); 209 System.out.println("Got " + aslist.length + " records."); 210 211 Iterator it = getItems().iterator(); 212 if (aslist.length > 0) { 213 214 for (int i = 0; i < aslist.length; i++) { 215 Comboitem myci = null; 216 if (it != null && it.hasNext()) { 217 myci = ((Comboitem) it.next()); 218 myci.setLabel(aslist[i]); 219 } else { 220 it = null; 221 myci = new Comboitem(aslist[i]); 222 myci.setParent(this); 223 } 224 myci.setDescription("description goes here... "); 225 } 226 } else { 227 if (it != null && it.hasNext()) { 228 ((Comboitem) it.next()).setLabel("No species found."); 229 } else { 230 it = null; 231 new Comboitem("No species found.").setParent(this); 232 } 233 234 } 235 236 while (it != null && it.hasNext()) { 237 it.next(); 238 it.remove(); 239 } 240 241 } catch (Exception e) { 242 System.out.println("Oopss! something went wrong in SpeciesAutoComplete.refreshRemote"); 243 e.printStackTrace(System.out); 244 245 new Comboitem("No species found. error.").setParent(this); 246 } 247 248 } 249}