Beispiel #1
0
        private SData LoadEntityFromDisk(Type t)
        {
            JEntity entityAttr = AttrHelper.GetClassAttribute <JEntity>(t);

            if (entityAttr.DsType != typeof(JsonDs))
            {
                throw new InvalidOperationException("Not a json file stored entity");
            }

            //long tstart = DateTime.Now.Ticks;
            SData sdata = new SData()
            {
                DataType = t
            };
            Type   lt       = typeof(List <>);
            Type   listType = lt.MakeGenericType(t);
            object list     = null;

            string   filename = GetDataFilePathForType(t);
            FileInfo fileInfo = new FileInfo(filename);

            if (fileInfo.Exists)
            {
                list = JsonSerializeHelper.LoadForType(filename, listType);
            }
            else
            {
                list = Activator.CreateInstance(listType);
            }
            foreach (var x in (IList)list)
            {
                INotifyPropertyChanged notifier = x as INotifyPropertyChanged;
                if (notifier != null)
                {
                    notifier.PropertyChanged += Notifier_PropertyChanged;
                }
            }
            PropertyInfo pkProp = AttrHelper.GetProperty <JPrimaryKey>(t);

            if (pkProp != null)
            {
                foreach (var o in (IList)list)
                {
                    object pk = pkProp.GetValue(o);
                    try
                    {
                        sdata.PkCache.Add(pk, o);
                    }
                    catch (ArgumentException ex)
                    {
                        Log.ProcessDebug("Dublicate pk entity: " + t + " pk value:  " + pk + " Error text: " + ex);
                    }
                }
            }
            sdata.DataList = list;
            sdatas.Add(sdata);

            return(sdata);
        }
Beispiel #2
0
        protected ToolStripMenuItem CreateMainMenuItemForEntityType(ToolStripMenuItem parentItem, Type t)
        {
            JEntity           entityAttr = AttrHelper.GetClassAttribute <JEntity>(t);
            ToolStripMenuItem menuItem   = new ToolStripMenuItem();

            menuItem.Name   = "menuItem_" + t.FullName;
            menuItem.Text   = ModelHelper.GetEntityJDescriptionOrName(t);
            menuItem.Click += (s, em) =>
            {
                CreateList(t);
            };
            parentItem.DropDownItems.AddRange(new ToolStripItem[] {
                menuItem
            });
            return(menuItem);
        }
Beispiel #3
0
 ///////////////////////////
 ///  /// <summary>
 /// Save all data to file system
 /// </summary>
 public void SaveAllEntitiesData(bool allways)
 {
     foreach (var s in sdatas)
     {
         Type    entityType = s.DataType;
         JEntity entityAttr = AttrHelper.GetClassAttribute <JEntity>(entityType);
         if (s.Modified || allways)
         {
             SaveEntityData(s);
         }
     }
     foreach (var s in joinDatas)
     {
         if (s.Modified || allways)
         {
             SaveJoinEntityDataLocal(s);
         }
     }
 }
Beispiel #4
0
 public bool IsCustomSetting()
 {
     if (this.ValueType != null)
     {
         if (DictId != null)
         {
             return(true);
         }
         else if (ValueType != typeof(string))
         {
             JEntity entityAttr = AttrHelper.GetClassAttribute <JEntity>(ValueType);
             if (entityAttr != null)
             {
                 return(true);
             }
         }
     }
     return(false);
 }
Beispiel #5
0
        protected ToolStripMenuItem CreateMainMenuItemForWindowType(ToolStripMenuItem parentItem, string name, Type t)
        {
            JEntity           entityAttr = AttrHelper.GetClassAttribute <JEntity>(t);
            ToolStripMenuItem menuItem   = new ToolStripMenuItem();

            menuItem.Name   = "menuItem_" + t.FullName;
            menuItem.Text   = name != null ? name : ModelHelper.GetEntityJDescriptionOrName(t);
            menuItem.Click += (s, em) =>
            {
                try
                {
                    AppManager.Instance.CreateContentAndProcessView(this, t, null);
                }
                catch (Exception ex)
                {
                    Log.ShowError(ex);
                }
            };
            parentItem.DropDownItems.AddRange(new ToolStripItem[] {
                menuItem
            });
            return(menuItem);
        }
