org.mongodb.morphia.mapping.MappedField Java Examples

The following examples show how to use org.mongodb.morphia.mapping.MappedField. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CauseActionConverterTest.java    From DotCi with MIT License 6 votes vote down vote up
@Test
public void should_get_cause_from_dbObject() throws Exception {
    BasicDBObject cause1DbObject = new BasicDBObject("cause1", "cause1");
    DBObject causes = new BasicDBObjectBuilder().add("causes", Arrays.asList(cause1DbObject)).get();

    Mapper mapper = Mockito.mock(Mapper.class);
    Cause cause1 = new NullBuildCause();
    when(mapper.fromDBObject(null, cause1DbObject, null)).thenReturn(cause1);

    CauseActionConverter converter = new CauseActionConverter();
    converter.setMapper(mapper);
    CauseAction action = converter.decode(CauseAction.class, causes, Mockito.mock(MappedField.class));

    Assert.assertEquals(1, action.getCauses().size());
    Assert.assertEquals(cause1, action.getCauses().get(0));

}
 
Example #2
Source File: JenkinsEmbeddedMapper.java    From DotCi with MIT License 6 votes vote down vote up
@Override
public void fromDBObject(final DBObject dbObject, final MappedField mf, final Object entity, final EntityCache cache, final Mapper mapper) {

    final Key key = extractKey(mapper, mf, (DBObject) dbObject.get(mf.getNameToStore()));
    if (key != null && cache.getEntity(key) != null) {
        final Object object = cache.getEntity(key);
        mf.setFieldValue(entity, object);
    } else if (this.customMappers.containsKey(mf.getType())) {
        this.customMappers.get(mf.getType()).fromDBObject(dbObject, mf, entity, cache, mapper);
    } else if (isAwkwardMap(mf)) {
        this.awkwardMapper.fromDBObject(dbObject, mf, entity, cache, mapper);
    } else {
        // the first two conditions (isMap and isMultipleValues) are part of the hack to fix handling primitives
        if (mf.isMap()) {
            readMap(dbObject, mf, entity, cache, mapper);
        } else if (mf.isMultipleValues()) {
            readCollection(dbObject, mf, entity, cache, mapper);
        } else {
            mapper.getOptions().getEmbeddedMapper().fromDBObject(dbObject, mf, entity, cache, mapper);
        }

        this.serializationMethodInvoker.callReadResolve(mf.getFieldValue(entity));
    }


}
 
Example #3
Source File: ParametersDefinitionPropertyCoverter.java    From DotCi with MIT License 6 votes vote down vote up
@Override
public Object decode(Class targetClass, Object fromDBObject, MappedField optionalExtraInfo) {
    if (fromDBObject == null) return null;

    List<DBObject> list = (List) fromDBObject;

    List<ParameterDefinition> parameterDefinitions = new ArrayList<ParameterDefinition>();

    // TODO parsing checks
    for (DBObject dbObject : list) {
        ParameterDefinition definition = (ParameterDefinition) getMapper()
            .fromDBObject(ParameterDefinition.class, dbObject, getMapper().createEntityCache());

        parameterDefinitions.add(definition);
    }

    return new ParametersDefinitionProperty(parameterDefinitions);
}
 
Example #4
Source File: JenkinsMappedClass.java    From DotCi with MIT License 6 votes vote down vote up
@Override
protected MappedField mapField(final Field field) {
    if ("core".equals(field.getName())) {
        final ManuallyConfiguredMappedField mf = new ManuallyConfiguredMappedField(field, getClazz()) {
            @Override
            public void discover() {
                this.realType = this.field.getType();
                this.isMap = false;
                this.isSet = false;
                this.isSingleValue = false;
                this.isArray = this.realType.isArray();
                this.isCollection = Collection.class.isAssignableFrom(this.realType);
                this.constructor = null;
                this.isMongoType = false;
                this.subType = this.realType.getComponentType();
            }
        };

        mf.discover();

        return mf;
    }
    return super.mapField(field);
}
 
