Example #1
0
 private static void SaveToXmi(XNamespace xmi, Dictionary<MetaModel, XNamespace> metaModels, Dictionary<MetaModel, string> prefixes, XElement elem, ModelObject mo)
 {
     if (!string.IsNullOrEmpty(mo.MetaID))
     {
         elem.Add(new XAttribute(xmi + "id", mo.MetaID));
     }
     foreach (var prop in mo.MGetProperties())
     {
         if (!prop.IsCollection && !prop.IsReadonly)
         {
             if (prop.Type.IsPrimitive || prop.Type.IsEnum || prop.Type.Equals(typeof(string)))
             {
                 if (!mo.MIsDefault(prop))
                 {
                     object value = mo.MGet(prop);
                     if (value != null)
                     {
                         elem.Add(new XAttribute(prop.Name.ToCamelCase(), value));
                     }
                     else
                     {
                         elem.Add(new XAttribute(prop.Name.ToCamelCase(), string.Empty));
                     }
                 }
             }
             else
             {
                 if (!mo.MIsDefault(prop))
                 {
                     object value = mo.MGet(prop);
                     ModelObject valueObject = value as ModelObject;
                     if (valueObject != null)
                     {
                         if (!prop.OppositeProperties.Any(p => p.IsContainment))
                         {
                             elem.Add(new XAttribute(prop.Name.ToCamelCase(), valueObject.MetaID));
                         }
                     }
                     else if (value != null)
                     {
                         elem.Add(new XAttribute(prop.Name.ToCamelCase(), value.ToString()));
                     }
                     else
                     {
                         elem.Add(new XAttribute(prop.Name.ToCamelCase(), string.Empty));
                     }
                 }
             }
         }
         else if (prop.IsContainment)
         {
             if (prop.IsCollection)
             {
                 ModelCollection children = mo.MGet(prop) as ModelCollection;
                 if (children != null)
                 {
                     foreach (var childObj in children)
                     {
                         ModelObject child = childObj as ModelObject;
                         if (child != null && child.MMetaClass != null && child.MMetaClass.Model != null)
                         {
                             string ns = prefixes[mo.MMetaClass.Model];
                             XElement childElem =
                                 new XElement(
                                     prop.Name.ToCamelCase(),
                                     new XAttribute(xmi + "type", ns + ":" + child.MMetaClass.Name));
                             elem.Add(childElem);
                             ModelExchange.SaveToXmi(xmi, metaModels, prefixes, childElem, child);
                         }
                     }
                 }
             }
             else
             {
                 ModelObject child = mo.MGet(prop) as ModelObject;
                 if (child != null && child.MMetaClass != null && child.MMetaClass.Model != null)
                 {
                     string ns = prefixes[mo.MMetaClass.Model];
                     XElement childElem =
                         new XElement(
                             prop.Name.ToCamelCase(),
                             new XAttribute(xmi + "type", ns + ":" + child.MMetaClass.Name));
                     elem.Add(childElem);
                     ModelExchange.SaveToXmi(xmi, metaModels, prefixes, childElem, child);
                 }
             }
         }
         else 
         {
             if (prop.IsCollection)
             {
                 ModelCollection children = mo.MGet(prop) as ModelCollection;
                 if (children != null)
                 {
                     foreach (var childObj in children)
                     {
                         ModelObject child = childObj as ModelObject;
                         if (child != null && child.MMetaClass != null && child.MMetaClass.Model != null)
                         {
                             //XNamespace ns = metaModels[mo.MMetaClass.Model];
                             elem.Add(new XElement(prop.Name.ToCamelCase(), new XAttribute(xmi + "idref", child.MetaID)));
                         }
                     }
                 }
             }
             else if (!prop.IsReadonly)
             {
                 ModelObject child = mo.MGet(prop) as ModelObject;
                 if (child != null && child.MMetaClass != null && child.MMetaClass.Model != null)
                 {
                     //string ns = prefixes[mo.MMetaClass.Model];
                     elem.Add(new XAttribute(prop.Name.ToCamelCase(), child.MetaID));
                 }
             }
         }
     }
 }