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:net.tbnr.util.TPluginBungee.java

License:Open Source License

protected Object bungeeConfigGet(String key) {
    DBObject config = this.getBungeeConfig();
    if (!config.containsField(key))
        return null;
    return config.get(key);
}

From source file:org.alfresco.bm.api.AbstractRestResource.java

License:Open Source License

/**
 * Find and mask property values./*from  ww w .j a  va  2  s  .c  o  m*/
 * <p/>
 * Properties will be searched for deeply.
 * 
 * @param obj           the object to modify
 */
public static DBObject maskValues(DBObject dbObject) {
    if (dbObject instanceof BasicDBList) {
        BasicDBList objListOrig = (BasicDBList) dbObject;
        // Copy entries to a new list
        BasicDBList newObjList = new BasicDBList();
        for (Object origListObjT : objListOrig) {
            DBObject origListObj = (DBObject) origListObjT;
            // Mask any values
            DBObject newListObj = maskValues(origListObj);
            newObjList.add(newListObj);
        }
        // Done
        return newObjList;
    } else if (dbObject.containsField(FIELD_MASK)) {
        boolean mask = Boolean.parseBoolean((String) dbObject.get(FIELD_MASK));
        if (mask) {
            DBObject newObj = copyDBObject(dbObject);
            // We have a copy to play with
            newObj.put(FIELD_DEFAULT, MASK);
            if (dbObject.get(FIELD_VALUE) != null) {
                newObj.put(FIELD_VALUE, MASK);
            }
            return newObj;
        } else {
            return dbObject;
        }
    } else if (dbObject.containsField(FIELD_PROPERTIES)) {
        // There are properties
        BasicDBList propsObj = (BasicDBList) dbObject.get(FIELD_PROPERTIES);
        BasicDBList newPropsObj = (BasicDBList) maskValues(propsObj);
        // Copy
        DBObject newObj = copyDBObject(dbObject);
        newObj.put(FIELD_PROPERTIES, newPropsObj);
        // Done
        return newObj;
    } else {
        // Not a list and does not contain the mask field
        return dbObject;
    }
}

From source file:org.alfresco.bm.event.mongo.MongoEventService.java

License:Open Source License

/**
 * Helper method to convert a {@link DBObject persistable object} into an {@link Event}
 *///from   w  w  w  .  j  av a 2s .c o m
private Event convertDBObject(DBObject obj) {
    String id = obj.get(Event.FIELD_ID).toString();
    Object data = obj.get(Event.FIELD_DATA);
    String dataOwner = (String) obj.get(Event.FIELD_DATA_OWNER);
    String lockOwner = (String) obj.get(Event.FIELD_LOCK_OWNER);
    long lockTime = obj.containsField(Event.FIELD_LOCK_TIME) ? ((Date) obj.get(Event.FIELD_LOCK_TIME)).getTime()
            : Long.valueOf(0L);
    String name = (String) obj.get(Event.FIELD_NAME);
    long scheduledTime = obj.containsField(Event.FIELD_SCHEDULED_TIME)
            ? ((Date) obj.get(Event.FIELD_SCHEDULED_TIME)).getTime()
            : Long.valueOf(0L);
    String sessionId = (String) obj.get(Event.FIELD_SESSION_ID);
    String driver = (String) obj.get(Event.FIELD_DRIVER);

    // Check to see if we should be getting the data from memory
    if (dataOwner != null) {
        if (data != null) {
            throw new IllegalStateException("Event should not be stored with data AND a data owner: " + obj);
        }
        // The data was tagged as being held in VM
        data = runLocalData.get(id);
        if (data == null) {
            throw new IllegalStateException("Event data is not available in the VM: " + obj);
        }
    }

    Event event = new Event(name, scheduledTime, data);
    event.setId(id);
    event.setLockOwner(lockOwner);
    event.setLockTime(lockTime);
    event.setSessionId(sessionId);
    event.setDriver(driver);

    // Done
    return event;
}

From source file:org.alfresco.bm.event.mongo.MongoResultService.java

License:Open Source License

/**
 * Creates an {@see EventDetails} object from the MongoDB record
 * //from w w  w.j a  va 2 s .c  om
 * @param eventDetailsObj (DBObject)
 * 
 * @return EventDetails
 */