Example #5
Source File: CopyOnWriteListMapper.java    From DotCi with MIT License 6 votes vote down vote up
@Override
public void fromDBObject(DBObject dbObject, MappedField mf, Object entity, EntityCache cache, Mapper mapper) {
    BasicDBList cowlist = (BasicDBList) dbObject.get(mf.getNameToStore());

    if (cowlist == null)
        throw new IllegalArgumentException("Improperly formatted DBObject for CopyOnWriteList");

    List core = new ArrayList();
    for (Object obj : cowlist) {
        DBObject listEntryDbObj = (DBObject) obj;

        // Hack until we can coax MappedField to understand what CopyOnWriteList is. Eliminate as soon as possible.
        // Currently mf.getSubType() is null because MappedField does not use Iterable to determine a list and thus
        // does not check for subtypes.
        Class clazz = mapper.getOptions().getObjectFactory().createInstance(mapper, mf, listEntryDbObj).getClass();

        core.add(mapper.fromDBObject(clazz, listEntryDbObj, cache));
    }
    mf.setFieldValue(entity, new CopyOnWriteList(core));
}
 
Example #6
Source File: DescribableListConverter.java    From DotCi with MIT License 6 votes vote down vote up
@Override
public Object decode(Class targetClass, Object fromDBObject, MappedField optionalExtraInfo) {
    if (fromDBObject == null) return null;

    BasicDBList rawList = (BasicDBList) fromDBObject;

    List list = new ArrayList();
    for (Object obj : rawList) {
        DBObject dbObj = (DBObject) obj;
        list.add(getMapper().fromDBObject(optionalExtraInfo.getSubClass(), dbObj, getMapper().createEntityCache()));
    }

    Saveable owner = null; // TODO figure out how to associate the deserialized project here

    return new DescribableList(owner, list);
}
 
Example #7
Source File: DateTimeConverter.java    From alchemy with MIT License 5 votes vote down vote up
@Override
public Object encode(Object value, MappedField optionalExtraInfo) {
    final DateTime dateTime = (DateTime) value;
    if (dateTime == null) {
        return null;
    }
    return dateTime.getMillis();
}
 
Example #8
Source File: JenkinsEmbeddedMapper.java    From DotCi with MIT License 5 votes vote down vote up
@Deprecated
private void readMap(final DBObject dbObject, final MappedField mf, final Object entity, final EntityCache cache, final Mapper mapper) {
    final Map map = mapper.getOptions().getObjectFactory().createMap(mf);

    final DBObject dbObj = (DBObject) mf.getDbObjectValue(dbObject);
    new IterHelper<>().loopMap(dbObj, new IterHelper.MapIterCallback<Object, Object>() {
        @Override
        public void eval(final Object key, final Object val) {
            Object newEntity = null;

            //run converters
            if (val != null) {
                if (mapper.getConverters().hasSimpleValueConverter(mf) || mapper.getConverters()
                    .hasSimpleValueConverter(mf.getSubClass())) {
                    newEntity = mapper.getConverters().decode(mf.getSubClass(), val, mf);
                } else if (mapper.getConverters().hasSimpleValueConverter(val.getClass())) { // added this condition to handle incorrectly mapped primitives.
                    newEntity = mapper.getConverters().decode(val.getClass(), val, mf);
                } else {
                    if (val instanceof DBObject) {
                        newEntity = readMapOrCollectionOrEntity((DBObject) val, mf, cache, mapper);
                    } else {
                        throw new MappingException("Embedded element isn't a DBObject! How can it be that is a " + val.getClass());
                    }

                }
            }

            final Object objKey = mapper.getConverters().decode(mf.getMapKeyClass(), key);
            map.put(objKey, newEntity);
        }
    });

    if (!map.isEmpty()) {
        mf.setFieldValue(entity, map);
    }
}
 
Example #9
Source File: JenkinsEmbeddedMapper.java    From DotCi with MIT License 5 votes vote down vote up
private Key extractKey(final Mapper mapper, final MappedField mf, final DBObject dbObject) {
    if (mapper == null || mf == null || dbObject == null || (dbObject instanceof BasicDBList))
        return null;

    final ObjectId objectId = (ObjectId) dbObject.get(mapper.ID_KEY);
    //HACKY GET RID OF SOON
    final Object obj = mapper.getOptions().getObjectFactory().createInstance(mapper, mf, dbObject);

    if (objectId == null || obj == null) return null;

    return new Key(obj.getClass(), objectId);
}
 
