0 Replies Latest reply on Sep 2, 2009 7:29 PM by farrows76

    Trying to Map Value Objects

    farrows76

      I am a Rails developer and very new to Flex. I have to Value Objects that receive data from the server -- UsedVehicleVO and StyleVO.  Each UsedVehicle belongs to a Style, so in my UsedVehicleVO I have a styleId.  I am able to retrieve the used vehicles and styles from the server but I cant map any used vehicle to its style.


      I am able to display the styles in a data grid with the dataProvider = "model.styles" and can display the used vehicles in a data grid with the dataProvider = "model.usedVehicles".  But in the Used Vehcle data grid I cant display the style[name] attribute.  Thanks for any help.


      Here is my code:

       

      ListUsedVehcles view

      <?xml version="1.0" encoding="utf-8"?>
      <mx:VBox xmlns:mx="http://www.adobe.com/2006/mxml"
           width="100%" height="100%"
           label="Used Vehicles">
           <mx:Script>
                <![CDATA[
                     import com.rapidcarsearch.Horizon.model.RCSModelLocator;
                     import com.rapidcarsearch.Horizon.model.UsedVehicle;
                     import com.rapidcarsearch.Horizon.model.Style;
                     import com.rapidcarsearch.Horizon.util.CairngormUtils;
                     
                     [Bindable]
                     public var model:RCSModelLocator = RCSModelLocator.getInstance();
                ]]>
           </mx:Script>
           <mx:DataGrid dataProvider="{model.usedVehicles}"
           width="100%" height="100%">
                <mx:columns>
                     <mx:DataGridColumn dataField="id"                    headerText="Key"/>
                     <mx:DataGridColumn dataField="style.name"           headerText="Style" />
                     <mx:DataGridColumn dataField="stock"               headerText="Stock Number"/>
                     <mx:DataGridColumn dataField="vin"                    headerText="VIN"/>
                     <mx:DataGridColumn dataField="price"               headerText="Price"/>
                     <mx:DataGridColumn dataField="internetPrice"     headerText="Internet Price"/>
                     <mx:DataGridColumn dataField="transmission"          headerText="Transmission"/>
                   </mx:columns>
              </mx:DataGrid>
      </mx:VBox>
      


      UsedVehicleVO

      package com.rapidcarsearch.Horizon.vo
      {
      [RemoteClass(alias="UsedVehicleVO")]
      [Bindable]
      public class UsedVehicleVO
      {
      public var id:int;
      public var stock:String;
      public var vin:String;
      public var mileage:int;
      public var price:int;
      public var internetPrice:int;
      public var transmission:String;
      public var equipment:String;
      public var comment:String;
      public var ownerType:String;
      public var ownerId:int;
      public var active:Boolean;
      public var enteredInventory:Date;
      public var createdAt:Date;
      public var updatedAt:Date;
      public var styleId:int;
      }
      }



      StyleVO

      package com.rapidcarsearch.Horizon.vo
      {
          [RemoteClass(alias="StyleVO")]
          [Bindable]
          public class StyleVO
          {
              public var id:int;
              public var year:int;
              public var name:String;
              public var doors:int;
              public var gvrwRange:String;
              public var installedTechnicalEquipment:String;
              public var baseMsrp:int;
              public var baseInvoice:int;
              public var destination:int;
              public var createdAt:Date;
              public var updatedAt:Date;
          }
      }


      ModelLocator

      package com.rapidcarsearch.Horizon.model
      {
           import com.adobe.cairngorm.model.IModelLocator;
           import com.rapidcarsearch.Horizon.vo.StyleVO;
           import com.rapidcarsearch.Horizon.vo.UsedVehicleVO;
           
           import mx.collections.ArrayCollection;
           import mx.collections.ListCollectionView;
           
           [Bindable]
           public class RCSModelLocator implements IModelLocator
           {
                // workflow states
                public static const VIEWING_SPLASH_SCREEN:int = 0;
                public static const VIEWING_MAIN_APP:int = 1;
                public static const VIEWING_STYLES:int = 2;
                
                // Public properties
                public var usedVehicles:ListCollectionView;
                
                public var styles:ListCollectionView;
                
                public var styleIDMap:Object;
                
                public var workflowState:int = VIEWING_SPLASH_SCREEN;
                
                // Mappings from Value Objects
                
                public function setUsedVehiclesFromVOs(usedVehicleVOs:ArrayCollection):void {
                     var usedVehiclesArray:Array = [];
                     for each(var item:UsedVehicleVO in usedVehicleVOs) {
                          usedVehiclesArray.push(UsedVehicle.fromVO(item));
                     }
                     usedVehicles = new ArrayCollection(usedVehiclesArray);
                }
                
                public function setStylesFromVOs(styleVOs:ArrayCollection):void {
                     var stylesArray:Array = [];
                     for each(var item:StyleVO in styleVOs) {
                          stylesArray.push(Style.fromVO(item));
                     }
                     setStyles(stylesArray);
                }
                
                public function setStyles(stylesArray:Array):void {
                     styleIDMap = {};
                     for each(var style:Style in stylesArray) {
                          styleIDMap[style.id] = style;
                     }
                     styles = new ArrayCollection(stylesArray);
                }
                
                public function getStyle(styleID:int):Style {
                     if (styleIDMap == null) return null;
                     return styleIDMap[styleID]
                }
                
                // Singleton Stuff
                private static var modelLocator:RCSModelLocator;
                
                public static function getInstance():RCSModelLocator {
                     if(modelLocator == null) {
                          modelLocator = new RCSModelLocator();
                     }
                     return modelLocator;
                }
                
                public function RCSModelLocator()
                {
                     if (modelLocator != null) {
                          throw new Error("Only one RCSModelLocator instance may be instantiated.")
                     }
                }
           }
      }
      
      


      UsedVehicle Model

      package com.rapidcarsearch.Horizon.model
      {
           import com.rapidcarsearch.Horizon.vo.UsedVehicleVO;
           
           public class UsedVehicle
           {                    
                [Bindable]
              public var id:int;
              
              [Bindable]
              public var style:Style;
              
              [Bindable]
              public var stock:String;
              
              [Bindable]
              public var vin:String;
      
              [Bindable]
              public var mileage:int;
              
              [Bindable]
              public var price:int;
              
              [Bindable]
              public var internetPrice:int;
      
              [Bindable]
              public var transmission:String;
              
              [Bindable]
              public var equipment:String;
              
              [Bindable]
              public var comment:String;
              
              [Bindable]
              public var ownerType:String;
              
              [Bindable]
              public var active:Boolean;
              
              [Bindable]
              public var enteredInventory:Date;
      
                public function UsedVehicle(
                     id:int,
                     style:Style,
                     stock:String,
                     vin:String,
                     mileage:int,
                     price:int,
                     internetPrice:int,
                     transmission:String,
                     equipment:String,
                     comment:String,
                     ownerType:String,
                     active:Boolean,
                     enteredInventory:Date)
                {
                     this.id = id;
                     this.style;
                     this.stock = stock;
                     this.vin = vin;
                     this.mileage = mileage;
                     this.price = price;
                     this.internetPrice = internetPrice;
                     this.transmission = transmission;
                     this.equipment = equipment;
                     this.comment = comment;
                     this.ownerType = ownerType;
                     this.active = active;
                     this.enteredInventory = enteredInventory;
                }
                
                public function toVO():UsedVehicleVO {
                  var usedVehicleVO:UsedVehicleVO = new UsedVehicleVO();
                  usedVehicleVO.id = id;
                  usedVehicleVO.styleId = style.id;
                  usedVehicleVO.stock = stock;
                  usedVehicleVO.vin = vin;
                  usedVehicleVO.mileage = mileage;
                  usedVehicleVO.price = price;
                  usedVehicleVO.internetPrice = internetPrice;
                  usedVehicleVO.transmission = transmission;
                  usedVehicleVO.equipment = equipment;
                  usedVehicleVO.comment = comment;
                  usedVehicleVO.ownerType = ownerType;
                  usedVehicleVO.active = active;
                  usedVehicleVO.enteredInventory = enteredInventory;
                  return usedVehicleVO;
              }
      
              public static function fromVO(usedVehicleVO:UsedVehicleVO):UsedVehicle {
                   var model:RCSModelLocator = RCSModelLocator.getInstance();
                  return new UsedVehicle(
                      usedVehicleVO.id,
                      model.getStyle(usedVehicleVO.styleId),
                      usedVehicleVO.stock,
                      usedVehicleVO.vin,
                      usedVehicleVO.mileage,
                      usedVehicleVO.price,
                      usedVehicleVO.internetPrice,
                      usedVehicleVO.transmission,
                      usedVehicleVO.equipment,
                      usedVehicleVO.comment,
                      usedVehicleVO.ownerType,
                      usedVehicleVO.active,
                      usedVehicleVO.enteredInventory);
              }
           }
      }