Examples of MappedField


Examples of com.github.jmkgreen.morphia.mapping.MappedField

        this(query, field, op, value, validateNames, validateTypes, false);
    }

    protected FieldCriteria(QueryImpl<?> query, String field, FilterOperator op, Object value, boolean validateNames, boolean validateTypes, boolean not) {
        StringBuffer sb = new StringBuffer(field); //validate might modify prop string to translate java field name to db field name
        MappedField mf = DefaultMapper.validate(query.getEntityClass(), query.getDatastore().getMapper(), sb, op, value, validateNames, validateTypes);
        field = sb.toString();

        Mapper mapr = query.getDatastore().getMapper();

        MappedClass mc = null;
        try {
            if (value != null && !ReflectionUtils.isPropertyType(value.getClass()) && !ReflectionUtils.implementsInterface(value.getClass(), Iterable.class))
                if (mf != null && !mf.isTypeMongoCompatible())
                    mc = mapr.getMappedClass((mf.isSingleValue()) ? mf.getType() : mf.getSubClass());
                else
                    mc = mapr.getMappedClass(value);
        } catch (Exception e) {
            //Ignore these. It is likely they related to mapping validation that is unimportant for queries (the query will fail/return-empty anyway)
            log.debug("Error during mapping of filter criteria: ", e);
View Full Code Here

Examples of com.github.jmkgreen.morphia.mapping.MappedField

  void processIgnoreFieldsAnnotations(){
    for(MappedClass mc : ds.getMapper().getMappedClasses()) {
      IgnoreFields ignores = (IgnoreFields) mc.getAnnotation(IgnoreFields.class);
      if (ignores != null) {
        for(String field : ignores.value().split(",")) {
          MappedField mf = mc.getMappedFieldByJavaField(field);
          mc.getMappedFields().remove(mf);
        }
      }
    }
  }
View Full Code Here

Examples of com.github.jmkgreen.morphia.mapping.MappedField

            destMC.addAnnotation(e.getKey(), ann);
      }
      //copy the fields.
      for(MappedField mf : sourceMC.getMappedFields()){
        Map<Class<? extends Annotation>, Annotation> annMap = mf.getAnnotations();
        MappedField destMF = destMC.getMappedFieldByJavaField(mf.getJavaFieldName());
        if (destMF != null && annMap != null && annMap.size() > 0) {
          for(Entry e : annMap.entrySet())
            destMF.addAnnotation((Class)e.getKey(), (Annotation)e.getValue());
        }
      }

    }
View Full Code Here

Examples of com.github.jmkgreen.morphia.mapping.MappedField

    MappedClass addAnnotation(Class clazz, String field, Annotation... annotations) {
      if (annotations == null || annotations.length == 0)
        throw new IllegalArgumentException("Must specify annotations");

      MappedClass mc = mapr.getMappedClass(clazz);
      MappedField mf = mc.getMappedFieldByJavaField(field);
      if(mf == null)
        throw new IllegalArgumentException("Field \""+ field + "\" does not exist on: " + mc);

      for(Annotation an : annotations)
        mf.putAnnotation(an);

      return mc;}
View Full Code Here

Examples of com.github.jmkgreen.morphia.mapping.MappedField

        };
    }

  @Test
  public void testVersionFieldNameContribution() throws Exception {
    MappedField mappedFieldByJavaField = morphia.getMapper().getMappedClass(ALong.class).getMappedFieldByJavaField("v");
    Assert.assertEquals("versionNameContributedByAnnotation", mappedFieldByJavaField.getNameToStore());
  }
View Full Code Here

