ImportManager.java 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. package org.django.acquabooks;
  2. import java.io.File;
  3. import java.io.FileNotFoundException;
  4. import java.io.FileReader;
  5. import java.io.IOException;
  6. import java.util.Date;
  7. import java.util.Scanner;
  8. import com.google.gson.stream.JsonReader;
  9. import org.apache.commons.lang3.StringUtils;
  10. import org.django.acquabooks.io.Console;
  11. import org.django.acquabooks.pojos.Libro;
  12. import org.slf4j.Logger;
  13. import org.slf4j.LoggerFactory;
  14. import com.google.gson.Gson;
  15. public class ImportManager {
  16. public static int CONSOLE_ROW_LENGHT = 80;
  17. public static int MODE_INSERT = 0;
  18. public static int MODE_EDIT = 1;
  19. public static int MODE_AZZERA_VENDUTO = 2;
  20. private static final Logger logger = LoggerFactory
  21. .getLogger(CommandLineLauncher.class);
  22. private ElasticRESTClient elclient;
  23. private File fileIn;
  24. // se MODE_EDIT una volta settato rimane, il che impedisce all'oggetto di essere
  25. // un singleton usabile sia per gli import che gli export, ritenuto che
  26. // non è in previsione una versione ad interfaccia grafica stile ncurses
  27. // o swing o altro, si è ritenuto che via command line "one shot" ogni azione
  28. // crea un nuovo oggetto e lo termina.
  29. private int mode;
  30. public ImportManager(String filename) {
  31. elclient = ElasticRESTClient.getInstance("http", "localhost", "9200");
  32. fileIn = new File(filename);
  33. }
  34. /*
  35. * default: is an Import
  36. */
  37. public ImportManager(String filename, ElasticRESTClient client){
  38. this.elclient = client;
  39. this.fileIn = new File(filename);
  40. this.mode = MODE_INSERT;
  41. }
  42. public ImportManager(String filename, int mode , ElasticRESTClient client){
  43. this.elclient = client;
  44. this.fileIn = new File(filename);
  45. this.mode = mode;
  46. }
  47. public void run(){
  48. JsonReader reader = null;
  49. try {
  50. Gson gson = new Gson();
  51. reader = new JsonReader(new FileReader(fileIn));
  52. reader.beginArray();
  53. while (reader.hasNext()) {
  54. Libro lin = new Libro();
  55. reader.beginObject();
  56. while (reader.hasNext()) {
  57. String prop = reader.nextName();
  58. switch (prop){
  59. case "barcode":
  60. lin.setBarcode(reader.nextString());
  61. break;
  62. case "editore":
  63. lin.setEditore(reader.nextString());
  64. break;
  65. case "tag":
  66. lin.setTag(reader.nextString());
  67. break;
  68. case "autore":
  69. lin.setAutore(reader.nextString());
  70. break;
  71. case "titolo":
  72. lin.setTitolo(reader.nextString());
  73. break;
  74. case "prezzo":
  75. lin.setPrezzo(reader.nextDouble());
  76. break;
  77. case "percentuale":
  78. lin.setPercentuale(reader.nextDouble());
  79. break;
  80. case "qa":
  81. lin.setQa(((int) reader.nextLong()));
  82. break;
  83. case "qv":
  84. lin.setQv(((int) reader.nextLong()));
  85. break;
  86. case "sconto":
  87. lin.setSconto(reader.nextDouble());
  88. break;
  89. default:
  90. reader.skipValue();
  91. break;
  92. }
  93. }
  94. reader.endObject();
  95. if(mode == MODE_INSERT || mode == MODE_AZZERA_VENDUTO){
  96. Libro l = elclient.getDetail(lin.getBarcode());
  97. if(l != null && MODE_INSERT == mode){
  98. logErr("FALLITO record già presente!! Barcode: "+l.getBarcode());
  99. continue;
  100. }
  101. if(l == null && MODE_AZZERA_VENDUTO == mode){
  102. logErr("FALLITO libro NON presente!! Barcode: "+l.getBarcode());
  103. continue;
  104. }else if(MODE_AZZERA_VENDUTO == mode){ //l !=null is implicit
  105. l.setQa(l.getQa() - l.getQv());
  106. l.setQv(0);
  107. lin = l;
  108. }
  109. }
  110. if(elclient.index(lin)){
  111. Console.genericWarn("Indicizzato record con barcode: " + lin.getBarcode());
  112. }else{
  113. Console.genericErr("FALLITO record con barcode: " + lin.getBarcode());
  114. }
  115. }
  116. reader.endArray();
  117. } catch (FileNotFoundException e) {
  118. e.printStackTrace();
  119. } catch (IOException e) {
  120. e.printStackTrace();
  121. }finally {
  122. try {
  123. reader.close();
  124. } catch (IOException e) {
  125. logger.error("Error on closing reader",e);
  126. }
  127. }
  128. }
  129. private void logWarn(String msg){
  130. this.logger.warn(" (((");
  131. this.logger.warn(" (@ @)");
  132. this.logger.warn("-----------------------------------ooO--(_)--Ooo--------------------------------");
  133. this.logger.warn("| "+ StringUtils.rightPad(msg, CONSOLE_ROW_LENGHT - 3 - msg.length()) +"|");
  134. this.logger.warn("--------------------------------------------------------------------------------");
  135. }
  136. private void logErr(String msg){
  137. this.logger.error(" §§§");
  138. this.logger.error(" (+ +)");
  139. this.logger.error("-----------------------------------ooO--(_)--Ooo--------------------------------");
  140. this.logger.error("| "+ StringUtils.rightPad(msg, CONSOLE_ROW_LENGHT - 3 - msg.length()) +"|");
  141. this.logger.error("--------------------------------------------------------------------------------");
  142. }
  143. }