Esempio n. 1
0
        /// <summary>
        /// Update OpenErp entity with current entity values
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="entity"></param>
        internal int UpdateEntity <T>(T entity) where T : IOdooObject
        {
            OdooCommandContext context = OdooCommandContextFactory.BuildCommandContextFromEntity <T>(entity);

            UpdateCommand(context);
            return(entity.Id);
        }
Esempio n. 2
0
        public void AndOrTest()
        {
            Assert.Throws <NotImplementedException>(() => OdooCommandContextFactory.BuildCommandContextFromExpression <OdooPartner>(x =>
                                                                                                                                    x.Email == "test" && (x.Name == "test" || x.Name == "info")));

            Assert.Throws <NotImplementedException>(() => OdooCommandContextFactory.BuildCommandContextFromExpression <OdooPartner>(x =>
                                                                                                                                    x.Email == "test" && x.Name == "test" || x.Name == "info"));
        }
Esempio n. 3
0
        /// <summary>
        /// Add entity to OpenErp
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="entity"></param>
        internal int AddEntity <T>(T entity) where T : IOdooObject
        {
            var context = OdooCommandContextFactory.BuildCommandContextFromEntity <T>(entity);
            var id      = AddCommand(context);

            entity.Id = id;
            return(id);
        }
Esempio n. 4
0
        public void SinglePropStringSearchTest()
        {
            var command = OdooCommandContextFactory.BuildCommandContextFromExpression <OdooPartner>(
                x => x.Name == "test");

            Assert.IsNotNull(command);
            Assert.That(command.Arguments.Any());
            Assert.That(command.Arguments.Count == 1);
            Assert.That(command.Arguments.First().Operation == "=");
        }
Esempio n. 5
0
        public void MultiOrTest()
        {
            var command = OdooCommandContextFactory.BuildCommandContextFromExpression <OdooPartner>(x =>
                                                                                                    x.Name == "test" || x.Name == "info" || x.Name == "test2");

            Assert.IsNotNull(command);
            Assert.That(command.Arguments.Any());

            var args = command.GetArguments();

            Assert.That(args.Count() == 4);
        }
Esempio n. 6
0
 public void LoadData()
 {
     if (!DataLoaded)
     {
         if (EntitiesId != null && EntitiesId.Length > 0)
         {
             this.DataLoaded = true;
             //Call for get entities by ids.
             OdooCommandContext context = new OdooCommandContext();
             context.EntityName = OdooCommandContextFactory.GetOdooEntityName(typeof(T));
             ResultSet result = this.Service.GetEntityCommand(context, this.EntitiesId);
             OdooObjectFactory.BuildEntities <T>(Service, result, this);
         }
     }
 }
Esempio n. 7
0
        public void SinglePropBoolFalseSearchTest()
        {
            var command = OdooCommandContextFactory.BuildCommandContextFromExpression <OdooPartner>(x =>
                                                                                                    !x.IsCompany);

            Assert.IsNotNull(command);
            Assert.That(command.Arguments.Any());

            var param = command.GetArguments();

            Assert.That(param.Any());
            Assert.That(param.Count() == 1);

            var arg1 = (object[])param[0];

            Assert.That((string)arg1[1] == "=");
            Assert.That((bool)arg1[2] == false);
        }
Esempio n. 8
0
        public void DoublePropAndStringSearchTest()
        {
            var command = OdooCommandContextFactory.BuildCommandContextFromExpression <OdooPartner>(
                x => x.Name == "test" && x.Email.Contains("@"));

            Assert.IsNotNull(command);
            Assert.That(command.Arguments.Any());

            var param = command.GetArguments();

            Assert.That(param.Any());
            Assert.That(param.Count() == 2);

            var arg1 = (object[])param[0];
            var arg2 = (object[])param[1];

            Assert.That((string)arg1[1] == "=");
            Assert.That((string)arg2[1] == "ilike");
        }
Esempio n. 9
0
 internal IEnumerable <T> GetEntities <T>(Expression <Func <T, bool> > conditions, int?offset = null, int?limit = null, string order = null) where T : IOdooObject, new()
 {
     try
     {
         OdooCommandContext context;
         context        = OdooCommandContextFactory.BuildCommandContextFromExpression <T>(conditions);
         context.Limit  = limit ?? 0;
         context.Offset = offset ?? 0;
         context.Order  = order;
         IEnumerable <object> ids = SearchCommand(context);
         context.ClearArguments();
         ResultSet       result   = GetEntityCommand(context, ids);
         IEnumerable <T> entities = OdooObjectFactory.BuildEntities <T>(this, result);
         return(entities);
     }
     catch (Exception ex)
     {
         return(null);
     }
 }
