/*********************************************************************************
 *                                                                               *
 * WARNING: File automatically generated by DMXmlJson. DON'T CHANGE IT manually! *
 *                                                                               *
 *********************************************************************************/

package bz.davide.dmxmljsonexamples.model.invoice;


public class Example01Unmarshaller_Helper extends bz.davide.dmxmljson.unmarshalling.Unmarshaller
{
   protected Example01Unmarshaller_Helper()
   {
      this.putInstanceFactory("bz.davide.dmxmljsonexamples.model.invoice.Invoice", new bz.davide.dmxmljson.unmarshalling.InstanceFactory() {
         @Override public Object newInstance() throws Exception {
            return new bz.davide.dmxmljsonexamples.model.invoice.Invoice((Void)null);
         }
      });

      this.putClassUnmarshaller("bz.davide.dmxmljsonexamples.model.invoice.Invoice", new bz.davide.dmxmljson.unmarshalling.ClassUnmarshaller() {
         @Override public void unmarshall(bz.davide.dmxmljson.unmarshalling.Structure structure, Object obj, java.util.HashMap<String, Object> identities) throws Exception {
            String id = structure.getId();
            if (id != null)
               identities.put(id, obj);
            bz.davide.dmxmljson.unmarshalling.Value value;
            // customer
            if ((value = structure.property("customer")) != null)
               if (value.isNull())
                  ((Invoice)obj).customer = null;
               else
               {
                  String refid = value.structure().getRefId();    
                  if (refid != null)                              
                     ((Invoice)obj).customer = (bz.davide.dmxmljsonexamples.model.invoice.Person)identities.get(refid);
                  else {
                     Object o = newInstance(value.structure().getRuntimeClassName("Person"));              
                     internalUnmarschall(value.structure(), o.getClass().getName(), o, identities); 
                     ((Invoice)obj).customer = (bz.davide.dmxmljsonexamples.model.invoice.Person)o;
                  }
               }
            // date
            if ((value = structure.property("date")) != null)
               if (value.isNull())
                  ((Invoice)obj).date = null;
               else
               {
                  ((Invoice)obj).date = value.string();
               }
            // number
            if ((value = structure.property("number")) != null)
               if (value.isNull())
                  new RuntimeException("Impossibile value for primitive type");
               else
               {
                  ((Invoice)obj).number = (int)value.integer();
               }
            // rows
            if ((value = structure.property("rows")) != null)
               if (value.isNull())
                  ((Invoice)obj).rows = null;
               else
               {
                  bz.davide.dmxmljson.unmarshalling.Array arr = value.array();        
                  java.util.ArrayList arrayList = new java.util.ArrayList();       
                  while ((value = arr.nextItem()) != null) {                       
                     if (value.isNull())                                           
                        arrayList.add(null);                                       
                     else                                                          
                     {                                                                   
                        String refid = value.structure().getRefId();    
                        if (refid != null)                              
                           arrayList.add(identities.get(refid));                                                
                        else {
                           Object o = newInstance(value.structure().getRuntimeClassName("InvoiceRow"));              
                           internalUnmarschall(value.structure(), o.getClass().getName(), o, identities); 
                           arrayList.add(o);                                                
                        }
                     }                                                                   
                  }                                                                   
                  ((Invoice)obj).rows = arrayList;
               }
         }
      });
      this.putInstanceFactory("bz.davide.dmxmljsonexamples.model.invoice.DVD", new bz.davide.dmxmljson.unmarshalling.InstanceFactory() {
         @Override public Object newInstance() throws Exception {
            return new bz.davide.dmxmljsonexamples.model.invoice.DVD((Void)null);
         }
      });

      this.putClassUnmarshaller("bz.davide.dmxmljsonexamples.model.invoice.DVD", new bz.davide.dmxmljson.unmarshalling.ClassUnmarshaller() {
         @Override public void unmarshall(bz.davide.dmxmljson.unmarshalling.Structure structure, Object obj, java.util.HashMap<String, Object> identities) throws Exception {
            internalUnmarschall(structure, "bz.davide.dmxmljsonexamples.model.invoice.Product", obj, identities);
            String id = structure.getId();
            if (id != null)
               identities.put(id, obj);
            bz.davide.dmxmljson.unmarshalling.Value value;
            // title
            if ((value = structure.property("title")) != null)
               if (value.isNull())
                  ((DVD)obj).title = null;
               else
               {
                  ((DVD)obj).title = value.string();
               }
         }
      });
      this.putInstanceFactory("bz.davide.dmxmljsonexamples.model.invoice.Person", new bz.davide.dmxmljson.unmarshalling.InstanceFactory() {
         @Override public Object newInstance() throws Exception {
            return new bz.davide.dmxmljsonexamples.model.invoice.Person((Void)null);
         }
      });

      this.putClassUnmarshaller("bz.davide.dmxmljsonexamples.model.invoice.Person", new bz.davide.dmxmljson.unmarshalling.ClassUnmarshaller() {
         @Override public void unmarshall(bz.davide.dmxmljson.unmarshalling.Structure structure, Object obj, java.util.HashMap<String, Object> identities) throws Exception {
            String id = structure.getId();
            if (id != null)
               identities.put(id, obj);
            bz.davide.dmxmljson.unmarshalling.Value value;
            // name
            if ((value = structure.property("name")) != null)
               if (value.isNull())
                  ((Person)obj).name = null;
               else
               {
                  ((Person)obj).name = value.string();
               }
            // nr
            if ((value = structure.property("nr")) != null)
               if (value.isNull())
                  new RuntimeException("Impossibile value for primitive type");
               else
               {
                  ((Person)obj).nr = (int)value.integer();
               }
            // street
            if ((value = structure.property("street")) != null)
               if (value.isNull())
                  ((Person)obj).street = null;
               else
               {
                  ((Person)obj).street = value.string();
               }
         }
      });
      this.putInstanceFactory("bz.davide.dmxmljsonexamples.model.invoice.InvoiceRow", new bz.davide.dmxmljson.unmarshalling.InstanceFactory() {
         @Override public Object newInstance() throws Exception {
            return new bz.davide.dmxmljsonexamples.model.invoice.InvoiceRow((Void)null);
         }
      });

      this.putClassUnmarshaller("bz.davide.dmxmljsonexamples.model.invoice.InvoiceRow", new bz.davide.dmxmljson.unmarshalling.ClassUnmarshaller() {
         @Override public void unmarshall(bz.davide.dmxmljson.unmarshalling.Structure structure, Object obj, java.util.HashMap<String, Object> identities) throws Exception {
            String id = structure.getId();
            if (id != null)
               identities.put(id, obj);
            bz.davide.dmxmljson.unmarshalling.Value value;
            // product
            if ((value = structure.property("product")) != null)
               if (value.isNull())
                  ((InvoiceRow)obj).product = null;
               else
               {
                  String refid = value.structure().getRefId();    
                  if (refid != null)                              
                     ((InvoiceRow)obj).product = (bz.davide.dmxmljsonexamples.model.invoice.Product)identities.get(refid);
                  else {
                     Object o = newInstance(value.structure().getRuntimeClassName("Product"));              
                     internalUnmarschall(value.structure(), o.getClass().getName(), o, identities); 
                     ((InvoiceRow)obj).product = (bz.davide.dmxmljsonexamples.model.invoice.Product)o;
                  }
               }
            // quantity
            if ((value = structure.property("quantity")) != null)
               if (value.isNull())
                  new RuntimeException("Impossibile value for primitive type");
               else
               {
                  ((InvoiceRow)obj).quantity = (int)value.integer();
               }
         }
      });
      this.putInstanceFactory("bz.davide.dmxmljsonexamples.model.invoice.Product", new bz.davide.dmxmljson.unmarshalling.InstanceFactory() {
         @Override public Object newInstance() throws Exception {
            return new bz.davide.dmxmljsonexamples.model.invoice.Product((Void)null);
         }
      });

      this.putClassUnmarshaller("bz.davide.dmxmljsonexamples.model.invoice.Product", new bz.davide.dmxmljson.unmarshalling.ClassUnmarshaller() {
         @Override public void unmarshall(bz.davide.dmxmljson.unmarshalling.Structure structure, Object obj, java.util.HashMap<String, Object> identities) throws Exception {
            String id = structure.getId();
            if (id != null)
               identities.put(id, obj);
            bz.davide.dmxmljson.unmarshalling.Value value;
            // name
            if ((value = structure.property("name")) != null)
               if (value.isNull())
                  ((Product)obj).name = null;
               else
               {
                  ((Product)obj).name = value.string();
               }
         }
      });

   }
}
