Example usage for com.mongodb DBRef DBRef

List of usage examples for com.mongodb DBRef DBRef

Introduction

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

Prototype

public DBRef(@Nullable final String databaseName, final String collectionName, final Object id) 

Source Link

Document

Construct an instance.

Usage

From source file:com.bugull.mongo.utils.ReferenceUtil.java

License:Apache License

private static DBRef toDBRef(Class<?> clazz, String idStr) {
    DB db = null;/*from   ww  w .  j  a  v a2s  .  c om*/
    try {
        db = BuguConnection.getInstance().getDB();
    } catch (DBConnectionException ex) {
        logger.error(ex.getMessage(), ex);
    }
    String name = MapperUtil.getEntityName(clazz);
    Object dbId = IdUtil.toDbId(clazz, idStr);
    return new DBRef(db, name, dbId);
}

From source file:com.edgytech.umongo.EditObjectIdDialog.java

License:Apache License

@Override
public Object getValue() {
    String str = getStringFieldValue(Item.string);
    ObjectId id = null;//from w w  w.j a va2  s.c o m
    if (!str.isEmpty()) {
        id = new ObjectId(str);
    } else {
        int time = getIntFieldValue(Item.time);
        int machine = getIntFieldValue(Item.machine);
        int inc = getIntFieldValue(Item.inc);
        id = new ObjectId(time, machine, inc);
    }
    String ns = getStringFieldValue(Item.namespace);
    if (ns.trim().isEmpty()) {
        return id;
    }
    return new DBRef(null, ns, id);
}

From source file:com.espirit.moddev.examples.uxbridge.newsdrilldown.jpa.NewsHandler.java

License:Apache License

/**
 * handle the update and save process for the metacategories.
 *
 * @param metaCat//  w  ww . j  a va 2 s  .  c  o  m
 *            the NewsMetaCategory object
 * @param category
 *            the NewsCategory object to reference to
 * @throws Exception
 *             the exception
 */
private void handleMetaCategory(NewsMetaCategory metaCat, NewsCategory category) {
    DBObject item = getMetaCategoryById(metaCat.getFs_id(), metaCat.getLanguage());

    if (item != null) {
        item.put("fs_id", metaCat.getFs_id());
        item.put("language", metaCat.getLanguage());
        item.put("name", metaCat.getName());
        item.put("version", metaCat.getVersion());
        item.put("lastmodified", metaCat.getLastmodified());

        // category
        DBObject cat = getCategoryById(category.getFs_id(), category.getLanguage());
        if (cat != null) {
            DBRef ref = new DBRef(mdb, CATEGORY_COLLECTION_NAME, cat.get("_id"));
            item.put("category", ref);
        }

        BasicDBObject query = new BasicDBObject();
        query.put("_id", item.get("_id"));
        dbMetaCategories.update(query, item);
    } else {
        item = new BasicDBObject();

        item.put("fs_id", metaCat.getFs_id());
        item.put("language", metaCat.getLanguage());
        item.put("name", metaCat.getName());
        item.put("version", metaCat.getVersion());
        item.put("lastmodified", metaCat.getLastmodified());

        // category
        DBObject cat = getCategoryById(category.getFs_id(), category.getLanguage());
        if (cat != null) {
            DBRef ref = new DBRef(mdb, CATEGORY_COLLECTION_NAME, cat.get("_id"));
            item.put("category", ref);
        }

        item.put("_id", generateIdentifier(META_CATEGORY_COLLECTION_NAME, mdb));

        dbMetaCategories.insert(item);
    }
}

From source file:com.github.jmkgreen.morphia.mapping.DefaultMapper.java

License:Open Source License

public DBRef keyToRef(Key key) {
    if (key == null)
        return null;
    if (key.getKindClass() == null && key.getKind() == null)
        throw new IllegalStateException("How can it be missing both?");
    if (key.getKind() == null)
        key.setKind(getCollectionName(key.getKindClass()));

    return new DBRef(null, key.getKind(), toMongoObject(key.getId(), false));
}

From source file:com.google.code.morphia.mapping.Mapper.java

License:Open Source License

public DBRef keyToRef(Key key) {
    if (key == null)
        return null;
    if (key.getKindClass() == null && key.getKind() == null)
        throw new IllegalStateException("How can it be missing both?");
    if (key.getKind() == null)
        key.setKind(getCollectionName(key.getKindClass()));

    return new DBRef(null, key.getKind(), key.getId());
}

From source file:com.smbtec.xo.mongodb.impl.MongoDbRelationshipManager.java

License:Apache License

