×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: Java
Posted by: Peter Nicholls
Added: Jan 2, 2019 3:40 PM
Views: 3613
Tags: no tags
  1. package com.qxlva.nhs.service;
  2.  
  3. import java.io.FileOutputStream;
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6. import java.io.OutputStream;
  7. import java.time.LocalDateTime;
  8. import java.time.format.DateTimeFormatter;
  9. import java.util.Properties;
  10.  
  11. import javax.annotation.PostConstruct;
  12.  
  13. import org.slf4j.Logger;
  14. import org.slf4j.LoggerFactory;
  15. import org.springframework.stereotype.Component;
  16.  
  17. public class LastQueriedUpperDateStore
  18. {
  19.  
  20.     private static final Logger LOGGER = LoggerFactory.getLogger(LastQueriedUpperDateStore.class);
  21.  
  22.     private static final String FILESTORE_NAME = "last_queried_upper_date.properties";
  23.  
  24.     private static final String PROPERTY_NAME = "last.queried.upper.date";
  25.  
  26.     private static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS");
  27.  
  28.     private Properties properties;
  29.  
  30.     private LocalDateTime lastQueriedUpperDate;
  31.  
  32.     @PostConstruct
  33.     @SuppressWarnings("squid:S3457")
  34.     public void initProperties()
  35.     {
  36.  
  37.         properties = new Properties();
  38.  
  39.         try (InputStream input = LastQueriedUpperDateStore.class.getClassLoader().getResourceAsStream(FILESTORE_NAME))
  40.         {
  41.  
  42.             if (input == null)
  43.             {
  44.                 LOGGER.info("No last queried upper date store found");
  45.                 lastQueriedUpperDate = null;
  46.                 return;
  47.             }
  48.  
  49.             properties.load(input);
  50.  
  51.             String lastQueriedUpperDateAsString = properties.getProperty(PROPERTY_NAME);
  52.  
  53.             lastQueriedUpperDate = LocalDateTime.parse(lastQueriedUpperDateAsString, FORMATTER);
  54.  
  55.         }
  56.         catch (IOException e)
  57.         {
  58.             LOGGER.error("Error reading last queried upper date property from file", e);
  59.         }
  60.  
  61.     }
  62.  
  63.     public LocalDateTime getLastQueriedUpperDate()
  64.     {
  65.         return lastQueriedUpperDate;
  66.     }
  67.  
  68.     public void setLastQueriedUpperDate(LocalDateTime lastQueriedUpperDate)
  69.     {
  70.         this.lastQueriedUpperDate = lastQueriedUpperDate;
  71.         saveToFile();
  72.     }
  73.  
  74.     private void saveToFile()
  75.     {
  76.  
  77.         if (lastQueriedUpperDate == null)
  78.         {
  79.             return;
  80.         }
  81.  
  82.         try (OutputStream output = new FileOutputStream(FILESTORE_NAME))
  83.         {
  84.  
  85.             String formattedDateTime = lastQueriedUpperDate.format(FORMATTER); // "1986-04-08 12:30"
  86.             // set the properties value
  87.             properties.setProperty(PROPERTY_NAME, formattedDateTime);
  88.  
  89.             // save properties to project root folder
  90.             properties.store(output, "The last queried upper date time for the GUID Extractor tool");
  91.  
  92.         }
  93.         catch (IOException e)
  94.         {
  95.             LOGGER.error("Failed to store last queried upper date property in file due to IO Exception", e);
  96.         }
  97.  
  98.     }
  99.  
  100. }
  101.