/// <summary> /// Executes delete command in OpenErp. Get id from context. /// </summary> /// <param name="context"></param> private void DeleteCommand(OdooCommandContext context) { _context.UserId = Login(); OdooCommandArgument idArgument = context.Arguments.FirstOrDefault(x => x.Property.Equals("id")); if (idArgument != null) { DeleteCommand((int)idArgument.Value, context.EntityName); } }
private static void ProcessMemberAccess(Expression expression, OdooCommandContext context, int counter) { if (expression.Type == typeof(bool)) { var argument = new OdooCommandArgument(); argument.Order = counter; argument.Value = expression.NodeType != ExpressionType.Not; argument.Operation = "="; SetProperty(expression, context, argument); context.Arguments.Add(argument); } }
private static void ProcessMethodCallExpression(MethodCallExpression expression, OdooCommandContext context, int order) { var argument = new OdooCommandArgument(); argument.Order = order; if (expression != null) { switch (expression.Method.Name) { case "Contains": argument.Value = GetValue(expression, context); SetProperty(expression, context, argument); if (argument.Value is string) { argument.Operation = "ilike"; } if (argument.Value is Array) { argument.Operation = "in"; } context.Arguments.Add(argument); break; case "StartsWith": argument.Value = GetValue(expression, context) + "%"; SetProperty(expression, context, argument); argument.Operation = "like"; context.Arguments.Add(argument); break; case "EndsWith": argument.Value = "%" + GetValue(expression, context); SetProperty(expression, context, argument); argument.Operation = "like"; context.Arguments.Add(argument); break; case "Equals": argument.Value = GetValue(expression, context); SetProperty(expression, context, argument); argument.Operation = "="; context.Arguments.Add(argument); break; } } }
/// <summary> /// Create a <see cref="OdooCommandContext"/> with all /// </summary> /// <typeparam name="T"></typeparam> /// <param name="entity"></param> /// <returns></returns> public static OdooCommandContext BuildCommandContextFromEntity <T>(T entity) where T : IOdooObject { var context = new OdooCommandContext(); context.EntityType = typeof(T); var attributes = (OdooMapAttribute[])context.EntityType.GetCustomAttributes(typeof(OdooMapAttribute), false); if (attributes.Length > 0) { context.EntityName = attributes[0].OdooName; } else { String message = string.Format("Entity {0} has no Odoo attributed defined", context.EntityType.Name); throw new InvalidOperationException(message); } foreach (PropertyInfo property in context.EntityType.GetProperties()) { var argument = new OdooCommandArgument(); attributes = (OdooMapAttribute[])property.GetCustomAttributes(typeof(OdooMapAttribute), false); if (attributes.Length > 0) { argument.Property = attributes[0].OdooName; argument.ArgumentType = attributes[0].OdooType; argument.Value = property.GetValue(entity, null); argument.Operation = "="; argument.ReadOnly = attributes[0].ReadOnly; if (argument.Value == null) { argument.Value = false; } context.Arguments.Add(argument); } } return(context); }
private static void ProcessBinaryExpression(BinaryExpression operation, OdooCommandContext context, int order) { var argument = new OdooCommandArgument(); argument.Order = order; argument.Value = GetValue(operation, context); SetProperty(operation, context, argument); switch (operation.NodeType) { case ExpressionType.GreaterThan: argument.Operation = ">"; break; case ExpressionType.GreaterThanOrEqual: argument.Operation = ">="; break; case ExpressionType.LessThan: argument.Operation = "<"; break; case ExpressionType.LessThanOrEqual: argument.Operation = "<="; break; case ExpressionType.Equal: argument.Operation = "="; break; case ExpressionType.NotEqual: argument.Operation = "!="; break; } context.Arguments.Add(argument); }
private static void SetProperty(Expression expression, OdooCommandContext context, OdooCommandArgument argument) { if (expression == null) { return; } argument.Property = string.Empty; switch (expression.NodeType) { case ExpressionType.Call: MethodCallExpression methodCall = expression as MethodCallExpression; SetProperty(methodCall.Object, context, argument); if (string.IsNullOrEmpty(argument.Property)) { foreach (Expression item in methodCall.Arguments) { SetProperty(item, context, argument); } } break; case ExpressionType.MemberAccess: MemberExpression memberAccess = expression as MemberExpression; if (memberAccess.Expression is ParameterExpression) { if ((memberAccess.Expression as ParameterExpression).Name.Equals(context.ParameterName)) { argument.Property = GetOdooPropertyName(memberAccess as MemberExpression); argument.ArgumentType = GetOdooPropertyType(memberAccess as MemberExpression); } } else { } break; case ExpressionType.Constant: argument.Property = (expression as ConstantExpression).Value.ToString(); break; default: if (expression is BinaryExpression) { BinaryExpression binary = expression as BinaryExpression; SetProperty(binary.Left, context, argument); if (string.IsNullOrEmpty(argument.Property)) { SetProperty(binary.Right, context, argument); } } break; } }