Shedules fix

This commit is contained in:
vfedosevich 2013-06-04 17:53:11 +03:00
parent 645229bef1
commit 476f7a2b19
7 changed files with 461 additions and 542 deletions

View file

@ -36,11 +36,11 @@ using WebsitePanel.Providers;
namespace WebsitePanel.EnterpriseServer
{
/// <summary>
/// Summary description for ObjectUtils.
/// </summary>
public class ObjectUtils
{
/// <summary>
/// Summary description for ObjectUtils.
/// </summary>
public class ObjectUtils
{
public static DT ConvertObject<ST, DT>(ST so)
{
Dictionary<string, PropertyInfo> sProps = GetTypePropertiesHash(typeof(ST));
@ -53,23 +53,23 @@ namespace WebsitePanel.EnterpriseServer
{
if (dProps.ContainsKey(propName) && sProps[propName].Name != "Item")
{
if (sProps[propName].CanRead)
{
object val = sProps[propName].GetValue(so, null);
if (dProps[propName] != null)
{
if (val != null && dProps[propName].CanWrite)
{
dProps[propName].SetValue(dobj, val, null);
}
}
}
if (sProps[propName].CanRead)
{
object val = sProps[propName].GetValue(so, null);
if (dProps[propName] != null)
{
if (val != null && dProps[propName].CanWrite)
{
dProps[propName].SetValue(dobj, val, null);
}
}
}
}
}
return dobj;
}
private static Hashtable typeProperties = new Hashtable();
private static Hashtable typeProperties = new Hashtable();
public static Hashtable GetObjectProperties(object obj, bool persistentOnly)
{
@ -82,7 +82,7 @@ namespace WebsitePanel.EnterpriseServer
{
// check for persistent attribute
object[] attrs = prop.GetCustomAttributes(typeof(PersistentAttribute), false);
if (!persistentOnly || (persistentOnly && attrs.Length > 0))
if (!persistentOnly || (persistentOnly && attrs.Length > 0) && !hash.ContainsKey(prop.Name))
{
object val = prop.GetValue(obj, null);
string s = "";
@ -110,60 +110,60 @@ namespace WebsitePanel.EnterpriseServer
return hash;
}
public static void FillCollectionFromDataSet<T>(List<T> list, DataSet ds)
{
if(ds.Tables.Count == 0)
return;
public static void FillCollectionFromDataSet<T>(List<T> list, DataSet ds)
{
if (ds.Tables.Count == 0)
return;
FillCollectionFromDataView<T>(list, ds.Tables[0].DefaultView);
}
}
public static void FillCollectionFromDataView<T>(List<T> list, DataView dv)
{
{
Type type = typeof(T);
PropertyInfo[] props = GetTypeProperties(type);
PropertyInfo[] props = GetTypeProperties(type);
foreach(DataRowView dr in dv)
{
// create an instance
T obj = (T)Activator.CreateInstance(type);
list.Add(obj);
foreach (DataRowView dr in dv)
{
// create an instance
T obj = (T)Activator.CreateInstance(type);
list.Add(obj);
// fill properties
for(int i = 0; i < props.Length; i++)
{
string propName = props[i].Name;
if(dv.Table.Columns[propName] == null)
continue;
// fill properties
for (int i = 0; i < props.Length; i++)
{
string propName = props[i].Name;
if (dv.Table.Columns[propName] == null)
continue;
object propVal = dr[propName];
if(propVal == DBNull.Value)
props[i].SetValue(obj, GetNull(props[i].PropertyType), null);
else
{
try
{
// try implicit type conversion
props[i].SetValue(obj, propVal, null);
}
catch
{
// convert to string and then set property value
try
{
string strVal = propVal.ToString();
props[i].SetValue(obj, Cast(strVal, props[i].PropertyType), null);
}
catch
{
// skip property init
}
}
}
} // for properties
} // for rows
}
object propVal = dr[propName];
if (propVal == DBNull.Value)
props[i].SetValue(obj, GetNull(props[i].PropertyType), null);
else
{
try
{
// try implicit type conversion
props[i].SetValue(obj, propVal, null);
}
catch
{
// convert to string and then set property value
try
{
string strVal = propVal.ToString();
props[i].SetValue(obj, Cast(strVal, props[i].PropertyType), null);
}
catch
{
// skip property init
}
}
}
} // for properties
} // for rows
}
public static List<T> CreateListFromDataReader<T>(IDataReader reader)
{
@ -179,201 +179,201 @@ namespace WebsitePanel.EnterpriseServer
return list;
}
public static void FillCollectionFromDataReader<T>(List<T> list, IDataReader reader)
{
public static void FillCollectionFromDataReader<T>(List<T> list, IDataReader reader)
{
Type type = typeof(T);
try
{
// get type properties
PropertyInfo[] props = GetTypeProperties(type);
try
{
// get type properties
PropertyInfo[] props = GetTypeProperties(type);
// iterate through reader
while(reader.Read())
{
T obj = (T)Activator.CreateInstance(type);
list.Add(obj);
// iterate through reader
while (reader.Read())
{
T obj = (T)Activator.CreateInstance(type);
list.Add(obj);
// set properties
for(int i = 0; i < props.Length; i++)
{
string propName = props[i].Name;
try
{
// set properties
for (int i = 0; i < props.Length; i++)
{
string propName = props[i].Name;
object propVal = reader[propName];
if(propVal == DBNull.Value)
props[i].SetValue(obj, GetNull(props[i].PropertyType), null);
else
{
try
{
// try implicit type conversion
props[i].SetValue(obj, propVal, null);
}
try
{
object propVal = reader[propName];
if (propVal == DBNull.Value)
props[i].SetValue(obj, GetNull(props[i].PropertyType), null);
else
{
try
{
// try implicit type conversion
props[i].SetValue(obj, propVal, null);
}
catch (Exception ex)
{
// convert to string and then set property value
try
{
string strVal = propVal.ToString();
props[i].SetValue(obj, Cast(strVal, props[i].PropertyType), null);
}
catch(Exception e)
{
// skip property init
}
}
}
}
{
// convert to string and then set property value
try
{
string strVal = propVal.ToString();
props[i].SetValue(obj, Cast(strVal, props[i].PropertyType), null);
}
catch (Exception e)
{
// skip property init
}
}
}
}
catch (Exception e) { } // just skip
} // for properties
}
}
finally
{
reader.Close();
}
}
} // for properties
}
}
finally
{
reader.Close();
}
}
public static T FillObjectFromDataView<T>(DataView dv)
{
public static T FillObjectFromDataView<T>(DataView dv)
{
Type type = typeof(T);
T obj = default(T);
T obj = default(T);
// get type properties
PropertyInfo[] props = GetTypeProperties(type);
// get type properties
PropertyInfo[] props = GetTypeProperties(type);
// iterate through reader
foreach(DataRowView dr in dv)
{
obj = (T)Activator.CreateInstance(type);
// iterate through reader
foreach (DataRowView dr in dv)
{
obj = (T)Activator.CreateInstance(type);
// set properties
for(int i = 0; i < props.Length; i++)
{
string propName = props[i].Name;
try
{
// verify if there is such a column
if (!dr.Row.Table.Columns.Contains(propName.ToLower()))
{
// if not, we move to another property
// because this one we cannot set
continue;
}
// set properties
for (int i = 0; i < props.Length; i++)
{
string propName = props[i].Name;
object propVal = dr[propName];
if(propVal == DBNull.Value)
props[i].SetValue(obj, GetNull(props[i].PropertyType), null);
else
{
try
{
string strVal = propVal.ToString();
try
{
// verify if there is such a column
if (!dr.Row.Table.Columns.Contains(propName.ToLower()))
{
// if not, we move to another property
// because this one we cannot set
continue;
}
//convert to DateTime
if (props[i].PropertyType.UnderlyingSystemType.FullName == typeof(DateTime).FullName)
{
DateTime date = DateTime.MinValue;
if (DateTime.TryParse(strVal, out date))
{
props[i].SetValue(obj, date, null);
}
}
else
{
//Convert generic
props[i].SetValue(obj, Cast(strVal, props[i].PropertyType), null);
}
}
catch
{
// skip property init
}
}
}
catch{} // just skip
} // for properties
}
object propVal = dr[propName];
if (propVal == DBNull.Value)
props[i].SetValue(obj, GetNull(props[i].PropertyType), null);
else
{
try
{
string strVal = propVal.ToString();
return obj;
}
//convert to DateTime
if (props[i].PropertyType.UnderlyingSystemType.FullName == typeof(DateTime).FullName)
{
DateTime date = DateTime.MinValue;
if (DateTime.TryParse(strVal, out date))
{
props[i].SetValue(obj, date, null);
}
}
else
{
//Convert generic
props[i].SetValue(obj, Cast(strVal, props[i].PropertyType), null);
}
}
catch
{
// skip property init
}
}
}
catch { } // just skip
} // for properties
}
public static T FillObjectFromDataReader<T>(IDataReader reader)
return obj;
}
public static T FillObjectFromDataReader<T>(IDataReader reader)
{
Type type = typeof(T);
T obj = default(T);
try
{
// get type properties
PropertyInfo[] props = GetTypeProperties(type);
try
{
// get type properties
PropertyInfo[] props = GetTypeProperties(type);
// iterate through reader
while(reader.Read())
{
obj = (T)Activator.CreateInstance(type);
// iterate through reader
while (reader.Read())
{
obj = (T)Activator.CreateInstance(type);
// set properties
for(int i = 0; i < props.Length; i++)
{
string propName = props[i].Name;
// set properties
for (int i = 0; i < props.Length; i++)
{
string propName = props[i].Name;
try
{
if (!IsColumnExists(propName, reader.GetSchemaTable()))
{
continue;
}
object propVal = reader[propName];
if(propVal == DBNull.Value)
props[i].SetValue(obj, GetNull(props[i].PropertyType), null);
else
{
try
{
//try string first
if (props[i].PropertyType.UnderlyingSystemType.FullName == typeof(String).FullName)
{
props[i].SetValue(obj, propVal.ToString(), null);
}
else
{
// then, try implicit type conversion
props[i].SetValue(obj, propVal, null);
}
}
catch
{
// convert to string and then set property value
try
{
string strVal = propVal.ToString();
props[i].SetValue(obj, Cast(strVal, props[i].PropertyType), null);
}
catch
{
// skip property init
}
}
}
}
catch{} // just skip
} // for properties
}
}
finally
{
reader.Close();
}
try
{
if (!IsColumnExists(propName, reader.GetSchemaTable()))
{
continue;
}
object propVal = reader[propName];
return obj;
}
if (propVal == DBNull.Value)
props[i].SetValue(obj, GetNull(props[i].PropertyType), null);
else
{
try
{
//try string first
if (props[i].PropertyType.UnderlyingSystemType.FullName == typeof(String).FullName)
{
props[i].SetValue(obj, propVal.ToString(), null);
}
else
{
// then, try implicit type conversion
props[i].SetValue(obj, propVal, null);
}
}
catch
{
// convert to string and then set property value
try
{
string strVal = propVal.ToString();
props[i].SetValue(obj, Cast(strVal, props[i].PropertyType), null);
}
catch
{
// skip property init
}
}
}
}
catch { } // just skip
} // for properties
}
}
finally
{
reader.Close();
}
return obj;
}
private static Hashtable propertiesCache = new Hashtable();
@ -384,7 +384,7 @@ namespace WebsitePanel.EnterpriseServer
Hashtable propValues = new Hashtable();
foreach (DataRowView dr in dv)
{
if (propValues[dr[nameColumn]] == null)
if (propValues[dr[nameColumn]] == null && !propValues.ContainsKey(dr[nameColumn]))
propValues.Add(dr[nameColumn], dr[valueColumn]);
}
@ -398,7 +398,7 @@ namespace WebsitePanel.EnterpriseServer
Hashtable propValues = new Hashtable();
while (reader.Read())
{
if (propValues[reader[nameColumn]] == null)
if (propValues[reader[nameColumn]] == null && !propValues.ContainsKey(reader[nameColumn]))
propValues.Add(reader[nameColumn], reader[valueColumn]);
}
reader.Close();
@ -416,50 +416,54 @@ namespace WebsitePanel.EnterpriseServer
return obj;
}
public static void CopyPersistentPropertiesFromSource<T>(T source, T target)
where T : ServiceProviderItem
{
//
var typeSource = source.GetType();
var typeTarget = target.GetType();
// get all property infos
Hashtable props = null;
if (propertiesCache[typeSource.Name] != null)
{
// load properties from cache
props = (Hashtable)propertiesCache[typeSource.Name];
}
else
{
// create properties cache
props = new Hashtable();
//
PropertyInfo[] objProps = typeSource.GetProperties(BindingFlags.Instance
//| BindingFlags.DeclaredOnly
| BindingFlags.Public);
foreach (PropertyInfo prop in objProps)
{
// check for persistent attribute
object[] attrs = prop.GetCustomAttributes(typeof(PersistentAttribute), false);
// Persistent only
if (attrs.Length > 0)
{
// add property to hash
props.Add(prop.Name, prop);
}
}
// add to cache
propertiesCache.Add(typeSource.Name, props);
}
public static void CopyPersistentPropertiesFromSource<T>(T source, T target)
where T : ServiceProviderItem
{
//
var typeSource = source.GetType();
var typeTarget = target.GetType();
// get all property infos
Hashtable props = null;
if (propertiesCache[typeSource.Name] != null)
{
// load properties from cache
props = (Hashtable)propertiesCache[typeSource.Name];
}
else
{
// create properties cache
props = new Hashtable();
//
PropertyInfo[] objProps = typeSource.GetProperties(BindingFlags.Instance
//| BindingFlags.DeclaredOnly
| BindingFlags.Public);
foreach (PropertyInfo prop in objProps)
{
// check for persistent attribute
object[] attrs = prop.GetCustomAttributes(typeof(PersistentAttribute), false);
// Persistent only
if (attrs.Length > 0 && !props.ContainsKey(prop.Name))
{
// add property to hash
props.Add(prop.Name, prop);
}
}
// Copy the data
foreach (PropertyInfo propertyInfo in props.Values)
{
propertyInfo.SetValue(target, propertyInfo.GetValue(source, null), null);
}
}
if (!propertiesCache.ContainsKey(typeSource.Name))
{
// add to cache
propertiesCache.Add(typeSource.Name, props);
}
}
public static void CreateObjectFromHash(object obj, Hashtable propValues, bool persistentOnly)
// Copy the data
foreach (PropertyInfo propertyInfo in props.Values)
{
propertyInfo.SetValue(target, propertyInfo.GetValue(source, null), null);
}
}
public static void CreateObjectFromHash(object obj, Hashtable propValues, bool persistentOnly)
{
Type type = obj.GetType();
@ -481,15 +485,18 @@ namespace WebsitePanel.EnterpriseServer
{
// check for persistent attribute
object[] attrs = prop.GetCustomAttributes(typeof(PersistentAttribute), false);
if (!persistentOnly || (persistentOnly && attrs.Length > 0))
if (!persistentOnly || (persistentOnly && attrs.Length > 0) && !props.ContainsKey(prop.Name))
{
// add property to hash
props.Add(prop.Name, prop);
}
}
// add to cache
propertiesCache.Add(type.Name, props);
if (!propertiesCache.ContainsKey(type.Name))
{
// add to cache
propertiesCache.Add(type.Name, props);
}
}
// fill properties
@ -513,37 +520,37 @@ namespace WebsitePanel.EnterpriseServer
else
if (prop.PropertyType == typeof(long))
prop.SetValue(obj, long.Parse(val), null);
else
if (prop.PropertyType == typeof(Boolean))
prop.SetValue(obj, Boolean.Parse(val), null);
else if (prop.PropertyType == typeof(Single))
prop.SetValue(obj, Single.Parse(val), null);
else if (prop.PropertyType.IsEnum)
prop.SetValue(obj, Enum.Parse(prop.PropertyType, val, true), null);
else
if (prop.PropertyType == typeof(Guid))
prop.SetValue(obj, new Guid(val), null);
else
if (prop.PropertyType == typeof(string[]))
{
if (val == "")
prop.SetValue(obj, new string[0], null);
else
prop.SetValue(obj, val.Split(';'), null);
}
else if (prop.PropertyType == typeof(int[]))
{
string[] svals = val.Split(';');
int[] ivals = new int[svals.Length];
if (prop.PropertyType == typeof(Boolean))
prop.SetValue(obj, Boolean.Parse(val), null);
else if (prop.PropertyType == typeof(Single))
prop.SetValue(obj, Single.Parse(val), null);
else if (prop.PropertyType.IsEnum)
prop.SetValue(obj, Enum.Parse(prop.PropertyType, val, true), null);
else
if (prop.PropertyType == typeof(Guid))
prop.SetValue(obj, new Guid(val), null);
else
if (prop.PropertyType == typeof(string[]))
{
if (val == "")
prop.SetValue(obj, new string[0], null);
else
prop.SetValue(obj, val.Split(';'), null);
}
else if (prop.PropertyType == typeof(int[]))
{
string[] svals = val.Split(';');
int[] ivals = new int[svals.Length];
for (int i = 0; i < svals.Length; i++)
ivals[i] = Int32.Parse(svals[i]);
for (int i = 0; i < svals.Length; i++)
ivals[i] = Int32.Parse(svals[i]);
if (val == "")
ivals = new int[0];
if (val == "")
ivals = new int[0];
prop.SetValue(obj, ivals, null);
}
prop.SetValue(obj, ivals, null);
}
}
}
}
@ -553,50 +560,55 @@ namespace WebsitePanel.EnterpriseServer
Dictionary<string, PropertyInfo> hash = new Dictionary<string, PropertyInfo>();
PropertyInfo[] props = GetTypeProperties(type);
foreach (PropertyInfo prop in props)
hash.Add(prop.Name, prop);
{
if (!hash.ContainsKey(prop.Name))
{
hash.Add(prop.Name, prop);
}
}
return hash;
}
private static PropertyInfo[] GetTypeProperties(Type type)
{
string typeName = type.AssemblyQualifiedName;
if(typeProperties[typeName] != null)
return (PropertyInfo[])typeProperties[typeName];
private static PropertyInfo[] GetTypeProperties(Type type)
{
string typeName = type.AssemblyQualifiedName;
if (typeProperties[typeName] != null)
return (PropertyInfo[])typeProperties[typeName];
PropertyInfo[] props = type.GetProperties(BindingFlags.Instance | BindingFlags.Public);
typeProperties[typeName] = props;
return props;
}
PropertyInfo[] props = type.GetProperties(BindingFlags.Instance | BindingFlags.Public);
typeProperties[typeName] = props;
return props;
}
public static object GetNull(Type type)
{
if(type == typeof(string))
return null;
if(type == typeof(Int32))
return 0;
if(type == typeof(Int64))
return 0;
if(type == typeof(Boolean))
return false;
if(type == typeof(Decimal))
return 0M;
else
return null;
}
public static object GetNull(Type type)
{
if (type == typeof(string))
return null;
if (type == typeof(Int32))
return 0;
if (type == typeof(Int64))
return 0;
if (type == typeof(Boolean))
return false;
if (type == typeof(Decimal))
return 0M;
else
return null;
}
public static object Cast(string val, Type type)
{
if(type == typeof(string))
return val;
if(type == typeof(Int32))
return Int32.Parse(val);
if(type == typeof(Int64))
return Int64.Parse(val);
if(type == typeof(Boolean))
return Boolean.Parse(val);
if(type == typeof(Decimal))
return Decimal.Parse(val);
if(type == typeof(string[]) && val != null)
public static object Cast(string val, Type type)
{
if (type == typeof(string))
return val;
if (type == typeof(Int32))
return Int32.Parse(val);
if (type == typeof(Int64))
return Int64.Parse(val);
if (type == typeof(Boolean))
return Boolean.Parse(val);
if (type == typeof(Decimal))
return Decimal.Parse(val);
if (type == typeof(string[]) && val != null)
{
return val.Split(';');
}
@ -611,49 +623,49 @@ namespace WebsitePanel.EnterpriseServer
iarr[i] = Int32.Parse(sarr[i]);
return iarr;
}
else
return val;
}
else
return val;
}
public static string GetTypeFullName(Type type)
{
return type.FullName + ", " + type.Assembly.GetName().Name;
}
public static string GetTypeFullName(Type type)
{
return type.FullName + ", " + type.Assembly.GetName().Name;
}
#region Helper Functions
#region Helper Functions
/// <summary>
/// This function is used to determine whether IDataReader contains a Column.
/// </summary>
/// <param name="columnName">Name of the column.</param>
/// <param name="schemaTable">The schema <see cref="DataTable"/> that decribes result-set <see cref="IDataReader"/> contains.</param>
/// <returns>True, when required column exists in the <paramref name="schemaTable"/>. Otherwise, false.</returns>
/// <remark>
/// The followin example shows how to look for the "Role" column in the <see cref="IDataReader"/>.
/// <example>
/// IDataReader reader = ....
/// if (!IsColumnExists("Role", reader.GetSchemaTable())
/// {
/// continue;
/// }
///
/// object roleValue = reader["Role"];
/// </example>
/// </remark>
static bool IsColumnExists(string columnName, DataTable schemaTable)
{
foreach (DataRow row in schemaTable.Rows)
{
if (String.Compare(row[0].ToString(), columnName, StringComparison.OrdinalIgnoreCase) == 0)
{
return true;
}
}
/// <summary>
/// This function is used to determine whether IDataReader contains a Column.
/// </summary>
/// <param name="columnName">Name of the column.</param>
/// <param name="schemaTable">The schema <see cref="DataTable"/> that decribes result-set <see cref="IDataReader"/> contains.</param>
/// <returns>True, when required column exists in the <paramref name="schemaTable"/>. Otherwise, false.</returns>
/// <remark>
/// The followin example shows how to look for the "Role" column in the <see cref="IDataReader"/>.
/// <example>
/// IDataReader reader = ....
/// if (!IsColumnExists("Role", reader.GetSchemaTable())
/// {
/// continue;
/// }
///
/// object roleValue = reader["Role"];
/// </example>
/// </remark>
static bool IsColumnExists(string columnName, DataTable schemaTable)
{
foreach (DataRow row in schemaTable.Rows)
{
if (String.Compare(row[0].ToString(), columnName, StringComparison.OrdinalIgnoreCase) == 0)
{
return true;
}
}
return false;
}
return false;
}
#endregion
}
}
#endregion
}
}