Beispiel #6
0
        public SimpleTreeListWindow()
        {
            InitializeComponent();
            treeControl.LabelEdit = true;
            //treeControl.ChangeNodeImageOnExpand = true;
            treeControl.AllowDrop        = true;
            treeControl.ShowNodeToolTips = true;

            treeControl.OnTreeNodeSelectEvent += TreeControl_OnTreeNodeSelectEvent;

            this.treeControl.CanExpandGetter += delegate(TreeNode parentNode)
            {
                if (parentNode == null)
                {
                    return(true);
                }
                object x = parentNode.Tag;
                if (x == null)
                {
                    return(false);
                }
                if (x is string)
                {
                    return(true);            //folder
                }
                Type type = x.GetType();
                if (x is RootGroupTreeFolder)
                {
                    Type etype = (x as RootGroupTreeFolder).EntityType;
                    if (ModelHelper.IsSingleHierEntity(etype))
                    {
                        return(Dm.Instance.FindRootList(etype).Count > 0);
                    }
                    else
                    {
                        return(Dm.Instance.FindAll(etype).Count > 0);
                    }
                }
                else if (x is BranchGroupTreeFolder)
                {
                    BranchGroupTreeFolder bf = (x as BranchGroupTreeFolder);
                    if (AttrHelper.GetAttribute <JManyToMany>(bf.RefEntityInfo.foreinProperty) != null)
                    {
                        return(Dm.Instance.ResolveManyToManyRelation(bf.ParentObject, bf.RefEntityInfo.RefEntity).Count > 0);
                    }
                    else if (AttrHelper.GetAttribute <JManyToOne>(bf.RefEntityInfo.foreinProperty) != null)
                    {
                        return(Dm.Instance.ResolveOneToManyRelation(bf.ParentObject, bf.RefEntityInfo.RefEntity,
                                                                    bf.RefEntityInfo.foreinProperty.Name).Count > 0);
                    }
                    else if (AttrHelper.GetAttribute <JOneToOne>(bf.RefEntityInfo.foreinProperty) != null)
                    {
                        return(Dm.Instance.ResolveOneToOneRelation(bf.ParentObject, bf.RefEntityInfo.RefEntity,
                                                                   bf.RefEntityInfo.foreinProperty.Name) != null);
                    }
                }
                else
                {
                    JEntity entityAttr = x.GetType().GetCustomAttribute <JEntity>();
                    if (entityAttr != null)
                    {
                        if (showGroupsFolder)
                        {
                            List <RefEntityInfo> rels = Dm.Instance.GetAllReferencedToEntity(x, false);
                            return(rels.Count(s => (s.foreinProperty != null)) > 0);

                            /*
                             * foreach (var rt in rels)
                             * {
                             *  HashSet<object> refs = rt.Records;
                             *  if (refs.Count > 0)
                             *  {
                             *      return true;
                             *  }
                             * }
                             */
                        }
                        else
                        {
                            //find all ManyToOne relations rels to this entity type
                            List <RefEntityInfo> rels = Dm.Instance.GetAllReferencedToEntity(x);
                            foreach (var rt in rels)
                            {
                                HashSet <object> refs = rt.Records;
                                if (refs.Count > 0)
                                {
                                    return(true);
                                }
                            }
                        }
                    }
                }
                return(false);
            };
            treeControl.ChildrenGetter += delegate(TreeNode parentNode)
            {
                IList lo = new List <object>();
                if (parentNode == null)
                {
                    //root
                    foreach (var e in rootEntites)
                    {
                        RootGroupTreeFolder ef = new RootGroupTreeFolder();
                        ef.EntityType = e;
                        lo.Add(ef);
                    }
                }
                else
                {
                    object x = parentNode.Tag;
                    if (x == null)
                    {
                        return(new List <object>());
                    }
                    if (x is RootGroupTreeFolder)
                    {
                        Type type = (x as RootGroupTreeFolder).EntityType;
                        if (ModelHelper.IsSingleHierEntity(type))
                        {
                            return(Dm.Instance.FindRootList(type));
                        }
                        else
                        {
                            return(Dm.Instance.FindAll(type));
                        }
                    }
                    else if (x is BranchGroupTreeFolder)
                    {
                        BranchGroupTreeFolder bf = (x as BranchGroupTreeFolder);
                        if (AttrHelper.GetAttribute <JManyToMany>(bf.RefEntityInfo.foreinProperty) != null)
                        {
                            return(Dm.Instance.ResolveManyToManyRelation(bf.ParentObject, bf.RefEntityInfo.RefEntity));
                        }
                        else if (AttrHelper.GetAttribute <JManyToOne>(bf.RefEntityInfo.foreinProperty) != null)
                        {
                            return(Dm.Instance.ResolveOneToManyRelation(bf.ParentObject, bf.RefEntityInfo.RefEntity,
                                                                        bf.RefEntityInfo.foreinProperty.Name));
                        }
                        else if (AttrHelper.GetAttribute <JOneToOne>(bf.RefEntityInfo.foreinProperty) != null)
                        {
                            lo.Add(Dm.Instance.ResolveOneToOneRelation(bf.ParentObject, bf.RefEntityInfo.RefEntity,
                                                                       bf.RefEntityInfo.foreinProperty.Name));
                        }
                    }
                    else
                    {
                        bool    isSingleHierEntity = ModelHelper.IsSingleHierEntity(x.GetType());
                        JEntity entityAttr         = x.GetType().GetCustomAttribute <JEntity>();
                        if (entityAttr != null)
                        {
                            if (showGroupsFolder)
                            {
                                //List<RefEntityInfo> rels = Dm.Instance.GetAllReferencedToEntity(x, false);
                                List <RefEntityInfo> rels = Dm.Instance.GetAllReferencedToEntity(x);
                                foreach (var rt in rels)
                                {
                                    if (rt.foreinProperty != null)
                                    {
                                        if (rt.IsSelfRelation() && isSingleHierEntity)
                                        {
                                            //IList lll = Dm.Instance.ResolveOneToManyRelation(x, rt.RefEntity,
                                            //   rt.foreinProperty.Name);
                                            //foreach (var l1 in lll)
                                            //  lo.Add(l1);
                                            foreach (var l1 in rt.Records)
                                            {
                                                lo.Add(l1);
                                            }
                                        }
                                        else
                                        {
                                            if (rt.Records.Count > 0)
                                            {
                                                BranchGroupTreeFolder bf = new BranchGroupTreeFolder();
                                                bf.ParentObject  = x;
                                                bf.RefEntityInfo = rt;
                                                lo.Add(bf);
                                            }
                                        }
                                    }
                                }
                            }
                            else
                            {
                                List <RefEntityInfo> rels = Dm.Instance.GetAllReferencedToEntity(x);

                                foreach (var rt in rels)
                                {
                                    HashSet <object> refs = rt.Records;
                                    if (refs.Count > 0)
                                    {
                                        foreach (var l in refs)
                                        {
                                            lo.Add(l);
                                        }
                                    }
                                }
                            }
                        }
                        return(lo);
                    }
                }
                return(lo);
            };
            treeControl.AfterEditTreeNodeLabel += delegate(object model, string labelText)
            {
                if (model == null)
                {
                    return(false);
                }
                Type    t          = model.GetType();
                JEntity entityAttr = t.GetCustomAttribute <JEntity>();
                if (entityAttr != null)
                {
                    PropertyInfo pName = AttrHelper.GetProperty <JNameProperty>(t);
                    if (pName != null)
                    {
                        object oldValue = pName.GetValue(model);
                        try
                        {
                            pName.SetValue(model, labelText);//force exception if no set method
                            Dm.Instance.SaveObject(model);
                            return(true);
                        }
                        catch (JValidationException ex)
                        {
                            AppManager.ShowValidationErrorMessage(ex.ValidationResult);
                            pName.SetValue(model, oldValue);
                        }
                    }
                }
                return(false);
            };
        }
