public static IList <String> GetMappedProperties(this GraphMap graphMap, String templateId, int templateIndex, String parentClassId, int parentClassIndex, IList <String> propertyList = null)
        {
            if (propertyList == null)
            {
                propertyList = new List <String>();
            }

            var classTemplateMap = graphMap.GetClassTemplateMap(parentClassId, parentClassIndex);

            if (classTemplateMap != null)
            {
                var template = classTemplateMap.templateMaps.SingleOrDefault(x => (x.id == templateId && x.index == templateIndex));
                if (template != null)
                {
                    foreach (var role in template.roleMaps.Where(x => !String.IsNullOrWhiteSpace(x.propertyName)))
                    {
                        propertyList.Add(role.propertyName);
                    }
                    foreach (var role in template.roleMaps.Where(x => x.classMap != null))
                    {
                        var classTemplate = graphMap.GetClassTemplateMap(role.classMap.id, role.classMap.index);
                        foreach (var tmpl in classTemplate.templateMaps)
                        {
                            if (tmpl != null)
                            {
                                graphMap.GetMappedProperties(tmpl.id, tmpl.index, role.classMap.id, role.classMap.index, propertyList);
                            }
                        }
                    }
                }
            }
            return(propertyList);
        }
        public static Cardinality GetCardinality(this GraphMap graphMap, RoleMap roleMap, DataDictionary dataDictionary, string fixedIdentifierBoundary)
        {
            ClassTemplateMap ctm = graphMap.GetClassTemplateMap(roleMap.classMap.id, roleMap.classMap.index);

            if (ctm == null || ctm.classMap == null)
            {
                return(Cardinality.Self);
            }

            // determine cardinality to related class
            foreach (string identifier in roleMap.classMap.identifiers)
            {
                if (!(identifier.StartsWith(fixedIdentifierBoundary) && identifier.EndsWith(fixedIdentifierBoundary)))
                {
                    string[] propertyParts = identifier.Split('.');

                    if (propertyParts.Length > 2)
                    {
                        DataObject       dataObject       = dataDictionary.dataObjects.First(c => c.objectName == propertyParts[0]);
                        DataRelationship dataRelationship = dataObject.dataRelationships.First(c => c.relationshipName == propertyParts[1]);

                        if (dataRelationship.relationshipType == RelationshipType.OneToMany)
                        {
                            return(Cardinality.OneToMany);
                        }
                    }
                }
            }

            return(Cardinality.OneToOne);
        }
        public static void RearrangeClassMapIndex(this GraphMap graphMap, string classMapId = null)
        {
            var distinctClassIds = graphMap.classTemplateMaps.Where(x => classMapId == null ? true : x.classMap.id == classMapId).Select(x => x.classMap.id).Distinct();

            foreach (var classId in distinctClassIds)
            {
                IList <ClassMap> classMaps = (from ctm in graphMap.classTemplateMaps
                                              where ctm.classMap.id == classId
                                              select ctm.classMap).ToList();
                int startIndex = -1;

                foreach (ClassMap cm in classMaps)
                {
                    SetNewIndexOfClass(graphMap, classId, cm.index, startIndex);
                    startIndex--;
                }

                startIndex = 0;
                foreach (ClassMap cm in classMaps)
                {
                    SetNewIndexOfClass(graphMap, classId, cm.index, startIndex);
                    startIndex++;
                }
            }
        }
 public static void SetClassPath(this GraphMap graphMap)
 {
     if (graphMap != null && graphMap.classTemplateMaps != null && graphMap.classTemplateMaps[0] != null)
     {
         ClassTemplateMap ctm = graphMap.classTemplateMaps[0];
         ctm.classMap.path = ""; // class path for root class
         SetClassPath(graphMap, ctm.classMap);
     }
 }
        public static ClassTemplateMap GetClassTemplateMap(this GraphMap graphMap, string classId, int index)
        {
            foreach (ClassTemplateMap classTemplateMap in graphMap.classTemplateMaps)
            {
                if (classTemplateMap.classMap.id == classId && classTemplateMap.classMap.index == index)
                {
                    return(classTemplateMap);
                }
            }

            return(default(ClassTemplateMap));
        }
        public static void AddTemplateMap(this GraphMap graphMap, ClassMap classMap, TemplateMap templateMap)
        {
            graphMap.AddClassMap(null, classMap);
            ClassTemplateMap classTemplateMap = graphMap.classTemplateMaps.Where(c => (c.classMap.id == classMap.id && c.classMap.index == classMap.index)).FirstOrDefault();

            var tmpIndexes = classTemplateMap.templateMaps.Where(x => x.id == templateMap.id).Select(x => x.index).ToArray();

            templateMap.index = tmpIndexes.Count() == 0 ? 0 : tmpIndexes.Max() + 1;

            if (classTemplateMap.classMap != null)
            {
                classTemplateMap.templateMaps.Add(templateMap);
            }
        }
        public static void DeleteRoleMap(this GraphMap graphMap, TemplateMap templateMap, string roleId)
        {
            RoleMap roleMap = templateMap.roleMaps.Where(c => c.id == roleId).FirstOrDefault();

            if (roleMap != null)
            {
                if (roleMap.classMap != null)
                {
                    graphMap.DeleteClassMap(roleMap.classMap.id, roleMap.classMap.index);
                    roleMap.value    = null;
                    roleMap.classMap = null;
                }
            }
        }
        public static int GetClassMapMaxIndex(this GraphMap graphMap, string classId)
        {
            var list = from c in graphMap.classTemplateMaps
                       where c.classMap.id == classId
                       select c.classMap.index;

            if (list != null && list.Count() > 0)
            {
                return(list.Max());
            }
            else
            {
                return(-1);
            }
        }
        public static void RearrangeTemplateMapIndex(this GraphMap graphMap)
        {
            foreach (var tMaps in graphMap.classTemplateMaps.Select(x => x.templateMaps))
            {
                var distinctTemplateIds = tMaps.Select(x => x.id).Distinct();

                foreach (var templateIds in distinctTemplateIds)
                {
                    IList <TemplateMap> tMapList = tMaps.Where(x => x.id == templateIds).ToList();
                    for (int i = 0; i < tMapList.Count(); i++)
                    {
                        tMapList[i].index = i;
                    }
                }
            }
        }
        public static ClassTemplateMap GetClassTemplateMapByName(this GraphMap graphMap, string className)
        {
            if (!String.IsNullOrEmpty(className))
            {
                foreach (ClassTemplateMap classTemplateMap in graphMap.classTemplateMaps)
                {
                    if (classTemplateMap.classMap != null &&
                        Utility.TitleCase(classTemplateMap.classMap.name).ToLower() ==
                        Utility.TitleCase(className).ToLower())
                    {
                        return(classTemplateMap);
                    }
                }
            }

            return(default(ClassTemplateMap));
        }
        public static ClassMap CrossClassMap(this ClassMap classMap, GraphMap graphMap, Class manifestClass)
        {
            ClassMap newClassMap = new ClassMap
            {
                id   = classMap.id,
                name = classMap.name,
                identifierKeyMaps   = manifestClass.keys.CloneIdentifiers(),
                identifierDelimiter = classMap.identifierDelimiter,
                identifiers         = new Identifiers(),
                index = classMap.index,
                path  = classMap.path
            };

            foreach (Key key in manifestClass.keys)
            {
                foreach (ClassTemplateMap classTemplateMap in graphMap.classTemplateMaps)
                {
                    if (classTemplateMap.classMap.id == key.classId && classTemplateMap.classMap.index == manifestClass.index)
                    {
                        foreach (TemplateMap templateMap in classTemplateMap.templateMaps)
                        {
                            if (templateMap.id == key.templateId)
                            {
                                foreach (RoleMap roleMap in templateMap.roleMaps)
                                {
                                    if (roleMap.id == key.roleId)
                                    {
                                        newClassMap.identifiers.Add(roleMap.propertyName);
                                    }
                                }
                            }
                        }
                    }
                }
            }

            // if manifiest key is not mapped, then use mapping class identifier
            if (newClassMap.identifiers == null || newClassMap.identifiers.Count == 0)
            {
                newClassMap.identifiers = classMap.identifiers;
            }

            return(newClassMap);
        }
        public static void DeleteTemplateMap(this GraphMap graphMap, string parentClassId, int parentClassIndex, int templateMapIndex)
        {
            ClassTemplateMap ctm = graphMap.GetClassTemplateMap(parentClassId, parentClassIndex);
            TemplateMap      tm  = ctm.templateMaps[templateMapIndex];

            foreach (RoleMap rm in tm.roleMaps)
            {
                DeleteRoleMap(graphMap, tm, rm.id);
            }

            ctm.templateMaps.RemoveAt(templateMapIndex);

            //int newIndex = 0;
            //foreach (TemplateMap tMap in ctm.templateMaps.Where(x => x.id == tm.id))
            //{
            //  tMap.index = newIndex++;
            //}

            graphMap.SetClassPath();
        }
        private static void SetNewIndexOfClass(GraphMap graphMap, string classId, int oldIndex, int newIndex)
        {
            foreach (ClassTemplateMap ctm in graphMap.classTemplateMaps)
            {
                if (ctm.classMap.id == classId && ctm.classMap.index == oldIndex)
                {
                    ctm.classMap.index = newIndex;
                }

                foreach (TemplateMap tm in ctm.templateMaps)
                {
                    foreach (RoleMap rm in tm.roleMaps)
                    {
                        if (rm.classMap != null && rm.classMap.id == classId && rm.classMap.index == oldIndex)
                        {
                            rm.classMap.index = newIndex;
                        }
                    }
                }
            }
        }
        private static void SetClassPath(this GraphMap graphMap, ClassMap parentClassMap)
        {
            if (parentClassMap.path == null)
            {
                parentClassMap.path = "";
            }

            ClassTemplateMap ctm = graphMap.classTemplateMaps.SingleOrDefault(x => x.classMap.id == parentClassMap.id && x.classMap.index == parentClassMap.index);

            if (ctm != null)
            {
                ctm.classMap.path = parentClassMap.path;
                foreach (TemplateMap tm in ctm.templateMaps)
                {
                    foreach (RoleMap rm in tm.roleMaps.Where(x => x.classMap != null))
                    {
                        rm.classMap.path = string.Format("{0}/{1}/{2}({3})/{4}", ctm.classMap.path, ctm.classMap.id, tm.id, tm.index, rm.id).Trim('/');

                        SetClassPath(graphMap, rm.classMap);
                    }
                }
            }
        }
        public static void DeleteClassMap(this GraphMap graphMap, string classId, int classIndex)
        {
            ClassTemplateMap classTemplateMap = graphMap.GetClassTemplateMap(classId, classIndex);

            if (classTemplateMap != null && classTemplateMap.classMap != null)
            {
                List <TemplateMap> templateMaps = classTemplateMap.templateMaps;

                foreach (TemplateMap templateMap in templateMaps)
                {
                    RoleMap classRole = templateMap.roleMaps.Where(c => c.classMap != null).FirstOrDefault();
                    if (classRole != null)
                    {
                        graphMap.DeleteClassMap(classRole.classMap.id, classRole.classMap.index);
                        classRole.classMap = null;
                    }
                }

                templateMaps.Clear();
                graphMap.classTemplateMaps.Remove(classTemplateMap);
            }

            //RearrangeClassMapIndex(graphMap, classId);
        }
        public static void AddClassMap(this GraphMap graphMap, RoleMap roleMap, ClassMap classMap)
        {
            ClassTemplateMap ctm = graphMap.GetClassTemplateMap(classMap.id, classMap.index);

            if (ctm == null)
            {
                graphMap.classTemplateMaps.Add(
                    new ClassTemplateMap
                {
                    classMap     = classMap,
                    templateMaps = new TemplateMaps()
                }
                    );
            }
            else
            {
                ctm.classMap = classMap;
            }

            if (roleMap != null)
            {
                roleMap.classMap = classMap;
            }
        }
 public static void RearrangeIndexAndPath(this GraphMap graphMap)
 {
     graphMap.RearrangeTemplateMapIndex();
     graphMap.RearrangeClassMapIndex();
     graphMap.SetClassPath();
 }
        public static IDictionary <String, IDictionary <String, ValueList> > GetMappedValueListsWithPath(this GraphMap graphMap, ValueListMaps valueListMaps, String templateId, int templateIndex, String parentClassId, int parentClassIndex, IDictionary <String, IDictionary <String, ValueList> > valueList = null)
        {
            if (valueList == null)
            {
                valueList = new Dictionary <String, IDictionary <String, ValueList> >();
            }

            var classTemplateMap = graphMap.GetClassTemplateMap(parentClassId, parentClassIndex);



            if (classTemplateMap != null)
            {
                var template = classTemplateMap.templateMaps.SingleOrDefault(x => (x.id == templateId && x.index == templateIndex));
                if (template != null)
                {
                    foreach (var role in template.roleMaps.Where(x => !String.IsNullOrWhiteSpace(x.propertyName)))
                    {
                        if (role.type == RoleType.ObjectProperty)
                        {
                            string path = (string.IsNullOrEmpty(classTemplateMap.classMap.path) ? "" : classTemplateMap.classMap.path) + "/" + classTemplateMap.classMap.id.ToString() + "/" + template.id.ToString() + "/" + role.id.ToString();
                            Dictionary <String, ValueList> valListDict = new Dictionary <String, ValueList>();
                            ValueMaps       valueMaps = GetValueListMap(valueListMaps, role.valueListName);
                            ValueList       valList   = new ValueList();
                            ValueCollection valCol    = new ValueCollection();
                            valList.name = role.valueListName;
                            foreach (var item in valueMaps)
                            {
                                ValueItem valItem = new ValueItem();
                                valItem.label = item.label;
                                valItem.value = item.internalValue;
                                valItem.uri   = item.uri;
                                valCol.Add(valItem);
                            }
                            valList.values = valCol;
                            valListDict.Add(role.propertyName, valList);
                            valueList.Add(path, valListDict);
                        }
                    }
                    foreach (var role in template.roleMaps.Where(x => x.classMap != null))
                    {
                        var classTemplate = graphMap.GetClassTemplateMap(role.classMap.id, role.classMap.index);
                        foreach (var tmpl in classTemplate.templateMaps)
                        {
                            if (tmpl != null)
                            {
                                graphMap.GetMappedValueListsWithPath(valueListMaps, tmpl.id, tmpl.index, role.classMap.id, role.classMap.index, valueList);
                            }
                        }
                    }
                }
            }
            return(valueList);
        }
        public static IDictionary <String, IDictionary <String, Identifiers> > GetMappedPropertiesWithPath(this GraphMap graphMap, String templateId, int templateIndex, String parentClassId, int parentClassIndex, IDictionary <String, IDictionary <String, Identifiers> > propertyList = null)
        {
            if (propertyList == null)
            {
                propertyList = new Dictionary <String, IDictionary <String, Identifiers> >();
            }

            var classTemplateMap = graphMap.GetClassTemplateMap(parentClassId, parentClassIndex);

            if (classTemplateMap != null)
            {
                var template = classTemplateMap.templateMaps.SingleOrDefault(x => (x.id == templateId && x.index == templateIndex));
                if (template != null)
                {
                    foreach (var role in template.roleMaps.Where(x => !String.IsNullOrWhiteSpace(x.propertyName)))
                    {
                        //   if (role.type != RoleType.ObjectProperty)
                        //  {
                        string path = (string.IsNullOrEmpty(classTemplateMap.classMap.path) ? "" : classTemplateMap.classMap.path) + "/" + classTemplateMap.classMap.id.ToString() + "/" + template.id.ToString() + "/" + role.id.ToString();
                        Dictionary <String, Identifiers> identifierList = new Dictionary <String, Identifiers>();
                        identifierList.Add(role.propertyName, classTemplateMap.classMap.identifiers);
                        propertyList.Add(path, identifierList);
                        //  }
                    }
                    foreach (var role in template.roleMaps.Where(x => x.classMap != null))
                    {
                        var classTemplate = graphMap.GetClassTemplateMap(role.classMap.id, role.classMap.index);
                        foreach (var tmpl in classTemplate.templateMaps)
                        {
                            if (tmpl != null)
                            {
                                graphMap.GetMappedPropertiesWithPath(tmpl.id, tmpl.index, role.classMap.id, role.classMap.index, propertyList);
                            }
                        }
                    }
                }
            }
            return(propertyList);
        }
        public static string BuildClassPath(this GraphMap graphMap, ClassMap parentClassMap, TemplateMap templateMap, RoleMap roleMap)
        {
            string path = string.Format("{0}/{1}/{2}({3})/{4}", parentClassMap.path, parentClassMap.id, templateMap.id, templateMap.index, roleMap.id).Trim('/');

            return(path);
        }