PageRenderTime 50ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/storage/src/main/java/jahspotify/storage/media/MongoDBMediaStorage.java

http://github.com/johanlindquist/jahspotify
Java | 211 lines | 170 code | 38 blank | 3 comment | 14 complexity | 279a4016b37016458315fd339ced05b4 MD5 | raw file
  1. package jahspotify.storage.media;
  2. import javax.annotation.*;
  3. import com.google.gson.Gson;
  4. import com.mongodb.*;
  5. import com.mongodb.util.JSON;
  6. import jahspotify.media.*;
  7. import org.apache.commons.logging.*;
  8. import org.springframework.beans.factory.annotation.*;
  9. import org.springframework.stereotype.Service;
  10. /**
  11. * @author Johan Lindquist
  12. */
  13. @Service
  14. @Qualifier(value = "mongodb")
  15. public class MongoDBMediaStorage implements MediaStorage
  16. {
  17. private Log _log = LogFactory.getLog(MongoDBMediaStorage.class);
  18. @Value(value = "${jahspotify.storage.mongodb.host}")
  19. private String _dbHost = "localhost";
  20. @Value(value = "${jahspotify.storage.mongodb.port}")
  21. private int _dbPort = 27017;
  22. @Value(value = "${jahspotify.storage.mongodb.db-name}")
  23. private String _dbName = "JahSpotify";
  24. private Mongo _mongoDBInstance;
  25. private DB _db;
  26. @PreDestroy
  27. public void shutdown()
  28. {
  29. if (_mongoDBInstance != null)
  30. {
  31. _log.debug("Shutting down Mongo DB instance");
  32. _mongoDBInstance.close();
  33. _mongoDBInstance = null;
  34. }
  35. }
  36. @PostConstruct
  37. public void initialize()
  38. {
  39. try
  40. {
  41. _log.debug("Initializing Mongo DB instance");
  42. _mongoDBInstance = new Mongo();
  43. _db = _mongoDBInstance.getDB(_dbName);
  44. }
  45. catch (Exception e)
  46. {
  47. }
  48. }
  49. @Override
  50. public void store(final Track track)
  51. {
  52. final DBCollection tracks = _db.getCollection("tracks");
  53. final BasicDBObject basicDBObject = (BasicDBObject) JSON.parse(new Gson().toJson(track));
  54. basicDBObject.put("_id", track.getId().getId());
  55. final WriteResult insert = tracks.insert(basicDBObject);
  56. }
  57. @Override
  58. public Track readTrack(final Link uri)
  59. {
  60. final DBCollection tracks = _db.getCollection("tracks");
  61. final BasicDBObject query = new BasicDBObject();
  62. query.put("_id", uri.getId());
  63. final DBObject one = tracks.findOne(query);
  64. if (one != null)
  65. {
  66. return new Gson().fromJson(JSON.serialize(one), Track.class);
  67. }
  68. return null;
  69. }
  70. @Override
  71. public void deleteTrack(final Link uri)
  72. {
  73. final DBCollection tracks = _db.getCollection("tracks");
  74. BasicDBObject query = new BasicDBObject();
  75. query.put("_id", uri.getId());
  76. final DBObject andRemove = tracks.findAndRemove(query);
  77. }
  78. @Override
  79. public void store(final Artist artist)
  80. {
  81. final DBCollection tracks = _db.getCollection("artists");
  82. final BasicDBObject basicDBObject = (BasicDBObject) JSON.parse(new Gson().toJson(artist));
  83. basicDBObject.put("_id", artist.getId().getId());
  84. final WriteResult insert = tracks.insert(basicDBObject);
  85. }
  86. @Override
  87. public Artist readArtist(final Link uri)
  88. {
  89. final DBCollection tracks = _db.getCollection("artists");
  90. final BasicDBObject query = new BasicDBObject();
  91. query.put("_id", uri.getId());
  92. final DBObject one = tracks.findOne(query);
  93. if (one != null)
  94. {
  95. return new Gson().fromJson(JSON.serialize(one), Artist.class);
  96. }
  97. return null;
  98. }
  99. @Override
  100. public void store(final Album album)
  101. {
  102. final DBCollection tracks = _db.getCollection("albums");
  103. final BasicDBObject basicDBObject = (BasicDBObject) JSON.parse(new Gson().toJson(album));
  104. basicDBObject.put("_id", album.getId().getId());
  105. final WriteResult insert = tracks.insert(basicDBObject);
  106. }
  107. @Override
  108. public Album readAlbum(final Link uri)
  109. {
  110. final DBCollection tracks = _db.getCollection("albums");
  111. final BasicDBObject query = new BasicDBObject();
  112. query.put("_id", uri.getId());
  113. final DBObject one = tracks.findOne(query);
  114. if (one != null)
  115. {
  116. return new Gson().fromJson(JSON.serialize(one), Album.class);
  117. }
  118. return null;
  119. }
  120. @Override
  121. public void store(final Playlist playlist)
  122. {
  123. final DBCollection tracks = _db.getCollection("playlists");
  124. final BasicDBObject basicDBObject = (BasicDBObject) JSON.parse(new Gson().toJson(playlist));
  125. if (playlist.getId() != null)
  126. {
  127. basicDBObject.put("_id", playlist.getId().getId());
  128. final WriteResult insert = tracks.insert(basicDBObject);
  129. }
  130. }
  131. @Override
  132. public Playlist readPlaylist(final Link uri)
  133. {
  134. final DBCollection tracks = _db.getCollection("playlists");
  135. final BasicDBObject query = new BasicDBObject();
  136. query.put("_id", uri.getId());
  137. final DBObject one = tracks.findOne(query);
  138. if (one != null)
  139. {
  140. return new Gson().fromJson(JSON.serialize(one), Playlist.class);
  141. }
  142. return null;
  143. }
  144. @Override
  145. public void store(final Image image)
  146. {
  147. final DBCollection tracks = _db.getCollection("images");
  148. final BasicDBObject basicDBObject = (BasicDBObject) JSON.parse(new Gson().toJson(image));
  149. basicDBObject.put("_id", image.getId().getId());
  150. final WriteResult insert = tracks.insert(basicDBObject);
  151. }
  152. @Override
  153. public Image readImage(final Link uri)
  154. {
  155. final DBCollection tracks = _db.getCollection("images");
  156. final BasicDBObject query = new BasicDBObject();
  157. query.put("_id", uri.getId());
  158. final DBObject one = tracks.findOne(query);
  159. if (one != null)
  160. {
  161. return new Gson().fromJson(JSON.serialize(one), Image.class);
  162. }
  163. return null;
  164. }
  165. @Override
  166. public void deletePlaylist(final Link uri)
  167. {
  168. final DBCollection tracks = _db.getCollection("playlists");
  169. BasicDBObject query = new BasicDBObject();
  170. query.put("_id", uri.getId());
  171. final DBObject andRemove = tracks.findAndRemove(query);
  172. }
  173. }