public object Find(Type entityType, object primaryKeValue) { if (primaryKeValue == null) { return(null); } if (AttrHelper.IsSimple(primaryKeValue.GetType()) == false) { if (primaryKeValue.GetType() != entityType) { throw new InvalidDataException("Find by pk whitch is not simple and not same type"); } primaryKeValue = ModelHelper.GetPKValue(primaryKeValue); if (primaryKeValue == null) { throw new InvalidDataException("Find by pk whitch is not simple and not have pk"); } } object o = null; PropertyInfo pPK = AttrHelper.GetProperty <JPrimaryKey>(entityType); SData sdata = GetSData(entityType); sdata.PkCache.TryGetValue(primaryKeValue, out o); return(o); }
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); }
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); }
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); }
public static void SetPKValue(object o, object pkValue) { PropertyInfo pPK = AttrHelper.GetProperty(typeof(JPrimaryKey), o.GetType()); if (pPK != null) { pPK.SetValue(o, pkValue); } }
public static PropertyInfo GetPK(Type t) { PropertyInfo pPK = AttrHelper.GetProperty(typeof(JPrimaryKey), t); if (pPK != null) { return(pPK); } return(null); }
public static object GetPKValue(object o) { PropertyInfo pPK = AttrHelper.GetProperty(typeof(JPrimaryKey), o.GetType()); if (pPK != null) { return(pPK.GetValue(o)); } return(null); }
private void AddObjectToPKCache(object o) { Type t = o.GetType(); PropertyInfo pkProp = AttrHelper.GetProperty <JPrimaryKey>(t); if (pkProp != null) { object pk = pkProp.GetValue(o); GetSData(t).PkCache.Add(pk, o); } }
private void RemoveObjectFromPKCache(object o) { Type t = o.GetType(); PropertyInfo pkProp = AttrHelper.GetProperty <JPrimaryKey>(t); if (pkProp != null) { object pk = pkProp.GetValue(o); GetSData(t).PkCache.Remove(pk); } }
static public string GetShortNameForObject(object o) { if (o == null) { return(null); } Type t = o.GetType(); PropertyInfo pName = AttrHelper.GetProperty <JShortNameProperty>(t); if (pName != null) { object on = pName.GetValue(o); return(on != null ? on.ToString() : null); } else { return(GetNameForObject(o)); } }
public void GenNextPkValue(object o) { Type t = o.GetType(); PropertyInfo pPK = AttrHelper.GetProperty <JPrimaryKey>(t); if (pPK != null) { if (AttrHelper.IsAttributePresent <JAutoIncrement>(t, pPK.Name)) { //string maxValue = DataUtils.genKey(null); IList list = FindAll(t); SData sdata = GetSData(t); int maxValue = 0; if (sdata.MaxId == null) { foreach (var l in list) { var pkValue = pPK.GetValue(l); int curValue = int.Parse(pkValue.ToString()); if (curValue > maxValue) { maxValue = curValue; } } sdata.MaxId = maxValue; } sdata.MaxId = sdata.MaxId + 1; if (pPK.PropertyType == typeof(string)) { pPK.SetValue(o, sdata.MaxId.ToString()); } else { pPK.SetValue(o, sdata.MaxId); } } } }
public static object ReplaceObjectByPkOnlyObject(object realObject) { if (realObject != null) { Type foreinEntityType = realObject.GetType(); object blankObject = Activator.CreateInstance(foreinEntityType); PropertyInfo fePkProp = AttrHelper.GetProperty <JPrimaryKey>(foreinEntityType); if (fePkProp == null) { throw new Exception("No pk property in entity type: " + foreinEntityType.FullName); } object pkValue = fePkProp.GetValue(realObject); if (pkValue == null) { throw new Exception("Empty pk value found in entity type: " + foreinEntityType.FullName); } fePkProp.SetValue(blankObject, pkValue); return(blankObject); } else { return(null); } }
/// <summary> /// Generate a list of OLVColumns based on the attributes of the given type /// If allProperties to true, all public properties will have a matching column generated. /// If allProperties is false, only properties that have a OLVColumn attribute will have a column generated. /// </summary> /// <param name="type"></param> /// <param name="allProperties">Will columns be generated for properties that are not marked with [OLVColumn].</param> /// <returns>A collection of OLVColumns matching the attributes of Type that have OLVColumnAttributes.</returns> public override IList <OLVColumn> GenerateColumns(Type type, bool allProperties) { List <OLVColumn> columns = new List <OLVColumn>(); // Sanity if (type == null) { return(columns); } PropertyInfo[] pList = AttrHelper.GetBasePropertiesFirst(type); PropertyInfo pName = AttrHelper.GetProperty <JNameProperty>(type); if (pName != null) { pList = pList.OrderBy(x => x != pName).ToArray(); } // Iterate all public properties in the class and build columns from those that have // an OLVColumn attribute and that are not ignored. //foreach (PropertyInfo pinfo in type.GetProperties()) foreach (PropertyInfo pinfo in pList) { if (Attribute.GetCustomAttribute(pinfo, typeof(OLVIgnoreAttribute)) != null) { continue; } //added j if (Attribute.GetCustomAttribute(pinfo, typeof(JIgnore)) != null) { continue; } //if (Attribute.GetCustomAttribute(pinfo, typeof(JManyToOne)) != null) // continue; //обычно дублирована с JForeinKey поэтому оставляем одну OLVColumnAttribute attr = Attribute.GetCustomAttribute(pinfo, typeof(OLVColumnAttribute)) as OLVColumnAttribute; OLVColumn column = null; if (attr == null) { if (allProperties) { column = this.MakeColumnFromPropertyInfo(pinfo); } } else { column = this.MakeColumnFromAttribute(pinfo, attr); } if (column != null) { columns.Add(column); } } // How many columns have DisplayIndex specifically set? int countPositiveDisplayIndex = 0; foreach (OLVColumn col in columns) { if (col.DisplayIndex >= 0) { countPositiveDisplayIndex += 1; } } // Give columns that don't have a DisplayIndex an incremental index int columnIndex = countPositiveDisplayIndex; foreach (OLVColumn col in columns) { if (col.DisplayIndex < 0) { col.DisplayIndex = (columnIndex++); } } columns.Sort(delegate(OLVColumn x, OLVColumn y) { return(x.DisplayIndex.CompareTo(y.DisplayIndex)); }); return(columns); }
override protected void MakeListColumns() { JModelOVLGenerator.Instance = new JModelOVLGenerator(); JModelOVLGenerator.GenerateColumns(this.listView, SourceObjectType, true); foreach (var column in this.listView.AllColumns) { JHeaderImage headerImageAttr = AttrHelper.GetAttribute <JHeaderImage>(SourceObjectType, column.AspectName); PropertyInfo propInfo = AttrHelper.GetProperty(SourceObjectType, column.AspectName); Type pType = propInfo.PropertyType; if (pType == typeof(bool)) { column.CheckBoxes = true; column.HeaderCheckBox = true;// -not SubItemChecking, but only HeaderCheckBoxChanging } if (AttrHelper.GetAttribute <JUrl>(propInfo) != null) { column.Hyperlink = true; } column.Text = ModelHelper.GetPropertyJDescriptionOrName(propInfo); if (headerImageAttr != null && headerImageAttr.HeaderImageName != null) { this.CreateColumHeaderImage(column, headerImageAttr.HeaderImageName); column.ShowTextInHeader = false; } if (AttrHelper.GetAttribute <JReadOnly>(SourceObjectType, column.AspectName) != null) { column.IsEditable = false; } if (AppManager.Instance.IsCustomEditProperty(SourceObjectType, column.AspectName)) { column.AspectGetter = delegate(Object rowObject) { try { if (AppManager.Instance.IsOnlyImageColumnProperty(SourceObjectType, column.AspectName)) { return(null); } else { return(Dm.Instance.GetCustomPropertyValue(rowObject, column.AspectName, true, AppManager.Instance.ListCellTruncatedMaxItemCount, AppManager.Instance.ListCellTruncatedMaxLength)); } } catch (Exception ex) { Log.LogError(ex); return(ex.ToString()); } }; CreateImageGetterDelegate(column, SourceObjectType); } } if (ModelHelper.IsIsArchiveFieldPresent(SourceObjectType)) { isActiveCheckBox.Text = FrwCRUDRes.Active; isActiveCheckBox.Checked = true; isActiveCheckBox.CheckStateChanged += (s, ex) => { listView.UpdateColumnFiltering(); }; AddToolStripItem(new ToolStripControlHost(isActiveCheckBox)); listView.AdditionalFilter = new ModelFilter(delegate(object x) { return(isActiveCheckBox.Checked ? !ModelHelper.GetIsArchiveValue(x) : true); }); } }
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); }; }
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); } } } } } } } } }
public SimpleMultivalueFieldItemListDialog(Type SourceObjectType) { InitializeComponent(); this.Text = FrwCRUDRes.SimpleMultivalueFieldItemListDialog_Title; this.SourceObjectType = SourceObjectType; ((System.ComponentModel.ISupportInitialize)(listView)).BeginInit(); //ovl settings listView.EmptyListMsg = FrwCRUDRes.List_No_Records; listView.EmptyListMsgFont = new Font("Tahoma", 9);//todo; listView.FullRowSelect = true; listView.UseCompatibleStateImageBehavior = false; listView.View = System.Windows.Forms.View.Details; listView.UseFiltering = true; listView.UseFilterIndicator = true; listView.AllowColumnReorder = true; listView.TriStateCheckBoxes = false; listView.CellEditUseWholeCell = false; listView.TintSortColumn = true; listView.ShowItemToolTips = true; listView.UseHotItem = true; listView.UseHyperlinks = true; listView.ShowCommandMenuOnRightClick = true; listView.TintSortColumn = true; OLVColumn column = null; PropertyInfo pPK = AttrHelper.GetProperty <JPrimaryKey>(SourceObjectType); column = new OLVColumn(); if (pPK != null) { column.AspectName = pPK.Name; column.Text = ModelHelper.GetPropertyJDescriptionOrName(pPK); } else { column.AspectName = "Id"; column.Text = FrwCRUDRes.SimpleMultivalueFieldItemListDialog_Id; } column.Width = 50; //column.HeaderCheckBox = true;//for checkbox selection AddColumnToList(column); PropertyInfo pName = AttrHelper.GetProperty <JNameProperty>(SourceObjectType); column = new OLVColumn(); if (pName != null) { column.Text = ModelHelper.GetPropertyJDescriptionOrName(pName); column.AspectName = pName.Name; } else { column.AspectName = "Name"; column.Text = FrwCRUDRes.SimpleMultivalueFieldItemListDialog_Name; } column.Width = 350; column.FillsFreeSpace = true; AddColumnToList(column); ((System.ComponentModel.ISupportInitialize)(listView)).EndInit(); }
// create a typical handler for the image protected bool CreateImageGetterDelegate(OLVColumn column, Type sourceObjectType) { Type pType = AttrHelper.GetPropertyType(sourceObjectType, column.AspectName); // the code for the case of the dictionary is implemented JImageName imageNameAttr = AttrHelper.GetAttribute <JImageName>(SourceObjectType, column.AspectName); JImageRef imageRefAttr = AttrHelper.GetAttribute <JImageRef>(SourceObjectType, column.AspectName); JDictProp dictAttr = AttrHelper.GetAttribute <JDictProp>(SourceObjectType, column.AspectName); if (dictAttr != null && dictAttr.DictPropertyStyle != DisplyPropertyStyle.TextOnly && dictAttr.AllowMultiValues == false) //todo { column.ImageGetter = delegate(object x) { if (dictAttr.AllowMultiValues) { return(null); } object value = AttrHelper.GetPropertyValue(x, column.AspectName); if (value == null) { return(null); } JDictItem item = Dm.Instance.GetDictText(dictAttr.Id, value.ToString()); if (item == null) { return(null); } Image smallImage = AddImageToImageList(this.listView, item.Image, null); if (smallImage != null && item.Image == null) { item.Image = smallImage; } return(smallImage); }; return(true); } else if (imageNameAttr != null && imageNameAttr.DictPropertyStyle != DisplyPropertyStyle.TextOnly) { column.ImageGetter = delegate(object x) { object value = AttrHelper.GetPropertyValue(x, column.AspectName); if (value == null) { return(null); } Image smallImage = AddImageToImageList(this.listView, null, value.ToString()); //if (smallImage != null && item.Image == null) item.Image = smallImage; return(smallImage); }; return(true); } else if (imageRefAttr != null && imageRefAttr.DictPropertyStyle != DisplyPropertyStyle.TextOnly) { column.ImageGetter = delegate(object x) { object value = AttrHelper.GetPropertyValue(x, column.AspectName); if (value == null) { return(null); } PropertyInfo propInfo = x.GetType().GetProperty(column.AspectName); PropertyInfo imagePropInfo = AttrHelper.GetPropertiesWithAttribute <JImageName>(propInfo.PropertyType).FirstOrDefault <PropertyInfo>(); if (imagePropInfo == null) { return(null); } value = imagePropInfo.GetValue(value); if (value == null) { return(null); } AttrHelper.GetProperty <JImageName>(propInfo.PropertyType); Image smallImage = AddImageToImageList(this.listView, null, value.ToString()); //if (smallImage != null && item.Image == null) item.Image = smallImage; return(smallImage); }; return(true); } else if (pType == typeof(JAttachment) || AttrHelper.IsGenericListTypeOf(pType, typeof(JAttachment)) || AttrHelper.GetAttribute <JText>(sourceObjectType, column.AspectName) != null) { column.ImageGetter = delegate(object x) { object v = AttrHelper.GetPropertyValue(x, column.AspectName); if (v != null) { Image smallImage = null; if (AttrHelper.GetAttribute <JText>(sourceObjectType, column.AspectName) != null) { if (string.IsNullOrEmpty(v as string) == false) { smallImage = (Image)Properties.Resources.book_open; } } else if (pType == typeof(JAttachment) || AttrHelper.IsGenericListTypeOf(pType, typeof(JAttachment))) { smallImage = (Image)Properties.Resources.attachment; } return(smallImage); } else { return(null); } }; return(true); } return(false); }
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); }; }
/// <summary> /// Generate a list of OLVColumns based on the attributes of the given type /// If allProperties to true, all public properties will have a matching column generated. /// If allProperties is false, only properties that have a OLVColumn attribute will have a column generated. /// </summary> /// <param name="type"></param> /// <param name="allProperties">Will columns be generated for properties that are not marked with [OLVColumn].</param> /// <returns>A collection of OLVColumns matching the attributes of Type that have OLVColumnAttributes.</returns> public override IList <OLVColumn> GenerateColumns(Type type, bool allProperties) { List <OLVColumn> columns = new List <OLVColumn>(); // Sanity if (type == null) { return(columns); } PropertyInfo[] pList = AttrHelper.GetBasePropertiesFirst(type); PropertyInfo pName = AttrHelper.GetProperty <JNameProperty>(type); if (pName != null) { pList = pList.OrderBy(x => x != pName).ToArray(); } // Iterate all public properties in the class and build columns from those that have // an OLVColumn attribute and that are not ignored. //foreach (PropertyInfo pinfo in type.GetProperties()) foreach (PropertyInfo pinfo in pList) { if (Attribute.GetCustomAttribute(pinfo, typeof(OLVIgnoreAttribute)) != null) { continue; } //added j if (Attribute.GetCustomAttribute(pinfo, typeof(JIgnore)) != null) { continue; } OLVColumnAttribute attr = Attribute.GetCustomAttribute(pinfo, typeof(OLVColumnAttribute)) as OLVColumnAttribute; OLVColumn column = null; if (attr == null) { if (allProperties) { column = this.MakeColumnFromPropertyInfo(pinfo); } } else { column = this.MakeColumnFromAttribute(pinfo, attr); } if (column != null) { //reset checkboxes (setted by ConfigurePossibleBooleanColumn()) for correct working of virtual lists column.CheckBoxes = false; columns.Add(column); } } // How many columns have DisplayIndex specifically set? int countPositiveDisplayIndex = 0; foreach (OLVColumn col in columns) { if (col.DisplayIndex >= 0) { countPositiveDisplayIndex += 1; } } // Give columns that don't have a DisplayIndex an incremental index int columnIndex = countPositiveDisplayIndex; foreach (OLVColumn col in columns) { if (col.DisplayIndex < 0) { col.DisplayIndex = (columnIndex++); } } columns.Sort(delegate(OLVColumn x, OLVColumn y) { return(x.DisplayIndex.CompareTo(y.DisplayIndex)); }); return(columns); }
override public void ProcessView() { if (SourceObjectType != null) { string descr = ModelHelper.GetEntityJDescriptionOrName(SourceObjectType); SetNewCaption(descr + " " + FrwCRUDRes.SimplePropertyWindow_Props); } if (tempSourceObject != null) { if (tempSourceObject.GetType().Equals(SourceObjectType) == false) { throw new ArgumentException("SourceObject not of SourceObjectType"); } bag1 = new PropertyBag(); bag1.GetValue += new PropertySpecEventHandler(this.bag1_GetValue); bag1.SetValue += new PropertySpecEventHandler(this.bag1_SetValue); bag1.ValueModified += Bag1_ValueModified; bag1.SourceObject = tempSourceObject; bag1.SourceObjectType = SourceObjectType; PropertyInfo[] propsList = AttrHelper.GetBasePropertiesFirst(tempSourceObject.GetType()); PropertyInfo pName = AttrHelper.GetProperty <JNameProperty>(tempSourceObject.GetType()); if (pName != null) { propsList = propsList.OrderBy(x => x != pName).ToArray(); } PropertySpec props = null; foreach (PropertyInfo p in propsList) { if (AttrHelper.IsGenericListTypeOf(p.PropertyType, typeof(IField))) { IList itemDatas = (IList)p.GetValue(tempSourceObject); if (itemDatas != null) { foreach (var it in itemDatas) { IField itemdata = (IField)it; string group = null; props = new PropertySpec(ITEM_ATTRIBUTE_PREFIX + itemdata.FieldSysname, typeof(string), group, itemdata.Name); props.PropTag = itemdata; if (viewMode == ViewMode.View || viewMode == ViewMode.ViewContent) { props.Attributes = new Attribute[] { new ReadOnlyAttribute(true) }; } bag1.Properties.Add(props); } } } else { JReadOnly readOnlyAttr = AttrHelper.GetAttribute <JReadOnly>(SourceObjectType, p.Name); JIgnore ignoreAttr = AttrHelper.GetAttribute <JIgnore>(SourceObjectType, p.Name); if (ignoreAttr != null) { continue; } string desc = ModelHelper.GetPropertyJDescriptionOrName(p); bool isCustomEdit = false; if (AppManager.Instance.IsCustomEditProperty(SourceObjectType, p.Name)) { isCustomEdit = true; } Type pType = null; if (isCustomEdit) { pType = typeof(string); //disabled comboboxes for list type fields } else { pType = p.PropertyType; } props = new PropertySpec(desc, pType, null, desc); props.PropTag = p.Name; if (readOnlyAttr != null || viewMode == ViewMode.View || viewMode == ViewMode.ViewContent) { props.Attributes = new Attribute[] { new ReadOnlyAttribute(true) }; } if (isCustomEdit) { //not only for edit props.EditorTypeName = typeof(CustomPropertyEditor).ToString(); } bag1.Properties.Add(props); } this.propertyGrid1.SelectedObjects = new object[] { bag1 }; } } else { this.propertyGrid1.SelectedObjects = null; } }