Esempio n. 1
0
        // Receive the data changed from the cloud sync in this method.
        // We will use this method to send the data to the user, of any data that was changed.
        internal void DataChanged(DataChange dataChange, Row row)
        {
            try
            {
                // Here we will send the data to the user if the user is listening to a change.
                // Lets Validate if the Row has to be sent to the listener.
                bool canProceed = false;
                // Now we will Split the Where Clauses according to AND & OR
                if (whereObjs.Count == 0)
                {
                    // There are no where clause, lets send the changed data to the user.
                    canProceed = true;
                }
                else if (whereObjs.Count == 1)
                {
                    // There is only one where clause.
                    // Lets process that, and then if it passes send it to the user.
                    try
                    {
                        Where whereObj = whereObjs[0];
                        switch (whereObj.operation)
                        {
                        case Where.Type.EQUAL:
                            // The Condition is Equals.
                            if (whereObj.GetColumnData().ToString().ToLower()
                                .Equals(row.GetString(whereObj.GetColumnName()).ToLower()))
                            {
                                // The row comes under the where condition.
                                canProceed = true;
                            }
                            break;

                        case Where.Type.GREATER:
                            // The Condition is Greater.
                            if (row.GetBigInt(whereObj.GetColumnName()) > long.Parse(whereObj.GetColumnData().ToString()))
                            {
                                // The row comes under the where condition.
                                canProceed = true;
                            }
                            break;

                        case Where.Type.GREATER_EQUAL:
                            // The Condition is Greater.
                            if (row.GetBigInt(whereObj.GetColumnName()) >= long.Parse(whereObj.GetColumnData().ToString()))
                            {
                                // The row comes under the where condition.
                                canProceed = true;
                            }
                            break;

                        case Where.Type.LESSER:
                            // The Condition is Greater.
                            if (row.GetBigInt(whereObj.GetColumnName()) < long.Parse(whereObj.GetColumnData().ToString()))
                            {
                                // The row comes under the where condition.
                                canProceed = true;
                            }
                            break;

                        case Where.Type.LESSER_EQUAL:
                            // The Condition is Greater.
                            if (row.GetBigInt(whereObj.GetColumnName()) <= long.Parse(whereObj.GetColumnData().ToString()))
                            {
                                // The row comes under the where condition.
                                canProceed = true;
                            }
                            break;

                        default:
                            // There is no valid condition.
                            canProceed = false;
                            break;
                        }
                    }
                    catch (Exception e)
                    {
                        // There was an Error.
                    }
                }
                else
                {
                    // There are many where clauses, lets send do complex processing and send to the user if it passes.
                    List <Where>         andWhereObjs    = new List <Where>();
                    List <List <Where> > combOrWhereObjs = new List <List <Where> >();
                    for (int i = 0; i < whereObjs.Count; i++)
                    {
                        try
                        {
                            // Lets check which where clause is this.
                            if (i == 0)
                            {
                                // This is the first where clause.
                                // Add the where clause to the sectioning where.
                                andWhereObjs.Add(whereObjs[i]);
                            }
                            else
                            {
                                // This is after the first where clause.
                                // Lets now separate the where clauses.
                                if (whereObjs[i].GetConnector().Trim().ToUpper().Equals("AND"))
                                {
                                    // It is an And.
                                    // Add the where clause to the sectioning where.
                                    andWhereObjs.Add(whereObjs[i]);
                                }
                                else
                                {
                                    // Its an OR.
                                    // Add the Section Where Clauses to a combination of the where sections.
                                    combOrWhereObjs.Add(andWhereObjs);
                                    // Lets now clean the section where clause.
                                    andWhereObjs = new List <Where>();
                                    // Lets add this new clause to the section where clause.
                                    andWhereObjs.Add(whereObjs[i]);
                                }
                            }
                        }
                        catch (Exception e)
                        {
                            // There was an Error.
                        }
                    }
                    // Check if there is any where clauses in the where section.
                    if (andWhereObjs.Count > 0)
                    {
                        // There are where clauses in the where clause section.
                        combOrWhereObjs.Add(andWhereObjs);
                        // Clean where clause section.
                        andWhereObjs = new List <Where>();
                    }
                    // Now the Where Clause separation has been completed.
                    // Lets now validate the row according to the where clauses.
                    for (int i = 0; i < combOrWhereObjs.Count; i++)
                    {
                        // Lets test the Clauses within.
                        List <Where> secWheres = combOrWhereObjs[i];
                        bool         isValid   = false;
                        for (int j = 0; j < secWheres.Count; j++)
                        {
                            // Lets test the Conditions.
                            Where whereObj = secWheres[j];
                            switch (whereObj.operation)
                            {
                            case Where.Type.EQUAL:
                                // The Condition is Equals.
                                if (whereObj.GetColumnData().ToString().ToLower()
                                    .Equals(row.GetString(whereObj.GetColumnName()).ToLower()))
                                {
                                    // The row comes under the where condition.
                                    isValid = true;
                                }
                                else
                                {
                                    // The row fails the condition.
                                    isValid = false;
                                }
                                break;

                            case Where.Type.GREATER:
                                // The Condition is Greater.
                                if (row.GetBigInt(whereObj.GetColumnName()) > long.Parse(whereObj.GetColumnData().ToString()))
                                {
                                    // The row comes under the where condition.
                                    isValid = true;
                                }
                                else
                                {
                                    // The row fails the condition.
                                    isValid = false;
                                }
                                break;

                            case Where.Type.GREATER_EQUAL:
                                // The Condition is Greater.
                                if (row.GetBigInt(whereObj.GetColumnName()) >= long.Parse(whereObj.GetColumnData().ToString()))
                                {
                                    // The row comes under the where condition.
                                    isValid = true;
                                }
                                else
                                {
                                    // The row fails the condition.
                                    isValid = false;
                                }
                                break;

                            case Where.Type.LESSER:
                                // The Condition is Greater.
                                if (row.GetBigInt(whereObj.GetColumnName()) < long.Parse(whereObj.GetColumnData().ToString()))
                                {
                                    // The row comes under the where condition.
                                    isValid = true;
                                }
                                else
                                {
                                    // The row fails the condition.
                                    isValid = false;
                                }
                                break;

                            case Where.Type.LESSER_EQUAL:
                                // The Condition is Greater.
                                if (row.GetBigInt(whereObj.GetColumnName()) <= long.Parse(whereObj.GetColumnData().ToString()))
                                {
                                    // The row comes under the where condition.
                                    isValid = true;
                                }
                                else
                                {
                                    // The row fails the condition.
                                    isValid = false;
                                }
                                break;

                            default:
                                // There is no valid condition.
                                isValid = false;
                                break;
                            }
                        }
                        if (isValid)
                        {
                            canProceed = true;
                            break;
                        }
                    }
                }
                // Lets proceed if the user is allowed.
                if (canProceed)
                {
                    // Lets send the data to the user.
                    onDataChangedListener?.Invoke(dataChange, row);
                }
            }
            catch (Exception e)
            {
                // There was an Error.
            }
        }