コード例 #1
0
  public boolean updateBrand(Brand to) {
    try {
      Long id = to.getId();
      System.out.println("id: " + id);
      Brand po = SpringUtil.getBrandManager().getBrand(id);
      //    		String bkn = po.getKeyName();
      //    		String fkn = null;
      //    		String fid = null;
      //    		DSFile newFile = null, oldFile = null;
      MyPropertyUtil.copySimpleProperties(po, to);
      //    		if(to.getLogoFile()==null) {
      //    			// no new logofile, do nothing
      //    		}
      //    		else {
      //    			oldFile = po.getLogoFile();
      //        		newFile = to.getLogoFile();
      //    			fkn = DataStoreUtils.genKeyName(newFile);
      //    			fid = KeyFactory.keyToString(new KeyFactory.Builder("Brand",bkn).addChild("DSFile",
      // fkn).getKey());
      //    			newFile.setKeyName(fkn);
      //    			po.setLogoFileId(fid);
      //    			po.setLogoFile(newFile);
      //    			boolean suc = txdelete(oldFile);
      //    		}

      SpringUtil.getBrandManager().saveBrand(po);
      Long res = po.getId();
      return true;
    } catch (Exception e) {
      e.printStackTrace();
      throw new RuntimeException(e);
    }
  }
コード例 #2
0
  // to be used in DAOImpl.update only
  public static void copySimpleProperties(ModelObject dest, ModelObject orig) {
    try {
      PropertyDescriptor srcDescriptors[] =
          BeanUtilsBean.getInstance().getPropertyUtils().getPropertyDescriptors(dest);
      for (PropertyDescriptor pd : srcDescriptors) {
        String name = pd.getName();
        if ("class".equals(name)) {
          continue;
        }
        if ("id".equals(name)) {
          // TODO hardcoded.  for update, never change Id field
          continue;
        }

        Class type = pd.getPropertyType();
        debug("copySimpleProperties--  name=" + name + ", type=" + type);
        if (Collection.class.isAssignableFrom(type)) {
          Field field = dest.getClass().getDeclaredField(name);
          if (MyPropertyUtil.isFieldCollectionOfModel(field)) {
            // ingore collection of Model
            continue;
          } else {

            PropertyUtils.setProperty(dest, name, PropertyUtils.getProperty(orig, name));
          }
        } else if (ModelObject.class.isAssignableFrom(type)) {
          // we do not support editing of associated object at this point
          // nor pointing to another associated object
          continue;
        } else {
          PropertyUtils.setProperty(dest, name, PropertyUtils.getProperty(orig, name));
        }
      }
    } catch (Exception e) {
      throw new RuntimeException(e);
    }
  }
