Exemplo n.º 1
0
 private FoodProxy addToUserFoodsIfMissing(String source, Food f) {
   FoodDataSource allegedSource = Datasources.getSource(source);
   if (allegedSource != null) {
     FoodProxy f2 = allegedSource.getFoodProxy(f.getSourceUID());
     if (f2 != null) {
       if (f2.getFood().equals(f)) {
         return f2;
       }
     }
   }
   // TODO: scan all foods for identical matches first!
   Datasources.getUserFoods().addFood(f);
   return f.getProxy();
 }
Exemplo n.º 2
0
  public void load(Element e) {
    FoodDataSource source = Datasources.getUserFoods();
    if (e.hasAttribute("source")) {
      source = Datasources.getSource(e.getAttribute("source"));
    }

    FoodProxy proxy = null;

    // load the food if stored as a child
    NodeList nl = e.getChildNodes();
    for (int i = 0; i < nl.getLength(); i++) {
      if (nl.item(i).getNodeName().equals("food") || nl.item(i).getNodeName().equals("recipe")) {
        Food f = XMLFoodLoader.loadFood((Element) nl.item(i));
        if (f != null) {
          proxy = addToUserFoodsIfMissing(e.getAttribute("source"), f);
        }
      }
    }

    if (proxy == null) {
      proxy = source.getFoodProxy(e.getAttribute("food"));
    }

    setFood(proxy);
    if (proxy == null) {
      System.err.println("Failed to load food [" + source + ":" + e.getAttribute("food") + "]");
      return;
    }

    if (e.hasAttribute("date")) {
      setDate(new Date(Long.parseLong(e.getAttribute("date"))));
    }
    setGrams(Double.parseDouble(e.getAttribute("grams")));
    if (e.hasAttribute("meal")) {
      setMeal(Integer.parseInt(e.getAttribute("meal")));
    }
    if (e.hasAttribute("measure")) {
      setMeasure(e.getAttribute("measure"));
    }
  }