Example usage for com.mongodb DBCollection insert

List of usage examples for com.mongodb DBCollection insert

Introduction

In this page you can find the example usage for com.mongodb DBCollection insert.

Prototype

public WriteResult insert(final List<? extends DBObject> documents) 

Source Link

Document

Insert documents into a collection.

Usage

From source file:org.netbeans.modules.mongodb.ui.windows.collectionview.actions.AddDocumentAction.java

License:Open Source License

@Override
public void actionPerformed(ActionEvent e) {
    final DBObject document = JsonUI.showEditor(Bundle.addDocumentTitle(), "{}");
    if (document != null) {
        try {/* w ww  . ja va  2s .c o  m*/
            final DBCollection dbCollection = getView().getLookup().lookup(DBCollection.class);
            dbCollection.insert(document);
            getView().refreshResults();
        } catch (MongoException ex) {
            DialogDisplayer.getDefault().notify(
                    new NotifyDescriptor.Message(ex.getLocalizedMessage(), NotifyDescriptor.ERROR_MESSAGE));
        }
    }
}

From source file:org.opendaylight.controller.samples.onftappingapp.TappingApp.java

License:Apache License

public void addTapPolicy(TapPolicy tapPolicy) throws DuplicateEntryException {

    // Check whether there is an existing policy with the same name
    if (tapPolicyExistsByName(tapPolicy.getName()))
        throw new DuplicateEntryException();

    // Get the tap policy table
    DBCollection table = database.getCollection(DatabaseNames.getTapPolicyTableName());

    // Create a document containing the new tap policy
    BasicDBObject document = tapPolicy.getAsDocument();

    // Add the new tap policy document to the tap policy table
    table.insert(document);

    // Get the object ID from mongo and update it in the tap policy object
    ObjectId objectId = document.getObjectId("_id");
    tapPolicy.setObjectId(objectId.toString());

    logger.info("Added tap policy " + tapPolicy.getName() + " object ID " + tapPolicy.getObjectId());

    // Notify the application that the tap policy changed
    // tappingAppLogic.NotifyConfigChange(DBTableChangeEnum.TAP_POLICY, tapPolicy.getObjectId());
}

From source file:org.opendaylight.controller.samples.onftappingapp.TappingApp.java

License:Apache License

public void addMatchCriteria(MatchCriteria matchCriteria) throws DuplicateEntryException {

    // Check whether there is an existing match criteria with the same name
    if (matchCriteriaExistsByName(matchCriteria.getName()))
        throw new DuplicateEntryException();

    // Get the match criteria table
    DBCollection table = database.getCollection(DatabaseNames.getMatchCriteriaTableName());

    // Create a document containing the new match criteria
    BasicDBObject document = matchCriteria.getAsDocument();

    // Add the new tap policy document to the MatchCriteria table
    table.insert(document);

    // Get the object ID from mongo and update it in the MatchCriteria object
    ObjectId objectId = document.getObjectId("_id");
    matchCriteria.setObjectId(objectId.toString());

    logger.info(/*from   w  w  w .  ja va 2  s . c om*/
            "Added match criteria " + matchCriteria.getName() + " object ID " + matchCriteria.getObjectId());

    // tappingAppLogic.NotifyConfigChange(DBTableChangeEnum.MATCH_CRITERIA, matchCriteria.getObjectId());
}

From source file:org.opendaylight.controller.samples.onftappingapp.TappingApp.java

License:Apache License

public void addSwitchEntry(SwitchEntry switchEntry) throws DuplicateEntryException, NotSupportedException {

    // We aren't supporting this mode yet
    if (switchEntry.getTappingMode() == TappingModeEnum.NORMAL) {
        throw new NotSupportedException("Normal Mode is not suppored");
    }/*from   w  ww . ja v  a 2 s.  co m*/

    // Check whether there is an existing switch entry with the same name
    if (switchEntryExistsByName(switchEntry.getName()))
        throw new DuplicateEntryException();

    // Get the match criteria table
    DBCollection table = database.getCollection(DatabaseNames.getSwitchEntryTableName());

    // Create a document containing the new match criteria
    BasicDBObject document = switchEntry.getAsDocument();

    // Add the new tap policy document to the tap policy table
    table.insert(document);

    // Get the object ID from mongo and update it in the switch entry object
    ObjectId objectId = document.getObjectId("_id");
    switchEntry.setObjectId(objectId.toString());

    logger.info("Added Switch Entry " + switchEntry.getName() + " object ID " + switchEntry.getObjectId());
}