Example #10
Source File: SpecialClassConverter.java    From DotCi with MIT License 5 votes vote down vote up
@Override
public Object encode(final Object value, final MappedField optionalExtraInfo) {
    if (value == null) {
        return null;
    } else {
        return ((Class) value).getName();
    }
}
 
Example #11
Source File: SpecialClassConverter.java    From DotCi with MIT License 5 votes vote down vote up
@Override
public Object decode(final Class targetClass, final Object fromDBObject, final MappedField optionalExtraInfo) {
    if (fromDBObject == null) {
        return null;
    }

    final String l = fromDBObject.toString();
    try {
        return classLoader.loadClass(l);
    } catch (ClassNotFoundException e) {
        throw new MappingException("Cannot create class from Name '" + l + "'", e);
    }
}
 
Example #12
Source File: CopyOnWriteListMapper.java    From DotCi with MIT License 5 votes vote down vote up
@Override
public void toDBObject(Object entity, MappedField mf, DBObject dbObject, Map<Object, DBObject> involvedObjects, Mapper mapper) {
    final String name = mf.getNameToStore();
    CopyOnWriteList copyOnWriteList = (CopyOnWriteList) mf.getFieldValue(entity);
    List core = new ArrayList();

    mf.getSubType();

    for (Object obj : copyOnWriteList) {
        core.add(mapper.toDBObject(obj, involvedObjects));
    }

    dbObject.put(name, core);
}
 
Example #13
Source File: CopyOnWriteListConverter.java    From DotCi with MIT License 5 votes vote down vote up
@Override
public Object encode(Object value, MappedField optionalExtraInfo) {
    if (value == null) return null;

    CopyOnWriteList copyOnWriteList = (CopyOnWriteList) value;
    List core = new BasicDBList();

    for (Object obj : copyOnWriteList) {
        core.add(getMapper().toDBObject(obj));
    }

    return core;
}
 
Example #14
Source File: CopyOnWriteListConverter.java    From DotCi with MIT License 5 votes vote down vote up
@Override
public Object decode(Class targetClass, Object fromDBObject, MappedField optionalExtraInfo) {
    if (fromDBObject == null) return null;

    BasicDBList rawList = (BasicDBList) fromDBObject;

    List core = new ArrayList();
    for (Object obj : rawList) {
        DBObject dbObj = (DBObject) obj;
        core.add(getMapper().fromDBObject(optionalExtraInfo.getSubClass(), dbObj, getMapper().createEntityCache()));
    }

    return new CopyOnWriteList(core);
}
 
Example #15
Source File: DescribableListConverter.java    From DotCi with MIT License 5 votes vote down vote up
@Override
public Object encode(Object value, MappedField optionalExtraInfo) {
    if (value == null) return null;

    DescribableList describableList = (DescribableList) value;

    BasicDBList convertedList = new BasicDBList();

    for (Object obj : describableList.toList()) {
        convertedList.add(getMapper().toDBObject(obj));
    }

    return convertedList;
}
 
Example #16
Source File: MapKeyValueMapper.java    From DotCi with MIT License 5 votes vote down vote up
@Override
public void fromDBObject(final DBObject dbObject, final MappedField mf, final Object entity, final EntityCache cache, final Mapper mapper) {
    final BasicDBList entriesList = (BasicDBList) dbObject.get(mf.getNameToStore());

    final Map map = mapper.getOptions().getObjectFactory().createMap(mf);

    for (final Object obj : entriesList) {
        final DBObject listEntryDbObj = (DBObject) obj;
        //Map.Entry entry = (Map.Entry) mapper.getOptions().getObjectFactory().createInstance(Map.Entry.class);
        final Pair entry = new Pair();
        try {
            final Field key = entry.getClass().getDeclaredField(KEY);
            final EntryMappedField keyField = new EntryMappedField(key, entry.getClass(), mf);

            final Field value = entry.getClass().getDeclaredField(VALUE);
            final EntryMappedField valueField = new EntryMappedField(value, entry.getClass(), mf);

            mapper.getOptions().getEmbeddedMapper().fromDBObject(listEntryDbObj, keyField, entry, cache, mapper);
            mapper.getOptions().getEmbeddedMapper().fromDBObject(listEntryDbObj, valueField, entry, cache, mapper);

            map.put(entry.getKey(), entry.getValue());

        } catch (final NoSuchFieldException e) {
            throw new RuntimeException(e);
        }
    }

    mf.setFieldValue(entity, map);
}
 
