Package com.github.jmkgreen.morphia.mapping

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


  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

            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

    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

        };
    }

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

    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

     * @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

        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

TOP

Related Classes of com.github.jmkgreen.morphia.mapping.MappedField

Copyright © 2018 www.massapicom. 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.