Advertisement
mgogo4

Untitled

Oct 19th, 2019
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.45 KB | None | 0 0
  1.     private final ResourceConfiguration configuration;
  2.     private static final List<Folder> folders = new ArrayList<Folder>();
  3.  
  4.     public FolderRepositoryImpl(ResourceConfiguration configuration) {
  5.         this.configuration = configuration;
  6.     }
  7.  
  8.     /**
  9.      * Jeżeli starczy czasu to będzie można ładnie rozłożyć te rzeczy z RepositoryImpl.
  10.      */
  11.     @PostConstruct
  12.     private void init() {
  13.         ObjectMapper objectMapper = new ObjectMapper();
  14.         File inputFile = new File(configuration.getFileName());
  15.  
  16.         try (FileInputStream fis = new FileInputStream(inputFile)) {
  17.             int iByteCount = fis.read();
  18.             //populate the folders list only if the input file is not empty
  19.             if (iByteCount != -1) {
  20.                 JsonNode jsonNode = objectMapper.readValue(inputFile, JsonNode.class);
  21.                 Iterator<Map.Entry<String, JsonNode>> iterator = jsonNode.fields();
  22.  
  23.                 while (iterator.hasNext()) {
  24.                     Map.Entry<String, JsonNode> currentIterator = iterator.next();
  25.                     String key = currentIterator.getKey();
  26.                     Folder folder = objectMapper.readValue(currentIterator.getValue().toString(), Folder.class);
  27.                     folder.setPath(key);
  28.                     folders.add(folder);
  29.                 }
  30.             }
  31.         } catch (IOException e) {
  32.             System.out.println("Cannot read Folder Data file.");
  33.         }
  34.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement