Example usage for com.mongodb DBObject containsField

List of usage examples for com.mongodb DBObject containsField

Introduction

In this page you can find the example usage for com.mongodb DBObject containsField.

Prototype

boolean containsField(String s);

Source Link

Document

Checks if this object contains a field with the given name.

Usage

From source file:Welcome.java

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
    String accno = JOptionPane.showInputDialog(this, "Enter Your Account Number");
    if (isInteger(accno)) {

        int value = JOptionPane.showConfirmDialog(this, "Do You Want to add Account No. " + accno);
        if (value == 0) {
            String Banks[] = { "", "Allahabad Bank", "Andhra Bank", "Bank of Baroda", "Bank of India",
                    "Bank of Maharashtra", "Canara Bank", "Central Bank of India", "Corporation Bank",
                    "Dena Bank", "Indian Bank", "Indian Overseas Bank", "Oriental Bank of Commerce",
                    "Punjab & Sind Bank", "Punjab National Bank", "Syndicate Bank", "UCO Bank",
                    "Union Bank of India", "United Bank of India", "Vijaya Bank", "Axis Bank",
                    "City Union Bank", "Dhanlaxmi Bank", "Federal Bank", "HDFC Bank", "ICICI Bank", "IDFC Bank",
                    "Karnataka Bank", "IndusInd Bank", "ING Vysya Bank", "Jammu and Kashmir Bank",
                    "Karur Vysya Bank", "Kotak Mahindra Bank", "Yes bank", "Citi Bank", "State Bank of India",
                    "State Bank of Patiala", "State Bank of Mysore", "State Bank of Travancore",
                    "State Bank of Bikaner and Jaipur", "State Bank of Hyderabad", "State Bank Of Saurashtra"

            };//  w  w w.j av  a2  s.c  o m
            Arrays.sort(Banks);
            JComboBox jcb = new JComboBox(Banks);
            jcb.setEditable(true);
            JOptionPane.showMessageDialog(this, jcb, "Select Your Bank", JOptionPane.QUESTION_MESSAGE);
            while (jcb.getSelectedItem().equals("")) {
                JOptionPane.showMessageDialog(this, "Please Select the Bank");
                JOptionPane.showMessageDialog(this, jcb, "Select Your Bank", JOptionPane.QUESTION_MESSAGE);
            }
            String money = JOptionPane.showInputDialog(this,
                    "Enter the amount present in " + accno + " account");
            String Bank = (String) jcb.getSelectedItem();
            while (!isInteger(money)) {
                JOptionPane.showMessageDialog(this, "You entered a Wrong Value. Please Enter Correct Value");
                money = JOptionPane.showInputDialog(this, "Enter the amount present in " + accno + " account");
            }
            MongoClient client = new MongoClient("localhost", 27017);
            DB db;
            db = client.getDB("ExpenseManager");
            DBCollection reg = (DBCollection) db.getCollection("Registration");
            DBObject query = new BasicDBObject("unm", unm);
            DBObject update = new BasicDBObject();

            DBCursor find = reg.find(query);
            while (find.hasNext()) {
                DBObject next = find.next();
                int count = 1;
                for (int i = 1; i <= 5; i++) {
                    String str1 = "Account" + i;
                    String str2 = "Bank" + i;
                    String str3 = "Money" + i;
                    System.out.println(str1);
                    if (!(next.containsField(str1))) {
                        update.put("$set",
                                new BasicDBObject(str1, accno).append(str2, Bank).append(str3, money));
                        WriteResult result = reg.update(query, update);
                        setListVal();
                        break;
                    }
                }
            }

        }

    } else {
        JOptionPane.showMessageDialog(this, "You Entered a wrong Number");
    }

    // TODO add your handling code here:
}

From source file:act.shared.helpers.XMLToImportantChemicals.java

License:Open Source License

private void process(XMLStreamReader xml) throws XMLStreamException {
    String tag;//from   w  ww  . j a v  a 2  s  . c  o m
    String root = null;
    Stack<DBObject> json = new Stack<DBObject>();
    DBObject js;
    while (xml.hasNext()) {
        int eventType = xml.next();
        while (xml.isWhiteSpace() || eventType == XMLEvent.SPACE)
            eventType = xml.next();

        switch (eventType) {
        case XMLEvent.START_ELEMENT:
            tag = xml.getLocalName();
            if (root == null) {
                root = tag;
            } else {
                json.push(new BasicDBObject());
            }
            break;
        case XMLEvent.END_ELEMENT:
            tag = xml.getLocalName();
            if (tag.equals(root)) {
                // will terminate in next iteration
            } else {
                js = json.pop();
                if (json.size() == 0) {
                    if (tag.equals(rowTag))
                        printEntry(js);
                    else
                        printUnwantedEntry(js);
                } else {
                    putListStrOrJSON(json.peek(), tag, js);
                }
            }
            break;

        case XMLEvent.CHARACTERS:
            String txt = xml.getText();
            js = json.peek();
            if (js.containsField(strTag)) {
                txt = js.get(strTag) + txt;
                js.removeField(strTag);
            }
            js.put(strTag, txt);
            break;

        case XMLEvent.START_DOCUMENT:
            break;
        case XMLEvent.END_DOCUMENT:
            break;
        case XMLEvent.COMMENT:
        case XMLEvent.ENTITY_REFERENCE:
        case XMLEvent.ATTRIBUTE:
        case XMLEvent.PROCESSING_INSTRUCTION:
        case XMLEvent.DTD:
        case XMLEvent.CDATA:
        case XMLEvent.SPACE:
            System.out.format("%s --\n", eventType);
            break;
        }
    }
}

