PageRenderTime 57ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/src/egats/Data.java

https://github.com/augie/egats
Java | 58 lines | 37 code | 5 blank | 16 comment | 5 complexity | bd005caa1940132a507fcad2592f090c MD5 | raw file
  1. package egats;
  2. import com.google.gson.Gson;
  3. import com.mongodb.DB;
  4. import com.mongodb.DBCollection;
  5. import com.mongodb.Mongo;
  6. import com.mongodb.WriteResult;
  7. /**
  8. * Static variables and methods for interacting with the Mongo database.
  9. * @author Augie Hill - augie@umich.edu
  10. */
  11. public class Data {
  12. private static int TEST_ID = 0;
  13. public static final Gson GSON = new Gson();
  14. public static Mongo MONGO;
  15. public static DB EGATS;
  16. public static DBCollection PROCESSES, OBJECTS, WORKFLOWS;
  17. static {
  18. try {
  19. MONGO = new Mongo("localhost", 27017);
  20. EGATS = MONGO.getDB("egats");
  21. PROCESSES = EGATS.getCollection("egat_processes");
  22. WORKFLOWS = EGATS.getCollection("workflows");
  23. OBJECTS = EGATS.getCollection("objects");
  24. } catch (Exception e) {
  25. e.printStackTrace();
  26. // Log
  27. // TODO
  28. }
  29. }
  30. /**
  31. * Saves the given object to the given database collection.
  32. * @param c
  33. * @param o
  34. * @throws Exception
  35. */
  36. public static void save(DBCollection c, DataObject o) throws Exception {
  37. // Do not do anything if testing
  38. if (Flags.TESTING) {
  39. // Fake an ID if necessary
  40. if (!o.containsField(DataObject.ATTR_ID)) {
  41. o.put(DataObject.ATTR_ID, String.valueOf(TEST_ID++));
  42. }
  43. return;
  44. }
  45. // Save to the database
  46. WriteResult r = c.save(o);
  47. // Check for error
  48. String error = r.getError();
  49. if (error != null && !error.equals("")) {
  50. throw new Exception(error);
  51. }
  52. }
  53. }