Esempio n. 1
0
        /// <summary>
        /// Adds the or updates data table with objects specified. Returns number of new items added into collection
        /// </summary>
        /// <param name="input">The input.</param>
        /// <param name="policy">The policy.</param>
        /// <returns></returns>
        protected int AddOrUpdate(IEnumerable <object> input, objectTableUpdatePolicy policy = objectTableUpdatePolicy.overwrite)
        {
            if (ReadOnlyEnforce())
            {
                return(0);
            }
            int output = 0;

            lock (AddOrUpdateLock2)
            {
                foreach (object inp in input)
                {
                    if (AddOrUpdate(inp, policy))
                    {
                        output++;
                    }

                    if (IsInstanceLinkActive)
                    {
                        UpdateBase(input as IObjectTableEntry);
                    }
                }
            }

            return(output);
        }
Esempio n. 2
0
        /// <summary>
        /// Loads external <c>inputTable</c>
        /// </summary>
        /// <param name="inputTable">The input table.</param>
        /// <param name="loger">The loger.</param>
        /// <param name="policy">The policy.</param>
        /// <returns>True if any row was loaded</returns>
        public int Load(DataTable inputTable, ILogBuilder loger, objectTableUpdatePolicy policy = objectTableUpdatePolicy.overwrite)
        {
            if (Count > 0)
            {
                if (ReadOnlyEnforce())
                {
                    return(0);
                }
            }
            // inputTable = checkTableShema(inputTable);

            int i    = 0;
            int c    = 0;
            int imax = inputTable.Rows.Count / 10;

            bool headingRowFound = false;

            foreach (DataRow dr in inputTable.Rows)
            {
                bool skipThisRow = false;
                if (!headingRowFound)
                {
                    foreach (DataColumn dc in inputTable.Columns)
                    {
                        if (dr[dc.ColumnName].toStringSafe() == dc.ColumnName)
                        {
                            headingRowFound = true;
                            skipThisRow     = true;
                            break;
                        }
                    }
                }

                if (dr[primaryKeyName].toStringSafe("") == "")
                {
                    skipThisRow = true;
                }
                else
                {
                }

                if (!skipThisRow)
                {
                    string keyValue = dr[primaryKeyName].toStringSafe("");

                    if (!keyCache.Contains(keyValue))
                    {
                        keyCache.Add(keyValue);                               //.toStringSafe("");
                    }
                    if (!keyValue.isNullOrEmpty())
                    {
                        object item = GetOrCreate(keyValue);

                        SetObjectByCustomRow(dr, item);

                        AddOrUpdate(item, policy);
                    }

                    c++;
                }

                if (i > imax)
                {
                    if (loger != null)
                    {
                        double ratio = (double)c / (double)inputTable.Rows.Count;
                        loger.log("External table loaded: " + ratio.ToString("P2"));
                        i = 0;
                    }
                }

                i++;
            }

            return(c);
        }
