raulk / camel-mongodb

MongoDB Camel Component
7 stars 2 forks source link

Problems with MongoDbBasicConverters -> fromBasicDBObjectToMap #2

Open rsvalerio opened 10 years ago

rsvalerio commented 10 years ago

This type converter needs to return a "real" java.util.Map?

From:

@Converter
    public static Map<String, Object> fromBasicDBObjectToMap(BasicDBObject basicDbObject) {
        return basicDbObject;
    }

To:

@Converter
    public static Map<String, Object> fromBasicDBObjectToMap(BasicDBObject basicDbObject) {
        return basicDbObject.toMap();
    }

This way, wee can convert mongodb response body, directly to xml, o send generated Map directly to a relational database using spring simple jdbc insert, etc...

More, we can use simple language like: ${body[_id]}

What do you think @raulk ?

Or maybe i'm doing things the wrong way :(

Thanks.

raulk commented 10 years ago

Hi @rsvalerio!

BasicDBObject implements Map<String, Object>: http://api.mongodb.org/java/current/com/mongodb/BasicDBObject.html.

So this should already be possible.

What exception are you getting?

Regards, Raúl.

rsvalerio commented 10 years ago

Hi @raulk .

Well, my problem is:

Route:

        from("direct:get.beers")
        .to("mongodb:local?database=poc&collection=beers&operation=findAll")
        .split().body()
            .convertBodyTo(String.class)
            .unmarshal().xmljson()
            .log(LoggingLevel.DEBUG, "### ${body}")
            .end();

Throws:

nu.xom.IllegalNameException: NCNames cannot start with the character 24

Because:

{
  "_id": {
    "$oid": "538b8b63d69eb687a19caef7"
  },
  "type": "strong"
}

But even toMap() have an invalid json response:

    public void toMap (
            Exchange exchange,
            @Body Map<String, Object> bodyImplementsMap ) {

        Map<String, Object> bodyToMap = ((BasicDBObject)bodyImplementsMap).toMap();

        exchange.getIn().setHeader("bodyImplementsMap", bodyImplementsMap);
        exchange.getIn().setHeader("bodyToMap", bodyToMap);
    }

bodyImplementsMap (valid json, invalid xml because $) :

{
  "_id": {
    "$oid": "538b8b63d69eb687a19caef7"
  },
  "type": "strong"
}

bodyToMap (invalid json, miss "", valid xml ) :

{
  _id = 538b8b63d69eb687a19caef7, 
  type = strong
}
rsvalerio commented 10 years ago

By now, converting response body from mongodb to Map as follows:

    public void toMap ( Exchange exchange, @Body BasicDBObject body) {
        ObjectId id = body.getObjectId("_id");
        body.put("_id", id.toString());
        exchange.getIn().setBody(body.clone());
    }