Example usage for com.mongodb DBObject toMap

List of usage examples for com.mongodb DBObject toMap

Introduction

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

Prototype

Map toMap();

Source Link

Document

Returns a map representing this BSONObject.

Usage

From source file:com.edduarte.argus.document.Occurrence.java

License:Apache License

public Occurrence(DBObject mongoObject) {
    super(mongoObject.toMap());
}

From source file:com.eywa.impl.app.mongo.services.PaymentService.java

License:Open Source License

/**
 * Process payments//from  w w w .j  av  a  2  s. c  om
 *
 * @param transactions List of payments to process
 */
private static void processPayments(final List<DBObject> transactions) {
    for (final DBObject transaction : transactions) {
        final DBObject callbackParams = ItemPayTransaction.getCallbackParams(transaction);
        final Map<String, String> mapParams = null != callbackParams ? callbackParams.toMap() : new HashMap<>();
        try {
            // process only payments with no errors
            if (!StringUtils.hasText(ItemPayTransaction.getError(transaction))) {
                final String sellerType = ItemPayTransaction.getSellerType(transaction); // payment methods are saved into users billing data
                if (ItemBillingUser.isFree(sellerType) || ItemBillingUser.isPro(sellerType)
                        || ItemBillingUser.isAdmin(sellerType)) {
                    // pay with pay-pal
                    final String id = ItemPayTransaction.getPaymentId(transaction);
                    final DBObject cart = ItemPayTransaction.getCart(transaction);
                    final double price = ItemSessionDataCart.getItemsPrice(cart);
                    final String currency = ItemSessionDataCart.getCurrencyCode(cart);
                    final List<Receiver> receivers = new LinkedList<>();
                    if (ItemBillingUser.isPro(sellerType)) {
                        // PRO SELLER
                        final Receiver rec_seller = new Receiver(price);
                        rec_seller.setPrimary(true);
                        rec_seller.setEmail(ItemPayTransaction.getSellerEmail(transaction));

                        receivers.add(rec_seller);
                    } else if (ItemBillingUser.isFree(sellerType)) {
                        // FREE SELLER
                        final double eywa_price = MathUtils.round(price * AppEywa.getTransactionFee(), 2);
                        final double seller_price = price;
                        final Receiver rec_seller = new Receiver(seller_price);
                        rec_seller.setPrimary(true);
                        rec_seller.setEmail(ItemPayTransaction.getSellerEmail(transaction));
                        final Receiver rec_eywa = new Receiver(eywa_price);
                        rec_eywa.setPrimary(false);
                        rec_eywa.setEmail(AppEywa.getTransactionEmail());

                        receivers.add(rec_seller);
                        receivers.add(rec_eywa);
                    } else {
                        // admin (test only)
                        final double eywa_price = price * AppEywa.getTransactionFee();
                        final double seller_price = price;
                        final Receiver rec_seller = new Receiver(seller_price);
                        rec_seller.setPrimary(true);
                        rec_seller.setEmail(ItemPayTransaction.getSellerEmail(transaction));
                        final Receiver rec_eywa = new Receiver(eywa_price);
                        rec_eywa.setPrimary(false);
                        rec_eywa.setEmail(AppEywa.getTransactionEmail());

                        receivers.add(rec_seller);
                        receivers.add(rec_eywa);
                    }
                    // transaction
                    final PayPalResponse response = PayPalManager.getInstance().pay(currency, receivers, "", // ItemPayTransaction.getCustomerEmail(transaction),  // customer
                            "", // ipn
                            id, // tracking id
                            mapParams, // some useful url parameters
                            "", // cancel url
                            ""); // return url
                    ItemPayTransaction.setPayResponse(transaction, MongoUtils.parseObject(response.toString()));
                    ItemPayTransaction.setPayKey(transaction, response.getPayKey());
                    ItemPayTransaction.setPayExecStatus(transaction, response.getPayExecStatus());
                    ItemPayTransaction.setPayUrl(transaction, response.getPayUrl());
                    if (response.hasError()) {
                        ItemPayTransaction.setError(transaction, response.getFirstErrorMessage());
                    }
                } else {
                    // unmanaged seller type
                    ItemPayTransaction.setError(transaction, "Unmanaged Seller Account Type: " + sellerType);
                    // TODO: Add here more payments types
                }
            }
        } catch (Throwable t) {
            ItemPayTransaction.setError(transaction, "Transaction error: " + t.toString());
        }
    } // end for
}

From source file:com.eywa.impl.app.mongo.services.UserService.java

License:Open Source License

public DBObject signup(final DBObject hash_user, final boolean auto_enable) throws Exception {
    // get template and apply to hash_user
    final String template = UserTemplate.getJson(hash_user.toMap());
    final DBObject user = MongoUtils.parseObject(template);

    if (!hasLang(user)) {
        User.setLang(user, Smartly.getLang());
    } else {/*  w  ww.j  a v a 2  s.co m*/
        User.setLang(user, LocaleUtils.getLanguage(User.getLang(user)));
    }

    // email
    if (StringUtils.hasText(User.getEmail(user))) {
        User.addEmail(user, User.getEmail(user));
    }

    User.setEnabled(user, auto_enable);
    User.setFindable(user, true);
    if (!hasId(user)) {
        User.setId(user, User.createUUID());
    }

    User.setBilling(user, new ItemBillingUser());

    final String password = User.getPassword(user);
    // encode password
    User.setPassword(user, MD5.encode(password));

    //-- insert new user--//
    this.insert(user);
    this.initCountry(user);
    // reinsert password in clear text
    User.setPassword(user, password);

    return user;
}

From source file:com.garyclayburg.data.UserService.java

License:Open Source License

