Ejemplo n.º 1
0
        protected JoinEntityData FindAllJoinData(Type t1, Type t2, string joinTableName)
        {
            JoinEntityData joinData = GetJoinData(t1, t2, joinTableName);

            if (joinData == null)
            {
                long tstart = DateTime.Now.Ticks;

                //sort before add
                TypeComparer typeComparer = new TypeComparer();
                Type[]       ts           = new Type[] { t1, t2 };
                Array.Sort(ts, typeComparer);

                joinData = new JoinEntityData()
                {
                    DataType1 = ts[0], DataType2 = ts[1], JoinTableName = joinTableName
                };

                var listType = typeof(List <JoinEntityDataItem>);
                List <JoinEntityDataItem> list = null;
                string filename = Path.Combine(Path.Combine(FrwConfig.Instance.ProfileDir, DATA_STORAGE), joinData.DataType1.FullName + "_" + joinData.DataType2.FullName +
                                               (joinTableName != null ? ("_" + joinTableName) : "") + ".json");
                FileInfo fileInfo = new FileInfo(filename);
                if (fileInfo.Exists)
                {
                    list = JsonSerializeHelper.LoadForType(filename, listType);
                }
                else
                {
                    list = (List <JoinEntityDataItem>)Activator.CreateInstance(listType);
                }
                joinData.DataList = list;

                long tstartConvert = DateTime.Now.Ticks;

                PropertyInfo pkProp1 = AttrHelper.GetProperty <JPrimaryKey>(joinData.DataType1);

                if (pkProp1.PropertyType != typeof(string))
                {
                    foreach (var l in joinData.DataList)
                    {
                        l.Pk1 = JsonSerializeHelper.DeserializeString(l.Pk1.ToString(), pkProp1.PropertyType);
                    }
                }
                PropertyInfo pkProp2 = AttrHelper.GetProperty <JPrimaryKey>(joinData.DataType1);
                if (pkProp2.PropertyType != typeof(string))
                {
                    foreach (var l in joinData.DataList)
                    {
                        l.Pk2 = JsonSerializeHelper.DeserializeString(l.Pk2.ToString(), pkProp2.PropertyType);
                    }
                }
                joinDatas.Add(joinData);
                long tend = DateTime.Now.Ticks;
                //Log.ProcessDebug("======= Loaded join data : " + sdata.DataType1 + " " + sdata.DataType2  + " Count: " + ((IList)list).Count + " Time: " + (tend - tstart) / 10000 + " mils" + " include time converting: " + (tend - tstartConvert) / 10000 + " mils");
            }
            return(joinData);
        }
Ejemplo n.º 2
0
        public IList FindFromJoin(object rowObject, Type foreinEntityType, string joinName = null)
        {
            IList values = (IList)Activator.CreateInstance(typeof(List <>).MakeGenericType(foreinEntityType));

            if (rowObject == null)
            {
                throw new InvalidDataException(" FindFromJoin: NULL object to find");                    // return values;
            }
            Type sourceEntityType = rowObject.GetType();

            PropertyInfo   pkProp  = AttrHelper.GetProperty <JPrimaryKey>(sourceEntityType);
            JoinEntityData cross   = FindAllJoinData(sourceEntityType, foreinEntityType, joinName);
            bool           reverse = false;

            if (cross.DataType1 != sourceEntityType)
            {
                reverse = true;
            }
            List <object> cValues       = new List <object>();
            object        sourcePKValue = AttrHelper.IsSimple(rowObject.GetType()) ? rowObject :  pkProp.GetValue(rowObject);

            if (sourcePKValue == null)
            {
                throw new InvalidDataException(" FindFromJoin: wrong object to find");
            }
            //from crosstable
            foreach (var l in cross.DataList)
            {
                if (reverse)
                {
                    if (sourcePKValue.Equals(l.Pk2))
                    {
                        cValues.Add(l.Pk1);
                    }
                }
                else
                {
                    if (sourcePKValue.Equals(l.Pk1))
                    {
                        cValues.Add(l.Pk2);
                    }
                }
            }

            foreach (var foreinKeyValue in cValues)
            {
                object foreinEntityValue = Find(foreinEntityType, foreinKeyValue);
                if (foreinEntityValue != null)
                {
                    values.Add(foreinEntityValue);
                }
                else
                {
                    //todo
                }
            }
            return(values);
        }