コード例 #3
0
  public boolean updateGoods(Goods to) {
    try {
      Long id = to.getId();
      System.out.println("id: " + id);
      Goods po = SpringUtil.getGoodsManager().getGoods(id);

      System.out.println("1) size of po.gallery: " + po.getGalleries().size());

      //    		String goodskn = po.getKeyName();

      MyPropertyUtil.copySimpleProperties(po, to);
      // TODO image/thumb

      Set<Gallery> toGalleries = to.getGalleries();
      for (Gallery gallery : toGalleries) {
        if (gallery.getId() != null || gallery.getId() < 0) {
          // existing gallery
          for (Gallery gpo : po.getGalleries()) {
            if (gpo.getId().equals(gallery.getId())) {
              gpo.setDescription(gallery.getDescription());
              break;
            }
          }
          //					dao.update(gallery);
          //					gallery.setImageFileId(gallery.getImageFile().getPkId());
        } else {
          // new gallery
          //					String gkn = DataStoreUtils.genKeyName(gallery);
          //					gallery.setKeyName(gkn);
          //					DSFile file = gallery.getImageFile();
          //					String fkn = DataStoreUtils.genKeyName(file);
          //					file.setKeyName(fkn);
          //					String fid = KeyFactory.keyToString(new
          // KeyFactory.Builder("Goods",goodskn).addChild(GoodsGallery.class.getSimpleName(),
          // gkn).addChild("DSFile", fkn).getKey());
          //					gallery.setImageFileId(fid);
          //
          //					DSFile thumbfile = gallery.getThumbFile();
          //					String tfkn = DataStoreUtils.genKeyName(thumbfile);
          //					String tfid = KeyFactory.keyToString(new
          // KeyFactory.Builder("Goods",goodskn).addChild(GoodsGallery.class.getSimpleName(),
          // gkn).addChild("DSFile", tfkn).getKey());
          //					thumbfile.setKeyName(tfkn);
          //					gallery.setThumbFileId(tfid);
          //
          //					gallery.setLongId(UUIDLongGenerator.newUUID());
          po.getGalleries().add(gallery);
        }
      }

      System.out.println("2) size of po.gallery: " + po.getGalleries().size());

      Set<Gallery> galleries = po.getGalleries();
      if (galleries.size() > 0) {
        Gallery gallery = (Gallery) (galleries.iterator().next());
        po.setImage(gallery.getImageUrl());
        po.setThumb(gallery.getThumbUrl());
      } else {
        po.setImage(null);
      }

      po.getAttributes().clear();
      Set<GoodsAttribute> gts = to.getAttributes();
      for (GoodsAttribute gt : gts) {
        //				String gkn = DataStoreUtils.genKeyName(gt);
        //				gt.setKeyName(gkn);
        //				gt.setLongId(UUIDLongGenerator.newUUID());
        po.getAttributes().add(gt);
      }

      System.out.println("System.out.println(updateResponse);");
      SpringUtil.getGoodsManager().saveGoods(po);

      //			GoogleBaseUtil gbUtil = new
      // GoogleBaseUtil(SpringUtil.getShopConfigManager().getCachedShopConfig("en"));
      //			String token = gbUtil.authenticate();
      //			gbUtil.buildDataItem(po);
      //			String updateResponse = gbUtil.updateItem( token , po.getGoogleBaseDataId());
      //			System.out.println(updateResponse);

      return true;
    } catch (Exception e) {
      e.printStackTrace();
      throw new RuntimeException(e);
    }
  }
コード例 #4
0
  // by default only copy simple fields and associated Object's Id, unless requested with
  // wantedFields
  public static Map<String, Object> to2Form(ModelObject obj, List<String> wantedFields)
      throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
    Map<String, Object> props = new HashMap<String, Object>();

    try {
      if (wantedFields == null) {
        Map<String, Object> res = new HashMap<String, Object>();
        PropertyDescriptor srcDescriptors[] =
            BeanUtilsBean.getInstance().getPropertyUtils().getPropertyDescriptors(obj);
        for (PropertyDescriptor pd : srcDescriptors) {
          String name = pd.getName();
          if ("class".equals(name)) {
            continue;
          }

          Class type = pd.getPropertyType();
          //	        	debug("to2Form-- name="+name+", type="+type+",
          // value="+PropertyUtils.getProperty(obj, name));

          if (ModelObject.class.isAssignableFrom(type)) {
            //	        		continue;
            // return associated object's id
            // OpenPersistenceManagerInViewFilter and lazy-loading works here
            ModelObject assoc = (ModelObject) PropertyUtils.getProperty(obj, name);
            if (assoc != null) {
              res.put(name, assoc.getModelId());
            }
          } else if (Set.class.isAssignableFrom(type)) {
            Field field = obj.getClass().getDeclaredField(name);
            if (MyPropertyUtil.isFieldCollectionOfModel(field)) {
              // ingore collection of Model
            } else {
              // TODO leon convert from org.datanucleus.sco.backed.HashSet to a normal HashSet
              // just to make it work, need refine
              Set t = (Set) PropertyUtils.getProperty(obj, name);
              Set t2 = new HashSet();
              if (t != null) {
                t2.addAll(t);
              }
              res.put(name, t2);
            }
          } else if (PropertyUtils.isReadable(obj, name)) {
            res.put(name, PropertyUtils.getProperty(obj, name));
          }
        }
        return res;
      } else {
        // not tested yet
        for (String wantedField : wantedFields) {
          String[] chainedNames = StringUtils.split(wantedField, "_");
          int i = 1, noOfNames = chainedNames.length;
          ModelObject curObj = obj;
          for (String name : chainedNames) {
            Object value = PropertyUtils.getProperty(curObj, name);
            if (i == noOfNames) {
              if (value instanceof ModelObject || value instanceof Collection) {
                throw new RuntimeException(
                    "reached the end of chainedNames but found no simple value");
              }
              if (value != null) {
                props.put(wantedField, value);
                break;
              }
            } else {
              if (value instanceof ModelObject) {
                curObj = (ModelObject) value;
              } else {
                throw new RuntimeException(
                    "not reached the end of chainedNames but found a simple value");
              }
            }
            i++;
          }
        }
      }

      return props;
    } catch (Exception ex) {
      throw new RuntimeException(ex);
    }
  }
