List of usage examples for com.mongodb.client MongoCollection insertOne
void insertOne(TDocument document);
From source file:me.lucko.luckperms.common.storage.backing.MongoDBBacking.java
License:Open Source License
@Override public boolean saveUUIDData(String username, UUID uuid) { return call(() -> { MongoCollection<Document> c = database.getCollection("uuid"); try (MongoCursor<Document> cursor = c.find(new Document("_id", uuid)).iterator()) { if (cursor.hasNext()) { c.replaceOne(new Document("_id", uuid), new Document("_id", uuid).append("name", username.toLowerCase())); } else { c.insertOne(new Document("_id", uuid).append("name", username.toLowerCase())); }/*from w w w .j av a2 s . c o m*/ } return true; }, false); }
From source file:me.lucko.luckperms.common.storage.dao.mongodb.MongoDao.java
License:MIT License
@Override public boolean logAction(LogEntry entry) { MongoCollection<Document> c = database.getCollection(prefix + "action"); Document doc = new Document().append("timestamp", entry.getTimestamp()).append("actor", entry.getActor()) .append("actorName", entry.getActorName()) .append("type", Character.toString(entry.getType().getCode())) .append("actedName", entry.getActedName()).append("action", entry.getAction()); if (entry.getActed().isPresent()) { doc.append("acted", entry.getActed().get()); }// www. ja va2 s .c o m c.insertOne(doc); return true; }
From source file:me.lucko.luckperms.common.storage.dao.mongodb.MongoDao.java
License:MIT License
@Override public boolean createAndLoadGroup(String name) { Group group = plugin.getGroupManager().getOrMake(name); group.getIoLock().lock();/*from w ww .j a v a 2s. c o m*/ try { MongoCollection<Document> c = database.getCollection(prefix + "groups"); try (MongoCursor<Document> cursor = c.find(new Document("_id", group.getName())).iterator()) { if (cursor.hasNext()) { Document d = cursor.next(); Set<Node> nodes = nodesFromDoc(d).stream().map(NodeModel::toNode).collect(Collectors.toSet()); group.setEnduringNodes(nodes); } else { c.insertOne(groupToDoc(group)); } } } finally { group.getIoLock().unlock(); } group.getRefreshBuffer().requestDirectly(); return true; }
From source file:me.lucko.luckperms.common.storage.dao.mongodb.MongoDao.java
License:MIT License
@Override public boolean createAndLoadTrack(String name) { Track track = plugin.getTrackManager().getOrMake(name); track.getIoLock().lock();//from w ww .j a va 2 s . c om try { MongoCollection<Document> c = database.getCollection(prefix + "tracks"); try (MongoCursor<Document> cursor = c.find(new Document("_id", track.getName())).iterator()) { if (!cursor.hasNext()) { c.insertOne(trackToDoc(track)); } else { Document d = cursor.next(); //noinspection unchecked track.setGroups((List<String>) d.get("groups")); } } } finally { track.getIoLock().unlock(); } return true; }
From source file:me.lucko.luckperms.storage.methods.MongoDBDatastore.java
License:Open Source License
@Override public boolean saveUser(User user) { if (!plugin.getUserManager().shouldSave(user)) { user.getIoLock().lock();//from ww w . j a v a 2 s. c om try { return call(() -> { MongoCollection<Document> c = database.getCollection("users"); return c.deleteOne(new Document("_id", user.getUuid())).wasAcknowledged(); }, false); } finally { user.getIoLock().unlock(); } } user.getIoLock().lock(); try { return call(() -> { MongoCollection<Document> c = database.getCollection("users"); try (MongoCursor<Document> cursor = c.find(new Document("_id", user.getUuid())).iterator()) { if (!cursor.hasNext()) { c.insertOne(fromUser(user)); } else { c.replaceOne(new Document("_id", user.getUuid()), fromUser(user)); } } return true; }, false); } finally { user.getIoLock().unlock(); } }
From source file:module.CreateStudy.java
License:Open Source License
@SuppressWarnings("unchecked") public CreateStudy() { // ===== Connection ===== MongoClient mongoClient = MongoUtil.buildMongoClient(); MongoDatabase db = mongoClient.getDatabase("epimed_experiments"); // === Excel data loader === String inputfile = this.getInputDirectory() + this.getDirSeparator() + "prolung2_expgrp4.xlsx"; System.out.println("LOADING \t " + inputfile); ExcelService excelService = new ExcelService(); excelService.load(inputfile);/* ww w .j a va2s.c o m*/ List<Object> listCel = excelService.extractColumn(0); Integer indCel = excelService.getHeaderMap().get("gse8894_sample_cel"); // === New Series === MongoCollection<Document> collectionSeries = db.getCollection("series"); Document docSeries = new Document(); docSeries.append("_id", "PROLUNG").append("title", "Lung cancerous and non-cancerous samples") .append("platforms", null).append("submission_date", today).append("last_update", today) .append("import_date", today); UpdateResult updateResult = collectionSeries.updateOne(Filters.eq("_id", docSeries.get("_id")), new Document("$set", docSeries)); if (updateResult.getMatchedCount() == 0) { collectionSeries.insertOne(docSeries); } // === Add samples to new series === MongoCollection<Document> collectionSamples = db.getCollection("samples"); for (int i = 0; i < listCel.size(); i++) { String gsm = this.getGsm(listCel.get(i)); Document docSample = collectionSamples.find(Filters.eq("_id", gsm)).first(); if (docSample == null) { System.err.println("ERROR! Sample " + gsm + "doesn't exist. Try another column."); gsm = this.getGsm(excelService.getData().get(i).get(indCel)); docSample = collectionSamples.find(Filters.eq("_id", gsm)).first(); if (docSample == null) { System.err.println("ERROR! Sample " + gsm + " doesn't exist. Exit."); System.exit(0); } else { System.err.println("Found " + gsm); } } Document expGroup = (Document) docSample.get("exp_group"); setGpl.add(expGroup.get("id_platform").toString()); List<String> listSeries = (List<String>) docSample.get("series"); listSeries.add(docSeries.getString("_id")); docSample.put("series", listSeries); System.out.println(docSample); // updateResult = collectionSamples.updateOne(Filters.eq("_id", docSample.get("_id")), new Document("$set", docSample)); } // === Update platforms of the series === System.out.println(setGpl); docSeries.put("platforms", setGpl); updateResult = collectionSeries.updateOne(Filters.eq("_id", docSeries.get("_id")), new Document("$set", docSeries)); if (updateResult.getMatchedCount() == 0) { collectionSeries.insertOne(docSeries); } }
From source file:module.ImportArrayExpress.java
License:Open Source License
public ImportArrayExpress() { // ===== Connection ===== MongoClient mongoClient = MongoUtil.buildMongoClient(); MongoDatabase db = mongoClient.getDatabase("epimed_experiments"); MongoCollection<Document> collectionSeries = db.getCollection("series"); MongoCollection<Document> collectionSamples = db.getCollection("sample"); // ===== Pattern ===== String patternText = "\\[[\\p{Print}\\p{Space}]+\\]"; ;//from w w w. j a v a 2 s. c o m Pattern pattern = Pattern.compile(patternText); // ===== Series ===== for (String accession : listAccessions) { List<String> accessionAsList = new ArrayList<String>(); accessionAsList.add(accession); String urlString = "https://www.ebi.ac.uk/arrayexpress/files/" + accession + "/" + accession + ".idf.txt"; System.out.println(urlString); String text = webService.loadUrl(urlString); String[] parts = text.split(lineSeparator); List<String> dataSeries = new ArrayList<String>(Arrays.asList(parts)); AESeries series = new AESeries(dataSeries); System.out.println(series); // ===== Check if already imported as a GSE ===== boolean isGseFound = false; String gseNumber = null; for (String secondaryAccession : series.getListAccessions()) { if (secondaryAccession.startsWith("GSE")) { gseNumber = secondaryAccession; Document gse = db.getCollection("series").find(Filters.eq("_id", secondaryAccession)).first(); isGseFound = gse != null; } } int nbImportedSamples = 0; if (!isGseFound) { // ===== Create Mongo series ===== Document docSeries = mongoService.createSeries(accession, series.getTitle(), null, series.getSubmissionDate(), series.getSubmissionDate()); if (series.getListAccessions() != null && !series.getListAccessions().isEmpty()) { docSeries.put("secondary_accessions", series.getListAccessions()); } if (commit) { UpdateResult updateResult = collectionSeries.updateOne(Filters.eq("_id", accession), new Document("$set", docSeries)); if (updateResult.getMatchedCount() == 0) { collectionSeries.insertOne(docSeries); } } System.out.println(docSeries); // ===== Import clinical data ===== String url = "https://www.ebi.ac.uk/arrayexpress/files/" + accession + "/" + series.getSdrf(); System.out.println(url); String clindata = webService.loadUrl(url); String[] clinparts = clindata.split(lineSeparator); List<String> data = new ArrayList<String>(Arrays.asList(clinparts)); // ===== Recognize samples ===== List<String> header = this.createHeader(data.get(0), pattern); System.out.println(header); for (int i = 1; i < data.size(); i++) { Integer nbSamples = data.size() - 1; Map<String, Object> mapParameters = this.createMapParameters(data.get(i), header); String idSample = this.createIdSample(mapParameters); if (idSample == null) { System.err.println("ERROR: idSample is not recongnized for " + accession); System.out.println("Line " + i); System.out.println(mapParameters); mongoClient.close(); System.exit(0); } else { if (formatIdSample) { idSample = accession + "-" + idSample; idSample = idSample.trim().replaceAll(" ", "-"); } } idSample = idSample.split(" ")[0].trim(); // === Organism === String organism = (String) mapParameters.get("organism"); if (organism == null || organism.isEmpty()) { organism = defaultOrganism; } // === Platform === String platform = (String) mapParameters.get("LIBRARY_STRATEGY"); if (platform != null && !platform.isEmpty()) { platform = platform.toLowerCase().trim(); } else { platform = defaultPlatform; } Document docSampleExist = collectionSamples.find(Filters.eq("_id", idSample)).first(); boolean docAlreadyExist = docSampleExist != null; boolean analysed = false; if (docAlreadyExist) { analysed = (Boolean) docSampleExist.get("analyzed"); } // ===== Sample Document ===== Document docSample = mongoService.createSample(idSample, (String) docSeries.get("_id"), accessionAsList, organism, (Date) docSeries.get("submission_date"), (Date) docSeries.get("last_update"), analysed); Document expGroup = null; Document parameters = null; // System.out.println("------------------------------------------------------------------"); if (docAlreadyExist) { // === ID sample alredy exists === System.out.println(i + "/" + nbSamples + "\t " + docSeries.get("_id") + "\t " + idSample + ": already exists in the database, analyzed=" + analysed); expGroup = docSampleExist.get("exp_group", Document.class); parameters = mongoService.updateParameters(docSampleExist, mapParameters); } else { // === New sample === System.out.println(i + "/" + nbSamples + "\t " + docSeries.get("_id") + "\t " + idSample); expGroup = mongoService.createExpGroup(docSample, platform, null, null, organism); parameters = mongoService.createParameters(docSample, mapParameters); nbImportedSamples++; } // === Update sample_title, sample_source, layout === expGroup.put("sample_title", parameters.getString("organism part")); expGroup.put("sample_source", parameters.getString("Source Name")); expGroup.put("layout", parameters.getString("LIBRARY_LAYOUT")); docSample.append("exp_group", expGroup); docSample.append("parameters", parameters); if (commit) { // === Update old if already exist === if (docAlreadyExist) { // collectionSamples.deleteOne(eq("_id", idSample)); collectionSamples.updateOne(Filters.eq("_id", idSample), new Document("$set", docSample)); } else { // ===== Insert data ===== collectionSamples.insertOne(docSample); } // ===== Update series for platforms ===== List<String> listPlatforms = collectionSamples .distinct("exp_group.id_platform", Filters.in("series", accession), String.class) .into(new ArrayList<String>()); docSeries.append("platforms", listPlatforms); collectionSeries.updateOne(Filters.eq("_id", accession), new Document("$set", docSeries)); } } } else { System.out.println("GEO accession " + gseNumber + " corresponding to " + accession + " exists already. Skip import."); } System.out.println("Number of imported samples: " + nbImportedSamples); } mongoClient.close(); }
From source file:module.ImportArrayExpressInit.java
License:Open Source License
public ImportArrayExpressInit() { // ===== Connection ===== MongoClient mongoClient = MongoUtil.buildMongoClient(); MongoDatabase db = mongoClient.getDatabase("epimed_experiments"); MongoCollection<Document> collectionSeries = db.getCollection("series"); MongoCollection<Document> collectionSamples = db.getCollection("sample"); // ===== Pattern ===== String patternText = "\\[[\\p{Print}\\p{Space}]+\\]"; ;/*from ww w.j av a2 s. c om*/ Pattern pattern = Pattern.compile(patternText); // ===== Series ===== for (String accession : listAccessions) { String urlString = "https://www.ebi.ac.uk/arrayexpress/files/" + accession + "/" + accession + ".idf.txt"; System.out.println(urlString); String text = webService.loadUrl(urlString); String[] parts = text.split(lineSeparator); List<String> dataSeries = new ArrayList<String>(Arrays.asList(parts)); AESeries series = new AESeries(dataSeries); System.out.println(series); // ===== Check if already imported as a GSE ===== boolean isGseFound = false; String gseNumber = null; for (String secondaryAccession : series.getListAccessions()) { if (secondaryAccession.startsWith("GSE")) { gseNumber = secondaryAccession; Document gse = db.getCollection("series").find(Filters.eq("_id", secondaryAccession)).first(); isGseFound = gse != null; // System.out.println("GEO accession " + gseNumber + " found: " + isGseFound); } } if (!isGseFound) { // ===== Create Mongo series ===== List<String> listSeriesAcc = new ArrayList<String>(); listSeriesAcc.add(accession); Document docSeries = mongoService.createSeries(accession, series.getTitle(), null, series.getSubmissionDate(), series.getSubmissionDate()); if (series.getListAccessions() != null && !series.getListAccessions().isEmpty()) { listSeriesAcc.addAll(series.getListAccessions()); } docSeries.put("accessions", listSeriesAcc); UpdateResult updateResult = collectionSeries.updateOne(Filters.eq("_id", accession), new Document("$set", docSeries)); if (updateResult.getMatchedCount() == 0) { collectionSeries.insertOne(docSeries); } System.out.println(docSeries); // ===== Import clinical data ===== String url = "https://www.ebi.ac.uk/arrayexpress/files/" + accession + "/" + series.getSdrf(); System.out.println(url); String clindata = webService.loadUrl(url); String[] clinparts = clindata.split(lineSeparator); List<String> data = new ArrayList<String>(Arrays.asList(clinparts)); // ===== Samples ===== List<String> header = this.createHeader(data.get(0), pattern); System.out.println(header); for (int i = 1; i < data.size(); i++) { Integer nbSamples = data.size() - 1; Map<String, Object> mapParameters = this.createParameters(data.get(i), header); String idSample = this.createIdSample(mapParameters); if (idSample == null) { System.err.println("idSample is not recongnized for " + mapParameters); mongoClient.close(); System.exit(0); } String organism = (String) mapParameters.get("organism"); if (organism == null || organism.isEmpty()) { organism = "Homo sapiens"; } String platform = (String) mapParameters.get("LIBRARY_STRATEGY"); if (platform != null && !platform.isEmpty()) { platform = platform.toLowerCase().trim(); } else { platform = "rna-seq"; } String layout = (String) mapParameters.get("LIBRARY_LAYOUT"); if (layout != null && !layout.isEmpty()) { layout = layout.toLowerCase().trim(); } Document docSampleExist = collectionSamples.find(Filters.eq("_id", idSample)).first(); boolean docAlreadyExist = docSampleExist != null; boolean analysed = false; if (docAlreadyExist) { analysed = (Boolean) docSampleExist.get("analyzed"); System.out.println(i + "/" + nbSamples + "\t " + docSeries.get("_id") + "\t " + idSample + ": already exists in the database, analyzed=" + analysed); } else { System.out.println(i + "/" + nbSamples + "\t " + docSeries.get("_id") + "\t " + idSample); } // ===== Sample Document ===== Document docSample = mongoService.createSample(idSample, (String) docSeries.get("_id"), listSeriesAcc, organism, (Date) docSeries.get("submission_date"), (Date) docSeries.get("last_update"), analysed); // ===== Mandatory parameters ===== // Preserve "exp_group" if the document exists already Document expGroup = null; if (docAlreadyExist) { expGroup = (Document) docSampleExist.get("exp_group"); } else { expGroup = mongoService.createExpGroup(docSample, platform, (String) mapParameters.get("organism part"), (String) mapParameters.get("Source Name"), organism); if (layout != null) { expGroup.append("layout", layout); // run_name int j = 0; boolean isFound = false; String runName = null; while (!isFound && j < listRunNameParameters.length) { runName = (String) mapParameters.get(listRunNameParameters[j]); isFound = runName != null; j++; } if (runName != null) { expGroup.append("run_name", runName); } } } docSample.append("exp_group", expGroup); // ===== Supplementary parameters ===== Document parameters = mongoService.createParameters(docSample, mapParameters); docSample.append("parameters", parameters); // === Delete if already exist === collectionSamples.deleteOne(eq("_id", idSample)); // ===== Insert data ===== collectionSamples.insertOne(docSample); // ===== Update series for platforms ===== List<String> listPlatforms = collectionSamples .distinct("exp_group.id_platform", Filters.in("series", accession), String.class) .into(new ArrayList<String>()); docSeries.append("platforms", listPlatforms); collectionSeries.updateOne(Filters.eq("_id", accession), new Document("$set", docSeries)); } } else { System.out.println("GEO accession " + gseNumber + " corresponding to " + accession + " exists already. Skip import."); } } mongoClient.close(); }
From source file:module.ImportGeo.java
License:Open Source License
public ImportGeo() { // ===== Connection ===== MongoClient mongoClient = MongoUtil.buildMongoClient(); MongoDatabase db = mongoClient.getDatabase("epimed_experiments"); // ===== Insert data ===== for (int k = 0; k < listGseNumber.length; k++) { String gseNumber = listGseNumber[k]; System.out.println("------------------------------------------"); System.out.println(k + " Import " + gseNumber); // ===== Load GSE ===== NcbiGeoGse gse = new NcbiGeoGse(webService.loadGeo(gseNumber)); System.out.println(gse);//from w ww . j av a 2 s . c o m // ===== Series ===== MongoCollection<Document> collectionSeries = db.getCollection("series"); Document docSeries = mongoService.createSeries(gse.getGseNumber(), gse.getTitle(), gse.getListGpl(), gse.getSubmissionDate(), gse.getLastUpdate()); UpdateResult updateResult = collectionSeries.updateOne(Filters.eq("_id", gse.getGseNumber()), new Document("$set", docSeries)); if (updateResult.getMatchedCount() == 0) { collectionSeries.insertOne(docSeries); } // ===== Platforms ===== MongoCollection<Document> collectionPlatforms = db.getCollection("platform"); for (int i = 0; i < gse.getListGpl().size(); i++) { NcbiGeoGpl gpl = new NcbiGeoGpl(webService.loadGeo(gse.getListGpl().get(i))); System.out.println("\t Import platform " + gpl.getGplNumber()); Document docPlatforms = mongoService.createPlatform(gpl.getGplNumber(), gpl.getTitle(), gpl.getTaxid(), gpl.getOrganism(), gpl.getManufacturer(), gpl.getSubmissionDate(), gpl.getLastUpdate(), gpl.getTechnology()); UpdateResult res = collectionPlatforms.updateOne(Filters.eq("_id", gpl.getGplNumber()), new Document("$set", docPlatforms)); if (res.getMatchedCount() == 0) { collectionPlatforms.insertOne(docPlatforms); } } // ===== Samples ====== MongoCollection<Document> collectionSamples = db.getCollection("sample"); // for (int i=0; i<1; i++) { for (int i = 0; i < gse.getListGsm().size(); i++) { NcbiGeoGsm gsm = new NcbiGeoGsm(webService.loadGeo(gse.getListGsm().get(i))); Document docSampleExist = collectionSamples.find(Filters.eq("_id", gsm.getGsmNumber())).first(); boolean docAlreadyExist = docSampleExist != null; boolean analysed = false; if (docAlreadyExist) { analysed = (Boolean) docSampleExist.get("analyzed"); System.out.println(i + "/" + gse.getListGsm().size() + "\t " + gse.getGseNumber() + "\t " + gsm.getGsmNumber() + ": already exists in the database, analyzed=" + analysed); } else { System.out.println(i + "/" + gse.getListGsm().size() + "\t " + gse.getGseNumber() + "\t " + gsm.getGsmNumber()); } // ===== Sample Document ===== Document docSample = mongoService.createSample(gsm.getGsmNumber(), gse.getGseNumber(), gsm.getListGse(), gsm.getOrganism(), gsm.getSubmissionDate(), gsm.getLastUpdate(), analysed); // ===== Mandatory parameters ===== // Preserve "exp_group" if the document exists already Document expGroup = null; if (docAlreadyExist) { expGroup = (Document) docSampleExist.get("exp_group"); } else { expGroup = mongoService.createExpGroup(docSample, gsm.getGplNumber(), gsm.getTitle(), gsm.getSourceName(), gsm.getOrganism()); } docSample.append("exp_group", expGroup); // ===== Supplementary parameters ===== Document parameters = generateParameters(gsm); docSample.append("parameters", parameters); // === Delete if already exist === collectionSamples.deleteOne(eq("_id", gsm.getGsmNumber())); // ===== Insert data ===== collectionSamples.insertOne(docSample); } } mongoClient.close(); }
From source file:module.script.emtab365.CreateSeriesEMTAB365.java
License:Open Source License
@SuppressWarnings({ "unchecked" }) public CreateSeriesEMTAB365() { // ===== Session Mongo ===== MongoClient mongoClient = MongoUtil.buildMongoClient(); MongoDatabase db = mongoClient.getDatabase("epimed_experiments"); // MongoDatabase db = mongoClient.getDatabase("geo"); MongoCollection<Document> collectionSeries = db.getCollection("series"); Date submissionDate = null;//from w w w . j a v a2 s .c om Date lastUpdate = null; try { submissionDate = dateFormat.parse("2011-09-15"); lastUpdate = dateFormat.parse("2014-05-03"); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } List<String> platforms = new ArrayList<String>(); platforms.add("GPL570"); Document docSeries = new Document(); docSeries.append("_id", "E-MTAB-365") .append("title", "Transcription profiling by array of breast cancer samples to define breast cancer subsets") .append("platforms", platforms).append("submission_date", submissionDate) .append("last_update", lastUpdate).append("import_date", new Date()); System.out.println(docSeries); collectionSeries.insertOne(docSeries); mongoClient.close(); }