Ejemplo n.º 3
0
        public void SetJoinModified(Type t1, Type t2, string joinTableName)
        {
            JoinEntityData sdata = GetJoinData(t1, t2, joinTableName);

            if (sdata != null)
            {
                sdata.Modified = true;
            }
        }
Ejemplo n.º 4
0
        //for testing purpose only
        public bool IsJoinModified(Type t1, Type t2, string joinTableName)
        {
            JoinEntityData sdata = GetJoinData(t1, t2, joinTableName);

            if (sdata != null)
            {
                return(sdata.Modified);
            }
            else
            {
                return(false);
            }
        }
Ejemplo n.º 5
0
        private void SaveJoinEntityDataLocal(JoinEntityData s)
        {
            string        dirPath = Path.Combine(FrwConfig.Instance.ProfileDir, DATA_STORAGE);
            DirectoryInfo dir     = new DirectoryInfo(dirPath);

            if (dir.Exists == false)
            {
                Directory.CreateDirectory(dir.FullName);
            }
            string filename = Path.Combine(dirPath, s.DataType1.FullName + "_" + s.DataType2.FullName + ".json");
            object list     = s.DataList;

            JsonSerializeHelper.SaveToFile(list, filename);
            s.Modified = false;
        }
Ejemplo n.º 6
0
        private JoinEntityData GetJoinData(Type t1, Type t2, string crossTableName)
        {
            TypeComparer typeComparer = new TypeComparer();

            Type[] ts = new Type[] { t1, t2 };
            Array.Sort(ts, typeComparer);

            JoinEntityData sdata = null;

            foreach (var s in joinDatas)
            {
                if (s.DataType1.Equals(ts[0]) && s.DataType2.Equals(ts[1]) && (crossTableName == null || crossTableName.Equals(s.JoinTableName)))
                {
                    sdata = s;
                    break;
                }
            }
            return(sdata);
        }
Ejemplo n.º 7
0
        public void AddJoin(object o1, object o2, string joinTableName = null)
        {
            object         pk1Value = ModelHelper.GetPKValue(o1);
            object         pk2Value = ModelHelper.GetPKValue(o2);
            JoinEntityData join     = FindAllJoinData(o1.GetType(), o2.GetType(), joinTableName);
            bool           reverse  = false;

            if (join.DataType1 != o1.GetType())
            {
                reverse = true;
            }
            bool found = false;

            foreach (var c in join.DataList)
            {
                if ((!reverse && c.Pk1.Equals(pk1Value) && c.Pk2.Equals(pk2Value))
                    ||
                    (reverse && c.Pk2.Equals(pk1Value) && c.Pk1.Equals(pk2Value)))
                {
                    found = true;
                    break;
                }
            }
            if (!found)
            {
                JoinEntityDataItem joinObject = new JoinEntityDataItem();
                if (reverse)
                {
                    joinObject.Pk2 = pk1Value;
                    joinObject.Pk1 = pk2Value;
                }
                else
                {
                    joinObject.Pk1 = pk1Value;
                    joinObject.Pk2 = pk2Value;
                }
                join.DataList.Add(joinObject);
                SetJoinModified(o1.GetType(), o2.GetType(), joinTableName);
            }
        }
Ejemplo n.º 8
0
        public void DeleteJoin(object o1, object o2, string joinTableName = null)
        {
            object         pk1Value = ModelHelper.GetPKValue(o1);
            object         pk2Value = ModelHelper.GetPKValue(o2);
            JoinEntityData join     = FindAllJoinData(o1.GetType(), o2.GetType(), joinTableName);
            bool           reverse  = false;

            if (join.DataType1 != o1.GetType())
            {
                reverse = true;
            }
            foreach (var c in join.DataList)
            {
                if ((!reverse && c.Pk1.Equals(pk1Value) && c.Pk2.Equals(pk2Value))
                    ||
                    (reverse && c.Pk2.Equals(pk1Value) && c.Pk1.Equals(pk2Value)))
                {
                    join.DataList.Remove(c);
                    SetJoinModified(o1.GetType(), o2.GetType(), joinTableName);
                    break;
                }
            }
        }
Ejemplo n.º 9
0
        public void SaveJoinData(Type t1, Type t2, string joinTablename = null)
        {
            JoinEntityData s = GetJoinData(t1, t2, joinTablename);

            SaveJoinEntityDataLocal(s);
        }