Esempio n. 10
0
        private void LoadData()
        {
            if (!_dataLoaded)
            {
                var context = new OdooCommandContext();
                context.EntityName = OdooCommandContextFactory.GetOdooEntityName(typeof(T));

                var result = _service.GetEntityCommand(context, new List <object> {
                    _id
                });

                var collection = new Collection <T>();

                OdooObjectFactory.BuildEntities(_service, result, collection);

                if (collection.Any())
                {
                    @object = collection.First();
                }
            }
        }
Esempio n. 11
0
        /// <summary>
        /// If property passed in path argument is a OpenErp class,
        /// it will load from OpenErp and set path property.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <typeparam name="TProperty"></typeparam>
        /// <param name="source"></param>
        /// <param name="path">Property to load</param>
        /// <returns></returns>
        public static IEnumerable <T> Include <T, TProperty>(this IEnumerable <T> source, Expression <Func <T, TProperty> > path) where T : IOdooObject, new()
        {
            var enumerable = source as OdooCollection <T>;

            if (enumerable != null)
            {
                var service      = enumerable.Service;
                var memberAccess = path.Body as MemberExpression;
                if (memberAccess != null)
                {
                    var member = memberAccess.Member;
                    if (member != null)
                    {
                        //Search for openerpmap attribute
                        Type memberType = ((PropertyInfo)memberAccess.Member).PropertyType;
                        OdooMapAttribute[] attributes;
                        if (memberType != null)
                        {
                            if (memberType.IsGenericCollection())
                            {
                                //Collection, load entities
                                //If collection is type of OpenErpSet, just load data.
                                //Else, try to get openerpattribute
                                Type EnumerationType = memberType.GetGenericArguments()[0];
                                attributes = (OdooMapAttribute[])EnumerationType.GetCustomAttributes(typeof(OdooMapAttribute), false);
                                var accessFunction = path.Compile();
                                foreach (T item in enumerable)
                                {
                                    var value = accessFunction(item);
                                    if (value != null)
                                    {
                                        //Check for OpenErpSet and call load.
                                        Type openErpSetType = typeof(OdooCollection <>).MakeGenericType(EnumerationType);
                                        if (value.GetType() == openErpSetType)
                                        {
                                            //call load data
                                            var res = value.GetType().GetMethod("LoadData").Invoke(value, null);
                                        }
                                    }
                                    else
                                    {
                                        //if has openerpmap attribute, load related data, else nothing can be done
                                        if (EnumerationType != null)
                                        {
                                            if (attributes.Length > 0)
                                            {
                                                //property type has a OpenErp mapped attribute
                                                PropertyInfo       idProperty = member.DeclaringType.GetProperties().Where(p => p.Name.Equals("Id")).Single();
                                                int                id         = (int)idProperty.GetValue(item, null);
                                                OdooCommandContext context    = new OdooCommandContext();
                                                context.EntityName = attributes[0].OdooName;
                                                context.EntityType = EnumerationType;
                                                string fieldName = ((OdooMapAttribute)(member.GetCustomAttributes(false).First())).OdooName;
                                                context.Arguments.Add(new OdooCommandArgument()
                                                {
                                                    Operation = "=", Property = fieldName, Value = id
                                                });
                                                Object res = service.GetEntities(context);
                                                ((PropertyInfo)memberAccess.Member).SetValue(item, res, null);
                                            }
                                        }
                                    }
                                }
                            }
                            else
                            {
                                //Single entity to load.
                                //Check basic type
                                if (memberType.IsPrimitive || memberType.Equals(typeof(string)))
                                {
                                    // Nothing can be done
                                }
                                else
                                {
                                    // Have an entity
                                    OdooForeignKeyAttribute fk;
                                    OdooCommandContext      context = new OdooCommandContext();
                                    context.EntityName = OdooCommandContextFactory.GetOdooEntityName(memberType);
                                    context.EntityType = memberType;
                                    fk = (OdooForeignKeyAttribute)member.GetCustomAttributes(typeof(OdooForeignKeyAttribute), false).FirstOrDefault();
                                    if (fk != null)
                                    {
                                        PropertyInfo idProperty = member.DeclaringType.GetProperties().Where(p => p.Name.Equals(fk.PropertyName)).Single();
                                        foreach (T item in enumerable)
                                        {
                                            int id = (int)idProperty.GetValue(item, null);
                                            //Buscamos en OpenErp
                                            Object res = service.GetEntityById(context, id);
                                            ((PropertyInfo)memberAccess.Member).SetValue(item, res, null);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                else
                {
                    throw new ArgumentException("Not a valid property to include.");
                }
            }
            return(source);
        }
Esempio n. 12
0
        /// <summary>
        /// Delete entity from OpenErp
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="entity"></param>
        internal void DeleteEntity <T>(T entity) where T : IOdooObject
        {
            var context = OdooCommandContextFactory.BuildCommandContextFromEntity <T>(entity);

            DeleteCommand(context);
        }