private EventDetails convertToEventDetails(DBObject eventDetailsObj) {
    // get event date
    Date startTime = eventDetailsObj.containsField(EventRecord.FIELD_START_TIME)
            ? (Date) eventDetailsObj.get(EventRecord.FIELD_START_TIME)
            : new Date(0L);

    // get event success 
    boolean success = eventDetailsObj.containsField(EventRecord.FIELD_SUCCESS)
            ? (Boolean) eventDetailsObj.get(EventRecord.FIELD_SUCCESS)
            : false;

    // get inputData
    Object eventData = eventDetailsObj.get(EventRecord.FIELD_DATA);

    // get Event
    DBObject eventObj = (DBObject) eventDetailsObj.get(EventRecord.FIELD_EVENT);
    if (eventObj == null) {
        throw new IllegalArgumentException(
                "DBObject for EventDetails does not contain Event data: " + eventDetailsObj);
    }
    Event event = convertToEvent(eventObj);

    // get event name
    String name = event.getName();

    // get Event.Data
    Object inputData = event.getData();

    return new EventDetails(startTime, name, success, inputData, eventData);
}

From source file:org.alfresco.bm.event.mongo.MongoResultService.java

License:Open Source License

/**
 * Helper to convert Mongo-persisted object to a client-visible {@link EventRecord}
 */// ww w  .  ja  v a 2  s  . c om
private EventRecord convertToEventRecord(DBObject eventRecordObj) {
    if (eventRecordObj == null) {
        return null;
    }

    String processedBy = (String) eventRecordObj.get(EventRecord.FIELD_PROCESSED_BY);
    String driverId = (String) eventRecordObj.get(EventRecord.FIELD_DRIVER_ID);
    if (driverId == null) {
        // data model is too old
        throw new IllegalArgumentException(
                "DBObject for EventRecord doesn't contain a driver ID. The data model may be too old!");
    }
    boolean success = eventRecordObj.containsField(EventRecord.FIELD_SUCCESS)
            ? (Boolean) eventRecordObj.get(EventRecord.FIELD_SUCCESS)
            : false;
    long startTime = eventRecordObj.containsField(EventRecord.FIELD_START_TIME)
            ? ((Date) eventRecordObj.get(EventRecord.FIELD_START_TIME)).getTime()
            : Long.valueOf(0L);
    long time = eventRecordObj.containsField(EventRecord.FIELD_TIME)
            ? (Long) eventRecordObj.get(EventRecord.FIELD_TIME)
            : Long.valueOf(-1L);
    Object data = eventRecordObj.get(EventRecord.FIELD_DATA);
    String id = (String) eventRecordObj.get(EventRecord.FIELD_ID).toString();
    String warning = (String) eventRecordObj.get(EventRecord.FIELD_WARNING);
    boolean chart = eventRecordObj.containsField(EventRecord.FIELD_CHART)
            ? (Boolean) eventRecordObj.get(EventRecord.FIELD_CHART)
            : false;
    long startDelay = eventRecordObj.containsField(EventRecord.FIELD_START_DELAY)
            ? (Long) eventRecordObj.get(EventRecord.FIELD_START_DELAY)
            : Long.valueOf(-1L);

    // Extract the event
    DBObject eventObj = (DBObject) eventRecordObj.get(EventRecord.FIELD_EVENT);
    if (eventObj == null) {
        throw new IllegalArgumentException(
                "DBObject for EventRecord does not contain Event data: " + eventRecordObj);
    }
    Event event = convertToEvent(eventObj);

    EventRecord eventRecord = new EventRecord(driverId, success, startTime, time, data, event);
    eventRecord.setProcessedBy(processedBy);
    eventRecord.setId(id);
    eventRecord.setWarning(warning);
    eventRecord.setChart(chart);
    eventRecord.setStartDelay(startDelay);
    // Done
    if (logger.isTraceEnabled()) {
        logger.trace(
                "Converted EventRecord: \n" + "   In:  " + eventRecordObj + "\n" + "   Out: " + eventRecord);
    }
    return eventRecord;
}

From source file:org.alfresco.bm.event.mongo.MongoResultService.java

License:Open Source License

/**
 * Helper to convert Mongo-persisted object to a client-visible {@link Event}
 *//*  w  ww .j  a  va2 s .  co m*/
private Event convertToEvent(DBObject eventObj) {
    String name = (String) eventObj.get(Event.FIELD_NAME);
    long scheduledTime = eventObj.containsField(Event.FIELD_SCHEDULED_TIME)
            ? ((Date) eventObj.get(Event.FIELD_SCHEDULED_TIME)).getTime()
            : -1L;
    Object data = eventObj.get(Event.FIELD_DATA);
    String lockOwner = (String) eventObj.get(Event.FIELD_LOCK_OWNER);
    String sessionId = (String) eventObj.get(Event.FIELD_SESSION_ID);

    Event event = new Event(name, scheduledTime, data, false);
    event.setLockOwner(lockOwner);
    event.setSessionId(sessionId);
    // Done
    if (logger.isTraceEnabled()) {
        logger.trace("Converted Event: \n" + "   In:  " + eventObj + "\n" + "   Out: " + event);
    }
    return event;
}

From source file:org.alfresco.bm.test.mongo.MongoTestDAO.java

License:Open Source License

/**
 * Checks if the DBObject contains a field FIELD_MASK and returns true if
 * set/*from  w w  w  .j a  v  a2s.  c  o m*/
 * 
 * @param property
 *        (DBObject) property
 * 
 * @return true if masked, false if not or field not contained.
 */
public boolean isMaskedProperty(DBObject property) {
    ArgumentCheck.checkMandatoryObject(property, "property");

    boolean result = false;
    if (property.containsField(FIELD_MASK)) {
        Object maskObj = property.get(FIELD_MASK);
        if (null != maskObj) {
            if (maskObj instanceof String) {
                result = ((String) maskObj).equals("true");
            } else if (maskObj instanceof Boolean) {
                result = (Boolean) maskObj;
            } else {
                throw new IllegalArgumentException("Unknown type of field '" + FIELD_MASK + "'");
            }
        }
    }
    return result;
}

From source file:org.alfresco.extension.wcmdeployment.mongodb.MongoDbDeploymentTarget.java

License:Open Source License

private DBObject findOrCreateVersionDoc(final DB database) {
    DBObject result = findVersionDoc(database);

    if (result == null) {
        result = createVersionDoc(database);
    }/*from  ww  w .  j ava2 s.c  o  m*/

    if (result != null) {
        if (!result.containsField("version")) {
            // Shouldn't happen, but just in case...
            log.warn("Version document was missing 'version' field.  Resetting to 0.");
            result.put("version", 0);
            setVersion(database, 0);
        }
    } else {
        // Shouldn't happen, but just in case...
        throw new IllegalStateException("Unable to find or create version document.");
    }

    return (result);
}

From source file:org.apache.gora.mongodb.utils.BSONDecorator.java

License:Apache License

/**
 * Check if the field passed in parameter exists or not. The field is passed
 * as a fully qualified name that is the path to the field from the root of
 * the document (for example: "field1" or "parent.child.field2").
 *
 * @param fieldName//from   ww w .ja v a2s.  c o m
 *          fully qualified name of the field
 * @return true if the field and all its parents exists in the decorated
 *         {@link DBObject}, false otherwise
 */
public boolean containsField(String fieldName) {
    // Prepare for in depth setting
    String[] fields = fieldName.split("\\.");
    int i = 0;
    DBObject intermediate = myBson;

    // Set intermediate parents
    while (i < (fields.length - 1)) {
        if (!intermediate.containsField(fields[i]))
            return false;
        intermediate = (DBObject) intermediate.get(fields[i]);
        i++;
    }

    // Check final field
    return intermediate.containsField(fields[fields.length - 1]);
}

From source file:org.apache.karaf.jaas.modules.mongo.internal.DefaultUserDetailService.java

License:Apache License

@Override
public UserInfo getUserInfo(String username) throws Exception {

    DB db = getDB();/*www . j av a 2  s  .com*/

    DBCollection users = db.getCollection(configuration.getUserCollectionName());

    // populate user
    DBObject userQuery = new BasicDBObject("username", username);

    BasicDBObjectBuilder userProjectionBuilder = BasicDBObjectBuilder.start().add("_id", 0).add("username", 1)
            .add("passwordHash", 1);

    // also add all custom user fields
    for (String prop : configuration.getAdditionalAttributes()) {
        userProjectionBuilder.add(prop, 1);
    }

    DBObject user = users.findOne(userQuery, userProjectionBuilder.get());
    // if nothing comes back just return empty handed
    if (user == null) {
        return null;
    }

    UserInfo userInfo = new UserInfo().withName((String) user.get("username"))
            .withPassword((String) user.get("passwordHash"));

    for (String prop : configuration.getAdditionalAttributes()) {

        // only add if property is actually present in the database
        if (user.containsField(prop)) {
            Object val = user.get(prop);
            userInfo.addProperty(prop, val != null ? val.toString() : "");
        }

    }

    // populate group
    DBCollection groups = db.getCollection(configuration.getGroupCollectionName());

    DBObject groupQuery = new BasicDBObject("members", username);

    DBCursor gc = groups.find(groupQuery,
            BasicDBObjectBuilder.start().append("_id", 0).append("name", 1).get());

    while (gc.hasNext()) {
        DBObject group = gc.next();
        userInfo.addGroup((String) group.get("name"));
    }
    gc.close();

    return userInfo;

}