/** @param bioID */ private boolean setBioID(String bioID) { boolean toRet = false; try { JsonFactory factory = new JsonFactory(); JsonParser jparser = factory.createParser(bioID); String key = ""; // Get StartObject for JSON, after first { jparser.nextToken(); while (jparser.nextToken() != JsonToken.END_OBJECT) { key = jparser.getText(); if (key.equals("id")) { key = jparser.nextTextValue(); // Set bioID in Experiment selExp.setBioID(key); toRet = true; } else { jparser.skipChildren(); } } jparser.close(); } catch (JsonParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return toRet; }
@Port(direction = Direction.INPUT, name = "Set Experiment to upload", order = 1) public void setExperiment(IExperiment selExp) { this.selExp = selExp; if (this.selExp.getMapIntraExpsColors() != null) { isInter = true; } else { isInter = false; } try { // First step, send Experiment data String json = generateJSON(); // Second step, receive Experiment bio_ID from BiofOmics String bioID = getBioID(json); if (!bioID.isEmpty()) { // Third step, set bio_ID in the Experiment boolean success = setBioID(bioID); if (success) { // Four step, create Experiment files and send them to // BiofOmics String zipPath = saveExpInTempFolder(); json = generateJSON(zipPath); // Call webService to upload the file int serverCode = uploadExperiment(json); if (serverCode < 400) { ShowDialog.showInfo( I18n.get("updloadOkTitle"), I18n.get("uploadOk1") + selExp.getName() + I18n.get("uploadOk2") + BewConstants.USER + "."); } else { if (serverCode == 401) { ShowDialog.showError(I18n.get("updloadErrorTitle"), I18n.get("uploadBadLogin")); } else if (serverCode == 500) { ShowDialog.showError(I18n.get("updloadErrorTitle"), I18n.get("uploadServerError")); } } } else { ShowDialog.showError(I18n.get("updloadErrorTitle"), I18n.get("uploadIDError")); } } else { ShowDialog.showError(I18n.get("updloadErrorTitle"), I18n.get("uploadIDError")); } } catch (UnknownHostException e) { // TODO Auto-generated catch block e.printStackTrace(); ShowDialog.showError(I18n.get("updloadErrorTitle"), I18n.get("webServicesConnection")); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); ShowDialog.showError(I18n.get("updloadErrorTitle"), I18n.get("updloadError")); } }
/** * Method to fill the JSON with the experiment information. This JSON includes the files field. * * @param json Input JSON to fill with data. * @param zipPath Path of the temporary zip files for the Experiment. * @return String with the new JSON. * @throws IOException */ private String getJSONExperiment(String json, String zipPath) throws IOException { // Get bioID json += "\"id\":\"" + selExp.getBioID() + "\""; // Set zip file Path path = FileSystems.getDefault().getPath(zipPath); json += ",\"file\":\"" + new String(Base64Coder.encode(Files.readAllBytes(path))) + "\"}"; return json; }
/** * Method to save the selected Experiment, in xml and xls (if possible), in a temporary folder. * The files will be contained inside a zip. * * @return String with the path of the zip file. * @throws IOException */ private String saveExpInTempFolder() throws IOException { String toRet = ""; // Create temporary File String filePath = Files.createTempDirectory("").toString() + "/"; filePath = filePath.replace("\\", "/"); // Get bioID String bioID = selExp.getBioID(); if (bioID.isEmpty()) { bioID = "bio_"; } DataToFile.saveXMLData(selExp, filePath + "xml"); // Only save in XLS if the Experiment is an IntraExperiment if (!isInter) { DataToFile.saveXLSData(selExp, filePath + "xls"); } // Generate ZIP file with Java 7 Map<String, String> zipProperties = new HashMap<>(); zipProperties.put("create", "true"); zipProperties.put("encoding", "UTF-8"); // Create zip file URI zipDisk; toRet = filePath + bioID + ".zip"; if (toRet.startsWith("/")) { zipDisk = URI.create("jar:file:" + toRet); } else { zipDisk = URI.create("jar:file:/" + toRet); } // Adding files to zip try (FileSystem zipfs = FileSystems.newFileSystem(zipDisk, zipProperties)) { // Create file inside zip Path zipFilePath = zipfs.getPath(bioID + ".xml"); // Path where the file to be added resides Path addNewFile = Paths.get(filePath + "xml.xml"); // Append file to ZIP File Files.copy(addNewFile, zipFilePath); if (!isInter) { // Now go for the xls file zipFilePath = zipfs.getPath(bioID + ".xls"); addNewFile = Paths.get(filePath + "xls.xls"); Files.copy(addNewFile, zipFilePath); } } // Delete temp files Files.deleteIfExists(Paths.get(filePath + "xml.xml")); Files.deleteIfExists(Paths.get(filePath + "xls.xls")); return toRet; }