Beispiel #7
0
        override protected void ComplateNodeFromObject(TreeNode node, object o)
        {
            base.ComplateNodeFromObject(node, o);

            object x = node.Tag;

            if (x == null)
            {
                return;
            }
            Type type = x.GetType();

            if (x is RootGroupTreeFolder)
            {
                Type   etype = (x as RootGroupTreeFolder).EntityType;
                string name  = ModelHelper.GetEntityJDescriptionOrName(etype);
                node.Name        = name;
                node.Text        = name;
                node.ToolTipText = name;
            }
            else if (x is BranchGroupTreeFolder)
            {
                Type   etype = (x as BranchGroupTreeFolder).RefEntityInfo.RefEntity;
                string name  = (x as BranchGroupTreeFolder).RefEntityInfo.GetRelDescription();
                node.Name        = name;
                node.Text        = name;
                node.ToolTipText = name;
            }
            else
            {
                JEntity entityAttr = type.GetCustomAttribute <JEntity>();
                if (entityAttr != null && entityAttr.ImageName != null)
                {
                    if (treeControl.ImageList == null)
                    {
                        treeControl.ImageList = new ImageList();
                    }
                    if (!treeControl.ImageList.Images.ContainsKey(entityAttr.ImageName))
                    {
                        Image smallImage = null;
                        if (smallImage == null && entityAttr.Resource != null)
                        {
                            smallImage = TypeHelper.LookupImageResource(entityAttr.Resource, entityAttr.ImageName);
                        }
                        if (smallImage == null)
                        {
                            smallImage = (Image)Properties.Resources.ResourceManager.GetObject(entityAttr.ImageName);
                        }
                        //if not found in current assembly do advanced search
                        if (smallImage == null)
                        {
                            smallImage = TypeHelper.FindImageInAllDiskStorages(entityAttr.ImageName);
                        }
                        if (smallImage != null)
                        {
                            treeControl.ImageList.Images.Add(entityAttr.ImageName, smallImage);
                        }
                    }
                    node.ImageKey         = entityAttr.ImageName;
                    node.SelectedImageKey = entityAttr.ImageName;
                }
            }
        }
