private void putAssessment(Request request, Response response, String username) { try { NativeDocument doc = NativeDocumentFactory.newNativeDocument(); doc.parse(request.getEntity().getText()); AssessmentParser parser = new AssessmentParser(doc); AssessmentData assessment = parser.getAssessment(); AssessmentIOWriteResult result = assignIDAndSave(assessment, username); if (result.status.isSuccess()) { response.setEntity(assessment.getAssessmentID(), MediaType.TEXT_PLAIN); response.setStatus(Status.SUCCESS_OK); } else { response.setStatus(Status.CLIENT_ERROR_EXPECTATION_FAILED); } } catch (RegionConflictException e) { response.setStatus(Status.CLIENT_ERROR_CONFLICT); } catch (Exception e) { response.setStatus(Status.SERVER_ERROR_INTERNAL); } }
private void postAssessment(Request request, Response response, String username) { try { String entity = request.getEntity().getText(); NativeDocument doc = NativeDocumentFactory.newNativeDocument(); doc.parse(entity); AssessmentParser parser = new AssessmentParser(doc); AssessmentData assessment = parser.getAssessment(); VFSPath assessmentUrl = new VFSPath(ServerPaths.getPathForAssessment(assessment, username)); if (vfs.exists(assessmentUrl)) { Status status = FileLocker.impl.persistentLockAssessment( assessment.getAssessmentID(), BaseAssessment.DRAFT_ASSESSMENT_STATUS, LockType.SAVE_LOCK, username); if (status.isSuccess()) { AssessmentIOWriteResult result = saveAssessment(assessment, username); if (result.status.isSuccess()) { response.setEntity(result.newLastModified + "", MediaType.TEXT_PLAIN); response.setStatus(status); } else { response.setStatus(Status.CLIENT_ERROR_EXPECTATION_FAILED); } } else { response.setStatus(status); } } } catch (RegionConflictException e) { response.setStatus(Status.CLIENT_ERROR_CONFLICT); } catch (Exception e) { e.printStackTrace(); response.setStatus(Status.CLIENT_ERROR_BAD_REQUEST); } }
private String batchCreate(Request request, String username) { NativeDocument doc = NativeDocumentFactory.newNativeDocument(); StringBuffer successfulIDs = new StringBuffer(); StringBuffer extantIDs = new StringBuffer(); StringBuffer unsuccessfulIDs = new StringBuffer(); try { String text = request.getEntity().getText(); doc.parse(text); AssessmentFilter filter = AssessmentFilter.parseXML( doc.getDocumentElement() .getElementsByTagName(AssessmentFilter.HEAD_TAG) .elementAt(0)); NativeNodeList nodes = doc.getDocumentElement().getElementsByTagName("taxon"); boolean useTemplate = Boolean.parseBoolean( doc.getDocumentElement() .getElementsByTagName("useTemplate") .elementAt(0) .getTextContent()); System.out.println("Using template? " + useTemplate); for (int i = 0; i < nodes.getLength(); i++) { TaxonNode taxon = TaxaIO.readNode(nodes.elementAt(i).getTextContent(), vfs); AssessmentData curAss = null; curAss = doCreateAssessmentForBatch( request.getChallengeResponse().getIdentifier(), filter, useTemplate, taxon); try { AssessmentIOWriteResult result = assignIDAndSave(curAss, username); if (result.status.isSuccess()) successfulIDs.append( curAss.getSpeciesName() + (i == nodes.getLength() - 1 ? "" : ", ")); else unsuccessfulIDs.append( curAss.getSpeciesName() + (i == nodes.getLength() - 1 ? "" : ", ")); } catch (RegionConflictException e) { extantIDs.append(curAss.getSpeciesName() + (i == nodes.getLength() - 1 ? "" : ", ")); } } StringBuilder ret = new StringBuilder(); if (unsuccessfulIDs.length() > 0) ret.append( "<div>Unable to create an assessment for the following species: " + unsuccessfulIDs + "</div>\r\n"); if (extantIDs.length() > 0) ret.append( "<div>The following species already have draft assessments with the specific locality: " + extantIDs + "</div>\r\n"); if (successfulIDs.length() > 0) ret.append( "<div>Successfully created an assessment for the following species: " + successfulIDs + "</div>\r\n"); return ret.toString(); } catch (IOException e) { e.printStackTrace(); return null; } catch (NullPointerException e) { e.printStackTrace(); return null; } catch (Exception e) { e.printStackTrace(); return null; } }