From source file:org.opendaylight.controller.samples.onftappingapp.TappingApp.java

License:Apache License

public void addNextHopSwitch(NextHopSwitch nextHopSwitch) throws DuplicateEntryException {

    // Check whether there is an existing next hop switch with the same name
    if (nextHopSwitchExistsByName(nextHopSwitch.getName()))
        throw new DuplicateEntryException();

    // Get the match criteria table
    DBCollection table = database.getCollection(DatabaseNames.getNextHopSwitchTableName());

    // Create a document containing the new match criteria
    BasicDBObject document = nextHopSwitch.getAsDocument();

    // Add the new tap policy document to the tap policy table
    table.insert(document);

    // Get the object ID from mongo and update it in the switch entry object
    ObjectId objectId = document.getObjectId("_id");
    nextHopSwitch.setObjectId(objectId.toString());

    logger.info(/*from   w  ww  .ja v a2s. co  m*/
            "Added Next Hop Switch " + nextHopSwitch.getName() + " object ID " + nextHopSwitch.getObjectId());
}

From source file:org.opendaylight.controller.samples.onftappingapp.TappingApp.java

License:Apache License

public void addCaptureDevice(CaptureDev captureDev) throws DuplicateEntryException {

    // Check whether there is an existing capture device with the same name
    if (captureDeviceExistsByName(captureDev.getName()))
        throw new DuplicateEntryException();

    // Get the match criteria table
    DBCollection table = database.getCollection(DatabaseNames.getCaptureDevTableName());

    // Create a document containing the new match criteria
    BasicDBObject document = captureDev.getAsDocument();

    // Add the new tap policy document to the tap policy table
    table.insert(document);

    // Get the object ID from mongo and update it in the switch entry object
    ObjectId objectId = document.getObjectId("_id");
    captureDev.setObjectId(objectId.toString());

    logger.info("Added Next Hop Switch " + captureDev.getName() + " object ID " + captureDev.getObjectId());
}

From source file:org.opendaylight.controller.samples.onftappingapp.TappingApp.java

License:Apache License

public void addPortChain(PortChain portChain) throws DuplicateEntryException {

    // Check whether there is an existing port chain with the same name
    if (portChainExistsByName(portChain.getName()))
        throw new DuplicateEntryException();

    // Get the match criteria table
    DBCollection table = database.getCollection(DatabaseNames.getPortChainTableName());

    // Create a document containing the new match criteria
    BasicDBObject document = portChain.getAsDocument();

    // Add the new tap policy document to the tap policy table
    table.insert(document);

    // Get the object ID from mongo and update it in the switch entry object
    ObjectId objectId = document.getObjectId("_id");
    portChain.setObjectId(objectId.toString());

    logger.info("Added Port Chain " + portChain.getName() + " object ID " + portChain.getObjectId());
}

From source file:org.opentaps.notes.repository.impl.NoteRepositoryImpl.java

License:Open Source License

/** {@inheritDoc} */
public void persist(List<Note> notes) {
    if (notes == null) {
        throw new IllegalArgumentException();
    }//from   w  ww .jav a 2s . co m
    DBCollection coll = getNotesCollection();

    for (Note note : notes) {

        // transform POJO into BSON
        BasicDBObject noteDoc = noteToDbObject(note);

        // for creation set the created date
        if (note.getDateTimeCreated() == null) {
            note.setDateTimeCreated(new Timestamp(System.currentTimeMillis()));
        }
        noteDoc.put(Note.Fields.dateTimeCreated.getName(), note.getDateTimeCreated());

        // and the sequence
        if (note.getSequenceNum() == null) {
            note.setSequenceNum(nextSequenceNum(coll));
        }
        noteDoc.put(Note.Fields.sequenceNum.getName(), note.getSequenceNum());

        coll.insert(noteDoc);
        note.setNoteId(noteDoc.getObjectId(NoteMongo.MONGO_ID_FIELD).toString());
    }
}

From source file:org.opentestsystem.delivery.AccValidator.handlers.ValidationHandler.java