Beispiel #8
0
        static protected bool IsShortTable(Type t)
        {
            JEntity entityAttr = AttrHelper.GetClassAttribute <JEntity>(t);

            return(entityAttr.ShortTable);
        }
        public SimpleTreeListWindow()
        {
            InitializeComponent();
            treeControl.LabelEdit = true;
            //treeControl.ChangeNodeImageOnExpand = true;
            treeControl.AllowDrop        = true;
            treeControl.ShowNodeToolTips = true;

            treeControl.OnTreeNodeSelectEvent += TreeControl_OnTreeNodeSelectEvent;

            this.treeControl.CanExpandGetter += delegate(TreeNode currentNode)
            {
                if (currentNode == null)
                {
                    return(true);
                }
                //currentNode may be not object node (may by branch node), so find real object node to view object
                TreeNode currentObjectNode = (currentNode.Tag is BranchGroupTreeFolder) ? currentNode.Parent : currentNode;
                TreeNode parentObjectNode  = (currentObjectNode != null && currentObjectNode.Parent != null)
                    ? ((currentObjectNode.Parent.Tag is BranchGroupTreeFolder) ? currentObjectNode.Parent.Parent : currentObjectNode.Parent) : null;

                object curentObject = (currentNode.Tag is TreeObjectWrap) ? (currentNode.Tag as TreeObjectWrap).Tag : currentNode.Tag;
                object parentObject = (parentObjectNode != null) ? ((parentObjectNode.Tag is TreeObjectWrap)? (parentObjectNode.Tag as TreeObjectWrap).Tag : parentObjectNode.Tag) : null;

                RefEntityInfo currentNodeRel = (currentObjectNode != null && currentObjectNode.Tag is TreeObjectWrap) ? (currentObjectNode.Tag as TreeObjectWrap).Rel : null;
                RefEntityInfo parentNodeRel  = (parentObjectNode != null && parentObjectNode.Tag is TreeObjectWrap) ? (parentObjectNode.Tag as TreeObjectWrap).Rel : null;

                if (curentObject == null)
                {
                    return(false);
                }
                if (curentObject is string)
                {
                    return(true);                       //folder
                }
                Type type = curentObject.GetType();
                if (curentObject is RootGroupTreeFolder)
                {
                    Type etype = (curentObject as RootGroupTreeFolder).EntityType;
                    if (ModelHelper.IsSingleHierEntity(etype))
                    {
                        return(Dm.Instance.FindRootList(etype).Count > 0);
                    }
                    else
                    {
                        return(Dm.Instance.FindAll(etype).Count > 0);
                    }
                }
                else if (curentObject is BranchGroupTreeFolder)
                {
                    BranchGroupTreeFolder bf = (curentObject as BranchGroupTreeFolder);
                    if (bf.RefEntityInfo.PropertyInForeign != null)
                    {
                        if (AttrHelper.GetAttribute <JManyToMany>(bf.RefEntityInfo.PropertyInForeign) != null)
                        {
                            return(Dm.Instance.ResolveManyToManyRelation(bf.ParentObject, bf.RefEntityInfo.ForeignEntity).Count > 0);
                        }
                        else if (AttrHelper.GetAttribute <JManyToOne>(bf.RefEntityInfo.PropertyInForeign) != null)
                        {
                            return(Dm.Instance.ResolveOneToManyRelation(bf.ParentObject, bf.RefEntityInfo.ForeignEntity,
                                                                        bf.RefEntityInfo.PropertyInForeign.Name).Count > 0);
                        }
                        else if (AttrHelper.GetAttribute <JOneToOne>(bf.RefEntityInfo.PropertyInForeign) != null)
                        {
                            return(Dm.Instance.ResolveOneToOneRelationReverse(bf.ParentObject, bf.RefEntityInfo.ForeignEntity,
                                                                              bf.RefEntityInfo.PropertyInForeign.Name) != null);
                        }
                    }
                    else if (bf.RefEntityInfo.PropertyInSource != null)
                    {
                        if (AttrHelper.GetAttribute <JManyToOne>(bf.RefEntityInfo.PropertyInSource) != null || AttrHelper.GetAttribute <JOneToOne>(bf.RefEntityInfo.PropertyInSource) != null)
                        {
                            object o = AttrHelper.GetPropertyValue(bf.ParentObject, bf.RefEntityInfo.PropertyInSource);
                            return(o != null);
                        }
                    }
                }
                else
                {
                    JEntity entityAttr = curentObject.GetType().GetCustomAttribute <JEntity>();
                    if (entityAttr != null)
                    {
                        //if (showGroupsFolder)
                        //{
                        //   List<RefEntityInfo> rels = Dm.Instance.GetAllReferencedToEntity(parentObject, false);
                        //   return rels.Count(s => (s.RefFromProperty != null || s.RefToProperty != null)) > 0;
                        //}
                        //else
                        //{
                        //find all ManyToOne relations rels to this entity type
                        List <RefEntityInfo> rels = Dm.Instance.GetAllReferencedToEntity(curentObject);
                        foreach (var rt in rels)
                        {
                            HashSet <object> refs = rt.Records;
                            if (refs.Count > 0)
                            {
                                return(true);
                            }
                        }
                        //}
                    }
                }
                return(false);
            };
            treeControl.ChildrenGetter += delegate(TreeNode currentNode)
            {
                IList lo = new List <object>();
                if (currentNode == null)
                {
                    //root
                    foreach (var e in rootEntites)
                    {
                        RootGroupTreeFolder ef = new RootGroupTreeFolder();
                        ef.EntityType = e;
                        lo.Add(ef);
                    }
                }
                else
                {
                    TreeNode currentObjectNode = (currentNode.Tag is BranchGroupTreeFolder) ? currentNode.Parent : currentNode;
                    TreeNode parentObjectNode  = (currentObjectNode != null && currentObjectNode.Parent != null)
                        ? ((currentObjectNode.Parent.Tag is BranchGroupTreeFolder) ? currentObjectNode.Parent.Parent : currentObjectNode.Parent) : null;

                    object currentObject = (currentNode.Tag is TreeObjectWrap) ? (currentNode.Tag as TreeObjectWrap).Tag : currentNode.Tag;
                    object parentObject  = (parentObjectNode != null) ? ((parentObjectNode.Tag is TreeObjectWrap) ? (parentObjectNode.Tag as TreeObjectWrap).Tag : parentObjectNode.Tag) : null;

                    RefEntityInfo currentNodeRel = (currentObjectNode != null && currentObjectNode.Tag is TreeObjectWrap) ?  (currentObjectNode.Tag as TreeObjectWrap).Rel : null;
                    RefEntityInfo parentNodeRel  = (parentObjectNode != null && parentObjectNode.Tag is TreeObjectWrap) ? (parentObjectNode.Tag as TreeObjectWrap).Rel : null;

                    if (currentObject == null)
                    {
                        return(new List <object>());
                    }
                    if (currentObject is RootGroupTreeFolder)
                    {
                        Type type = (currentObject as RootGroupTreeFolder).EntityType;
                        if (ModelHelper.IsSingleHierEntity(type))
                        {
                            return(WrapList(Dm.Instance.FindRootList(type), null));
                        }
                        else
                        {
                            return(WrapList(Dm.Instance.FindAll(type), null));
                        }
                    }
                    else if (currentObject is BranchGroupTreeFolder)
                    {
                        BranchGroupTreeFolder bf = (currentObject as BranchGroupTreeFolder);

                        if (savedTreeState.IsRelationVisible(bf.RefEntityInfo.SourceEntity, bf.RefEntityInfo.Name))
                        {
                            if (bf.RefEntityInfo.PropertyInForeign != null)
                            {
                                if (AttrHelper.GetAttribute <JManyToMany>(bf.RefEntityInfo.PropertyInForeign) != null)
                                {
                                    return(WrapList(Dm.Instance.ResolveManyToManyRelation(bf.ParentObject, bf.RefEntityInfo.ForeignEntity), bf.RefEntityInfo));
                                }
                                else if (AttrHelper.GetAttribute <JManyToOne>(bf.RefEntityInfo.PropertyInForeign) != null)
                                {
                                    return(WrapList(Dm.Instance.ResolveOneToManyRelation(bf.ParentObject, bf.RefEntityInfo.ForeignEntity,
                                                                                         bf.RefEntityInfo.PropertyInForeign.Name), bf.RefEntityInfo));
                                }
                                else if (AttrHelper.GetAttribute <JOneToOne>(bf.RefEntityInfo.PropertyInForeign) != null)
                                {
                                    lo.Add(new TreeObjectWrap()
                                    {
                                        Tag = Dm.Instance.ResolveOneToOneRelationReverse(bf.ParentObject, bf.RefEntityInfo.ForeignEntity,
                                                                                         bf.RefEntityInfo.PropertyInForeign.Name),
                                        Rel = bf.RefEntityInfo
                                    });
                                }
                            }
                            else if (bf.RefEntityInfo.PropertyInSource != null)
                            {
                                if (AttrHelper.GetAttribute <JManyToOne>(bf.RefEntityInfo.PropertyInSource) != null || AttrHelper.GetAttribute <JOneToOne>(bf.RefEntityInfo.PropertyInSource) != null)
                                {
                                    object o = AttrHelper.GetPropertyValue(bf.ParentObject, bf.RefEntityInfo.PropertyInSource);

                                    if (o != null)
                                    {
                                        if (parentObject != null && parentObject.Equals(o) &&
                                            currentNodeRel != null && currentNodeRel.PropertyInForeign != null && bf.RefEntityInfo.PropertyInSource != null &&
                                            currentNodeRel.PropertyInForeign == bf.RefEntityInfo.PropertyInSource)
                                        {
                                        }
                                        else
                                        {
                                            lo.Add(new TreeObjectWrap()
                                            {
                                                Tag = o, Rel = bf.RefEntityInfo
                                            });
                                        }
                                    }
                                }
                            }
                        }
                    }
                    else
                    {
                        bool    isSingleHierEntity = ModelHelper.IsSingleHierEntity(currentObject.GetType());
                        JEntity entityAttr         = currentObject.GetType().GetCustomAttribute <JEntity>();
                        if (entityAttr != null)
                        {
                            List <RefEntityInfo> rels = Dm.Instance.GetAllReferencedToEntity(currentObject);
                            foreach (var rt in rels)
                            {
                                if (savedTreeState.IsRelationVisible(rt.SourceEntity, rt.Name))
                                {
                                    bool isSelfRelation = rt.IsSelfRelation();
                                    if ((notShowGroupsFolderTypes.Contains(rt.ForeignEntity) == false && rt.PropertyInForeign != null) && !(isSelfRelation && isSingleHierEntity))
                                    {
                                        if (rt.Records.Count > 0)
                                        {
                                            BranchGroupTreeFolder bf = new BranchGroupTreeFolder();
                                            bf.ParentObject  = currentObject;
                                            bf.RefEntityInfo = rt;
                                            lo.Add(bf);
                                        }
                                    }
                                    else
                                    {
                                        foreach (var l in rt.Records)
                                        {
                                            if (!(parentObject != null && parentObject.Equals(l) &&
                                                  currentNodeRel != null && currentNodeRel.PropertyInForeign != null && rt.PropertyInSource != null &&
                                                  currentNodeRel.PropertyInForeign == rt.PropertyInSource))
                                            {
                                                lo.Add(new TreeObjectWrap()
                                                {
                                                    Tag = l, Rel = rt
                                                });
                                            }
                                        }
                                    }
                                }
                            }//foreach (var rt in rels)
                        }
                        return(lo);
                    }
                }
                return(lo);
            };
            treeControl.AfterEditTreeNodeLabel += delegate(object model, string labelText)
            {
                if (model == null)
                {
                    return(false);
                }
                Type    t          = model.GetType();
                JEntity entityAttr = t.GetCustomAttribute <JEntity>();
                if (entityAttr != null)
                {
                    PropertyInfo pName = AttrHelper.GetProperty <JNameProperty>(t);
                    if (pName != null)
                    {
                        object oldValue = pName.GetValue(model);
                        try
                        {
                            pName.SetValue(model, labelText);//force exception if no set method
                            Dm.Instance.SaveObject(model);
                            return(true);
                        }
                        catch (JValidationException ex)
                        {
                            AppManager.ShowValidationErrorMessage(ex.ValidationResult);
                            pName.SetValue(model, oldValue);
                        }
                    }
                }
                return(false);
            };
        }
