PageRenderTime 247ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/javaProject/SerializationBiblio/src/controllers/IndexController.java

https://bitbucket.org/ericson_cepeda/repbiblioteca
Java | 69 lines | 55 code | 13 blank | 1 comment | 1 complexity | 06e9884de7b99c39cd07f29e1e2c1ec7 MD5 | raw file
  1. package controllers;
  2. import java.util.Iterator;
  3. import java.util.List;
  4. import models.IndexLibrary;
  5. import world.BibliotecaTDO;
  6. import world.DeweyClassification;
  7. import world.Serializacion;
  8. import com.google.gson.Gson;
  9. import com.google.gson.JsonObject;
  10. import com.mongodb.BasicDBObject;
  11. import com.mongodb.DBObject;
  12. import com.mongodb.util.JSON;
  13. public class IndexController
  14. {
  15. public static String getBook(String index){
  16. Serializacion ser = Serializacion.getInstance( );
  17. return ser.getGsonBiblioteca( ).toJson(ser.getObjetoBiblioteca().get(Integer.parseInt(index)));
  18. }
  19. public static String getStatistics(){
  20. Serializacion ser = Serializacion.getInstance( );
  21. JsonObject response = new JsonObject( );
  22. response.addProperty( "booksQuantity", ""+ser.getObjetoBiblioteca( ).size( ) );
  23. response.addProperty( "indexUniandes", ""+IndexLibrary.countIndexLibrary() );
  24. response.addProperty( "currentBook", ""+ser.getCurrentPosition() );
  25. return response.toString();
  26. }
  27. public static String getBookByTitle(String title){
  28. BasicDBObject responseObject = new BasicDBObject();
  29. Gson gson = Serializacion.getInstance().getGsonBiblioteca();
  30. List<DBObject> libraryIndexed = IndexLibrary.getIndexBookByTitle(title);
  31. for( Iterator<DBObject> iterator = libraryIndexed.iterator( ); iterator.hasNext( ); )
  32. {
  33. BasicDBObject bookObject = new BasicDBObject();
  34. DBObject dbObject = iterator.next( );
  35. bookObject.put("libraryHashPosition", dbObject);
  36. BibliotecaTDO bookDTO = Serializacion.getInstance().getAtPosition(Integer.parseInt((String)dbObject.get("position")));
  37. bookObject.put("libraryBook", JSON.parse(gson.toJson(bookDTO)));
  38. bookObject.put("deweyInfo", IndexLibrary.getDeweyCategories(bookDTO.getClasificacionDewey()));
  39. bookObject.put("collectionBooks", IndexLibrary.getBookByTitleAndAuthor(bookDTO));
  40. responseObject.put((String)dbObject.get("position"), bookObject);
  41. }
  42. return JSON.serialize(responseObject);
  43. }
  44. public static String cleanVariables(){
  45. Serializacion ser = Serializacion.getInstance( );
  46. ser.borrarvariables();
  47. System.gc();
  48. JsonObject response = new JsonObject( );
  49. //response.addProperty( "indexUniandes", ""+ser.getObjetoBiblioteca( ).size( ) );
  50. return response.toString();
  51. }
  52. public static String getNextBook(){
  53. Serializacion ser = Serializacion.getInstance( );
  54. return ser.darSiguiente();
  55. }
  56. }