PageRenderTime 250ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/javaProject/src/models/Books.java

https://bitbucket.org/ericson_cepeda/repbiblioteca
Java | 173 lines | 145 code | 28 blank | 0 comment | 3 complexity | e4aa96da8946284632a3ffdefb7967e7 MD5 | raw file
  1. package models;
  2. import java.util.ArrayList;
  3. import java.util.Iterator;
  4. import java.util.List;
  5. import com.google.gson.Gson;
  6. import com.google.gson.JsonObject;
  7. import com.mongodb.BasicDBObject;
  8. import com.mongodb.DB;
  9. import com.mongodb.DBCollection;
  10. import com.mongodb.DBObject;
  11. import com.mongodb.util.JSON;
  12. import config.MongoConnector;
  13. import dto.ArXivDTO;
  14. import dto.CoreDTO;
  15. import dto.GoogleDTO;
  16. import dto.IsbnBdDTO;
  17. import dto.OpenDTO;
  18. public class Books
  19. {
  20. private String coleccion;
  21. private CoreDTO core;
  22. private ArXivDTO arxiv;
  23. private GoogleDTO googleDTO;
  24. private IsbnBdDTO isbnBooks;
  25. private OpenDTO openLibrary;
  26. public Books( String coleccion )
  27. {
  28. int cambio = Integer.parseInt( coleccion );
  29. switch( cambio )
  30. {
  31. case BasesConstantes.COREINT:
  32. core = new CoreDTO( );
  33. break;
  34. case BasesConstantes.ARXIVINT:
  35. arxiv = new ArXivDTO( );
  36. break;
  37. case BasesConstantes.GOOGLEBOOKSINT:
  38. googleDTO = new GoogleDTO( );
  39. break;
  40. case BasesConstantes.OPENLIBRARYINT:
  41. openLibrary = new OpenDTO( );
  42. break;
  43. case BasesConstantes.ISBNDTOINT:
  44. isbnBooks = new IsbnBdDTO( );
  45. break;
  46. default:
  47. break;
  48. }
  49. }
  50. public OpenDTO getOpenLibrary( )
  51. {
  52. return openLibrary;
  53. }
  54. public void setOpenLibrary( OpenDTO openLibrary )
  55. {
  56. this.openLibrary = openLibrary;
  57. }
  58. public String getColeccion( )
  59. {
  60. return coleccion;
  61. }
  62. public void setColeccion( String coleccion )
  63. {
  64. this.coleccion = coleccion;
  65. }
  66. public IsbnBdDTO getIsbnBooks( )
  67. {
  68. return isbnBooks;
  69. }
  70. public void setIsbnBooks( IsbnBdDTO isbnBooks )
  71. {
  72. this.isbnBooks = isbnBooks;
  73. }
  74. public GoogleDTO getGoogleBooks( )
  75. {
  76. return googleDTO;
  77. }
  78. public void setGoogleBooks( GoogleDTO googleBooks )
  79. {
  80. this.googleDTO = googleBooks;
  81. }
  82. public CoreDTO getCore( )
  83. {
  84. return core;
  85. }
  86. public void setCore( CoreDTO core )
  87. {
  88. this.core = core;
  89. }
  90. public ArXivDTO getArxiv( )
  91. {
  92. return arxiv;
  93. }
  94. public void setArxiv( ArXivDTO arxiv )
  95. {
  96. this.arxiv = arxiv;
  97. }
  98. public static boolean saveBook( Books newBook )
  99. {
  100. MongoConnector mc = MongoConnector.getInstance( );
  101. DB bdConex = mc.getMongoDB( );
  102. DBCollection booksCollection = bdConex.getCollection( MongoConnector.BOOKS_COLLECTION );
  103. Gson json = new Gson( );
  104. Object obj = JSON.parse( json.toJson( newBook ) );
  105. DBObject objeto = ( DBObject )obj;
  106. booksCollection.insert( objeto );
  107. return objeto.get( "_id" ) != null ? true : false;
  108. }
  109. public static List<JSON> libros( BasicDBObject query )
  110. {
  111. MongoConnector mc = MongoConnector.getInstance( );
  112. DB bdConex = mc.getMongoDB( );
  113. DBCollection booksCollection = bdConex.getCollection( MongoConnector.BOOKS_COLLECTION );
  114. List<JSON> primerosCincuenta = new ArrayList<JSON>( );
  115. List<DBObject> found = booksCollection.find( query ).limit( 50 ).toArray( );
  116. for( Iterator<DBObject> iterator = found.iterator( ); iterator.hasNext( ); )
  117. {
  118. DBObject dbObject = iterator.next( );
  119. primerosCincuenta.add( ( JSON )JSON.parse( dbObject.toString( ) ) );
  120. }
  121. return primerosCincuenta;
  122. }
  123. public static long cantidadLibros( )
  124. {
  125. MongoConnector mc = MongoConnector.getInstance( );
  126. DB bdConex = mc.getMongoDB( );
  127. DBCollection booksCollection = bdConex.getCollection( MongoConnector.BOOKS_COLLECTION );
  128. return booksCollection.count( );
  129. }
  130. public static JsonObject getStatistics(){
  131. MongoConnector mc = MongoConnector.getInstance( );
  132. DB bdConex = mc.getMongoDB( );
  133. DBCollection booksCollection = bdConex.getCollection( MongoConnector.BOOKS_COLLECTION );
  134. BasicDBObject query = new BasicDBObject();
  135. query.put("coleccion", "");
  136. JsonObject response = new JsonObject();
  137. String[] collections = {BasesConstantes.ARXIV,BasesConstantes.CORE,BasesConstantes.GOOGLEBOOKS,BasesConstantes.ISBNDTO,BasesConstantes.OPENLIBRARY};
  138. for( int i = 0; i < collections.length; i++ )
  139. {
  140. query.put("coleccion", collections[i]);
  141. response.addProperty( collections[i], booksCollection.count( query ) );
  142. }
  143. return response;
  144. }
  145. }