List of usage examples for javax.persistence PersistenceException printStackTrace
public void printStackTrace()
From source file:controllers.ClimateServiceController.java
public Result addServiceEntry() { JsonNode json = request().body().asJson(); if (json == null) { System.out.println("Climate service not saved, expecting Json data"); return badRequest("Climate service not saved, expecting Json data"); }/* w w w . j av a2 s .co m*/ // Parse JSON file String versionNo = json.findPath("versionNo").asText(); String registerNote = json.findPath("registerNote").asText(); int count = json.findPath("count").asInt(); // String scenario = json.findPath("scenario").asText(); long serviceId = json.findPath("serviceId").asLong(); long creatorId = json.findPath("creatorId").asLong(); Date registerTime = new Date(); SimpleDateFormat format = new SimpleDateFormat(Common.DATE_PATTERN); try { registerTime = format.parse(json.findPath("registerTimeStamp").asText()); } catch (ParseException e) { System.out.println("No creation date specified, set to current time"); } Date latestAccessTime = new Date(); try { latestAccessTime = format.parse(json.findPath("latestAccessTimeStamp").asText()); } catch (ParseException e) { System.out.println("No creation date specified, set to current time"); } try { ClimateService climateService = climateServiceRepository.findOne(serviceId); User creator = userRepository.findOne(creatorId); ServiceEntry entry = new ServiceEntry(); entry.setClimateService(climateService); entry.setCount(count); entry.setRegisterNote(registerNote); entry.setVersionNo(versionNo); entry.setUser(creator); entry.setRegisterTimeStamp(registerTime); entry.setLatestAccessTimestamp(latestAccessTime); ServiceEntry savedServiceEntry = serviceEntryRepository.save(entry); System.out.println("Service Entry saved: " + savedServiceEntry.getId()); return created(new Gson().toJson(savedServiceEntry)); } catch (PersistenceException pe) { pe.printStackTrace(); System.out.println("Service Entry not saved: " + serviceId); return badRequest("Service Entry not saved: " + serviceId); } }
From source file:controllers.ClimateServiceController.java
public Result updateClimateServiceById(long id) { JsonNode json = request().body().asJson(); if (json == null) { System.out.println("Climate service not saved, expecting Json data"); return badRequest("Climate service not saved, expecting Json data"); }//from w w w. j av a 2s. c om // Parse JSON file long rootServiceId = json.findPath("rootServiceId").asLong(); long creatorId = json.findPath("creatorId").asLong(); String name = json.findPath("name").asText(); String purpose = json.findPath("purpose").asText(); String url = json.findPath("url").asText(); String scenario = json.findPath("scenario").asText(); String versionNo = json.findPath("versionNo").asText(); // Creation time should be immutable and not updated. try { ClimateService climateService = climateServiceRepository.findOne(id); User user = userRepository.findOne(creatorId); ServiceEntry serviceEntry = null; if (versionNo.equals(climateService.getVersionNo())) { List<ServiceEntry> serviceEntries = serviceEntryRepository .findByClimateServiceAndVersionNo(climateService, versionNo); if (serviceEntries.size() == 0) { String registerNote = "ClimateService Name: " + climateService.getName() + ", VersionNo: " + versionNo; serviceEntry = new ServiceEntry(climateService.getCreateTime(), versionNo, user, climateService.getCreateTime(), registerNote, initialcount, climateService); } else { serviceEntry = serviceEntries.get(0); } } else { String registerNote = "ClimateService Name:" + climateService.getName() + ", VersionNo: " + versionNo; serviceEntry = new ServiceEntry(climateService.getCreateTime(), versionNo, user, climateService.getCreateTime(), registerNote, initialcount, climateService); } climateService.setUser(user); climateService.setName(name); climateService.setPurpose(purpose); climateService.setRootServiceId(rootServiceId); climateService.setScenario(scenario); climateService.setUrl(url); climateService.setVersionNo(versionNo); ClimateService savedClimateService = climateServiceRepository.save(climateService); serviceEntry.setClimateService(savedClimateService); ServiceEntry savedServiceEntry = serviceEntryRepository.save(serviceEntry); System.out.println("Climate Service updated: " + savedClimateService.getName()); return created("Climate Service updated: " + savedClimateService.getName()); } catch (PersistenceException pe) { pe.printStackTrace(); System.out.println("Climate Service not updated: " + name); return badRequest("Climate Service not updated: " + name); } }
From source file:controllers.ClimateServiceController.java
public Result updateClimateServiceByName(String oldName) { JsonNode json = request().body().asJson(); if (json == null) { System.out.println("Climate service not saved, expecting Json data"); return badRequest("Climate service not saved, expecting Json data"); }/*from ww w. ja v a2 s .co m*/ System.out.println(json); // Parse JSON file long rootServiceId = json.findPath("rootServiceId").asLong(); long creatorId = json.findPath("creatorId").asLong(); String name = json.findPath("name").asText(); String purpose = json.findPath("purpose").asText(); String url = json.findPath("url").asText(); String scenario = json.findPath("scenario").asText(); String versionNo = json.findPath("versionNo").asText(); // Creation time is immutable and should not be updated if (oldName == null || oldName.length() == 0) { System.out.println("Old climate Service Name is null or empty!"); return badRequest("Old climate Service Name is null or empty!"); } try { ClimateService climateService = climateServiceRepository.findFirstByName(oldName); User user = userRepository.findOne(creatorId); ServiceEntry serviceEntry = null; if (versionNo.equals(climateService.getVersionNo())) { List<ServiceEntry> serviceEntries = serviceEntryRepository .findByClimateServiceAndVersionNo(climateService, versionNo); if (serviceEntries.size() == 0) { String registerNote = "ClimateService Name: " + climateService.getName() + ", VersionNo: " + versionNo; serviceEntry = new ServiceEntry(climateService.getCreateTime(), versionNo, user, climateService.getCreateTime(), registerNote, initialcount, climateService); } else { serviceEntry = serviceEntries.get(0); } } else { String registerNote = "ClimateService Name: " + climateService.getName() + ", VersionNo: " + versionNo; serviceEntry = new ServiceEntry(climateService.getCreateTime(), versionNo, user, climateService.getCreateTime(), registerNote, initialcount, climateService); } climateService.setName(name); climateService.setPurpose(purpose); climateService.setRootServiceId(rootServiceId); climateService.setScenario(scenario); climateService.setUrl(url); climateService.setUser(user); climateService.setVersionNo(versionNo); ClimateService savedClimateService = climateServiceRepository.save(climateService); serviceEntry.setClimateService(savedClimateService); ServiceEntry savedServiceEntry = serviceEntryRepository.save(serviceEntry); System.out.println("Climate Service updated: " + savedClimateService.getName()); return created("Climate Service updated: " + savedClimateService.getName()); } catch (PersistenceException pe) { pe.printStackTrace(); System.out.println("Climate Service not updated: " + name); return badRequest("Climate Service not updated: " + name); } }