use of org.mongodb.morphia.mapping.MappedField in project morphia by mongodb.
the class PathTarget method resolve.
private void resolve() {
context = this.root;
position = 0;
MappedField field = null;
while (hasNext()) {
String segment = next();
if (segment.equals("$") || segment.matches("[0-9]+")) {
// array operator
if (!hasNext()) {
return;
}
segment = next();
}
field = resolveField(segment);
if (field != null) {
translate(field.getNameToStore());
if (field.isMap() && hasNext()) {
// consume the map key segment
next();
}
} else {
if (validateNames) {
throw new ValidationException(format("Could not resolve path '%s' against '%s'.", join(segments, '.'), root.getClazz().getName()));
}
}
}
target = field;
resolved = true;
}
use of org.mongodb.morphia.mapping.MappedField in project morphia by mongodb.
the class IndexHelper method collectFieldIndexes.
@SuppressWarnings("deprecation")
private List<Index> collectFieldIndexes(final MappedClass mc) {
List<Index> list = new ArrayList<Index>();
for (final MappedField mf : mc.getPersistenceFields()) {
if (mf.hasAnnotation(Indexed.class)) {
final Indexed indexed = mf.getAnnotation(Indexed.class);
list.add(convert(indexed, mf.getNameToStore()));
} else if (mf.hasAnnotation(Text.class)) {
final Text text = mf.getAnnotation(Text.class);
list.add(convert(text, mf.getNameToStore()));
}
}
return list;
}
use of org.mongodb.morphia.mapping.MappedField in project morphia by mongodb.
the class QueryValidator method validateQuery.
/**
* Validate the path, and value type, returning the mapped field for the field at the path
*/
static MappedField validateQuery(final Class clazz, final Mapper mapper, final StringBuilder origProp, final FilterOperator op, final Object val, final boolean validateNames, final boolean validateTypes) {
MappedField mf = null;
final String prop = origProp.toString();
boolean hasTranslations = false;
if (!origProp.substring(0, 1).equals("$")) {
final String[] parts = prop.split("\\.");
if (clazz == null) {
return null;
}
MappedClass mc = mapper.getMappedClass(clazz);
//CHECKSTYLE:OFF
for (int i = 0; ; ) {
//CHECKSTYLE:ON
final String part = parts[i];
boolean fieldIsArrayOperator = part.equals("$") || part.matches("[0-9]+");
mf = mc.getMappedField(part);
//translate from java field name to stored field name
if (mf == null && !fieldIsArrayOperator) {
mf = mc.getMappedFieldByJavaField(part);
if (validateNames && mf == null) {
throw new ValidationException(format("The field '%s' could not be found in '%s' while validating - %s; if " + "you wish to continue please disable validation.", part, mc.getClazz().getName(), prop));
}
hasTranslations = true;
if (mf != null) {
parts[i] = mf.getNameToStore();
}
}
i++;
if (mf != null && mf.isMap()) {
//skip the map key validation, and move to the next part
i++;
}
if (i >= parts.length) {
break;
}
if (!fieldIsArrayOperator) {
//catch people trying to search/update into @Reference/@Serialized fields
if (validateNames && !canQueryPast(mf)) {
throw new ValidationException(format("Cannot use dot-notation past '%s' in '%s'; found while" + " validating - %s", part, mc.getClazz().getName(), prop));
}
if (mf == null && (mc.isInterface() || !validateNames)) {
break;
} else if (mf == null) {
throw new ValidationException(format("The field '%s' could not be found in '%s'", prop, mc.getClazz().getName()));
}
//get the next MappedClass for the next field validation
mc = mapper.getMappedClass((mf.isSingleValue()) ? mf.getType() : mf.getSubClass());
}
}
//record new property string if there has been a translation to any part
if (hasTranslations) {
// clear existing content
origProp.setLength(0);
origProp.append(parts[0]);
for (int i = 1; i < parts.length; i++) {
origProp.append('.');
origProp.append(parts[i]);
}
}
if (validateTypes && mf != null) {
List<ValidationFailure> typeValidationFailures = new ArrayList<ValidationFailure>();
boolean compatibleForType = isCompatibleForOperator(mc, mf, mf.getType(), op, val, typeValidationFailures);
List<ValidationFailure> subclassValidationFailures = new ArrayList<ValidationFailure>();
boolean compatibleForSubclass = isCompatibleForOperator(mc, mf, mf.getSubClass(), op, val, subclassValidationFailures);
if ((mf.isSingleValue() && !compatibleForType) || mf.isMultipleValues() && !(compatibleForSubclass || compatibleForType)) {
if (LOG.isWarningEnabled()) {
LOG.warning(format("The type(s) for the query/update may be inconsistent; using an instance of type '%s' " + "for the field '%s.%s' which is declared as '%s'", val.getClass().getName(), mf.getDeclaringClass().getName(), mf.getJavaFieldName(), mf.getType().getName()));
typeValidationFailures.addAll(subclassValidationFailures);
LOG.warning("Validation warnings: \n" + typeValidationFailures);
}
}
}
}
return mf;
}
use of org.mongodb.morphia.mapping.MappedField in project morphia by mongodb.
the class QueryImpl method retrieveKnownFields.
@Override
public Query<T> retrieveKnownFields() {
final MappedClass mc = ds.getMapper().getMappedClass(clazz);
final List<String> fields = new ArrayList<String>(mc.getPersistenceFields().size() + 1);
for (final MappedField mf : mc.getPersistenceFields()) {
fields.add(mf.getNameToStore());
}
retrievedFields(true, fields.toArray(new String[fields.size()]));
return this;
}
use of org.mongodb.morphia.mapping.MappedField in project morphia by mongodb.
the class EmbeddedReferenceType method verifyCoverage.
private void verifyCoverage(final DBObject dbObject) {
for (MappedField field : getMorphia().getMapper().getMappedClass(ReferenceType.class).getPersistenceFields()) {
String name = field.getNameToStore();
boolean found = dbObject.containsField(name);
if (!found) {
for (String s : dbObject.keySet()) {
found |= s.startsWith(name + ".");
}
}
assertTrue("Not found in dbObject: " + name, found);
}
}
Aggregations