Beispiel #10
0
        /// <summary>
        /// Saves all user settings
        /// </summary>
        public void SaveConfig()
        {
            foreach (var s in settings.Values)
            {
                if (s.Value != null && s.ValueType == null)
                {
                    s.ValueType = s.Value.GetType();
                }
                if (s.Value != null)
                {
                    if (s.ValueType != typeof(string))
                    {
                        JEntity entityAttr = AttrHelper.GetClassAttribute <JEntity>(s.ValueType);
                        if (entityAttr != null)
                        {
                            if (s.AllowMultiValues)
                            {
                                IList list   = (IList)s.Value;
                                IList values = (IList)Activator.CreateInstance(typeof(List <>).MakeGenericType(s.ValueType));
                                foreach (var realObject in list)
                                {
                                    values.Add(AttrHelper.ReplaceObjectByPkOnlyObject(realObject));
                                }
                                s.Value = values;
                            }
                            else
                            {
                                s.Value = AttrHelper.ReplaceObjectByPkOnlyObject(s.Value);
                            }
                        }
                        else
                        {
                            TypeConverter converter = TypeDescriptor.GetConverter(s.ValueType);
                            if (converter != null)
                            {
                                s.Value = converter.ConvertToString(s.Value);
                            }
                        }
                    }
                }
            }

            string        filename = Path.Combine(ProfileConfigDir, settingsFileName);
            FileInfo      fileInfo = new FileInfo(filename);
            DirectoryInfo dir      = fileInfo.Directory;

            if (dir.Exists == false)
            {
                Directory.CreateDirectory(dir.FullName);
            }
            List <JSetting> settingsList = new List <JSetting>();

            foreach (var s in settings.Values)
            {
                if (!s.IsAttachedToComputer)
                {
                    settingsList.Add(s);
                }
            }
            JsonSerializeHelper.SaveToFile(settingsList, filename);
            //stage 2
            filename = Path.Combine(ComputerUserDir, settingsFileName);
            fileInfo = new FileInfo(filename);
            dir      = fileInfo.Directory;
            if (dir.Exists == false)
            {
                Directory.CreateDirectory(dir.FullName);
            }
            settingsList = new List <JSetting>();
            foreach (var s in settings.Values)
            {
                if (s.IsAttachedToComputer)
                {
                    settingsList.Add(s);
                }
            }
            File.WriteAllText(filename, JsonSerializeHelper.SaveToString(settingsList), Encoding.UTF8);//short
        }
