00001 package crowdUser; 00002 00003 import java.io.BufferedInputStream; 00004 import java.io.BufferedOutputStream; 00005 import java.io.BufferedReader; 00006 import java.io.BufferedWriter; 00007 import java.io.File; 00008 import java.io.FileOutputStream; 00009 import java.io.FileWriter; 00010 import java.io.InputStream; 00011 import java.io.InputStreamReader; 00012 import java.io.OutputStreamWriter; 00013 import java.net.HttpURLConnection; 00014 import java.net.URL; 00015 import java.net.URLConnection; 00016 import java.net.URLEncoder; 00017 import java.util.*; 00018 import java.util.zip.ZipEntry; 00019 import java.util.zip.ZipInputStream; 00020 00021 import myDataBases.MySQLbase; 00022 import myDataBases.MediaBase; 00023 00024 00039 public class CrowdManager { 00040 00041 00042 // Fields 00043 00044 00049 private List<Integer> greater; 00054 private List<Integer> smaller; 00058 private List<String> axes; 00062 private Integer comparisonsNumber; 00067 private String cfKey ; 00074 private Integer originalJobId; 00078 private String resultsUrl; 00082 private MediaBase dbMedia ; 00086 private MySQLbase dbSQL; 00090 private Refiner refiner; 00091 00092 00093 00094 // Constructors 00095 00110 public CrowdManager (Integer firstJobId, String key, String resultsUrl, MySQLbase baseSQL, 00111 MediaBase baseDIR, List<String> fieldsHardData) 00112 { 00113 this.comparisonsNumber = 0; 00114 this.resultsUrl = resultsUrl; 00115 this.greater = new ArrayList<Integer>(); 00116 this.smaller = new ArrayList<Integer>(); 00117 this.axes = new ArrayList<String>(); 00118 this.originalJobId = firstJobId; 00119 this.cfKey = key; 00120 this.dbSQL = baseSQL; 00121 this.dbMedia = baseDIR ; 00122 this.refiner = new Refiner(fieldsHardData); 00123 }; 00124 00125 00126 // Methods 00127 00128 00139 public Integer createNewJob() 00140 { 00141 Integer id = 0; 00142 00143 String postUrl = "http://api.crowdflower.c../..bs/" + this.originalJobId 00144 + "/copy.json?key=" + this.cfKey ; 00145 // posting the request 00146 try 00147 { 00148 URL url = new URL(postUrl); 00149 HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 00150 // Building an HTTP request 00151 conn.setDoOutput(true); 00152 conn.setRequestMethod("POST"); 00153 conn.connect(); 00154 // getting the answer from CrowdFlower and reading the "id" field 00155 BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream())); 00156 String line, idString = ""; 00157 while ((line = rd.readLine()) != null) 00158 if (line.contains('"'+"id"+'"')) 00159 // we go to the index of ' "id": ' and read the number. In order no to take '"id":', 5 is added. 00160 for (int i = line.lastIndexOf('"' +"id"+'"')+5; i<line.length(); i++) 00161 if ( line.charAt(i) == ',' ) 00162 break; 00163 else 00164 idString += line.charAt(i) ; 00165 id = Integer.parseInt(idString); 00166 rd.close(); 00167 } catch (Exception e) { e.printStackTrace(); } 00168 00169 return id; 00170 } 00171 00172 00173 00187 public void uploadHIT(Integer jobId) 00188 { 00189 this.comparisonsNumber += this.greater.size() ; 00190 String post = ""; 00191 // writing the .csv file and the post String 00192 try{ 00193 BufferedWriter fichier = new BufferedWriter 00194 (new FileWriter("../data/HITdata/" + jobId + ".csv")); 00195 String line = "axis,idMedia1,idMedia2,urlMedia1,urlMedia2,miscData1,miscData2"; 00196 fichier.write(line); 00197 fichier.newLine(); 00198 post += line ; 00199 for (int i=0; i<this.greater.size(); i++) 00200 { 00201 line = this.axes.get(i) + "," + this.greater.get(i) + "," + this.smaller.get(i) + 00202 "," + this.dbMedia.getMedia(this.greater.get(i)) + 00203 "," + this.dbMedia.getMedia(this.smaller.get(i)) + 00204 ",\"" + this.dbMedia.getContent().get(this.greater.get(i)).get("miscData") + 00205 "\",\"" + this.dbMedia.getContent().get(this.smaller.get(i)).get("miscData") + "\""; 00206 fichier.write(line); 00207 fichier.newLine(); 00208 post += "\n" + line ; 00209 } 00210 fichier.close(); 00211 00212 } 00213 catch (Exception e) { e.printStackTrace(); } 00214 00215 // posting the file 00216 00217 try 00218 { 00219 URL url = new URL("http://api.crowdflower.c../..bs/" + jobId + 00220 "/upload.json?key=" + this.cfKey); 00221 HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 00222 // Building an HTTP request 00223 conn.setDoOutput(true); 00224 conn.setRequestMethod("PUT"); 00225 conn.setRequestProperty("Content-Type", "text/csv"); 00226 OutputStreamWriter osw = new OutputStreamWriter(conn.getOutputStream()); 00227 osw.write(post); 00228 osw.flush(); 00229 osw.close(); 00230 00231 // getting the answer from CrowdFlower 00232 BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream())); 00233 String line; 00234 while ((line = rd.readLine()) != null) { 00235 System.out.println(line); 00236 } 00237 00238 rd.close(); 00239 } catch (Exception e) { e.printStackTrace(); } 00240 00241 } 00242 00243 00259 public String getHITresults(Integer jobId) 00260 { 00261 try 00262 { 00263 // creating connection 00264 String urlString = this.resultsUrl + "csv/" + jobId + ".txt"; 00265 System.out.println(urlString); 00266 URL url = new URL(urlString); 00267 System.out.println("Opening connection to " + urlString + "..."); 00268 // reading input 00269 InputStream is = url.openStream(); 00270 System.out.flush(); 00271 // creating results$JOB_ID.csv file 00272 FileOutputStream fos=null; 00273 fos = new FileOutputStream("../data/HITresults/results" + jobId + ".csv"); 00274 // writing to file 00275 int oneChar, count=0; 00276 while ((oneChar=is.read()) != -1) 00277 { 00278 fos.write(oneChar); 00279 count++; 00280 } 00281 // close everything 00282 is.close(); 00283 fos.close(); 00284 System.out.println("csv results file downloaded, " + count + " byte(s) copied"); 00285 } 00286 catch (Exception e) { e.printStackTrace(); } 00287 // return the name of the file minus ".csv" 00288 return "results" + jobId ; 00289 } 00290 00291 00306 public void readHITresults( String path ) 00307 { 00308 List<String[]> entries ; 00309 // retrieve hard data from the csv file 00310 this.refiner.csv2List(path) ; 00311 entries = this.refiner.getData() ; 00312 this.dbSQL.insertHardDataInSQL(entries); 00313 // retrieve good data from the refiner 00314 this.refiner.getanotherlabel() ; 00315 entries = this.refiner.getData() ; 00316 this.dbSQL.insertResultsInSQL(entries,this.dbMedia) ; 00317 } 00318 00319 00331 // Yes, little friend / This is the end / My only friend / the end / Of everything that stands / the end 00332 // I'll never look into your eyes / again... 00333 public static void thisIsTheEnd(String antechamberUrl, String results) 00334 { 00335 try 00336 { 00337 URL url = new URL(antechamberUrl); 00338 HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 00339 // Building an HTTP request 00340 conn.setDoOutput(true); 00341 conn.setRequestMethod("POST"); 00342 OutputStreamWriter osw = new OutputStreamWriter(conn.getOutputStream()); 00343 String data = URLEncoder.encode("signal", "UTF-8") + "=" + URLEncoder.encode("sort_finished","UTF-8") 00344 + "&" + URLEncoder.encode("payload", "UTF-8") + "=" + URLEncoder.encode(results, "UTF-8"); 00345 osw.write(data); 00346 osw.flush(); 00347 osw.close(); 00348 // getting the answer from CrowdFlower 00349 BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream())); 00350 String line; 00351 while ((line = rd.readLine()) != null) { 00352 System.out.println(line); 00353 } 00354 rd.close(); 00355 } catch (Exception e) { e.printStackTrace(); } 00356 } 00357 00358 00359 // Accessor methods 00360 00361 00366 public List<Integer> getGreater() 00367 { return this.greater ; } 00368 00369 00374 public List<Integer> getSmaller() 00375 { return this.smaller; } 00376 00377 00382 public List<String> getAxes() 00383 { return this.axes; } 00384 00385 00390 public Integer getComparisonsNumber() 00391 { return this.comparisonsNumber; } 00392 00393 00394 }