Example #17
Source File: DateTimeConverter.java    From alchemy with MIT License 5 votes vote down vote up
@Override
public Object decode(Class clazz, Object o, MappedField mappedField) {
    final Long instant = (Long) o;
    if (instant == null) {
        return null;
    }
    return new DateTime(instant);
}
 
Example #18
Source File: CauseActionConverter.java    From DotCi with MIT License 5 votes vote down vote up
@Override
public CauseAction decode(final Class targetClass, final Object fromDBObject, final MappedField optionalExtraInfo) {
    try (ACLContext _ = ACL.as(Jenkins.ANONYMOUS)) {
        if (fromDBObject == null) return null;

        final List causes = new ArrayList();
        final List rawList = (List) ((DBObject) fromDBObject).get("causes");
        for (final Object obj : rawList) {
            final DBObject dbObj = (DBObject) obj;
            final Object cause = getMapper().fromDBObject(optionalExtraInfo.getSubClass(), dbObj, getMapper().createEntityCache());
            causes.add(cause);
        }
        return new CauseAction(causes);
    }
}
 
Example #19
Source File: CauseActionConverter.java    From DotCi with MIT License 5 votes vote down vote up
@Override
public Object encode(final Object value, final MappedField optionalExtraInfo) {
    if (value == null) return null;
    final CauseAction action = (CauseAction) value;
    final List causes = new BasicDBList();

    for (final Object obj : action.getCauses()) {
        causes.add(getMapper().toDBObject(obj));
    }
    return BasicDBObjectBuilder.start("causes", causes).add("className", CauseAction.class.getName()).get();
}
 
Example #20
Source File: ParametersDefinitionPropertyCoverter.java    From DotCi with MIT License 5 votes vote down vote up
@Override
public Object encode(Object value, MappedField optionalExtraInfo) {
    if (value == null) return null;

    ParametersDefinitionProperty parametersDefinitionProperty = (ParametersDefinitionProperty) value;

    BasicDBList parameters = new BasicDBList();
    for (ParameterDefinition definition : parametersDefinitionProperty.getParameterDefinitions()) {
        parameters.add(getMapper().toDBObject(definition));
    }

    return parameters;
}
 
Example #21
Source File: ResultConverter.java    From DotCi with MIT License 5 votes vote down vote up
@Override
public Object decode(Class targetClass, Object fromDBObject, MappedField optionalExtraInfo) {
    if (fromDBObject == null) return null;

    String resultName = (String) fromDBObject;
    return Result.fromString(resultName);
}
 
Example #22
Source File: AxisListConverter.java    From DotCi with MIT License 5 votes vote down vote up
@Override
public Object decode(Class targetClass, Object fromDBObject, MappedField optionalExtraInfo) {
    if (fromDBObject == null) return null;

    BasicDBList rawList = (BasicDBList) fromDBObject;

    AxisList axisList = new AxisList();
    for (Object obj : rawList) {
        DBObject dbObj = (DBObject) obj;
        axisList.add((Axis) getMapper().fromDBObject(optionalExtraInfo.getSubClass(), dbObj, getMapper().createEntityCache()));
    }

    return axisList;
}
 
Example #23
Source File: AxisListConverter.java    From DotCi with MIT License 5 votes vote down vote up
@Override
public Object encode(Object value, MappedField optionalExtraInfo) {
    if (value == null) return null;

    AxisList axisList = (AxisList) value;

    BasicDBList convertedList = new BasicDBList();

    for (Axis axis : axisList) {
        convertedList.add(getMapper().toDBObject(axis));
    }

    return convertedList;
}
 
