Exemple #1
0
        public bool Save(SynkContext context, bool useTriggers = true)
        {
            if (!this.isWritable)
            {
                return(false);
            }


            if (useTriggers)
            {
                /*if (!VerifyFields(context))
                 * {
                 *  return false;
                 * }*/

                ApplyGenerators(context);
            }

            var dbFields = this.GetFields();

            if (this.exists)
            {
                context.WriteLog("Saving into " + this.tableName + ": " + this.ToString());

                context.database.saveObject(dbName, tableName, dbFields, "id", this.id.ToString());
            }
            else
            {
                context.WriteLog("Inserting into " + this.tableName + ": " + this.ToString());

                dbFields["insertion_date"] = this.insertion_date.ToString();
                this.id     = context.database.insertObject(dbName, tableName, dbFields);
                this.exists = true;
            }

            if (useTriggers)
            {
                ApplyTriggers(context);
            }

            foreach (var val in this._currentValues)
            {
                context.WriteLog(val.Key + ": " + val.Value);
                _originalValues[val.Key] = val.Value;
            }

            context.site.InvalidateCache(this.className);

            return(true);
        }
Exemple #2
0
        protected void ApplyTriggers(SynkContext context)
        {
            foreach (var field in _fields.Values)
            {
                if (string.IsNullOrEmpty(field.entity))
                {
                    continue;
                }

                var fieldName  = field.name;
                var fieldValue = GetFieldValue(fieldName);
                var oldValue   = _originalValues[field.name];

                if (!string.IsNullOrEmpty(field.fieldLink) && !fieldValue.Equals(oldValue))
                {
                    var old     = GetRelationshipIDsFromList(oldValue);
                    var current = GetRelationshipIDsFromList(fieldValue);

                    var deleted = old.Except(current);
                    var added   = current.Except(old);

                    //DEBUGGER
                    context.WriteLog("TRIGGER: RELATIONSHIP CHANGES in " + field.name);
                    context.WriteLog("OLD: " + oldValue);
                    context.WriteLog("NEW: " + fieldValue);

                    foreach (var id in deleted)
                    {
                        var other = context.database.FetchEntityByID(field.entity, id);
                        if (other.exists)
                        {
                            other.RemoveRelationship(field.fieldLink, this);
                            other.Save(context, false);
                        }
                    }

                    foreach (var id in added)
                    {
                        var other = context.database.FetchEntityByID(field.entity, id);
                        if (other.exists)
                        {
                            other.AddRelationship(field.fieldLink, this);
                            other.Save(context, false);
                        }
                    }
                }
            }
        }
Exemple #3
0
        /*protected bool VerifyFields(SynkRequest context)
         * {
         *  foreach (var field in _fields.Values)
         *  {
         *      var fieldName = field.name;
         *
         *      if (field.validator != null)
         *      {
         *          if (!field.validator(context, this, field))
         *          {
         *              if (string.IsNullOrEmpty(context.error) && !context.WaitingForConfirmation())
         *              {
         *                  context.error = "Entity field '"+context.Translate("entity_"+this.GetType().Name.ToLower()+"_"+ field.name) +"' failed validation";
         *                  context.WriteLog(context.error);
         *              }
         *              return false;
         *          }
         *      }
         *  }
         *
         *  return true;
         * }*/

        protected void ApplyGenerators(SynkContext context)
        {
            foreach (var field in _fields.Values)
            {
                var fieldName = field.name;

                if (field.autoGenerator != null)
                {
                    if (field.autoGenerator(context, this, field))
                    {
                        context.WriteLog("Generating field " + field.name + " in " + this.ToString());
                    }
                }
            }
        }