License:Open Source License

/**
 * @param resourceFamilies//from  w w w. j  a va 2  s.  com
 */
public void storeResourceFamiliesIntoDB(List<ResourceFamily> resourceFamilies) throws StoringException {

    DB database = getMongoDBConnection();
    DBCollection collection = database.getCollection("resourceFamily");

    collection.drop();

    String name = null;
    String language = null;
    String label = null;
    String message = null;
    String description = null;
    String code = null;
    Integer order = null;
    String defaultSelection = null;
    String resourceType = null;
    Boolean mutuallyExclusive;
    Boolean disabled;

    for (ResourceFamily rf : resourceFamilies) {
        BasicDBObject resourceFamily = new BasicDBObject();
        BasicDBList subjects = new BasicDBList();

        List<AccFamilySubject> accFamilySubjects = rf.getSubject();

        for (int i = 0; i < accFamilySubjects.size(); i++) {
            BasicDBObject subject = new BasicDBObject();
            AccFamilySubject sub = accFamilySubjects.get(i);
            code = sub.getCode();
            if (code != null)
                subject.append("code", code);

            name = sub.getName();
            if (name != null)
                subject.append("name", name);

            subjects.add(subject);
        }

        BasicDBList grade = new BasicDBList();
        List<String> dbGrades = rf.getGrade();
        for (int j = 0; j < dbGrades.size(); j++) {
            grade.add(dbGrades.get(j).toString());

        }
        List<MasterResourceAccommodation> accommodations = rf.getMasterResourceAccommodation();
        BasicDBList masterResourceAccommodation = new BasicDBList();
        for (int k = 0; k < accommodations.size(); k++) {
            MasterResourceAccommodation mracc = accommodations.get(k);
            BasicDBObject accommodation = new BasicDBObject();
            BasicDBObject header = new BasicDBObject();
            BasicDBList headersList = new BasicDBList();
            BasicDBList options = new BasicDBList();
            List<AccommodationText> headerTexts = mracc.getHeader();
            if (headerTexts != null) {
                for (AccommodationText headerText : headerTexts) {
                    if (headerText != null) {
                        language = headerText.getLanguage();
                        if (language != null)
                            header.append("language", language);

                        label = headerText.getLabel();
                        if (label != null)
                            header.append("label", label);

                        message = headerText.getMessage();
                        if (message != null)
                            header.append("message", message);

                        description = headerText.getDescription();
                        if (description != null)
                            header.append("description", description);

                        headersList.add(header);
                    }
                }
            }
            code = mracc.getCode();
            if (code != null)
                accommodation.append("code", code);

            order = mracc.getOrder();
            if (order != 0)
                accommodation.append("order", order);

            defaultSelection = mracc.getDefaultSelection();
            if (defaultSelection != null)
                accommodation.append("defaultSelection", defaultSelection);

            if (header.size() > 0)
                accommodation.append("header", headersList);

            resourceType = mracc.getResourceType();
            if (mracc.getResourceType() != null)
                accommodation.append("resourceType", resourceType);

            disabled = mracc.isDisabled();
            if (mracc.isDisabled() != false) {
                accommodation.append("disabled", disabled);
            }

            List<AccommodationOption> accOptions = mracc.getOptions();
            for (int i = 0; i < accOptions.size(); i++) {
                AccommodationOption accOption = accOptions.get(i);

                if (accOption != null) {
                    BasicDBObject option = new BasicDBObject();
                    code = accOption.getCode();
                    if (code != null)
                        option.append("code", code);

                    order = accOption.getOrder();
                    if (order != null && Integer.valueOf(order) != 0)
                        option.append("order", order);

                    mutuallyExclusive = accOption.isMutuallyExclusive();
                    if (mutuallyExclusive != false)
                        option.append("mutuallyExclusive", mutuallyExclusive);
                    BasicDBList textList = new BasicDBList();
                    List<AccommodationText> accTexts = accOption.getText();
                    if (accTexts != null) {
                        for (AccommodationText accText : accTexts) {
                            if (accText != null) {
                                BasicDBObject text = new BasicDBObject();

                                language = accText.getLanguage();
                                if (language != null)
                                    text.append("language", language);

                                label = accText.getLabel();
                                if (label != null)
                                    text.append("label", label);

                                description = accText.getDescription();
                                if (description != null)
                                    text.append("description", description);

                                message = accText.getMessage();
                                if (message != null)
                                    text.append("message", message);
                                textList.add(text);

                            }
                            if (textList.size() > 0)
                                option.append("text", textList);
                        }
                    }
                    if (option.size() > 0)
                        options.add(option);
                }
            }

            if (options.size() > 0)
                accommodation.append("options", options);

            if (accommodation.size() > 0)
                masterResourceAccommodation.add(accommodation);
        }

        resourceFamily.append("subject", subjects);
        resourceFamily.append("grade", grade);
        resourceFamily.append("masterResourceAccommodation", masterResourceAccommodation);

        collection.insert(resourceFamily);
    }

}

