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); } }
public Long addGoods(Goods to) { try { // String goodskn = DataStoreUtils.genKeyName(to); // to.setKeyName(goodskn); // TODO need overcome the checkbox issue // just for test with Web Home page. // to.setIsBest(true); // to.setIsNew(true); // to.setIsHot(true); // to.setLongId(UUIDLongGenerator.newUUID()); Set<Gallery> galleries = to.getGalleries(); for (Gallery gallery : galleries) { // String gkn = DataStoreUtils.genKeyName(gallery); // gallery.setKeyName(gkn); // String gid = KeyFactory.keyToString(new // KeyFactory.Builder("Goods",goodskn).addChild("Gallery", gkn).getKey()); // gallery.setKeyName(gkn); // gallery.setId(gid); // DSFile file = gallery.getImageFile(); // String fkn = DataStoreUtils.genKeyName(file); // String fid = KeyFactory.keyToString(new // KeyFactory.Builder("Goods",goodskn).addChild(GoodsGallery.class.getSimpleName(), // gkn).addChild("DSFile", fkn).getKey()); // file.setKeyName(fkn); //// file.setId(fid); // 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()); } // TODO temporary. to ensure image not empty if (galleries.size() > 0) { Gallery gallery = (Gallery) galleries.iterator().next(); to.setImage(gallery.getImageUrl()); // TODO temporary to.setThumb(gallery.getThumbUrl()); } Set<GoodsAttribute> gts = to.getAttributes(); for (GoodsAttribute gt : gts) { // String gkn = DataStoreUtils.genKeyName(gt); // String gid = KeyFactory.keyToString(new // KeyFactory.Builder("Goods",goodskn).addChild(GoodsAttr.class.getSimpleName(), // gkn).getKey()); // gt.setKeyName(gkn); //// gt.setId(gid); // gt.setLongId(UUIDLongGenerator.newUUID()); } SpringUtil.getGoodsManager().saveGoods(to); Long res = to.getId(); // GoogleBaseUtil gbUtil = new // GoogleBaseUtil(SpringUtil.getShopConfigManager().getCachedShopConfig("en")); // String token = gbUtil.authenticate(); // gbUtil.buildDataItem(to); // String gbdid = gbUtil.postItem(token); // to.setGoogleBaseDataId(gbdid); // verify, debug only // for(Gallery gallery : galleries) { // System.out.println("galleryId: "+gallery.getId()); // } // String goodsId = to.getId(); // System.out.println("goodsId="+goodsId); // // Criteria criteria = new Criteria(); // Condition cond = new Condition(); // cond.setField(IGoodsGallery.GOODS); // cond.setOperator(Condition.EQUALS); // cond.setValue(goodsId); // criteria.addCondition(cond); // List<GoodsGallery> t1 = // SpringUtil.getGoodsGalleryManager().getGoodsGalleryList(criteria); // System.out.println("size: "+t1.size()); // // SpringUtil.getGoodsManager().saveGoods(to); // res = to.getId(); return res; } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); throw new RuntimeException(e); } }