コード例 #5
0
  public static void form2To(ModelObject dest, Map<String, Object> orig) {

    debug("form2To-- props:" + orig);

    try {

      HashMap<String, Object> _props = new HashMap<String, Object>();

      PropertyDescriptor srcDescriptors[] =
          BeanUtilsBean.getInstance().getPropertyUtils().getPropertyDescriptors(dest);

      for (PropertyDescriptor pd : srcDescriptors) {
        String fn = pd.getName();
        if ("class".equals(fn)) {
          continue;
        }
        Class type = pd.getPropertyType();

        debug("fn:" + fn + " type:" + type);
        if (!orig.containsKey(fn)) {
          continue;
        }

        Object value = orig.get(fn);
        if (value == null) {
          _props.put(fn, value);
          continue;
        }

        if (ModelObject.class.isAssignableFrom(type)) {
          String bean = type.getSimpleName();
          if (value instanceof String) {
            // the value is ID
            debug("form2To-- type=" + type);
            ModelObject mo = (ModelObject) type.newInstance();
            mo.setModelId((String) value);
            //                    ModelObject mo = getModelObject(bean, (String)value);
            _props.put(fn, mo);
          } else if (value instanceof Map) {
            Map<String, Object> cprops = (Map<String, Object>) value;
            ModelObject mo = (ModelObject) type.newInstance();
            form2To(mo, cprops);
            _props.put(fn, mo);
          } else {
            throw new RuntimeException(
                "Unknown value type: " + value + "," + value.getClass() + " bean:" + bean);
          }
        } else if (Collection.class.isAssignableFrom(type)) {
          Collection col = null;
          if (value != null) {
            if (MyPropertyUtil.isFieldCollectionOfModel(dest.getClass().getDeclaredField(fn))) {
              // NOTE: this means always Add/Update child ModelObject separately
              continue;
            } else {
              //                      String bean = config.getItemType(obj.getModelName(), fn);
              if (value instanceof String) {
                col = (Collection) PropertyUtils.getProperty(dest, fn);
                String val = (String) value;
                if (((String) val).indexOf(",") > 0) {
                  // for properties like Goods.categoryIds which is internally list of String,
                  // and submitted in the format of comma-separated string
                  // TODO escape ","
                  String[] values = ConvertUtil.split(val, ",");
                  col.addAll(Arrays.asList(values));
                } else {
                  col.add(value);
                }

              } else if (value instanceof Collection) {

                Collection c = (Collection) value;
                col = (Collection) PropertyUtils.getProperty(dest, fn);
                debug("size: " + c.size());
                col.addAll(c);
                //                            _props.put(fn, c);
              }
            }

            //            		else {
            ////                      throw new RuntimeException("Unknown value type: " +
            // value+","+value.getClass()+" bean:"+bean);
            //              		 throw new RuntimeException("Unknown value type: " +
            // value+","+value.getClass());
            //                  }
          } else {
            // keep the old value
            col = (Collection) PropertyUtils.getProperty(dest, fn);
          }
          //                _props.put(fn, col);
        } else {
          _props.put(fn, value);
        }
      }

      debug("form2To-- _props:" + _props);

      BeanUtils.populate(dest, _props);

    } catch (Exception e) {
      e.printStackTrace();
    }
  }