Beispiel #11
0
        /// <summary>
        /// Loads all user settings.
        /// </summary>
        public void LoadConfig()
        {
            string        filename = Path.Combine(ProfileConfigDir, settingsFileName);
            FileInfo      fileInfo = new FileInfo(filename);
            DirectoryInfo dir      = fileInfo.Directory;

            if (dir.Exists == false)
            {
                Directory.CreateDirectory(dir.FullName);
            }
            if (fileInfo.Exists)
            {
                List <JSetting> settingsList = null;
                settingsList = JsonSerializeHelper.LoadFromFile <List <JSetting> >(filename);
                foreach (var s in settingsList)
                {
                    s.IsAttachedToComputer = false;
                    settings.Add(s.Name, s);
                }
            }

            //Properties.Settings.Default path
            //https://msdn.microsoft.com/en-us/library/system.environment.specialfolder.aspx

            //stage 2
            if (ComputerUserDir != null)
            {
                filename = Path.Combine(ComputerUserDir, settingsFileName);
                fileInfo = new FileInfo(filename);
                dir      = fileInfo.Directory;
                if (dir.Exists == false)
                {
                    Directory.CreateDirectory(dir.FullName);
                }
                if (fileInfo.Exists)
                {
                    List <JSetting> settingsList = null;
                    settingsList = JsonSerializeHelper.LoadFromFile <List <JSetting> >(filename);
                    foreach (var s in settingsList)
                    {
                        s.IsAttachedToComputer = true;
                        settings[s.Name]       = s;
                    }
                }
            }

            foreach (JSetting setting in settings.Values)
            {
                if (setting.Value != null)
                {
                    Type type = setting.ValueType;
                    if (type != null)
                    {
                        if (type != typeof(string))
                        {
                            JEntity entityAttr = AttrHelper.GetClassAttribute <JEntity>(type);
                            if (entityAttr != null)
                            {
                                if (setting.AllowMultiValues)
                                {
                                    IList list   = (IList)setting.Value;
                                    IList values = (IList)Activator.CreateInstance(typeof(List <>).MakeGenericType(type));
                                    foreach (var pkOnlyObject in list)
                                    {
                                        string str        = JsonSerializeHelper.SaveToString(pkOnlyObject);
                                        object realObject = JsonSerializeHelper.LoadFromString(str, type);
                                        values.Add(realObject);
                                    }
                                    setting.Value = values;
                                }
                                else
                                {
                                    setting.Value = JsonSerializeHelper.LoadFromString(JsonSerializeHelper.SaveToString(setting.Value), type);
                                }
                            }
                            else
                            {
                                TypeConverter converter = TypeDescriptor.GetConverter(type);
                                if (converter != null)//for system types (Font, etc.)
                                {
                                    setting.Value = converter.ConvertFromString((string)setting.Value);
                                }
                            }
                        }
                    }
                }
            }

            CreateProperties();
        }