Example #24
Source File: MapKeyValueMapper.java    From DotCi with MIT License 5 votes vote down vote up
@Override
public void toDBObject(final Object entity, final MappedField mf, final DBObject dbObject, final Map<Object, DBObject> involvedObjects, final Mapper mapper) {
    final String name = mf.getNameToStore();

    final Map<?, ?> awkwardMap = (Map<?, ?>) mf.getFieldValue(entity);
    final List out = new ArrayList();

    for (final Map.Entry entry : awkwardMap.entrySet()) {
        final DBObject mappedEntry = new BasicDBObject();

        try {
            final Field key = entry.getClass().getDeclaredField(KEY);
            final EntryMappedField keyField = new EntryMappedField(key, entry.getClass(), mf);

            final Field value = entry.getClass().getDeclaredField(VALUE);
            final EntryMappedField valueField = new EntryMappedField(value, entry.getClass(), mf);

            mapper.getOptions().getDefaultMapper().toDBObject(entry, keyField, mappedEntry, involvedObjects, mapper);
            mapper.getOptions().getDefaultMapper().toDBObject(entry, valueField, mappedEntry, involvedObjects, mapper);

            out.add(mappedEntry);
        } catch (final NoSuchFieldException e) {
            throw new RuntimeException(e);
        }
    }

    dbObject.put(name, out);
}
 
Example #25
Source File: MapKeyValueMapper.java    From DotCi with MIT License 5 votes vote down vote up
EntryMappedField(final Field field, final Class<?> clazz, final MappedField mappedField) {
    super(field, clazz);
    super.discover();
    if (MapKeyValueMapper.KEY.equals(field.getName())) {
        this.realType = mappedField.getMapKeyClass();
        this.subType = this.realType.getComponentType();
    } else if (MapKeyValueMapper.VALUE.equals(field.getName())) {
        this.realType = mappedField.getSubClass();
        this.subType = mappedField.getSubType() instanceof ParameterizedType ? ((ParameterizedType) mappedField.getSubType()).getActualTypeArguments()[0] : null;
    } else {
        throw new RuntimeException("Entry field is neither key nor entry");
    }

    if (this.realType.isArray()
        || Collection.class.isAssignableFrom(this.realType)
        || Map.class.isAssignableFrom(this.realType)) {

        this.isSingleValue = false;

        this.isMap = Map.class.isAssignableFrom(this.realType);
        this.isSet = Set.class.isAssignableFrom(this.realType);
        //for debugging
        this.isCollection = Collection.class.isAssignableFrom(this.realType);
        this.isArray = this.realType.isArray();

        if (this.isMap) {
            this.mapKeyType = ReflectionUtils.getParameterizedType(field, 0);
        }
    }
    try {
        this.constructor = this.realType.getDeclaredConstructor();
        this.constructor.setAccessible(true);
    } catch (final NoSuchMethodException e) {
        this.constructor = null;
    }
}
 
Example #26
Source File: PermissionsConverter.java    From DotCi with MIT License 4 votes vote down vote up
@Override
public Object decode(Class targetClass, Object fromDBObject, MappedField optionalExtraInfo) {
    if (fromDBObject == null) return null;

    return Permission.fromId((String) fromDBObject);
}
 
Example #27
Source File: ResultConverter.java    From DotCi with MIT License 4 votes vote down vote up
@Override
public Object encode(Object value, MappedField optionalExtraInfo) {
    if (value == null) return null;

    return value.toString();
}
 
Example #28
Source File: PermissionsConverter.java    From DotCi with MIT License 4 votes vote down vote up
@Override
public Object encode(Object value, MappedField optionalExtraInfo) {
    if (value == null) return null;

    return ((Permission) value).getId();
}
 
Example #29
Source File: JenkinsMappedClass.java    From DotCi with MIT License 4 votes vote down vote up
protected MappedField mapField(final Field field) {
    final ManuallyConfiguredMappedField mf = new ManuallyConfiguredMappedField(field, getClazz());
    mf.discover();

    return mf;
}
 
Example #30
Source File: JenkinsEmbeddedMapper.java    From DotCi with MIT License 4 votes vote down vote up
private boolean isAwkwardMap(final MappedField mf) {
    return mf.isMap() && Permission.class == mf.getMapKeyClass();
}