Package play.data.binding

Examples of play.data.binding.BeanWrapper$Property


      if ( level01.getDataItems()[i] instanceof ElementaryDataItem ) {
        ElementaryDataItem edi = (ElementaryDataItem)level01.getDataItems()[i];
        String attrName = edi.getDataName();
        DataField dataField = (DataField)edi.interpret();
        if (dataField != null) {
          Property attr = (Property)attrs.get(attrName);
          Type type = dataField.getType() == Integer.class ? integerType : stringType;
          if (attr == null) {
            String timestampFormat = "";
            String dateFormat = "";
            String length = "" + dataField.getLength();
            String fraction = "" + dataField.getFraction();
            System.out.println("INFO [jIvalo]  - Adding new attribute: " + attrName);
            Property newAttr = addAttribute(c, attrName, type, timestampFormat, dateFormat, length, fraction);
          }
          else {
            System.out.println("INFO [jIvalo]  - Updating new attribute: " + attrName);
            attr.setType(type);
            setTagValue(attr, "length", "" + dataField.getLength());
            setTagValue(attr, "fraction", "" + dataField.getFraction());
            attrs.remove(attrName);
          }
        }
      }
      else if ( level01.getDataItems()[i] instanceof GroupDataItem ) {
        GroupDataItem gdi = (GroupDataItem)level01.getDataItems()[i];
        String className = c.getName() + "_" + gdi.getDataName();
        Class cl = getClass(p, className);
        if (cl == null) {
          addClass(p, className);
          System.out.println("INFO [jIvalo] Added class: " + className);
          cl = getClass(p, className);
        }
        else {
          System.out.println("INFO [jIvalo] Updating existing class: " + className);
        }
       
        if (cl != null) {
          addAttributes(gdi, p, cl, model);
   
          Property newAttr = null;
          if ( level01.getDataItems()[i] instanceof GroupDataItemOccurs ) {
            GroupDataItemOccurs gdio = (GroupDataItemOccurs)level01.getDataItems()[i];
            newAttr = c.createOwnedAttribute(className,p.getOwnedType(cl.getName()),gdio.getOccurs(),gdio.getOccurs());
          }
          else {
            newAttr = c.createOwnedAttribute(className,p.getOwnedType(cl.getName()),1,1);
          }
          if (newAttr != null) {
            newAttr.setVisibility(VisibilityKind.PUBLIC_LITERAL);
            addStereotype(newAttr, jIvaloAttributeStereotype);
            setTagValue(newAttr, "timestampFormat", "");
            setTagValue(newAttr, "dateFormat", "");
            setTagValue(newAttr, "length", "0");
            setTagValue(newAttr, "fraction", "0");
View Full Code Here


    cobolResourceDir = Namespaces.instance().getNamespace(namespace).getProperty("cobolResourceDir").getValue();
    targetPackage = Namespaces.instance().getNamespace(namespace).getProperty("targetPackage").getValue();
   
    jIvaloPackage = getPackage(jIvaloPackageName, model);
    jIvaloClass = getClass(jIvaloPackage, jIvaloClassName);
    Property attribute1 = getAttribute(jIvaloClass, "string");
    stringType = attribute1.getType();
    Property attribute2 = getAttribute(jIvaloClass, "integer");
    integerType = attribute2.getType();

    jIvaloClassStereotype = getStereotype(jIvaloClass, jIvaloClassSteroetypeName);
    jIvaloAttributeStereotype = getStereotype(attribute1, jIvaloAttrSteroetypeName);

  }
View Full Code Here

    Repositories.instance().getImplementation(encoding).writeModel(element,
        outputLocation, xmiVersion);
  }

  private Property addAttribute(Class c, String name, Type type, String timestampFormat, String dateFormat, String length, String fraction) {
    Property p = c.createOwnedAttribute(name, type, 1, 1);
    p.setVisibility(VisibilityKind.PUBLIC_LITERAL);
    setTagValue(p, "timestampFormat", timestampFormat);
    setTagValue(p, "dateFormat", dateFormat);
    setTagValue(p, "length", length);
    setTagValue(p, "fraction", fraction);
    return p;
View Full Code Here

   {
      public Property apply(V1Property from)
      {
         if (from != null)
         {
            Property result = WSRPTypeFactory.createProperty(from.getName(), from.getLang(), from.getStringValue());
            result.setType(WSRPConstants.XSD_STRING); // todo: not sure what to do here... :(

            List<Object> any = from.getAny();
            if (ParameterValidation.existsAndIsNotEmpty(any))
            {
               result.getAny().addAll(any);
            }
            return result;
         }
         else
         {
View Full Code Here

   public static Property createProperty(QName name, String lang, String stringValue)
   {
      ParameterValidation.throwIllegalArgExceptionIfNull(name, "name");

      Property property = new Property();
      property.setName(name);
      if (!ParameterValidation.isNullOrEmpty(lang))
      {
         property.setLang(lang);
      }
      property.setStringValue(stringValue);
      return property;
   }
View Full Code Here

   public static Property createProperty(QName name, String lang, String stringValue)
   {
      ParameterValidation.throwIllegalArgExceptionIfNull(name, "name");
      ParameterValidation.throwIllegalArgExceptionIfNullOrEmpty(lang, "language", "Property");

      Property property = new Property();
      property.setName(name);
      property.setLang(lang);
      property.setStringValue(stringValue);
      return property;
   }
View Full Code Here

   public static Property createProperty(QName name, String lang, String stringValue)
   {
      ParameterValidation.throwIllegalArgExceptionIfNull(name, "name");

      Property property = new Property();
      property.setName(name);
      if (!ParameterValidation.isNullOrEmpty(lang))
      {
         property.setLang(lang);
      }
      property.setStringValue(stringValue);
      return property;
   }
View Full Code Here

        return null != k ? siena.Util.readField(obj, k) : null;
    }
 
  public static <T> T edit(T o, String name, Map<String, String[]> params, Annotation[] annotations) {
    try {
      BeanWrapper bw = new BeanWrapper(o.getClass());
      // Start with relations
      Class<?> spClazz = o.getClass();
      /*Set<Field> fields = new HashSet<Field>();
      while (spClazz!=null) {
        Collections.addAll(fields, spClazz.getDeclaredFields());
        spClazz = spClazz.getSuperclass();
      }*/
      for (Field field : ClassInfo.getClassInfo(spClazz).allExtendedFields) {
        boolean isEntity = false;
        boolean isJson = false;
        String relation = null;
        boolean multiple = false;
        String owner = null;
        Class<?> clazz = field.getType();

        // ONE TO MANY or ONE TO ONE association
        if(ClassInfo.isModel(clazz)) {
          isEntity = true;
          relation = clazz.getName();
        }

        // MANY TO ONE association
        // type QUERY<T> + annotation @Filter
        else if(siena.Query.class.isAssignableFrom(clazz)){
          isEntity = true;
          multiple = true;
          Class<?> fieldType =
            (Class<?>) ((ParameterizedType)
                field.getGenericType()).getActualTypeArguments()[0];
          relation = fieldType.getName();
          owner = field.getAnnotation(Filter.class).value();
          // by default, takes the type of the parent entity in lower case
          if(owner == null || "".equals(owner)){
            owner = o.getClass().getName().toLowerCase();
          }
        }
        else if(Json.class.isAssignableFrom(clazz)){
          isJson = true;
        }
        else if(field.isAnnotationPresent(Embedded.class)){
          if(List.class.isAssignableFrom(clazz)){
            multiple = true;
                }
          else if(Map.class.isAssignableFrom(clazz)){
            multiple = true;
                }
                else {
                  multiple = false;
                }
        }
        else if(byte[].class.isAssignableFrom(clazz)
            /*|| Blob.class.isAssignableFrom(field.getType())*/)
        {
          // if params is present but empty, resets the older value
          @SuppressWarnings("unused")
          String[] posted = params.get(name + "." + field.getName());
          // TODO
          @SuppressWarnings("unused")
          Object val = field.get(o)
          //params.put(name + "." + field.getName(), val);
        }
       
        if (isEntity) {
          // builds entity list for many to one
          if (multiple) {
            // retrieves list to synchronize new and removed objects
            Query<?> q = (Query<?>)siena.Util.readField(o, field);
            // no limitation for the time being
            List<?> relObjs = q.fetch();

            @SuppressWarnings("unchecked")
            Class<? extends siena.Model> relClass = (Class<? extends siena.Model>)Play.classloader.loadClass(relation);
            String idName = keyName(relClass);
            String[] ids = params.get(name + "." + field.getName() + "@"+idName);
            if(ids == null) {
              ids = params.get(name + "." + field.getName() + "."+idName);
            }

            if (ids != null) {             
              params.remove(name + "." + field.getName() + "."+idName);
              params.remove(name + "." + field.getName() + "@"+idName);
             
              Field ownerField = siena.Util.getField(relClass, owner);
              for (String _id : ids) {
                if (_id.equals("")) {
                  continue;
                }
                Object idVal = Binder.directBind(_id, keyType(relClass));
               
                // verifies the field is not already owned by the object
                // if yes, no need to resave it with this owner
                boolean b = false;
                for(Object relObj:relObjs){
                  Object keyRelObj = keyValue(relObj);
                  if(keyRelObj != null && keyRelObj.equals(idVal)){
                    relObjs.remove(relObj);
                    b = true;
                    break;
                  }
                }
                if(!b){
                  siena.Model res =
                    siena.Model.all(relClass)
                      .filter(idName, idVal)
                      .get();
                  if(res!=null){
                    // sets the object to the owner field into the relation entity
                   
                    if(ownerField == null) {
                      throw new UnexpectedException("In related Model "+relClass.getName()+" owner field '"+owner+"' not found");
                    }
                    siena.Util.setField(res, ownerField, o);
                    res.save();
                  }
                   
                  else Validation.addError(name+"."+field.getName(), "validation.notFound", _id);
                }
               
              }
              // now remaining objects have to be unowned
              for(Object relObj:relObjs){
                siena.Util.setField(relObj, ownerField, null);
                SienaPlugin.pm().save(relObj);
              }

              // can't set arraylist to Query<T>
              // bw.set(field.getName(), o, l);
            }
          }
          // builds simple entity for simple association
          else {
            @SuppressWarnings("unchecked")
            Class<? extends siena.Model> relClass = (Class<? extends siena.Model>)Play.classloader.loadClass(relation);
            String idName = keyName(relClass);
            String[] ids = params.get(name + "." + field.getName() + "@"+idName);
            if(ids == null) {
              ids = params.get(name + "." + field.getName() + "."+idName);
            }
            if (ids != null && ids.length > 0 && !ids[0].equals("")) {
              params.remove(name + "." + field.getName() + "."+idName);
              params.remove(name + "." + field.getName() + "@"+idName);

              siena.Model res =
                siena.Model.all(relClass)
                  .filter(idName, Binder.directBind(ids[0], keyType(relClass)))
                  .get();
              if(res!=null)
                bw.set(field.getName(), o, res);
              else Validation.addError(name+"."+field.getName(), "validation.notFound", ids[0]);

            } else if(ids != null && ids.length > 0 && ids[0].equals("")) {
              bw.set(field.getName(), o , null);
              params.remove(name + "." + field.getName() + "."+idName);
              params.remove(name + "." + field.getName() + "@"+idName);
            }
          }                   
        }
        else if(isJson){
          String[] jsonStr = params.get(name + "." + field.getName());
          if (jsonStr != null && jsonStr.length > 0 && !jsonStr[0].equals("")) {
            try {
              //com.google.gson.JsonParser parser = new com.google.gson.JsonParser();
              //parser.parse(jsonStr[0]);
             
              Json json = Json.loads(jsonStr[0]);
              if(json!=null){
                bw.set(field.getName(), o, json);
                params.remove(name + "." + field.getName());
              }
              else Validation.addError(name+"."+field.getName(), "validation.notParsable");
            }catch(JsonParseException ex){
              ex.printStackTrace();
              Logger.error("json parserdelete exception:%s",
                  ex.getCause()!=null?ex.getCause().getMessage(): ex.getMessage());
              Validation.addError(
                  name+"."+field.getName(),
                  "validation.notParsable",
                  ex.getCause()!=null?ex.getCause().getMessage(): ex.getMessage());
            }catch(SienaException ex){
              ex.printStackTrace();
              Logger.error("json parserdelete exception:%s",
                  ex.getCause()!=null?ex.getCause().getMessage(): ex.getMessage());
              Validation.addError(
                  name+"."+field.getName(),
                  "validation.notParsable",
                  ex.getCause()!=null?ex.getCause().getMessage(): ex.getMessage());
            }
            catch(IllegalArgumentException ex){
              ex.printStackTrace();
              Logger.error("json parser exception:%s",
                  ex.getCause()!=null?ex.getCause().getMessage(): ex.getMessage());
              Validation.addError(
                  name+"."+field.getName(),
                  "validation.notParsable",
                  ex.getCause()!=null?ex.getCause().getMessage(): ex.getMessage());
            }
          }
        } 
      }
      // Then bind
      // all composites objects (simple entity, list and maps) are managed
      // by this function
      // v1.0.x code
      // bw.bind(name, o.getClass(), params, "", o);

            RootParamNode paramNode = RootParamNode.convert(params);
            // no http params to bind this object ? so we won't try to bind this object
            if(paramNode.getChild(name) == null) {
                return null;
            }
      // v1.1 compliant

            bw.bind(name, (Type)o.getClass(), params, "", o, o.getClass().getAnnotations());
     
      return (T) o;
    } catch (Exception e) {
      throw new UnexpectedException(e);
    }
View Full Code Here

      return (T) edit(this, name, params.all());
  }
 
  public static <T extends SienaSupport> T edit(Object o, String name, Map<String, String[]> params) {
    try {
      BeanWrapper bw = new BeanWrapper(o.getClass());
      // Start with relations
      Set<Field> fields = new HashSet<Field>();
      Class clazz = o.getClass();
      while (!clazz.equals(SienaSupport.class)) {
        Collections.addAll(fields, clazz.getDeclaredFields());
        clazz = clazz.getSuperclass();
      }
      for (Field field : fields) {
        boolean isEntity = false;
        boolean isJson = false;
        boolean isEmbedded = false;
        String relation = null;
        boolean multiple = false;
        String owner = null;
        String embedType = null;
        Class embedClass = null;

        // ONE TO MANY association
        // entity = type inherits SienaSupport
        if(SienaSupport.class.isAssignableFrom(field.getType())) {
          isEntity = true;
          relation = field.getType().getName();
        }

        // MANY TO ONE association
        // type QUERY<T> + annotation @Filter
        else if(Query.class.isAssignableFrom(field.getType())){
          isEntity = true;
          multiple = true;
          Class fieldType =
            (Class) ((ParameterizedType)
                field.getGenericType()).getActualTypeArguments()[0];
          relation = fieldType.getName();
          owner = field.getAnnotation(Filter.class).value();
          // by default, takes the type of the parent entity in lower case
          if(owner == null || "".equals(owner)){
            owner = o.getClass().getName().toLowerCase();
          }
        }
        else if(Json.class.isAssignableFrom(field.getType())){
          isJson = true;
        }
        else if(field.isAnnotationPresent(Embedded.class)){
          isEmbedded = true;
         
          if(List.class.isAssignableFrom(field.getType())){
            multiple = true;
                }
          else if(Map.class.isAssignableFrom(field.getType())){
            multiple = true;
                }
                else {
                  multiple = false;
                }
        }
        else if(byte[].class.isAssignableFrom(field.getType())
            || Blob.class.isAssignableFrom(field.getType()))
        {
          // if params is present but empty, resets the older value
          String[] posted = params.get(name + "." + field.getName());
          // TODO
          Object val = field.get(o)
          //params.put(name + "." + field.getName(), val);
        }
       
        if (isEntity) {
          // builds entity list for many to one
          if (multiple) {
            //Collection l = new ArrayList();

            String[] ids = params.get(name + "." + field.getName() + "@id");
            if(ids == null) {
              ids = params.get(name + "." + field.getName() + ".id");
            }

            if (ids != null) {
              params.remove(name + "." + field.getName() + ".id");
              params.remove(name + "." + field.getName() + "@id");
              for (String _id : ids) {
                if (_id.equals("")) {
                  continue;
                }
                Class relClass = Play.classloader.loadClass(relation);
                Object res =
                  Model.all(relClass)
                    .filter("id", Binder.directBind(_id, SienaUtils.findKeyType(relClass)))
                    .get();
                if(res!=null){
                  // sets the object to the owner field into the relation entity
                  relClass.getField(owner).set(res, o);
                }
                 
                else Validation.addError(name+"."+field.getName(), "validation.notFound", _id);
              }
              // can't set arraylist to Query<T>
              // bw.set(field.getName(), o, l);
            }
          }
          // builds simple entity for simple association
          else {
            String[] ids = params.get(name + "." + field.getName() + "@id");
            if(ids == null) {
              ids = params.get(name + "." + field.getName() + ".id");
            }
            if (ids != null && ids.length > 0 && !ids[0].equals("")) {
              params.remove(name + "." + field.getName() + ".id");
              params.remove(name + "." + field.getName() + "@id");

              Class relClass = Play.classloader.loadClass(relation);
              Object res =
                Model.all(relClass)
                  .filter("id", Binder.directBind(ids[0], SienaUtils.findKeyType(relClass)))
                  .get();
              if(res!=null)
                bw.set(field.getName(), o, res);
              else Validation.addError(name+"."+field.getName(), "validation.notFound", ids[0]);

            } else if(ids != null && ids.length > 0 && ids[0].equals("")) {
              bw.set(field.getName(), o , null);
              params.remove(name + "." + field.getName() + ".id");
              params.remove(name + "." + field.getName() + "@id");
            }
          }                   
        }
        else if(isJson){
          String[] jsonStr = params.get(name + "." + field.getName());
          if (jsonStr != null && jsonStr.length > 0 && !jsonStr[0].equals("")) {
            try {
              com.google.gson.JsonParser parser = new com.google.gson.JsonParser();
              parser.parse(jsonStr[0]);
             
              params.remove(name + "." + field.getName());
              Json json = Json.loads(jsonStr[0]);
              if(json!=null)
                bw.set(field.getName(), o, json);
              else Validation.addError(name+"."+field.getName(), "validation.notParsable");
            }catch(JsonParseException ex){
              ex.printStackTrace();
              Logger.error("json parser exception:%s",
                  ex.getCause()!=null?ex.getCause().getMessage(): ex.getMessage());
              Validation.addError(
                  name+"."+field.getName(),
                  "validation.notParsable",
                  ex.getCause()!=null?ex.getCause().getMessage(): ex.getMessage());
            }
            catch(IllegalArgumentException ex){
              ex.printStackTrace();
              Logger.error("json parser exception:%s",
                  ex.getCause()!=null?ex.getCause().getMessage(): ex.getMessage());
              Validation.addError(
                  name+"."+field.getName(),
                  "validation.notParsable",
                  ex.getCause()!=null?ex.getCause().getMessage(): ex.getMessage());
            }
          }
        } 
      }
      // Then bind
      // all composites objects (simple entity, list and maps) are managed
      // by this function
      // v1.0.x code
      // bw.bind(name, o.getClass(), params, "", o);

      // v1.1 compliant
      bw.bind(name, (Type)o.getClass(), params, "", o, o.getClass().getAnnotations());
     
      return (T) o;
    } catch (Exception e) {
      throw new UnexpectedException(e);
    }
View Full Code Here

  }
   
    public static <T extends SienaSupport> T addListElement(Object o, String fieldName) {
      try {
        Class clazz = o.getClass();
      BeanWrapper bw = new BeanWrapper(o.getClass());
      Field field = clazz.getField(fieldName);
     
      if(List.class.isAssignableFrom(field.getType())){
        List l = (List)field.get(o);
        if(l == null)
          l = new ArrayList();
       
        Class embedClass =
          (Class) ((ParameterizedType)
              field.getGenericType()).getActualTypeArguments()[0];
        BeanWrapper embedbw = new BeanWrapper(embedClass);
        Object embedObj = embedClass.newInstance();
       
        l.add(embedObj);
       
        Logger.debug(embedObj.toString());
View Full Code Here

TOP

Related Classes of play.data.binding.BeanWrapper$Property

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.