Esempio n. 3
0
        /// <summary>
        /// Sets the row with object. Specify row as <c>null</c> to create new row
        /// </summary>
        /// <param name="row">The row.</param>
        /// <param name="input">The input.</param>
        /// <param name="policy">The policy.</param>
        /// <returns></returns>
        protected DataRow SetRowWithObject(DataRow row, object input, objectTableUpdatePolicy policy)
        {
            bool addTheRow = false;

            if (row == null)
            {
                if (ReadOnlyEnforce())
                {
                    return(null);
                }
            }

            if (!ReadOnlyMode)
            {
                Monitor.Enter(SaveLinkedLock);
            }
            try
            {
                if (!ReadOnlyMode)
                {
                    Monitor.Enter(SelectTableLock);
                }
                try
                {
                    if (row == null)
                    {
                        row       = table.NewRow();
                        addTheRow = true;
                    }

                    foreach (settingsPropertyEntry spe in settings.spes.Values)
                    {
                        if (!spe.IsXmlIgnore)
                        {
                            if (spe.isPrimaryKey)
                            {
                                string primKey = row[primaryKeyName].toStringSafe("");
                                if (!keyCache.Contains(primKey))
                                {
                                    keyCache.Add(primKey);
                                }
                            }

                            #region SWITCH

                            switch (policy)
                            {
                            default:
                            case objectTableUpdatePolicy.none:
                            case objectTableUpdatePolicy.overwrite:
                                row[(string)spe.pi.Name] = input.imbGetPropertySafe(spe.pi, spe.pi.PropertyType.GetDefaultValue());
                                break;

                            case objectTableUpdatePolicy.updateIfLower:
                            case objectTableUpdatePolicy.updateIfHigher:
                                bool doUpdate = false;
                                if (spe.pi.PropertyType.isNumber())
                                {
                                    if (spe.pi.PropertyType == typeof(int))
                                    {
                                        int vlInt32 = (int)row[(string)spe.pi.Name].imbConvertValueSafeTyped <int>();
                                        int obInt32 = input.imbGetPropertySafe <int>(spe.pi);

                                        if (policy == objectTableUpdatePolicy.updateIfHigher)
                                        {
                                            doUpdate = (obInt32 > vlInt32);
                                        }
                                        else
                                        {
                                            doUpdate = (obInt32 < vlInt32);
                                        }
                                    }
                                    else if (spe.pi.PropertyType == typeof(double))
                                    {
                                        double vlDouble = (double)row[(string)spe.pi.Name].imbConvertValueSafeTyped <double>();
                                        double obDouble = input.imbGetPropertySafe <double>(spe.pi);
                                        if (policy == objectTableUpdatePolicy.updateIfHigher)
                                        {
                                            doUpdate = (obDouble > vlDouble);
                                        }
                                        else
                                        {
                                            doUpdate = (obDouble < vlDouble);
                                        }
                                    }
                                    else
                                    {
                                        doUpdate = true;
                                    }
                                }
                                if (doUpdate)
                                {
                                    row[(string)spe.pi.Name] = input.imbGetPropertySafe(spe.pi, spe.pi.PropertyType.GetDefaultValue());
                                }
                                break;
                            }

                            #endregion SWITCH
                        }
                    }

                    if (addTheRow)
                    {
                        table.Rows.Add(row);
                    }
                }
                finally
                {
                    if (!ReadOnlyMode)
                    {
                        Monitor.Exit(SelectTableLock);
                    }
                }
            }
            finally
            {
                if (!ReadOnlyMode)
                {
                    Monitor.Exit(SaveLinkedLock);
                }
            }

            InvokeChanged();

            return(row);
        }
Esempio n. 4
0
        /// <summary>
        /// Adds the or updates. Returns <c>true</c> if new row was added
        /// </summary>
        /// <param name="input">The input.</param>
        /// <param name="policy">The policy.</param>
        /// <returns></returns>
        protected bool AddOrUpdate(object input, objectTableUpdatePolicy policy = objectTableUpdatePolicy.overwrite)
        {
            if (ReadOnlyEnforce())
            {
                return(false);
            }
            if (input == null)
            {
                return(false);
            }

            if (!input.GetType().isCompatibileWith(type))
            {
                return(false);
            }

            bool output = false;

            string keyValue = GetKeyValue(input);

            //if (keyValue.isNullOrEmpty())
            //{
            //     aceLog.log("keyValue is null => " + name + " : " + GetType().Name);
            //}

            if (IsInstanceLinkActive)
            {
                if (instanceRegistry.ContainsKey(keyValue))
                {
                    IObjectTableEntry item = input as IObjectTableEntry;
                    if (instanceRegistry.TryUpdate(keyValue, item, item))
                    {
                        output = false;
                    }
                }
            }

            Boolean _exists = false;

            DataRow[] rows = null;
            if (!WriteOnlyEnforce())
            {
                string select_exp = GetPrimaryKeySelect(keyValue);
                rows    = tableSelect(select_exp);
                _exists = rows.Any();
            }

            if (_exists)
            {
                SetRowWithObject(rows.First(), input, policy);
                output = false;
            }
            else
            {
                SetRowWithObject(null, input, objectTableUpdatePolicy.overwrite);
                output = true;
            }

            if (IsInstanceLinkActive)
            {
                if (!instanceRegistry.ContainsKey(keyValue))
                {
                    IObjectTableEntry item = input as IObjectTableEntry;
                    if (instanceRegistry.TryAdd(keyValue, item))
                    {
                        output = true;
                    }
                }
            }

            if (output)
            {
                lastEntry = input;
            }

            return(output);
        }
Esempio n. 5
0
 /// <summary>
 /// Adds the or updates data table with objects specified. Returns number of new items added into collection
 /// </summary>
 /// <param name="input">The input.</param>
 /// <param name="policy">The policy.</param>
 /// <returns></returns>
 public int AddOrUpdate(IEnumerable <T> input, objectTableUpdatePolicy policy = objectTableUpdatePolicy.overwrite)
 {
     return(base.AddOrUpdate(input, policy));
 }
Esempio n. 6
0
 /// <summary>
 /// Adds object or update if existing row was found. Returns true if new row was created
 /// </summary>
 /// <param name="input">The input.</param>
 /// <param name="policy">The policy.</param>
 /// <returns></returns>
 public bool AddOrUpdate(T input, objectTableUpdatePolicy policy = objectTableUpdatePolicy.overwrite)
 {
     return(base.AddOrUpdate(input, policy));
 }