@RequestMapping(method = RequestMethod.POST) public ResponseEntity<? extends Serializable> insert(@RequestBody Visit visit) { ConstraintViolations cv = new ConstraintViolations(); visit.setVisitLocation(fieldBuilder.referenceField(visit.getVisitLocation(), cv)); visit.setCollectedBy(fieldBuilder.referenceField(visit.getCollectedBy(), cv)); if (cv.hasViolations()) { return new ResponseEntity<WebServiceCallException>( new WebServiceCallException(cv), HttpStatus.BAD_REQUEST); } try { visitService.createVisit(visit); } catch (ConstraintViolations e) { return new ResponseEntity<WebServiceCallException>( new WebServiceCallException(cv), HttpStatus.BAD_REQUEST); } return new ResponseEntity<Visit>(ShallowCopier.makeShallowCopy(visit), HttpStatus.CREATED); }
/** * Parses through SQL INSERT query, looking for certain keyword, extracts FOREIGN KEY references * according to the values found and returns them in a Map for further processing. * * <p>Will look for these keywords: - OPENHDS_VISIT_ID - OPENHDS_INDIVIDUAL_ID - * OPENHDS_HOUSEHOLD_ID - OPENHDS_LOCATION_ID If one of these is found, gets the appropriate extId * from the extraform object and puts the value into a map for further processing. * * @param extraForm Object that contains the extra-form submission information that should be * inserted into the extra-form table * @param query SQL INSERT statement with placeholders for FK data and VALUES * @return A Map<String, String> containing the UUID of the references found * @throws ConstraintViolations If an object could not be found with the given extId */ private Map<String, String> getForeignKeyData(ExtraForm extraForm, String query) throws ConstraintViolations { Map<String, String> foreignKeyData = new HashMap<String, String>(); if (query.contains("OPENHDS_VISIT_ID")) { Visit visit = genericDao.findByProperty(Visit.class, "extId", extraForm.getVisitId()); if (visit == null) throw new ConstraintViolations("Could not find visit with extId " + extraForm.getVisitId()); foreignKeyData.put("VISIT_UUID", "'" + visit.getUuid() + "'"); } if (query.contains("OPENHDS_INDIVIDUAL_ID") || query.contains("INDIVIDUAL_INFO_INDIVIDUAL_ID")) { Individual individual = genericDao.findByProperty(Individual.class, "extId", extraForm.getIndividualId()); if (individual == null) throw new ConstraintViolations( "Could not find individual with extId " + extraForm.getIndividualId()); foreignKeyData.put("INDIVIDUAL_UUID", "'" + individual.getUuid() + "'"); } if (query.contains("OPENHDS_HOUSEHOLD_ID")) { SocialGroup socialGroup = genericDao.findByProperty(SocialGroup.class, "extId", extraForm.getSocialGroupId()); if (socialGroup == null) throw new ConstraintViolations( "Could not find socialGroup with extId " + extraForm.getSocialGroupId()); foreignKeyData.put("HOUSEHOLD_UUID", "'" + socialGroup.getUuid() + "'"); } if (query.contains("OPENHDS_LOCATION_ID")) { Location location = genericDao.findByProperty(Location.class, "extId", extraForm.getLocationId()); if (location == null) throw new ConstraintViolations( "Could not find location with extId " + extraForm.getLocationId()); foreignKeyData.put("LOCATION_UUID", "'" + location.getUuid() + "'"); } return foreignKeyData; }