Exemplo n.º 1
0
        protected IDataObject ToDataObject(DataRow dataRow, DataObject objectDefinition)
        {
            IDataObject dataObject = null;

            if (dataRow != null)
            {
                try
                {
                    dataObject = new GenericDataObject()
                    {
                        ObjectType = objectDefinition.objectName
                    };
                }
                catch (Exception ex)
                {
                    _logger.Error(string.Format("Error instantiating data object: {0}", ex));
                    throw ex;
                }

                if (dataObject != null && objectDefinition.dataProperties != null)
                {
                    foreach (DataProperty objectProperty in objectDefinition.dataProperties)
                    {
                        try
                        {
                            if (objectProperty.columnName != null)
                            {
                                if (dataRow.Table.Columns.Contains(objectProperty.columnName))
                                {
                                    object value = dataRow[objectProperty.columnName];

                                    if (value.GetType() == typeof(System.DBNull))
                                    {
                                        value = null;
                                    }

                                    dataObject.SetPropertyValue(objectProperty.propertyName, value);
                                }
                                else
                                {
                                    _logger.Warn(String.Format("Value for column [{0}] not found in data row of table [{1}]",
                                                               objectProperty.columnName, objectDefinition.tableName));
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            _logger.Error(string.Format("Error getting data row value: {0}", ex));
                            throw ex;
                        }
                    }

                    // evaluate has-content expression
                    if (_settings["HasContentExpression"] != null)
                    {
                        string expression = _settings["HasContentExpression"].ToString();

                        foreach (DataProperty prop in objectDefinition.dataProperties)
                        {
                            if (expression.Contains(prop.propertyName))
                            {
                                object value = dataObject.GetPropertyValue(prop.propertyName);

                                if (value != null)
                                {
                                    string valueStr = value.ToString();

                                    if (prop.dataType == DataType.Char || prop.dataType == DataType.String)
                                    {
                                        valueStr = "\"" + valueStr + "\"";
                                    }

                                    expression = expression.Replace(prop.propertyName, valueStr);
                                }
                            }
                        }

                        if ((bool)Utility.Evaluate(expression))
                        {
                            ((GenericDataObject)dataObject).HasContent = true;
                        }
                    }
                }
            }
            else
            {
                dataObject = new GenericDataObject()
                {
                    ObjectType = objectDefinition.objectName
                };

                foreach (DataProperty objectProperty in objectDefinition.dataProperties)
                {
                    dataObject.SetPropertyValue(objectProperty.propertyName, null);
                }
            }

            return(dataObject);
        }
Exemplo n.º 2
0
        public static IDataObject ToDataObject(DataObject objectType, DataRow row)
        {
            GenericDataObject dataObject = null;

            if (row != null)
            {
                try
                {
                    dataObject = new GenericDataObject()
                    {
                        ObjectType = objectType.objectName
                    };
                }
                catch (Exception ex)
                {
                    _logger.Error(string.Format("Error instantiating data object: {0}", ex));
                    throw ex;
                }

                if (dataObject != null && objectType.dataProperties != null)
                {
                    foreach (DataProperty objectProperty in objectType.dataProperties)
                    {
                        try
                        {
                            if (objectProperty.propertyName != null)
                            {
                                if (row.Table.Columns.Contains(objectProperty.propertyName))
                                {
                                    object value = row[objectProperty.propertyName];

                                    if (value.GetType() == typeof(System.DBNull))
                                    {
                                        value = null;
                                    }

                                    if (objectProperty.dataType.ToString().ToUpper() == "DECIMAL" ||
                                        objectProperty.dataType.ToString().ToUpper() == "DOUBLE")
                                    {
                                        dataObject.SetPropertyNumericValue(objectProperty.propertyName, value);
                                    }
                                    else
                                    {
                                        dataObject.SetPropertyValue(objectProperty.propertyName, value);
                                    }
                                }
                                else
                                {
                                    _logger.Warn(String.Format("Value for column [{0}] not found in data row of table [{1}]",
                                                               objectProperty.columnName, objectType.tableName));
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            _logger.Error(string.Format("Error getting data row value: {0}", ex));
                            throw ex;
                        }
                    }
                }

                //Extension Properties____Starts
                if (dataObject != null && objectType.extensionProperties != null)
                {
                    foreach (ExtensionProperty extentionProperty in objectType.extensionProperties)
                    {
                        try
                        {
                            if (extentionProperty.propertyName != null)
                            {
                                if (row.Table.Columns.Contains(extentionProperty.propertyName))
                                {
                                    object value = row[extentionProperty.propertyName];

                                    if (value.GetType() == typeof(System.DBNull))
                                    {
                                        value = null;
                                    }

                                    dataObject.SetPropertyValue(extentionProperty.propertyName, value);
                                }
                                else
                                {
                                    _logger.Warn(String.Format("Value for column [{0}] not found in data row of table [{1}]",
                                                               extentionProperty.columnName, objectType.tableName));
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            _logger.Error(string.Format("Error getting data row value: {0}", ex));
                            throw ex;
                        }
                    }
                }
                //Extension Properties____Ends
            }
            else
            {
                dataObject = new GenericDataObject()
                {
                    ObjectType = objectType.objectName
                };

                foreach (DataProperty objectProperty in objectType.dataProperties)
                {
                    dataObject.SetPropertyValue(objectProperty.propertyName, null);
                }
            }

            if (row[HAS_CONTENT] != DBNull.Value && Convert.ToBoolean(row[HAS_CONTENT]))
            {
                dataObject.HasContent = true;
            }

            return(dataObject);
        }