public DBUser getUserById(String id) {
    DBUser existingDBUser = null;//from  w  ww. j  a v a2 s .  c  o  m
    BasicDBObject searchQuery = createUserQuery(id);
    DBObject one = getCollection().findOne(searchQuery);
    if (one != null) {
        existingDBUser = new DBUser(one.toMap()); //MongoDB java driver does not support a direct cast to User
    }
    return existingDBUser;
}

From source file:com.gigaspaces.persistency.datasource.MongoQueryFactory.java

License:Open Source License

@SuppressWarnings("static-access")
private static void replaceIdProperty(BasicDBObjectBuilder qResult, SpaceTypeDescriptor typeDescriptor) {

    DBObject q = qResult.get();

    if (q.containsField(typeDescriptor.getIdPropertyName())) {

        Object value = q.get(typeDescriptor.getIdPropertyName());

        q.removeField(typeDescriptor.getIdPropertyName());

        q.put(Constants.ID_PROPERTY, value);

        qResult.start(q.toMap());
    }/*from  ww w  .  j  a  v  a2s.co m*/
}

From source file:com.gigaspaces.persistency.datasource.MongoQueryFactory.java

License:Open Source License

public static BasicDBObjectBuilder bind(String parsedQuery, Object[] parameters,
        SpaceTypeDescriptor spaceTypeDescriptor) {

    SpaceDocumentMapper<DBObject> mapper = new DefaultSpaceDocumentMapper(spaceTypeDescriptor);

    DBObject obj = (DBObject) JSON.parse(parsedQuery);

    BasicDBObjectBuilder query = BasicDBObjectBuilder.start(obj.toMap());

    if (parameters != null) {
        query = replaceParameters(parameters, mapper, query, 0);
    }/*from  ww  w .  j  a  v a2s .  c o  m*/
    return query;
}

From source file:com.gigaspaces.persistency.datasource.MongoQueryFactory.java

License:Open Source License

private static BasicDBObjectBuilder replaceParameters(Object[] parameters, SpaceDocumentMapper<DBObject> mapper,
        BasicDBObjectBuilder builder, Integer index) {

    BasicDBObjectBuilder newBuilder = BasicDBObjectBuilder.start();

    DBObject document = builder.get();/*from w ww . j ava  2 s. c  o  m*/

    Iterator<String> iterator = document.keySet().iterator();

    while (iterator.hasNext()) {
        String field = iterator.next();
        Object ph = document.get(field);

        if (index >= parameters.length)
            return builder;

        if (ph instanceof String) {
            if (PARAM_PLACEHOLDER.equals(ph)) {
                Object p = mapper.toObject(parameters[index++]);

                if (p instanceof DBObject
                        && Constants.CUSTOM_BINARY.equals(((DBObject) p).get(Constants.TYPE))) {
                    newBuilder.add(field + "." + Constants.HASH, ((DBObject) p).get(Constants.HASH));
                } else {
                    newBuilder.add(field, p);
                }
            }
        } else if (ph instanceof Pattern) {
            Pattern p = (Pattern) ph;

            if (LIKE.equalsIgnoreCase(p.pattern())) {
                newBuilder.add(field, convertLikeExpression((String) parameters[index++]));
            } else if (RLIKE.equalsIgnoreCase(p.pattern())) {
                newBuilder.add(field, Pattern.compile((String) parameters[index++], Pattern.CASE_INSENSITIVE));
            }
        } else {
            DBObject element = (DBObject) ph;

            Object p = mapper.toObject(parameters[index]);

            if (p instanceof DBObject) {
                String t = (String) ((DBObject) p).get(Constants.TYPE);
                String op = element.keySet().iterator().next();

                if (Constants.CUSTOM_BINARY.equals(t) && !op.equals("$ne"))
                    return newBuilder;
            }

            BasicDBObjectBuilder doc = replaceParameters(parameters, mapper,
                    BasicDBObjectBuilder.start(element.toMap()), index);

            newBuilder.add(field, doc.get());
        }
    }
    return newBuilder;
}

From source file:com.github.camellabs.iot.cloudlet.document.driver.mongodb.BsonMapper.java

License:Apache License

public static DBObject bsonToJson(DBObject bson) {
    checkNotNull(bson, "BSON passed to the conversion can't be null.");
    LOG.debug("Converting BSON object to JSON: {}", bson);
    DBObject json = new BasicDBObject(bson.toMap());
    Object id = json.get("_id");
    if (id != null) {
        json.removeField("_id");
        json.put("id", id.toString());
    }//from  www.j  a  v a  2 s.  c  o m
    return json;
}

From source file:com.github.camellabs.iot.cloudlet.document.driver.mongodb.BsonMapper.java

License:Apache License

public static DBObject jsonToBson(DBObject json) {
    checkNotNull(json, "JSON passed to the conversion can't be null.");
    LOG.debug("Converting JSON object to BSON: {}", json);
    DBObject bson = new BasicDBObject(json.toMap());
    Object id = bson.get("id");
    if (id != null) {
        bson.removeField("id");
        bson.put("_id", new ObjectId(id.toString()));
    }//from w  w  w.  j av  a  2 s. c om
    return bson;
}

From source file:com.groupon.jenkins.dynamic.build.repository.DynamicBuildRepository.java

License:Open Source License

private static <T extends DbBackedBuild> Function<DBObject, T> getTransformer(final DbBackedProject project) {

    return new Function<DBObject, T>() {
        @Override//from  w w w .j a v  a  2 s .c o  m
        public T apply(@Nonnull DBObject input) {
            String xml = (String) input.get("xml");
            Object build = Run.XSTREAM.fromXML(xml);
            T dynamicBuild = (T) build;
            dynamicBuild.restoreFromDb(project, input.toMap());
            return dynamicBuild;
        }
    };
}