From source file:act.shared.helpers.XMLToImportantChemicals.java

License:Open Source License

private void putListStrOrJSON(DBObject json, String tag, DBObject toAdd) {
    // if it is a string add it unencapsulated
    if (toAdd.keySet().size() == 1 && toAdd.containsField(strTag))
        putElemOrList(json, tag, toAdd.get(strTag));
    else/*from   w ww  .j  a v a 2  s. c om*/
        putElemOrList(json, tag, toAdd);
}

From source file:act.shared.helpers.XMLToImportantChemicals.java

License:Open Source License

private void putElemOrList(DBObject json, String tag, Object add) {
    // if tag already present then make it a list
    if (json.containsField(tag)) {
        BasicDBList l;/*from w ww.j ava 2 s  .c  o  m*/
        Object already = json.get(tag);
        if (already instanceof BasicDBList) {
            l = (BasicDBList) already;
        } else {
            l = new BasicDBList();
            l.add(already);
        }
        l.add(add);
        json.removeField(tag);
        json.put(tag, l);
        return;
    }

    // else, just add as is
    json.put(tag, add);
    return;
}

From source file:act.shared.helpers.XMLToImportantChemicals.java

License:Open Source License

private String getInchi(DBObject json) {
    // under calculated-properties.property.[{kind=InChI, value=<inchi>}]
    DBObject o;//w  w  w  . jav  a  2  s  .c o  m
    if (json.containsField("calculated-properties")) {
        o = (DBObject) json.get("calculated-properties");
        if (o.containsField("property")) {
            o = (DBObject) o.get("property");
            if (o instanceof BasicDBList) {
                for (Object kv : (BasicDBList) o) {
                    o = (DBObject) kv;
                    if (o.containsField("kind") && o.get("kind").equals("InChI") && o.containsField("value")) {
                        return (String) o.get("value");
                    }
                }
            }
        }
    }
    return null;
}

From source file:at.ac.tuwien.dsg.smartcom.services.dao.MongoDBMessageInfoDAO.java

License:Apache License

MessageInformation deserializeMessageInfo(DBObject dbObject) {
    MessageInformation info = new MessageInformation();

    if (dbObject.containsField("_id")) {
        info.setKey(deserializeKey((DBObject) dbObject.get("_id")));
    }//from w  w w  . j a va 2s . c o m

    if (dbObject.containsField("purpose")) {
        info.setPurpose((String) dbObject.get("purpose"));
    }

    if (dbObject.containsField("validAnswer")) {
        info.setValidAnswer((String) dbObject.get("validAnswer"));
    }

    if (dbObject.containsField("validAnswerTypes")) {
        List<MessageInformation.Key> validAnswers = new ArrayList<>();

        BasicDBList list = (BasicDBList) dbObject.get("validAnswerTypes");
        for (Object o : list) {
            validAnswers.add(deserializeKey((DBObject) o));
        }
        info.setValidAnswerTypes(validAnswers);
    }

    if (dbObject.containsField("relatedMessages")) {
        List<MessageInformation.Key> related = new ArrayList<>();

        BasicDBList list = (BasicDBList) dbObject.get("relatedMessages");
        for (Object o : list) {
            related.add(deserializeKey((DBObject) o));
        }
        info.setRelatedMessages(related);
    }

    return info;
}

From source file:calliope.core.database.MongoConnection.java

License:Open Source License

/**
 * PUT a new json file to the database//from  w  w  w  .  j  av  a 2 s  .  c o m
 * @param collName the name of the collection
 * @param json the json to put there
 * @return the server response
 */
@Override
public String addToDb(String collName, String json) throws DbException {
    try {
        DBObject doc = (DBObject) JSON.parse(json);
        connect();
        DBCollection coll = getCollectionFromName(collName);
        if (doc.containsField(JSONKeys._ID)) {
            Object id = doc.get(JSONKeys._ID);
            DBObject query = new BasicDBObject(JSONKeys._ID, id);
            if (query != null) {
                WriteResult result = coll.update(query, doc, true, false);
                return result.toString();
            } else
                throw new Exception("Failed to update object " + id);
        } else {
            WriteResult result = coll.insert(doc, WriteConcern.ACKNOWLEDGED);
            // return the new document's id
            ObjectId id = (ObjectId) doc.get("_id");
            JSONObject jDoc = (JSONObject) JSONValue.parse(result.toString());
            jDoc.put("_id", id.toString());
            return jDoc.toJSONString();
        }
    } catch (Exception e) {
        throw new DbException(e);
    }
}

