CrudManager.java 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. package org.django.acquabooks;
  2. import java.io.BufferedReader;
  3. import java.io.InputStreamReader;
  4. import org.apache.commons.lang3.StringUtils;
  5. import org.django.acquabooks.pojos.Libro;
  6. import org.slf4j.Logger;
  7. import org.slf4j.LoggerFactory;
  8. public class CrudManager {
  9. public static String STOP_READING_SEQUENCE = "q";
  10. public static int CONSOLE_ROW_LENGHT = 80;
  11. private static final Logger logger = LoggerFactory
  12. .getLogger(CommandLineLauncher.class);
  13. private ElasticRESTClient elclient;
  14. public CrudManager() {
  15. elclient = ElasticRESTClient.getInstance("http", "localhost", "9200");
  16. }
  17. public CrudManager(ElasticRESTClient client){
  18. elclient = client;
  19. }
  20. public boolean delete(){
  21. String in = null;
  22. InputStreamReader isr= null;
  23. BufferedReader inp = null;
  24. try {
  25. isr = new InputStreamReader(System.in);
  26. inp = new BufferedReader(isr);
  27. logWarn("CANCELLAZIONE LIBRO");
  28. Libro l = new Libro();
  29. System.out.println("Codice a barre: ");
  30. l.setBarcode(inp.readLine());
  31. return elclient.delete(l.getBarcode());
  32. }catch(Exception ex){
  33. logErr(ex.getMessage());
  34. }finally{
  35. if(inp!=null){
  36. try {
  37. inp.close();
  38. }catch(Exception e){}
  39. }
  40. if(isr!=null){
  41. try {
  42. isr.close();
  43. }catch(Exception e){}
  44. }
  45. }
  46. return false;
  47. }
  48. public int insert(){
  49. String in = null;
  50. InputStreamReader isr= null;
  51. BufferedReader inp = null;
  52. try {
  53. isr = new InputStreamReader(System.in);
  54. inp = new BufferedReader(isr);
  55. logWarn("INSERIMENTO NUOVO LIBRO");
  56. Libro l = new Libro();
  57. System.out.println("Codice a barre: ");
  58. l.setBarcode(inp.readLine());
  59. Libro lin = elclient.getDetail(l.getBarcode());
  60. if(lin!=null){
  61. logWarn("Libro già presente usa la funzionalità di edit per modificarne i contenuti");
  62. return 1;
  63. }
  64. System.out.println("Editore: ");
  65. l.setEditore(inp.readLine());
  66. System.out.println("Autore: ");
  67. l.setAutore(inp.readLine());
  68. System.out.println("Titolo: ");
  69. l.setTitolo(inp.readLine());
  70. System.out.println("Prezzo [Es 14.00] : ");
  71. l.setPrezzo(Double.parseDouble(inp.readLine()));
  72. System.out.println("Percentuale notra [Es 0.25]: ");
  73. l.setPercentuale(Double.parseDouble(inp.readLine()));
  74. System.out.println("QA: ");
  75. l.setQa(Integer.parseInt(inp.readLine()));
  76. System.out.println("QV: ");
  77. l.setQv(Integer.parseInt(inp.readLine()));
  78. System.out.println("Tags [separati da virgola]: ");
  79. l.setTag(inp.readLine());
  80. //qui occorrerebbe validare
  81. elclient.index(l);
  82. }catch(Exception ex){
  83. logErr(ex.getMessage());
  84. }finally{
  85. if(inp!=null){
  86. try {
  87. inp.close();
  88. }catch(Exception e){}
  89. }
  90. if(isr!=null){
  91. try {
  92. isr.close();
  93. }catch(Exception e){}
  94. }
  95. }
  96. return 0;
  97. }
  98. public int update(){
  99. String in = null;
  100. InputStreamReader isr= null;
  101. BufferedReader inp = null;
  102. try {
  103. isr = new InputStreamReader(System.in);
  104. inp = new BufferedReader(isr);
  105. logWarn("MODIFICA LIBRO (tra parentesi graffe l'attuale valore del campo)");
  106. System.out.println("Codice a barre: ");
  107. Libro l = elclient.getDetail(inp.readLine());
  108. if(l==null){
  109. logWarn("Non eiste alcun libro con il codice a barre inserito");
  110. return 1;
  111. }
  112. String sTmp = null;
  113. System.out.println("Editore {" + l.getEditore() + "}: ");
  114. sTmp = inp.readLine();
  115. l.setEditore(!StringUtils.isEmpty(sTmp)?sTmp:l.getEditore());
  116. System.out.println("Autore {" + l.getAutore() + "}: ");
  117. sTmp = inp.readLine();
  118. l.setAutore(!StringUtils.isEmpty(sTmp)?sTmp:l.getAutore());
  119. System.out.println("Titolo {" + l.getTitolo() + "}: ");
  120. sTmp = inp.readLine();
  121. l.setTitolo(!StringUtils.isEmpty(sTmp)?sTmp:l.getTitolo());
  122. System.out.println("Prezzo [Es 14.00] {" + l.getPrezzo() + "}: ");
  123. sTmp = inp.readLine();
  124. l.setPrezzo(!StringUtils.isEmpty(sTmp)?Double.parseDouble(sTmp):l.getPrezzo());
  125. System.out.println("Percentuale notra [Es 0.25] {" + l.getPercentuale() + "}: ");
  126. sTmp = inp.readLine();
  127. l.setPercentuale(!StringUtils.isEmpty(sTmp)?Double.parseDouble(sTmp):l.getPercentuale());
  128. System.out.println("QA {" + l.getQa() + "}: ");
  129. sTmp = inp.readLine();
  130. l.setQa(!StringUtils.isEmpty(sTmp)?Integer.parseInt(sTmp):l.getQa());
  131. System.out.println("QV {" + l.getQv() + "}: ");
  132. sTmp = inp.readLine();
  133. l.setQv(!StringUtils.isEmpty(sTmp)?Integer.parseInt(sTmp):l.getQv());
  134. System.out.println("Tags [separati da virgola] {"+l.getTag()+"}: ");
  135. sTmp = inp.readLine();
  136. l.setTag(!StringUtils.isEmpty(sTmp)?sTmp:l.getTag());
  137. //qui occorrerebbe validare
  138. elclient.index(l);
  139. }catch(Exception ex){
  140. logErr(ex.getMessage());
  141. }finally{
  142. if(inp!=null){
  143. try {
  144. inp.close();
  145. }catch(Exception e){}
  146. }
  147. if(isr!=null){
  148. try {
  149. isr.close();
  150. }catch(Exception e){}
  151. }
  152. }
  153. return 0;
  154. }
  155. private void logWarn(String msg){
  156. this.logger.warn(" (((");
  157. this.logger.warn(" (@ @)");
  158. this.logger.warn("-----------------------------------ooO--(_)--Ooo--------------------------------");
  159. this.logger.warn("| "+ StringUtils.rightPad(msg, CONSOLE_ROW_LENGHT - 3 - msg.length()) +"|");
  160. this.logger.warn("--------------------------------------------------------------------------------");
  161. }
  162. private void logErr(String msg){
  163. this.logger.error(" §§§");
  164. this.logger.error(" (+ +)");
  165. this.logger.error("-----------------------------------ooO--(_)--Ooo--------------------------------");
  166. this.logger.error("| "+ StringUtils.rightPad(msg, CONSOLE_ROW_LENGHT - 3 - msg.length()) +"|");
  167. this.logger.error("--------------------------------------------------------------------------------");
  168. }
  169. }