@Override
public MongoDbRelation createRelation(MongoDbDocument source,
        RelationTypeMetadata<RelationshipMetadata> metadata, Direction direction, MongoDbDocument target,
        Map<PrimitivePropertyMethodMetadata<PropertyMetadata>, Object> exampleEntity) {
    final String label = metadata.getDatastoreMetadata().getDiscriminator();
    DBRef dbref = new DBRef(database, target.getLabel(), target.getDelegate().get(MongoDbConstants.MONGODB_ID));

    if (metadata.getDatastoreMetadata().isCollectionType()) {
        String collectionName = metadata.getDatastoreMetadata().getDiscriminator();
        Object old = source.getDelegate().get(collectionName);
        if (old != null) {
            @SuppressWarnings("unchecked")
            List<DBRef> oldDBRefList = (List<DBRef>) old;
            List<DBRef> dbRefList = new ArrayList<>(oldDBRefList);
            dbRefList.add(dbref);//from   www.j  av  a2s  .  c o m
            source.getDelegate().put(collectionName, dbRefList);
        } else {
            source.getDelegate().put(collectionName, Arrays.asList(dbref));
        }
    } else {
        source.getDelegate().put(label, dbref);
    }
    return new MongoDbReference(label, source, target);
}

From source file:com.sonyericsson.jenkins.plugins.bfa.db.MongoDBKnowledgeBase.java

License:Open Source License

/**
 * Adds the FailureCauses from the list to the DBObject.
 * @param object the DBObject to add to.
 * @param failureCauseStatisticsList the list of FailureCauseStatistics to add.
 * @throws UnknownHostException If the mongoDB host cannot be found.
 * @throws AuthenticationException if the mongoDB authentication fails.
 *//*w w  w  . ja  v a 2 s. com*/
private void addFailureCausesToDBObject(DBObject object,
        List<FailureCauseStatistics> failureCauseStatisticsList)
        throws UnknownHostException, AuthenticationException {
    if (failureCauseStatisticsList != null && failureCauseStatisticsList.size() > 0) {
        List<DBObject> failureCauseStatisticsObjects = new LinkedList<DBObject>();

        for (FailureCauseStatistics failureCauseStatistics : failureCauseStatisticsList) {
            DBObject failureCauseStatisticsObject = new BasicDBObject();
            ObjectId id = new ObjectId(failureCauseStatistics.getId());
            DBRef failureCauseRef = new DBRef(getDb(), COLLECTION_NAME, id);
            failureCauseStatisticsObject.put("failureCause", failureCauseRef);
            List<FoundIndication> foundIndicationList = failureCauseStatistics.getIndications();
            addIndicationsToDBObject(failureCauseStatisticsObject, foundIndicationList);
            failureCauseStatisticsObjects.add(failureCauseStatisticsObject);
        }
        object.put("failureCauses", failureCauseStatisticsObjects);
    }
}

From source file:io.liveoak.mongo.RootMongoResource.java

License:Open Source License

@Override
protected DBRef getDBRef(String uri) throws ResourceProcessingException {
    if (uri != null) {
        String rootURI = this.uri().toString();
        if (uri.startsWith(rootURI + "/")) {

            String resourcePath = uri.substring(rootURI.length());
            String[] paths = resourcePath.split("/");

            if (paths.length != 3) {
                throw new ResourceProcessingException(
                        "$DBRefs must be in the format /rootContextPath/collectionName/resourceID");
            } else {
                String collectionName = paths[1];
                String resourceID = paths[2];
                return new DBRef(this.db(), collectionName, resourceID);
            }//  www  . jav  a2 s.  com

        } else {
            throw new ResourceProcessingException(
                    "$DBRefs are only supported under the same context root. URL specified ('" + uri
                            + "')should start with " + rootURI + ".");
        }
    } else {
        throw new ResourceProcessingException("$DBRefs must be URL, they cannot be null.");
    }
}

From source file:net.tooan.ynpay.third.mongodb.mapper.ReferenceUtil.java

License:Apache License

private static DBRef toDBRef(Class<?> clazz, String idStr) {
    DB db = null;//  w w  w. j  a va 2s.c o  m
    String[] name = MapperUtil.getEntityName(clazz);
    try {
        db = BuguConnection.getInstance().getDB(name[0]);
    } catch (DBConnectionException ex) {
        logger.error(ex.getMessage(), ex);
    }

    Object dbId = IdUtil.toDbId(clazz, idStr);
    return new DBRef(db, name[1], dbId);
}

From source file:org.codinjutsu.tools.mongo.view.model.JsonTreeUtils.java

License:Apache License

private static DBRef buildDBRefObject(JsonTreeNode parentNode) {
    Object _id = null;/*from   ww  w  .j  a va  2s . c o  m*/
    String collectionName = null;
    String databaseName = null;
    Enumeration children = parentNode.children();
    while (children.hasMoreElements()) {
        JsonTreeNode node = (JsonTreeNode) children.nextElement();
        MongoNodeDescriptor descriptor = node.getDescriptor();
        String formattedKey = descriptor.getFormattedKey();
        switch (formattedKey) {
        case "\"$id\"":
            _id = descriptor.getValue();
            break;
        case "\"$ref\"":
            collectionName = (String) descriptor.getValue();
            break;
        case "\"$db\"":
            databaseName = (String) descriptor.getValue();
            break;
        default:
            throw new IllegalArgumentException("Unexpected key: " + formattedKey);
        }
    }
    if (collectionName == null || _id == null) {
        throw new IllegalArgumentException("When using DBRef, $ref and $id should be set.");
    }
    return new DBRef(databaseName, collectionName, _id);
}