From source file:calliope.export.PDEFArchive.java

License:Open Source License

/**
 * Remove fields that will be added by the database after import
 * @param json the original JSON string//from ww  w  . ja v a  2s .c o  m
 * @return a possibly modified JSON string with the fields removed 
 */
private String removeTabooFields(String json) {
    boolean changed = false;
    DBObject jdoc = (DBObject) JSON.parse(json);
    for (int i = 0; i < tabooFields.length; i++) {
        if (jdoc.containsField(tabooFields[i])) {
            jdoc.removeField(tabooFields[i]);
            changed = true;
        }
    }
    if (changed)
        json = jdoc.toString();
    return json;
}

From source file:cfel.service.PortalImportTask.java

License:Open Source License

private void fixThumbnailImages() {
    DBObject query = new BasicDBObject("card_image_url", new BasicDBObject("$exists", false));
    for (Iterator<DBObject> it = mPhotoCollection.find(query); it.hasNext();) {
        DBObject photo = it.next();
        if (!photo.containsField("portal_image_url")) {
            System.err.println("No portal_image_url in " + photo);
            continue;
        }//from   ww w . j ava2s .  c om
        String portal_image_url = photo.get("portal_image_url").toString();
        if (!portal_image_url.matches("^https?://.*")) {
            portal_image_url = Config.ALBUM_ROOT + portal_image_url;
        }
        try {
            URL url = new URL(portal_image_url);
            System.err.println("Missing thumbnail images for " + url);
            BufferedImage[] images = ImageUtil.convertImage(url);
            if (images != null) {
                DBObject updates = new BasicDBObject();
                saveThumbnailImages(updates, images, url.getPath().replace(".", "_") + ".png");
                mPhotoCollection.update(photo, new BasicDBObject("$set", updates));
            } else {
                System.err.println(url + " corrupted");
            }
        } catch (MalformedURLException e) {
            System.err.println("Unknown image URL" + portal_image_url);
            continue;
        }
    }
}

From source file:ch.windmobile.server.social.mongodb.UserServiceImpl.java

License:Open Source License

@Override
public List<Favorite> addToFavorites(String email, List<Favorite> localFavorites) throws UserNotFound {
    if (email == null) {
        throw new IllegalArgumentException("Email cannot be null");
    }/* www  . j a  v  a2  s. co  m*/
    DBCollection col = db.getCollection(MongoDBConstants.COLLECTION_USERS);
    // Search user by email
    DBObject userDb = col.findOne(new BasicDBObject(MongoDBConstants.USER_PROP_EMAIL, email));
    if (userDb == null) {
        throw new UserNotFound("Unable to find user with email '" + email + "'");
    }

    DBObject favoritesDb = (DBObject) userDb.get(MongoDBConstants.USER_PROP_FAVORITES);
    if (favoritesDb == null) {
        favoritesDb = new BasicDBObject();
    }

    if (localFavorites != null) {
        for (Favorite localFavorite : localFavorites) {
            DBObject favoriteItemsDb = (DBObject) favoritesDb.get(localFavorite.getStationId());
            long lastMessageIdDb = -1;
            if (favoriteItemsDb == null) {
                favoriteItemsDb = new BasicDBObject();
            } else {
                if (favoriteItemsDb.containsField(MongoDBConstants.USER_PROP_FAVORITE_LASTMESSAGEID)) {
                    lastMessageIdDb = (Long) favoriteItemsDb
                            .get(MongoDBConstants.USER_PROP_FAVORITE_LASTMESSAGEID);
                }
            }

            favoriteItemsDb.put(MongoDBConstants.USER_PROP_FAVORITE_LASTMESSAGEID,
                    Math.max(lastMessageIdDb, localFavorite.getLastMessageId()));
            favoritesDb.put(localFavorite.getStationId(), favoriteItemsDb);
        }
    }
    userDb.put(MongoDBConstants.USER_PROP_FAVORITES, favoritesDb);
    col.save(userDb);

    List<Favorite> returnValue = new ArrayList<Favorite>(favoritesDb.keySet().size());
    for (String stationId : favoritesDb.keySet()) {
        Favorite favorite = new Favorite();
        favorite.setStationId(stationId);
        DBObject favoriteItemDb = (DBObject) favoritesDb.get(stationId);
        favorite.setLastMessageId((Long) favoriteItemDb.get(MongoDBConstants.USER_PROP_FAVORITE_LASTMESSAGEID));
        returnValue.add(favorite);
    }
    return returnValue;
}