Java tutorial
package no.nlf.avvik.melwinSOAPconnection; import java.net.UnknownHostException; import java.util.ArrayList; import com.mongodb.BasicDBList; import com.mongodb.BasicDBObject; import com.mongodb.DB; import com.mongodb.DBCollection; import com.mongodb.DBCursor; import com.mongodb.DBObject; import com.mongodb.MongoClient; public class MongoOperations { private DB db; public MongoOperations() { db = null; try { db = (new MongoClient("localhost", 27017)).getDB("fnlfAnomaliesDb"); } catch (UnknownHostException e) { e.printStackTrace(); System.out.println("Could not connect. " + e.toString()); } if (db == null) { System.out.println("Ingen db"); } } public boolean isDbEmpty() { DBCollection dbCollectionJumpers = db.getCollection("jumpers"); return checkIfJumpersCollectionIsEmpty(dbCollectionJumpers); } /** * * @return */ public ArrayList<String> getMelwinAdmin() { ArrayList<String> melwinAdmin = new ArrayList<>(); DBCollection dbCollectionAdmin = db.getCollection("admin"); DBObject admin = dbCollectionAdmin.findOne(); melwinAdmin.add((String) admin.get("melwinID")); melwinAdmin.add((String) admin.get("melwinKey")); return melwinAdmin; } /** * * @return */ public ArrayList<Club> getClubsFromDb() { ArrayList<Club> clubs = new ArrayList<>(); DBCollection dbCollectionClubs = db.getCollection("clubs"); DBCursor cursor = dbCollectionClubs.find(); BasicDBObject mongoObject = new BasicDBObject(); if (cursor == null) System.out.println("Cursor er null"); try { while (cursor.hasNext()) { mongoObject = (BasicDBObject) cursor.next(); clubs.add(new Club(mongoObject.getObjectId("_id"), mongoObject.getInt("id"), mongoObject.getString("melwinId"), mongoObject.getString("name"), mongoObject.getString("chiefInstructorMelwinId"))); } } finally { cursor.close(); } return clubs; } /** * * @return */ public ArrayList<License> getLicensesFromDb() { ArrayList<License> licenses = new ArrayList<>(); DBCollection dbCollectionLicenses = db.getCollection("licenses"); DBCursor cursor = dbCollectionLicenses.find(); BasicDBObject mongoObject = new BasicDBObject(); if (cursor == null) System.out.println("Cursor er null, licenses"); try { while (cursor.hasNext()) { mongoObject = (BasicDBObject) cursor.next(); licenses.add(new License(mongoObject.getObjectId("_id"), mongoObject.getInt("id"), mongoObject.getString("melwinId"), mongoObject.getString("licenseName"), null, null)); } } finally { cursor.close(); } return licenses; } /** * * @return */ private boolean checkIfJumpersCollectionIsEmpty(DBCollection dbCollection) { if (dbCollection.getCount() == 0) { return true; } return false; } /** * * @param parachutists * @return */ public void addJumpers(ArrayList<Parachutist> parachutists) { DBCollection dbCollectionParachutists = db.getCollection("jumpers"); dbCollectionParachutists.drop(); setParachutistCounter(1); int counter; for (Parachutist parachutist : parachutists) { ArrayList<Integer> memberLicenses = new ArrayList<>(); for (License license : parachutist.getLicenses()) { memberLicenses.add(new Integer(license.getId())); } ArrayList<Integer> memberClubs = new ArrayList<>(); for (Club club : parachutist.getMemberclubs()) { memberClubs.add(new Integer(club.getId())); } counter = getParachutistCounter(); BasicDBObject parachutistMongoObject = new BasicDBObject("melwinId", parachutist.getMelwinId()) .append("_class", "no.nlf.models.mongoclasses.MongoParachutist") .append("memberclubs", memberClubs).append("licenses", memberLicenses) .append("firstname", parachutist.getFirstname()).append("lastname", parachutist.getLastname()) .append("id", counter).append("birthdate", parachutist.getBirthdate()) .append("gender", parachutist.getGender()).append("street", parachutist.getStreet()) .append("postnumber", parachutist.getPostnumber()) .append("postplace", parachutist.getPostplace()).append("mail", parachutist.getMail()) .append("phone", parachutist.getPhone()); dbCollectionParachutists.save(parachutistMongoObject); } } /** * * @param parachutistsFromMelwin */ public ArrayList<Parachutist> getParachutistsFromDB() { // HjelpeLister ArrayList<Club> clubs = getClubsFromDb(); ArrayList<License> licenses = getLicensesFromDb(); ArrayList<Parachutist> parachutistsInMongoDB = new ArrayList<>(); DBCollection dbCollectionParachutists = db.getCollection("jumpers"); DBCursor cursor = dbCollectionParachutists.find(); BasicDBObject mongoObject = new BasicDBObject(); try { int hoppteller = 0; while (cursor.hasNext()) { mongoObject = (BasicDBObject) cursor.next(); hoppteller++; ArrayList<Club> memberClubs = new ArrayList<>(); ArrayList<License> memberLicenses = new ArrayList<>(); BasicDBList referenceClubs = (BasicDBList) mongoObject.get("memberclubs"); BasicDBList referenceLicenses = (BasicDBList) mongoObject.get("licenses"); for (Object clubReference : referenceClubs) { for (Club club : clubs) { if ((int) clubReference == club.getId()) { memberClubs.add(club); } } } for (Object licenseReference : referenceLicenses) { for (License license : licenses) { if ((int) licenseReference == license.getId()) { memberLicenses.add(license); } } } Parachutist parachutist = new Parachutist(mongoObject.getObjectId("_id"), mongoObject.getInt("id"), mongoObject.getString("melwinId"), null, // nakKey new ArrayList<Club>(), // clubs new ArrayList<License>(), // licenses mongoObject.getString("firstname"), mongoObject.getString("lastname"), mongoObject.getDate("birthdate"), mongoObject.getString("gender"), mongoObject.getString("street"), mongoObject.getString("postnumber"), mongoObject.getString("postplace"), mongoObject.getString("mail"), mongoObject.getString("phone"), mongoObject.getString("password")); parachutist.setMemberclubs(memberClubs); parachutist.setLicenses(memberLicenses); parachutistsInMongoDB.add(parachutist); } } finally { cursor.close(); } return parachutistsInMongoDB; } public void updateParachutistsInDB(ArrayList<Parachutist> parachutists) { ArrayList<Parachutist> parachutistsInDB = getParachutistsFromDB(); boolean isParachutistInDB = false; int test = 0; for (Parachutist parachutist : parachutists) { for (Parachutist parachutistInDB : parachutistsInDB) { if (parachutist.getMelwinId().equals(parachutistInDB.getMelwinId())) { isParachutistInDB = true; if (!parachutist.equalsTest(parachutistInDB)) { updateJumperinDb(parachutist); test++; } } } if (!isParachutistInDB) { addJumperToDb(parachutist); isParachutistInDB = false; } } System.out.println("Antall oppdateringer: " + test); } /** * Saves a single Parachuteobject to mongoDB * * @param parachutist */ public void addJumperToDb(Parachutist parachutist) { DBCollection dbCollectionParachutistsCount = db.getCollection("jumpercounter"); DBCollection dbCollectionParachutists = db.getCollection("jumpers"); BasicDBObject incrementJumpcount = new BasicDBObject().append("$inc", new BasicDBObject().append("seq", 1)); BasicDBObject query = new BasicDBObject("_id", "jumperCount"); int countInt = (int) dbCollectionParachutistsCount.findOne().get("seq"); ArrayList<Integer> memberLicenses = new ArrayList<>(); for (License license : parachutist.getLicenses()) { memberLicenses.add(license.getId()); } ArrayList<Integer> memberClubs = new ArrayList<>(); for (Club club : parachutist.getMemberclubs()) { memberClubs.add(club.getId()); } dbCollectionParachutistsCount.update(query, incrementJumpcount); BasicDBObject parachutistMongoObject = new BasicDBObject("melwinId", parachutist.getMelwinId()) .append("_class", "no.nlf.models.mongoclasses.MongoParachutist").append("memberclubs", memberClubs) .append("licenses", memberLicenses).append("firstname", parachutist.getFirstname()) .append("lastname", parachutist.getLastname()).append("id", countInt + 1) .append("birthdate", parachutist.getBirthdate()).append("gender", parachutist.getGender()) .append("street", parachutist.getStreet()).append("postnumber", parachutist.getPostnumber()) .append("postplace", parachutist.getPostplace()).append("mail", parachutist.getMail()) .append("phone", parachutist.getPhone()); dbCollectionParachutists.save(parachutistMongoObject); } public void updateJumperinDb(Parachutist parachutist) { DBCollection dbCollectionParachutists = db.getCollection("jumpers"); ArrayList<Integer> memberLicenses = new ArrayList<>(); for (License license : parachutist.getLicenses()) { memberLicenses.add(license.getId()); } ArrayList<Integer> memberClubs = new ArrayList<>(); for (Club club : parachutist.getMemberclubs()) { memberClubs.add(club.getId()); } BasicDBObject mongojumper = new BasicDBObject("melwinId", parachutist.getMelwinId()) .append("memberclubs", memberClubs).append("id", parachutist.getId()) .append("licenses", memberLicenses).append("firstname", parachutist.getFirstname()) .append("lastname", parachutist.getLastname()).append("birthdate", parachutist.getBirthdate()) .append("gender", parachutist.getGender()).append("street", parachutist.getStreet()) .append("postnumber", parachutist.getPostnumber()).append("postplace", parachutist.getPostplace()) .append("mail", parachutist.getMail()).append("phone", parachutist.getPhone()); BasicDBObject searchQuery = new BasicDBObject().append("id", parachutist.getId()); dbCollectionParachutists.update(searchQuery, mongojumper); } public boolean isParachutistsEqual(Parachutist parachutist1, Parachutist parachutist2) { return parachutist1.equalsTest(parachutist2); } public void setParachutistCounter(int number) { DBCollection counterDbCollection = db.getCollection("counters"); BasicDBObject query = new BasicDBObject("_id", "jumpers"); BasicDBObject increment = new BasicDBObject().append("seq", number); counterDbCollection.findAndModify(query, increment); } public int getParachutistCounter() { DBCollection counterDbCollection = db.getCollection("counters"); BasicDBObject query = new BasicDBObject("_id", "jumpers"); BasicDBObject increment = new BasicDBObject().append("$inc", new BasicDBObject().append("seq", 1)); BasicDBObject counterDBObject = (BasicDBObject) counterDbCollection.findAndModify(query, increment); int seq = (int) counterDBObject.getInt("seq"); return seq; } }