/** * Creates a preview of a HIT in a file. * * @param previewFileName the file in which the HIT is copied * @param input the input needed for the HIT * @param props the properties of the HIT * @param question the question asked in the HIT * @throws ServiceException */ public void previewHIT(String previewFileName, HITDataInput input, HITProperties props, HITQuestion question) throws ServiceException { try { String previewString = previewHIT(input, props, question); if (previewString != null) { FileUtil fts = new FileUtil(previewFileName); fts.saveString(previewString, false); // overwrite } } catch (Exception e) { throw new ServiceException("Error generating preview file " + previewFileName, e); } }
/** * Creates multiple HITs. * * @param input the input data needed for the HITs * @param props the properties of the HITs * @param question the question asked in the HITs * @param numHITToLoad the number of HITs to create * @return an array of HIT objects * @throws Exception * @deprecated */ public HIT[] createHITs(HITDataReader input, HITProperties props, HITQuestion question, int numHITToLoad) { String prefix = input.getFileName(); if ( prefix == null || prefix.length() == 0 ) { prefix = "input"; } HITDataOutput success = null; HITDataOutput failure = null; try { success = new HITDataWriter(prefix + ".success"); failure = new HITDataWriter(prefix + ".failure"); return createHITs(input,props,question,numHITToLoad,success,failure); } catch (Exception e) { log.error("Error loading HITs", e); } finally { if (success != null) { success.close(); } if (failure != null) { failure.close(); } } return null; }
public void testPreviewHITFileNullProperties() throws ServiceException { HITProperties nullProps = null; try { service.previewHIT(defaultHITInput, nullProps, defaultHITQuestion); } catch (Exception e) { // expected } }
/** * Returns a preview of the HIT as HTML * * @param input the input needed for the HIT * @param props the properties of the HIT * @param question the question asked in the HIT * @return an HTML preview of the HIT * @throws Exception */ public String previewHIT(HITDataInput input, HITProperties props, HITQuestion question) throws Exception { if (props == null || question == null) throw new IllegalArgumentException(); String questionXML = null; if (input != null) { Map<String, String> inputMap = input.getRowAsMap(1); questionXML = question.getQuestion(inputMap); } else { questionXML = question.getQuestion(); } // Validate question before preview QAPValidator.validate(questionXML); String questionPreview = XslTransformer.convertQAPtoHTML(questionXML); InputStream headerURL = this.getClass().getResourceAsStream("previewHITHeader.xml"); InputStream footerURL = this.getClass().getResourceAsStream("previewHITFooter.xml"); if (headerURL == null) { log.error("Error reading the preview header file."); } if (footerURL == null) { log.error("Error reading the preview footer file."); } BufferedReader headerReader = new BufferedReader(new InputStreamReader(headerURL)); BufferedReader footerReader = new BufferedReader(new InputStreamReader(footerURL)); String thisLine = null; String header = ""; String footer = ""; while ((thisLine = headerReader.readLine()) != null) { header += thisLine + System.getProperty("line.separator"); } while ((thisLine = footerReader.readLine()) != null) { footer += thisLine + System.getProperty("line.separator"); } headerReader.close(); footerReader.close(); NumberFormat rewardFormatter = NumberFormat.getInstance(); rewardFormatter.setMaximumFractionDigits(2); rewardFormatter.setMinimumFractionDigits(2); Map<String, String> headerMap = new HashMap<String, String>(); headerMap.put("requester", "[Your Requester Name Here]"); headerMap.put("title", props.getTitle()); headerMap.put("description", props.getDescription()); headerMap.put("keywords", props.getKeywords()); headerMap.put("reward", rewardFormatter.format(props.getRewardAmount())); String mergedHeader = VelocityUtil.doMerge(header, headerMap); String previewString = mergedHeader + questionPreview + footer; return previewString; }
public void testLoadUTF8PropertiesFile() throws IOException { HITProperties p = new HITProperties(defaultUTF8PropertiesFileName); assertEquals("Tàu cánh ngầm của tôi đầy lươn", p.getTitle()); }
/** * Create the website category HITs. * */ public void createSiteCategoryHITs() { try { //Loading the input file. The input file is a tab delimited file where the first row //defines the fields/variables and the remaining rows contain the values for each HIT. //Each row represents a unique HIT. The SDK uses the Apache Velocity engine to merge //the input values into a templatized QAP file. //Please refer to http://velocity.apache.org for more details on this engine and //how to specify variable names. Apache Velocity is fully supported so there may be //additional functionality you can take advantage of if you know the Velocity syntax. HITDataInput input = new HITDataCSVReader(inputFile); //Loading the question (QAP) file. This QAP file contains Apache Velocity variable names where the values //from the input file are to be inserted. Essentially the QAP becomes a template for the input file. HITQuestion question = new HITQuestion(questionFile); //Loading the HIT properties file. The properties file defines two system qualifications that will //be used for the HIT. The properties file can also be a Velocity template. Currently, only //the "annotation" field is allowed to be a Velocity template variable. This allows the developer //to "tie in" the input value to the results. HITProperties props = new HITProperties(propertiesFile); HIT[] hits = null; // Create multiple HITs using the input, properties, and question files System.out.println("--[Loading HITs]----------"); Date startTime = new Date(); System.out.println(" Start time: " + startTime); //The simpliest way to bulk load a large number of HITs where all details are defined in files. //When using this method, it will automatically create output files of the following type: // - <your input file name>.success - A file containing the HIT IDs and HIT Type IDs of // all HITs that were successfully loaded. This file will // not exist if there are no HITs successfully loaded. // - <your input file name>.failure - A file containing the input rows that failed to load. // This file will not exist if there are no failures. //The .success file can be used in subsequent operations to retrieve the results that workers submitted. HITDataOutput success = new HITDataCSVWriter(inputFile + ".success"); HITDataOutput failure = new HITDataCSVWriter(inputFile + ".failure"); hits = service.createHITs(input, props, question, success, failure); System.out.println("--[End Loading HITs]----------"); Date endTime = new Date(); System.out.println(" End time: " + endTime); System.out.println("--[Done Loading HITs]----------"); System.out.println(" Total load time: " + (endTime.getTime() - startTime.getTime())/1000 + " seconds."); if (hits == null) { throw new Exception("Could not create HITs"); } } catch (Exception e) { System.err.println(e.getLocalizedMessage()); } }
/** * Creates multiple HITs. * * @param input the input data needed for the HITs * @param props the properties of the HITs * @param question the question asked in the HITs * @return an array of HIT objects * @throws Exception * @deprecated */ public HIT[] createHITs(HITDataReader input, HITProperties props, HITQuestion question) { return createHITs( input, props, question, RequesterService.LOAD_ALL ); }
/** * Creates multiple HITs. * * @param input the input data needed for the HITs * @param props the properties of the HITs * @param question the question in the HITs * @param success the file that contains the HITId and HITTypeId for the created * HITs * @param failure the failure file * @return an array of HIT objects * @throws Exception */ public HIT[] createHITs(HITDataInput input, HITProperties props, HITQuestion question, HITDataOutput success, HITDataOutput failure) throws Exception { return createHITs(input, props, question, RequesterService.LOAD_ALL, success, failure); }