From source file:org.opentestsystem.delivery.AccValidator.handlers.ValidationHandler.java

License:Open Source License

/**
 * @param masterResourceAccommodations//from  ww w . j ava  2  s. co m
 */
public void storeMasterResourceAccommodationIntoDB(
        List<MasterResourceAccommodation> masterResourceAccommodations) throws StoringException {

    String language = null;
    String label = null;
    String message = null;
    String description = null;
    String code = null;
    Integer order = null;
    String defaultSelection = null;
    String resourceType = null;
    Boolean mutuallyExclusive;

    try {

        DB database = getMongoDBConnection();
        DBCollection collection = database.getCollection("masterResourceAccommodation");

        collection.drop();
        for (MasterResourceAccommodation mracc : masterResourceAccommodations) {
            BasicDBObject accommodation = new BasicDBObject();
            BasicDBList headers = new BasicDBList();
            BasicDBObject header = new BasicDBObject();
            BasicDBList options = new BasicDBList();
            List<AccommodationText> headerTexts = mracc.getHeader();
            if (headerTexts != null) {
                for (AccommodationText headerText : headerTexts) {
                    language = headerText.getLanguage();
                    if (language != null)
                        header.append("language", language);

                    label = headerText.getLabel();
                    if (label != null)
                        header.append("label", label);

                    message = headerText.getMessage();
                    if (message != null)
                        header.append("message", message);

                    description = headerText.getDescription();
                    if (description != null)
                        header.append("description", description);
                    headers.add(header);
                }
            }
            code = mracc.getCode();
            if (code != null)
                accommodation.append("code", code);

            order = mracc.getOrder();
            if (order != null && Integer.valueOf(order) != 0)
                accommodation.append("order", order);

            defaultSelection = mracc.getDefaultSelection();
            if (defaultSelection != null)
                accommodation.append("defaultSelection", defaultSelection);

            accommodation.append("header", headers);

            resourceType = mracc.getResourceType();
            if (mracc.getResourceType() != null)
                accommodation.append("resourceType", resourceType);

            List<AccommodationOption> accOptions = mracc.getOptions();

            for (int i = 0; i < accOptions.size(); i++) {
                AccommodationOption accOption = accOptions.get(i);
                BasicDBObject option = new BasicDBObject();
                code = accOption.getCode();
                if (code != null)
                    option.append("code", code);

                order = accOption.getOrder();
                if (order != null)
                    option.append("order", order);

                mutuallyExclusive = accOption.isMutuallyExclusive();
                if (mutuallyExclusive != false)
                    option.append("mutuallyExclusive", mutuallyExclusive);

                List<AccommodationText> accTexts = accOption.getText();
                if (accTexts != null) {
                    BasicDBList textList = new BasicDBList();

                    for (AccommodationText accText : accTexts) {
                        BasicDBObject text = new BasicDBObject();

                        language = accText.getLanguage();
                        if (language != null)
                            text.append("language", language);

                        label = accText.getLabel();
                        if (label != null)
                            text.append("label", label);

                        description = accText.getDescription();
                        if (description != null)
                            text.append("description", description);

                        message = accText.getMessage();
                        if (message != null)
                            text.append("message", message);

                        textList.add(text);

                    }
                    if (textList.size() > 0)
                        option.append("text", textList);
                }

                if (option.size() > 0)
                    options.add(option);
            }

            if (options.size() > 0)
                accommodation.append("options", options);

            if (accommodation != null)
                collection.insert(accommodation);
        }

    } catch (Exception e) {
        e.printStackTrace();
    }

}