private void updateStatus() throws SQLException, IOException, MessagingException { List<License> licenses = licenseRepository.findAll(); for (License license : licenses) { State currentState = license.getState(); getLicenseWithNewState(license); State newState = license.getState(); if (currentState != newState) { licenseRepository.updateLicense(license); } } }
private void getLicenseWithNewState(License license) throws IOException, MessagingException { Date validFrom = license.getValidFrom(); Date validTill = license.getValidTill(); if (validFrom != null && validTill != null) { Instant now = Instant.now(); Instant lastValid = validTill.toInstant(); Instant lastActive = lastValid.minus(2, ChronoUnit.DAYS); if (now.isBefore(lastActive)) { license.setState(State.ACTIVE); } if (now.isAfter(lastActive) && now.isBefore(lastValid)) { license.setState(State.EXPIRATION_NEARING); // send mail to Admin notifying about soon expiring license mailService.sendExpirationNearingMail(license); } if (now.isAfter(lastValid)) { license.setState(State.TERMINATED); } } }
public License updateLicense(License license) throws SQLException { try (Connection conn = ds.getConnection()) { PreparedStatement statement = conn.prepareStatement( "UPDATE License SET " + "releaseId = ?, state = ?, licenseTypeId = ?, latestDeliveryDate = ? WHERE id = ?;"); State licenseState = license.getState(); statement.setObject(1, license.getRelease() == null ? null : license.getRelease().getId()); statement.setInt(2, licenseState.getStateNumber()); statement.setInt(3, license.getType().getId()); statement.setObject( 4, license.getLatestDeliveryDate() == null ? null : license.getLatestDeliveryDate()); statement.setInt(5, license.getId()); int rowCount = statement.executeUpdate(); if (rowCount == 0) { throw new RuntimeException("No license with id: " + license.getId()); } } return license; }
public License save(License license) throws SQLException { try (Connection conn = ds.getConnection()) { PreparedStatement statement = conn.prepareStatement( "INSERT INTO License (productId, releaseId, customerId, contractNumber, state, predecessorLicenseId, " + "validFrom, validTill, applicationSubmitDate) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)"); statement.setInt(1, license.getProduct().getId()); if (license.getRelease() == null) { statement.setNull(2, java.sql.Types.INTEGER); } else { statement.setInt(2, license.getRelease().getId()); } statement.setInt(3, license.getCustomer().getId()); statement.setString(4, license.getContractNumber()); statement.setInt(5, license.getState().getStateNumber()); statement.setString(6, license.getPredecessorLicenseId()); statement.setDate(7, license.getValidFrom()); statement.setDate(8, license.getValidTill()); // statement.setInt(9, license.getType().getId()); statement.setDate(9, license.getApplicationSubmitDate()); statement.execute(); try (ResultSet generatedKeys = statement.getGeneratedKeys()) { if (generatedKeys.next()) { license.setId(generatedKeys.getInt(1)); } } } return license; }