Beispiel #12
0
        public void ComplateSettingsRelations()
        {
            foreach (var s in settings.Values)
            {
                if (s.Value != null)
                {
                    Type type = s.ValueType;
                    if (type != null)
                    {
                        JEntity entityAttr = AttrHelper.GetClassAttribute <JEntity>(type);
                        if (entityAttr != null)//todo list
                        {
                            PropertyInfo pkProp = AttrHelper.GetProperty <JPrimaryKey>(type);
                            if (pkProp == null)
                            {
                                throw new Exception("Primary key not found in referenced entity");
                            }

                            if (s.AllowMultiValues)
                            {
                                IList list   = (IList)s.Value;
                                IList values = (IList)Activator.CreateInstance(typeof(List <>).MakeGenericType(type));
                                foreach (var val in list)
                                {
                                    object pvReal  = null;
                                    object pkValue = pkProp.GetValue(val);
                                    if (pkValue != null)
                                    {
                                        try
                                        {
                                            pvReal = Dm.Instance.Find(type, pkValue);
                                        }
                                        catch (Exception ex)
                                        {
                                            PropertyInfo nameProp = AttrHelper.GetProperty <JNameProperty>(type);
                                            if (nameProp != null)
                                            {
                                                nameProp.SetValue(val, Resources.Dm_ErrorFinding + ex);
                                            }
                                            pvReal = val;
                                        }
                                        if (pvReal != null)
                                        {
                                        }
                                        else
                                        {
                                            //may be removed
                                            PropertyInfo nameProp = AttrHelper.GetProperty <JNameProperty>(type);
                                            if (nameProp != null)
                                            {
                                                nameProp.SetValue(val, Resources.Dm_NotFound);
                                            }
                                            pvReal = val;
                                        }
                                    }
                                    values.Add(pvReal);
                                }
                                s.Value = values;
                            }
                            else
                            {
                                object pvReal  = null;
                                object pkValue = pkProp.GetValue(s.Value);
                                if (pkValue != null)
                                {
                                    try
                                    {
                                        pvReal = Dm.Instance.Find(type, pkValue);
                                    }
                                    catch (Exception ex)
                                    {
                                        PropertyInfo nameProp = AttrHelper.GetProperty <JNameProperty>(type);
                                        if (nameProp != null)
                                        {
                                            nameProp.SetValue(s.Value, Resources.Dm_ErrorFinding + ex);
                                        }
                                    }
                                    if (pvReal != null)
                                    {
                                        s.Value = pvReal;
                                    }
                                    else
                                    {
                                        //may be removed
                                        PropertyInfo nameProp = AttrHelper.GetProperty <JNameProperty>(type);
                                        if (nameProp != null)
                                        {
                                            nameProp.SetValue(s.Value, Resources.Dm_NotFound);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }