Import / Export - Code Included

May 21, 2012 at 5:02 PM

I went ahead and coded the Import/Export process (tested and working for me).  The following code needs added in BingLocationDriver.cs:

protected override void Exporting(BingLocationPart part, ExportContentContext context) {
    context.Element(part.PartDefinition.Name).SetAttributeValue("Latitude", part.Latitude.ToString());
    context.Element(part.PartDefinition.Name).SetAttributeValue("Longitude", part.Longitude.ToString());
    context.Element(part.PartDefinition.Name).SetAttributeValue("IsEnabled", part.IsEnabled.ToString());
    context.Element(part.PartDefinition.Name).SetAttributeValue("Width", part.Width.ToString());
    context.Element(part.PartDefinition.Name).SetAttributeValue("Height", part.Height.ToString());
    context.Element(part.PartDefinition.Name).SetAttributeValue("Zoom", part.Zoom.ToString());
    context.Element(part.PartDefinition.Name).SetAttributeValue("MapType", part.MapType);
    context.Element(part.PartDefinition.Name).SetAttributeValue("MapIcon", part.MapIcon);

    if (part.Record.BingMapList != null) {
        var bingMapListPart = _services.ContentManager.Query<BingMapListPart, BingMapListRecord>()
            .Where(x => x.Id == part.Record.BingMapList.Id).List().FirstOrDefault();
        if (bingMapListPart != null) {
            var bingMapListIdentity = _services.ContentManager.GetItemMetadata(bingMapListPart).Identity;
            context.Element(part.PartDefinition.Name).SetAttributeValue("BingMapList", bingMapListIdentity.ToString());
        }
    }
}

protected override void Importing(BingLocationPart part, ImportContentContext context) {
    var latitudeString = context.Attribute(part.PartDefinition.Name, "Latitude");
    if (!String.IsNullOrWhiteSpace(latitudeString)) {
        float latitude = 0;
        if (float.TryParse(latitudeString, out latitude)) {
            part.Latitude = latitude;
        }
    }
    var longitudeString = context.Attribute(part.PartDefinition.Name, "Longitude");
    if (!String.IsNullOrWhiteSpace(longitudeString)) {
        float longitude = 0;
        if (float.TryParse(longitudeString, out longitude)) {
            part.Longitude = longitude;
        }
    }
    var isEnabledString = context.Attribute(part.PartDefinition.Name, "IsEnabled");
    if (!String.IsNullOrWhiteSpace(isEnabledString)) {
        bool isEnabled = false;
        if (bool.TryParse(isEnabledString, out isEnabled)) {
            part.IsEnabled = isEnabled;
        }
    }
    var widthString = context.Attribute(part.PartDefinition.Name, "Width");
    if (!String.IsNullOrWhiteSpace(widthString)) {
        int width = 0;
        if (int.TryParse(widthString, out width)) {
            part.Width = width;
        }
    }
    var heightString = context.Attribute(part.PartDefinition.Name, "Height");
    if (!String.IsNullOrWhiteSpace(heightString)) {
        int height = 0;
        if (int.TryParse(heightString, out height)) {
            part.Height = height;
        }
    }
    var zoomString = context.Attribute(part.PartDefinition.Name, "Zoom");
    if (!String.IsNullOrWhiteSpace(zoomString)) {
        int zoom = 0;
        if (int.TryParse(zoomString, out zoom)) {
            part.Zoom = zoom;
        }
    }
    var mapType = context.Attribute(part.PartDefinition.Name, "MapType");
    part.MapType = mapType;
    var mapIcon = context.Attribute(part.PartDefinition.Name, "MapIcon");
    part.MapIcon = mapIcon;    
}

protected override void Imported(BingLocationPart part, ImportContentContext context) {
    // assign the bingMapList only when everything is imported
    var bingMapList = context.Attribute(part.PartDefinition.Name, "BingMapList");
    if (bingMapList != null) {
        part.Record.BingMapList = context.GetItemFromSession(bingMapList).As<BingMapListPart>().ContentItem.Record;
    }
}

The following code needs added in BingMapListDriver.cs:

protected override void Exporting(BingMapListPart part, ExportContentContext context) {
    context.Element(part.PartDefinition.Name).SetAttributeValue("CenterLatitude", part.CenterLatitude.ToString());
    context.Element(part.PartDefinition.Name).SetAttributeValue("CenterLongitude", part.CenterLongitude.ToString());
    context.Element(part.PartDefinition.Name).SetAttributeValue("Width", part.Width.ToString());
    context.Element(part.PartDefinition.Name).SetAttributeValue("Height", part.Height.ToString());
    context.Element(part.PartDefinition.Name).SetAttributeValue("Zoom", part.Zoom.ToString());
    context.Element(part.PartDefinition.Name).SetAttributeValue("MapType", part.MapType);
}

protected override void Importing(BingMapListPart part, ImportContentContext context) {
    var centerLatitudeString = context.Attribute(part.PartDefinition.Name, "CenterLatitude");
    if (!String.IsNullOrWhiteSpace(centerLatitudeString)) {
        float centerLatitude = 0;
        if (float.TryParse(centerLatitudeString, out centerLatitude)) {
            part.CenterLatitude = centerLatitude;
        }
    }
    var centerLongitudeString = context.Attribute(part.PartDefinition.Name, "CenterLongitude");
    if (!String.IsNullOrWhiteSpace(centerLongitudeString)) {
        float centerLongitude = 0;
        if (float.TryParse(centerLongitudeString, out centerLongitude)) {
            part.CenterLongitude = centerLongitude;
        }
    }
    var widthString = context.Attribute(part.PartDefinition.Name, "Width");
    if (!String.IsNullOrWhiteSpace(widthString)) {
        int width = 0;
        if (int.TryParse(widthString, out width)) {
            part.Width = width;
        }
    }
    var heightString = context.Attribute(part.PartDefinition.Name, "Height");
    if (!String.IsNullOrWhiteSpace(heightString)) {
        int height = 0;
        if (int.TryParse(heightString, out height)) {
            part.Height = height;
        }
    }
    var zoomString = context.Attribute(part.PartDefinition.Name, "Zoom");
    if (!String.IsNullOrWhiteSpace(zoomString)) {
        int zoom = 0;
        if (int.TryParse(zoomString, out zoom)) {
            part.Zoom = zoom;
        }
    }
    var mapType = context.Attribute(part.PartDefinition.Name, "MapType");
    part.MapType = mapType;
}

Also, don't forget to add 

using 

Orchard.ContentManagement.Handlers;

to both of these drivers.  Thanks for this great module and hopefully you can drop this in the source code. Here is the link where this is tracked in Issue Tracker: http://orchardbingmaplist.codeplex.com/workitem/9