private void ListView_FormatCell(object sender, FormatCellEventArgs e) { object item = e.Model; if (item != null) { PropertyInfo p = item.GetType().GetProperty("IsArchived"); if (p != null && p.PropertyType == typeof(bool) && (bool)(p.GetValue(item)) == true) { e.SubItem.ForeColor = Color.Gray; } else { if (e.Column.AspectName == null) { return; } Type pType = AttrHelper.GetPropertyType(SourceObjectType, e.Column.AspectName); PropertyInfo propInfo = SourceObjectType.GetProperty(e.Column.AspectName); JExpired expiredProp = AttrHelper.GetAttribute <JExpired>(propInfo); if (expiredProp != null) { object propValue = null; if (expiredProp.ExpiredProperty != null) { PropertyInfo propInfo1 = SourceObjectType.GetProperty(expiredProp.ExpiredProperty); if (propInfo1 != null) { propValue = AttrHelper.GetPropertyValue(e.Model, propInfo1); } } else { propValue = AttrHelper.GetPropertyValue(e.Model, propInfo); } if (propValue != null) { ModelHelper.ExpiredToColor(propValue.ToString(), e.SubItem.ForeColor); } } else { JDictProp dictProp = AttrHelper.GetAttribute <JDictProp>(propInfo); if (dictProp != null && (dictProp.DictPropertyStyle == DisplyPropertyStyle.ColoredTextOnly || dictProp.DictPropertyStyle == DisplyPropertyStyle.ColoredTextAndImage)) { object propValue = AttrHelper.GetPropertyValue(e.Model, propInfo); if (propValue != null) { JDictItem ditem = Dm.Instance.GetDictText(dictProp.Id, propValue.ToString()); if (ditem != null && ditem.TextColor != null && ditem.TextColor != Color.Black) { e.SubItem.ForeColor = ditem.TextColor; } } } } } } }
public static string ModelPropertyList(object obj, string lineDelimeter, string[] includes, string[] excludes) { Type t = obj.GetType(); var props = t.GetProperties(); var sb = new StringBuilder(); foreach (var p in props) { if (excludes != null && Array.Exists(excludes, element => element == p.Name)) { continue; } if (includes != null && !Array.Exists(includes, element => element == p.Name)) { continue; } JIgnore ignoreAttr = AttrHelper.GetAttribute <JIgnore>(t, p.Name); if (ignoreAttr != null) { continue; } string desc = GetPropertyJDescriptionOrName(p); object v = Dm.Instance.GetCustomPropertyValue(obj, p.Name, true, -1, -1); if (v != null) { sb.Append(desc + ": " + v); if (lineDelimeter != null) { sb.Append(lineDelimeter); } sb.AppendLine(); } } return(sb.ToString()); }
private void bag1_SetValue(object sender, PropertySpecEventArgs e) { try { SetModified(true); if (tempSourceObject != null) { if (e.Property.PropTag != null && e.Property.PropTag is IField) { (e.Property.PropTag as IField).Value = (string)e.Value; } else { string name = e.Property.PropTag as string; if (AppManager.Instance.IsCustomEditProperty(SourceObjectType, name)) { } else { object oldValue = null; oldValue = AttrHelper.GetPropertyValue(tempSourceObject, name); AttrHelper.SetPropertyValue(tempSourceObject, name, e.Value); } } } } catch (Exception ex) { Log.ShowError(ex); } }
override protected void ReloadList() { if (!(isVirtualList || NoDmMode || AttrHelper.IsAttributeDefinedForType <JEntity>(SourceObjectType, true) == false)) { this.listView.SetObjects(Dm.Instance.FindAll(SourceObjectType)); } }
private void bag1_GetValue(object sender, PropertySpecEventArgs e) { try { if (tempSourceObject != null) { if (e.Property.PropTag != null && e.Property.PropTag is IField) { e.Value = (e.Property.PropTag as IField).Value; } else { string aspectName = e.Property.PropTag as string;// Type pType = AttrHelper.GetPropertyType(SourceObjectType, aspectName); e.Value = Dm.Instance.GetCustomPropertyValue(tempSourceObject, aspectName, true, AppManager.Instance.PropertyWindowTruncatedMaxItemCount, AppManager.Instance.PropertyWindowTruncatedMaxLength); } } else { e.Value = null; } } catch (Exception ex) { Log.ShowError(ex); } }
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 void ListView_CellEditFinished(object sender, CellEditEventArgs e) { OLVColumn column = e.Column; object rowObject = e.RowObject; /* * if (AttrHelper.GetAttribute<JText>(SourceObjectType, column.AspectName) != null) * { * e.Cancel = true; * RefreshObject(rowObject); * //((ObjectListView)sender).RefreshItem(e.ListViewItem); * } */ try { Dm.Instance.SaveObject(e.RowObject); } catch (JValidationException ex) { dropLocker = true;//xak AppManager.ShowValidationErrorMessage(ex.ValidationResult, this); AttrHelper.SetPropertyValue(rowObject, column.AspectName, e.Value); RefreshObject(rowObject); e.Cancel = true; } catch (Exception ex) { Log.ShowError(ex); } }
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); }
protected void CreateMainMenuItemsForAllEntityTypes(ToolStripMenuItem parentItem) { var entityTypes = AttrHelper.GetTypesWithAttribute <JEntity>(true); foreach (var t in entityTypes) { CreateMainMenuItemForEntityType(parentItem, t); } }
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 object GetPKValue(object o) { PropertyInfo pPK = AttrHelper.GetProperty(typeof(JPrimaryKey), o.GetType()); if (pPK != null) { return(pPK.GetValue(o)); } return(null); }
public static PropertyInfo GetPK(Type t) { PropertyInfo pPK = AttrHelper.GetProperty(typeof(JPrimaryKey), t); if (pPK != null) { return(pPK); } 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 protected bool IsForeignKey(PropertyInfo p) { if (AttrHelper.GetAttribute <JManyToOne>(p) != null || AttrHelper.GetAttribute <JOneToOne>(p) != null) { return(true); } else { return(false); } }
static protected bool IsNecessary(PropertyInfo p) { if (AttrHelper.GetAttribute <JManyToOne>(p) != null || AttrHelper.GetAttribute <JOneToOne>(p) != null || AttrHelper.GetAttribute <JPrimaryKey>(p) != null) { return(true); } else { return(false); } }
static public bool IsTextColoredFieldPresent(Type t) { var pl = AttrHelper.GetPropertiesWithAttribute <JDictProp>(t); foreach (var p in pl) { JDictProp d = AttrHelper.GetAttribute <JDictProp>(p); if (d.DictPropertyStyle == DisplyPropertyStyle.ColoredTextOnly || d.DictPropertyStyle == DisplyPropertyStyle.ColoredTextAndImage) { return(true); } } return(false); }
static protected bool IsIgnore(PropertyInfo p) { if (AttrHelper.GetAttribute <JIgnore>(p) != null || AttrHelper.GetAttribute <JsonIgnoreAttribute>(p) != null || AttrHelper.GetAttribute <JManyToMany>(p) != null || AttrHelper.GetAttribute <JOneToMany>(p) != null ) { return(true); } else { return(false); } }
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); }
public IList FindBy(Type entityType, PropertyInfo p, object fieldValue) { IList list = (IList)Activator.CreateInstance(typeof(List <>).MakeGenericType(entityType)); bool isSimple = (fieldValue == null) || AttrHelper.IsSimple(fieldValue.GetType()); IList allMayBeReferenced = FindAll(entityType); foreach (object l in allMayBeReferenced) { object foreinEntityValue = p.GetValue(l); if (isSimple) { if (foreinEntityValue != null) { //pk comparation if (ModelHelper.GetPKValue(foreinEntityValue).Equals(fieldValue)) { if (list.Contains(l) == false) { list.Add(l); } } } else if (fieldValue == null) { if (list.Contains(l) == false) { list.Add(l); } } } else { //object comparation (working fine only if all records cached on init) if (foreinEntityValue == fieldValue)//also null == null { if (list.Contains(l) == false) { list.Add(l); } } } } return(list); }
static public PropertyInfo FindRefFieldInForeinEntity(Type whatFindType, Type whereFindType, Type reationTypeToFind, string nameToFind) { IEnumerable <PropertyInfo> whereEntityRefFields = AttrHelper.GetProperties(reationTypeToFind, whereFindType); PropertyInfo whereEntityRefField = null; if (nameToFind == null) { //nameToFind Can be null, then it is assumed that the foreinEntityRefField is the only one and matches the type (previously: by name with pk) foreach (var p in whereEntityRefFields) { Type refType = (typeof(JManyToOne).Equals(reationTypeToFind) || typeof(JOneToOne).Equals(reationTypeToFind)) ? p.PropertyType : AttrHelper.GetGenericListArgType(p.PropertyType); if (refType.Equals(whatFindType)) { if (whereEntityRefField != null) { throw new Exception("Found more than one field referenced to this entity type"); } whereEntityRefField = p; } } } else { //nameToFind Set explicitly, which is relevant if the table has several fields of the same type that refer to the same table foreach (var p in whereEntityRefFields) { Type refType = (typeof(JManyToOne).Equals(reationTypeToFind) || typeof(JOneToOne).Equals(reationTypeToFind)) ? p.PropertyType : AttrHelper.GetGenericListArgType(p.PropertyType); if (refType.Equals(whatFindType)) { if ( ((typeof(JManyToOne).Equals(reationTypeToFind) || typeof(JOneToMany).Equals(reationTypeToFind)) && (p.Name).Equals(nameToFind)) || (typeof(JManyToMany).Equals(reationTypeToFind) && (p.Name).Equals(nameToFind)) || (typeof(JOneToOne).Equals(reationTypeToFind) && (p.Name).Equals(nameToFind)) )//todo { whereEntityRefField = p; break; } } } } return(whereEntityRefField); }
/////////////////////////// /// /// <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); } } }
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); }
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)); } }
static protected bool IsComplex(PropertyInfo p) { //date if (p.PropertyType == typeof(DateTime) || p.PropertyType == typeof(DateTimeOffset)) { return(false); } //simple if (AttrHelper.IsSimple(p.PropertyType)) { return(false); } if (IsForeignKey(p)) { return(false); //for any case } //else return(true); }
public bool SaveChanges() { JValidationResult result = Dm.Instance.ValidateObject(tempSourceObject); if (!result.isError) { Dm.CopyObjectProperties(tempSourceObject, sourceObject, CopyRestrictLevel.AllPropertiesNewLists); if (AttrHelper.IsAttributeDefinedForType <JEntity>(SourceObjectType, true)) { Dm.Instance.SaveObject(sourceObject); if (ViewMode == ViewMode.New && CloningObject != null) { MethodInfo cloneDependenciesMethod = SourceObjectType.GetMethod("CloneDependencies"); if (cloneDependenciesMethod != null) { DialogResult res = MessageBox.Show("Clone dependencies?", FrwConstants.WARNING, MessageBoxButtons.YesNoCancel, MessageBoxIcon.Warning); if (res == DialogResult.Yes) { object cdResult = cloneDependenciesMethod.Invoke(null, new object[] { CloningObject, sourceObject });//Pass null as the first argument (no need for an instance) } } } } SetModified(false); ChildObjectUpdateEventArgs ev = new ChildObjectUpdateEventArgs(); ev.UpdatedObject = sourceObject; OnPropertyObjectUpdate(ev); if (ViewMode == ViewMode.New) { ViewMode = ViewMode.Edit; } return(true); } else { AppManager.ShowValidationErrorMessage(result); return(false); } }
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); } } } }
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); }