Examples of com.github.jmkgreen.morphia.mapping.MappedField

    protected void add(UpdateOperator op, String f, Object value, boolean convert) {
        if (value == null)
            throw new QueryException("Val cannot be null");

        Object val = null;
        MappedField mf = null;
        if (validateNames || validateTypes) {
            StringBuffer sb = new StringBuffer(f);
            mf = DefaultMapper.validate(clazz, mapr, sb, FilterOperator.EQUAL, val, validateNames, validateTypes);
            f = sb.toString();
        }
View Full Code Here

Examples of com.github.jmkgreen.morphia.mapping.MappedField

     * @return
     */
    protected <T> WriteResult tryVersionedUpdate(DBCollection dbColl, T entity, DBObject dbObj, WriteConcern wc, DB db, MappedClass mc) {
        WriteResult wr = null;

        MappedField mfVersion = mc.getFieldsAnnotatedWith(Version.class).get(0);
        String versionKeyName = mfVersion.getNameToStore();
        Long oldVersion = (Long) mfVersion.getFieldValue(entity);
        long newVersion = VersionHelper.nextValue(oldVersion);
        dbObj.put(versionKeyName, newVersion);

        if (oldVersion != null && oldVersion > 0) {
            Object idValue = dbObj.get(Mapper.ID_KEY);

            UpdateResults<T> res = update(find(dbColl.getName(), (Class<T>) entity.getClass()).filter(Mapper.ID_KEY, idValue).filter(versionKeyName, oldVersion),
                    dbObj,
                    false,
                    false,
                    wc);

            wr = res.getWriteResult();

            if (res.getUpdatedCount() != 1)
                throw new ConcurrentModificationException("Entity of class " + entity.getClass().getName()
                        + " (id='" + idValue + "',version='" + oldVersion + "') was concurrently updated.");
        } else if (wc == null)
            wr = dbColl.save(dbObj);
        else
            wr = dbColl.save(dbObj, wc);

        //update the version.
        mfVersion.setFieldValue(entity, newVersion);
        return wr;
    }
View Full Code Here

Examples of com.github.jmkgreen.morphia.mapping.MappedField

        MappedClass mc = mapr.getMappedClass(ent);
        Query<T> q = (Query<T>) createQuery(mc.getClazz());
        q.disableValidation().filter(Mapper.ID_KEY, mapr.getId(ent));

        if (mc.getFieldsAnnotatedWith(Version.class).size() > 0) {
            MappedField versionMF = mc.getFieldsAnnotatedWith(Version.class).get(0);
            Long oldVer = (Long) versionMF.getFieldValue(ent);
            q.filter(versionMF.getNameToStore(), oldVer);
            ops.set(versionMF.getNameToStore(), VersionHelper.nextValue(oldVer));
        }

        return update(q, ops);
    }
View Full Code Here

Examples of org.mongodb.morphia.mapping.MappedField

     */
    static MappedField validateQuery(final Class clazz, final Mapper mapper, final StringBuilder origProp, final FilterOperator op,
                                     final Object val, final boolean validateNames, final boolean validateTypes) {
        //TODO: cache validations (in static?).

        MappedField mf = null;
        final String prop = origProp.toString();
        boolean hasTranslations = false;

        if (validateNames) {
            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];
                mf = mc.getMappedField(part);

                //translate from java field name to stored field name
                if (mf == null) {
                    mf = mc.getMappedFieldByJavaField(part);
                    if (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,
                                                             clazz.getName(), prop
                                                            ));
                    }
                    hasTranslations = true;
                    parts[i] = mf.getNameToStore();
                }

                i++;
                if (mf.isMap()) {
                    //skip the map key validation, and move to the next part
                    i++;
                }

                //catch people trying to search/update into @Reference/@Serialized fields
                if (i < parts.length && !canQueryPast(mf)) {
                    throw new ValidationException(format("Can not use dot-notation past '%s' could not be found in '%s' while"
                                                         + " validating - %s", part, clazz.getName(), prop));
                }

                if (i >= parts.length) {
                    break;
                }
                //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) {
                origProp.setLength(0); // clear existing content
                origProp.append(parts[0]);
                for (int i = 1; i < parts.length; i++) {
                    origProp.append('.');
                    origProp.append(parts[i]);
                }
            }

            if (validateTypes) {
                List<ValidationFailure> validationFailures = new ArrayList<ValidationFailure>();
                boolean compatibleForType = isCompatibleForOperator(mf, mf.getType(), op, val, validationFailures);
                boolean compatibleForSubclass = isCompatibleForOperator(mf, mf.getSubClass(), op, val, validationFailures);

                if ((mf.isSingleValue() && !compatibleForType)
                    || mf.isMultipleValues() && !(compatibleForSubclass || compatibleForType)) {

                    if (LOG.isWarningEnabled()) {
                        String className = val == null ? "null" : val.getClass().getName();
                        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'", className,
                                           mf.getDeclaringClass().getName(), mf.getJavaFieldName(), mf.getType().getName()
                                          ));
                        LOG.warning("Validation warnings: \n" + validationFailures);
                    }
                }
            }
View Full Code Here

Examples of org.mongodb.morphia.mapping.MappedField

    @Test
    public void shouldAllowValuesOfList() {
        // expect
        MappedClass mappedClass = new MappedClass(SimpleEntity.class, new Mapper());
        MappedField mappedField = mappedClass.getMappedField("name");
        assertThat(QueryValidator.isCompatibleForOperator(mappedField, List.class, EQUAL, new ArrayList<String>(),
                                                          new ArrayList<ValidationFailure>()), is(true));
    }
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.