/alaspatial/src/main/java/org/ala/spatial/web/SpeciesAutoComplete.java

http://alageospatialportal.googlecode.com/ · Java · 249 lines · 160 code · 53 blank · 36 comment · 44 complexity · de9e47d3fa9cc6ff4694090ac6b6bb86 MD5 · raw file

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