/// <summary> /// Returns a new activity argument describing the argument type based on the column expression type and condition. /// </summary> /// <param name="reportColumnExpressionType">Type of the report column expression.</param> /// <param name="conditionType">Type of the condition.</param> /// <returns>ActivityArgument.</returns> public static ActivityArgument ArgumentForConditionType(ActivityArgument reportColumnExpressionType, ConditionType conditionType) { switch (conditionType) { case ConditionType.Equal: case ConditionType.NotEqual: case ConditionType.Contains: case ConditionType.StartsWith: case ConditionType.EndsWith: if (reportColumnExpressionType.Is <ResourceArgument>() || reportColumnExpressionType.Is <ResourceListArgument>()) { return(new StringArgument().As <ActivityArgument>()); } return(reportColumnExpressionType); case ConditionType.LastNDays: case ConditionType.LastNDaysTillNow: case ConditionType.NextNDaysFromNow: case ConditionType.NextNDays: case ConditionType.LastNWeeks: case ConditionType.NextNWeeks: case ConditionType.LastNMonths: case ConditionType.NextNMonths: case ConditionType.LastNQuarters: case ConditionType.NextNQuarters: case ConditionType.LastNYears: case ConditionType.NextNYears: case ConditionType.LastNFinancialYears: case ConditionType.NextNFinancialYears: return(new IntegerArgument().As <ActivityArgument>()); case ConditionType.DateEquals: return(new DateArgument().As <ActivityArgument>()); case ConditionType.FullTextSearch: return(new StringArgument().As <ActivityArgument>()); case ConditionType.AnyOf: case ConditionType.AnyExcept: case ConditionType.CurrentUser: if (reportColumnExpressionType.Is <ResourceArgument>() || reportColumnExpressionType.Is <ResourceListArgument>()) { return(reportColumnExpressionType); } return(new StringArgument().As <ActivityArgument>()); case ConditionType.Unspecified: return(new StringArgument().As <ActivityArgument>()); default: return(new StringArgument().As <ActivityArgument>()); } }
/// <summary> /// Defaults the type of the operator for. /// </summary> /// <param name="dataType">Type of the data.</param> /// <returns>ConditionType.</returns> private static Structured.ConditionType DefaultOperatorForType(ActivityArgument dataType) { Structured.ConditionType conditionType = Structured.ConditionType.Contains; if (dataType.Is <BoolArgument>()) { conditionType = Structured.ConditionType.Unspecified; } else if (dataType.Is <DateArgument>()) { conditionType = Structured.ConditionType.GreaterThan; } else if (dataType.Is <DateTimeArgument>()) { conditionType = Structured.ConditionType.GreaterThan; } else if (dataType.Is <DecimalArgument>()) { conditionType = Structured.ConditionType.GreaterThan; } else if (dataType.Is <IntegerArgument>()) { conditionType = Structured.ConditionType.GreaterThan; } else if (dataType.Is <CurrencyArgument>()) { conditionType = Structured.ConditionType.GreaterThan; } else if (dataType.Is <StringArgument>()) { conditionType = Structured.ConditionType.Contains; } else if (dataType.Is <TimeArgument>()) { conditionType = Structured.ConditionType.GreaterThan; } else if (dataType.Is <TypedArgument>()) { conditionType = Structured.ConditionType.AnyOf; } return(conditionType); }
private static void PopulateValueFromArgument(ActivityArgument argument, ReportAnalyserColumn reportColumn) { if (argument.Is <StringArgument>()) { StringArgument stringArgument = argument.As <StringArgument>(); reportColumn.Value = stringArgument.StringParameterValue; } else if (argument.Is <IntegerArgument>()) { IntegerArgument integerArgument = argument.As <IntegerArgument>(); if (integerArgument.IntParameterValue != null) { reportColumn.Value = integerArgument.IntParameterValue.ToString(); } } else if (argument.Is <CurrencyArgument>()) { CurrencyArgument currencyArgument = argument.As <CurrencyArgument>(); if (currencyArgument.DecimalParameterValue != null) { reportColumn.Value = currencyArgument.DecimalParameterValue.ToString(); } } else if (argument.Is <DecimalArgument>()) { DecimalArgument decimalArgument = argument.As <DecimalArgument>(); if (decimalArgument.DecimalParameterValue != null) { reportColumn.Value = decimalArgument.DecimalParameterValue.ToString(); } } else if (argument.Is <DateArgument>()) { DateArgument dateArgument = argument.As <DateArgument>(); if (dateArgument.DateParameterValue != null) { //convert the date value to YYYY-MM-DD format DateTime dateValue = (DateTime)dateArgument.DateParameterValue; reportColumn.Value = dateValue.ToString("yyyy-MM-dd"); } } else if (argument.Is <TimeArgument>()) { TimeArgument timeArgument = argument.As <TimeArgument>(); if (timeArgument.TimeParameterValue != null) { //convert the time value to YYYY-MM-DDTHH:mm:ssZ format DateTime timeValue = (DateTime)timeArgument.TimeParameterValue; reportColumn.Value = timeValue.ToString("yyyy-MM-ddTHH:mm:ssZ"); } } else if (argument.Is <DateTimeArgument>()) { DateTimeArgument dateTimeArgument = argument.As <DateTimeArgument>(); if (dateTimeArgument.DateTimeParameterValue != null) { //convert the datetime value to YYYY-MM-DDTHH:mm:ssZ format DateTime dateTimeValue = (DateTime)dateTimeArgument.DateTimeParameterValue; reportColumn.Value = dateTimeValue.ToString("yyyy-MM-ddTHH:mm:ssZ"); } } else if (argument.Is <GuidArgument>()) { GuidArgument guidArgument = argument.As <GuidArgument>(); if (guidArgument.GuidParameterValue != null) { reportColumn.Value = guidArgument.GuidParameterValue.ToString(); } } else if (argument.Is <BoolArgument>()) { BoolArgument boolArgument = argument.As <BoolArgument>(); if (boolArgument.BoolParameterValue != null) { reportColumn.Value = boolArgument.BoolParameterValue.ToString(); } } else if (argument.Is <TypedArgument>()) { TypedArgument typedArgument = argument.As <TypedArgument>(); EntityType type = Entity.Get <EntityType>(typedArgument.ConformsToType); if (type.IsOfType.FirstOrDefault(t => t.Alias == "core:enumType") != null) { // A choice field reportColumn.Type = new ChoiceRelationshipType(); } else { // Is a related resource reportColumn.Type = new InlineRelationshipType(); } ResourceListArgument rla = argument.As <ResourceListArgument>(); if (rla.ResourceListParameterValues != null && rla.ResourceListParameterValues.Count > 0) { Dictionary <long, string> values = new Dictionary <long, string>(); foreach (Resource resourceListParameterValue in rla.ResourceListParameterValues) { values[resourceListParameterValue.Id] = resourceListParameterValue.Name; } reportColumn.Values = values; } } }
/// <summary> /// Interrogates the expression instance populating the report column that is exposed to the Report API service. /// </summary> /// <param name="argument">The argument.</param> /// <param name="reportColumn">The report column.</param> /// <returns><c>true</c> if XXXX, <c>false</c> otherwise.</returns> private static bool PopulateTypeFromArgument(ActivityArgument argument, out DatabaseType type, out EntityType resourceType) { resourceType = null; if (argument.Is <StringArgument>()) { type = new StringType(); } else if (argument.Is <IntegerArgument>()) { type = new Int32Type(); } else if (argument.Is <CurrencyArgument>()) { type = new CurrencyType(); } else if (argument.Is <DecimalArgument>()) { type = new DecimalType(); } else if (argument.Is <DateArgument>()) { type = new DateType(); } else if (argument.Is <TimeArgument>()) { type = new TimeType(); } else if (argument.Is <DateTimeArgument>()) { type = new DateTimeType(); } else if (argument.Is <GuidArgument>()) { type = new GuidType(); } else if (argument.Is <BoolArgument>()) { type = new BoolType(); } else if (argument.Is <TypedArgument>()) { TypedArgument rla = argument.As <TypedArgument>(); resourceType = Entity.Get <EntityType>(rla.ConformsToType); if (resourceType == null) { type = null; return(false); } if (resourceType.IsOfType.FirstOrDefault(t => t.Alias == "core:enumType") != null) { // A choice field type = new ChoiceRelationshipType(); } else { // Is a related resource type = new InlineRelationshipType(); } } else { type = null; return(false); } return(true); }
public static ActivityArgument ArgumentForConditionType(ActivityArgument reportColumnExpressionType, string value) { IEntity result; if (reportColumnExpressionType.Is <StringArgument>()) { result = new StringArgument { StringParameterValue = value }; } else if (reportColumnExpressionType.Is <IntegerArgument>()) { int intValue; result = !int.TryParse(value, out intValue) ? new IntegerArgument() : new IntegerArgument { IntParameterValue = intValue }; } else if (reportColumnExpressionType.Is <CurrencyArgument>()) { decimal currencyValue; result = !decimal.TryParse(value, out currencyValue) ? new CurrencyArgument() : new CurrencyArgument { DecimalParameterValue = currencyValue }; } else if (reportColumnExpressionType.Is <DecimalArgument>()) { decimal decimalValue; result = !decimal.TryParse(value, out decimalValue) ? new DecimalArgument() : new DecimalArgument { DecimalParameterValue = decimalValue }; } else if (reportColumnExpressionType.Is <DateArgument>()) { DateTime dateValue; result = !DateTime.TryParse(value, out dateValue) ? new DateArgument() : new DateArgument { DateParameterValue = dateValue }; } else if (reportColumnExpressionType.Is <TimeArgument>()) { DateTime timeValue; result = !DateTime.TryParse(value, out timeValue) ? new TimeArgument() : new TimeArgument { TimeParameterValue = timeValue }; } else if (reportColumnExpressionType.Is <DateTimeArgument>()) { DateTime dateTimeValue; result = !DateTime.TryParse(value, out dateTimeValue) ? new DateTimeArgument() : new DateTimeArgument { DateTimeParameterValue = dateTimeValue }; } else if (reportColumnExpressionType.Is <GuidArgument>()) { Guid guidValue; result = !Guid.TryParse(value, out guidValue) ? new GuidArgument() : new GuidArgument { GuidParameterValue = guidValue }; } else if (reportColumnExpressionType.Is <BoolArgument>()) { bool boolValue; result = !bool.TryParse(value, out boolValue) ? new BoolArgument() : new BoolArgument { BoolParameterValue = boolValue }; } else if (reportColumnExpressionType.Is <ResourceArgument>()) { // Convert the value to an entityId TypedArgument tResult = reportColumnExpressionType.As <TypedArgument>(); long entityId; result = new ResourceArgument { ResourceParameterValue = long.TryParse(value, out entityId) ? Entity.Get(entityId).As <Resource>() : new Resource(), ConformsToType = tResult.ConformsToType }; } else if (reportColumnExpressionType.Is <ResourceListArgument>()) { TypedArgument tResult = reportColumnExpressionType.As <TypedArgument>(); long entityId; result = new ResourceListArgument { ResourceListParameterValues = new EntityCollection <Resource> { long.TryParse(value, out entityId) ? Entity.Get(entityId).As <Resource>() : new Resource() }, ConformsToType = tResult.ConformsToType }; } else { throw new Exception("Unhandled expression result type"); } // Caller must save return(result.As <ActivityArgument>()); }
/// <summary> /// Gets the database type represented by a typed value. /// (Avoids unnecessarily attempting to resolve values). /// </summary> /// <param name="argument">The argument.</param> /// <returns>Database type.</returns> /// <exception cref="System.Exception">Unhandled expression result type</exception> public static DatabaseType GetDatabaseType(ActivityArgument argument) { if (argument == null) { return(DatabaseType.UnknownType); } else if (argument.Is <StringArgument>( )) { return(DatabaseType.StringType); } else if (argument.Is <IntegerArgument>( )) { return(DatabaseType.Int32Type); } else if (argument.Is <CurrencyArgument>( )) { return(DatabaseType.CurrencyType); } else if (argument.Is <DecimalArgument>( )) { return(DatabaseType.DecimalType); } else if (argument.Is <DateArgument>( )) { return(DatabaseType.DateType); } else if (argument.Is <TimeArgument>( )) { return(DatabaseType.TimeType); } else if (argument.Is <DateTimeArgument>( )) { return(DatabaseType.DateTimeType); } else if (argument.Is <GuidArgument>( )) { return(DatabaseType.GuidType); } else if (argument.Is <BoolArgument>( )) { return(DatabaseType.BoolType); } else if (argument.Is <ResourceArgument>( ) || argument.Is <ResourceListArgument>( )) { TypedArgument typedArgument = argument.As <TypedArgument>( ); if (typedArgument != null && typedArgument.ConformsToType != null) { // Interrogate to get it's base type EntityType type = Entity.Get <EntityType>(typedArgument.ConformsToType.Id); EntityRef enumType = new EntityRef("core", "enumValue"); if (type.GetAncestorsAndSelf( ).FirstOrDefault(a => a.Id == enumType.Id) != null) { return(DatabaseType.ChoiceRelationshipType); } } return(DatabaseType.InlineRelationshipType); } throw new Exception("Result type is not of a recognised type."); }
/// <summary> /// Typeds the value from entity. /// </summary> /// <param name="argument">The argument.</param> /// <returns>List{TypedValue}.</returns> /// <exception cref="System.Exception">Unhandled expression result type</exception> public static List <TypedValue> TypedValueFromEntity(ActivityArgument argument) { List <TypedValue> typedValues = new List <TypedValue>(); TypedValue typedValue = new TypedValue(); if (argument == null) { typedValue.Type = DatabaseType.UnknownType; typedValues.Add(typedValue); } else if (argument.Is <StringArgument>()) { typedValue.Type = DatabaseType.StringType; StringArgument stringArgument = argument.As <StringArgument>(); if (stringArgument.StringParameterValue != null) { typedValue.Value = stringArgument.StringParameterValue; } typedValues.Add(typedValue); } else if (argument.Is <IntegerArgument>()) { typedValue.Type = DatabaseType.Int32Type; IntegerArgument integerArgument = argument.As <IntegerArgument>(); if (integerArgument.IntParameterValue.HasValue) { typedValue.Value = integerArgument.IntParameterValue.Value; } typedValues.Add(typedValue); } else if (argument.Is <CurrencyArgument>()) { typedValue.Type = DatabaseType.CurrencyType; CurrencyArgument currencyArgument = argument.As <CurrencyArgument>(); if (currencyArgument.DecimalParameterValue.HasValue) { typedValue.Value = currencyArgument.DecimalParameterValue.Value; } typedValues.Add(typedValue); } else if (argument.Is <DecimalArgument>()) { typedValue.Type = DatabaseType.DecimalType; DecimalArgument decimalArgument = argument.As <DecimalArgument>(); if (decimalArgument.DecimalParameterValue.HasValue) { typedValue.Value = decimalArgument.DecimalParameterValue.Value; } typedValues.Add(typedValue); } else if (argument.Is <DateArgument>()) { typedValue.Type = DatabaseType.DateType; DateArgument dateArgument = argument.As <DateArgument>(); if (dateArgument.DateParameterValue.HasValue) { typedValue.Value = dateArgument.DateParameterValue.Value; } typedValues.Add(typedValue); } else if (argument.Is <TimeArgument>()) { typedValue.Type = DatabaseType.TimeType; TimeArgument timeArgument = argument.As <TimeArgument>(); if (timeArgument.TimeParameterValue.HasValue) { typedValue.Value = timeArgument.TimeParameterValue.Value; } typedValues.Add(typedValue); } else if (argument.Is <DateTimeArgument>()) { typedValue.Type = DatabaseType.DateTimeType; DateTimeArgument dateTimeArgument = argument.As <DateTimeArgument>(); if (dateTimeArgument.DateTimeParameterValue.HasValue) { typedValue.Value = dateTimeArgument.DateTimeParameterValue.Value; } typedValues.Add(typedValue); } else if (argument.Is <GuidArgument>()) { typedValue.Type = DatabaseType.GuidType; GuidArgument guidArgument = argument.As <GuidArgument>(); if (guidArgument.GuidParameterValue.HasValue) { typedValue.Value = guidArgument.GuidParameterValue.Value; } typedValues.Add(typedValue); } else if (argument.Is <BoolArgument>()) { typedValue.Type = DatabaseType.BoolType; BoolArgument boolArgument = argument.As <BoolArgument>(); if (boolArgument.BoolParameterValue.HasValue) { typedValue.Value = boolArgument.BoolParameterValue.Value; } typedValues.Add(typedValue); } else if (argument.Is <ResourceArgument>()) { TypedArgument typedArgument = argument.As <TypedArgument>(); if (typedArgument != null && typedArgument.ConformsToType != null) { // Interrogate to get it's base type EntityType type = Entity.Get <EntityType>(typedArgument.ConformsToType.Id); EntityRef enumType = new EntityRef("core", "enumValue"); if (type.GetAncestorsAndSelf().FirstOrDefault(a => a.Id == enumType.Id) != null) { typedValue.Type = DatabaseType.ChoiceRelationshipType; } else { typedValue.Type = DatabaseType.InlineRelationshipType; } } ResourceArgument resourceArgument = argument.As <ResourceArgument>(); if (resourceArgument.ResourceParameterValue != null) { // Is this an enum type (or are any of it's base types an enum type?? var conformsToType = resourceArgument.ConformsToType; typedValue.SourceEntityTypeId = conformsToType != null ? conformsToType.Id : 0; typedValue.Value = resourceArgument.ResourceParameterValue.Id; } typedValues.Add(typedValue); } else if (argument.Is <ResourceListArgument>()) { TypedArgument typedArgument = argument.As <TypedArgument>(); if (typedArgument != null && typedArgument.ConformsToType != null) { // Interrogate to get it's base type EntityType type = Entity.Get <EntityType>(typedArgument.ConformsToType.Id); EntityRef enumType = new EntityRef("core", "enumValue"); if (type.GetAncestorsAndSelf().FirstOrDefault(a => a.Id == enumType.Id) != null) { typedValue.Type = DatabaseType.ChoiceRelationshipType; } else { typedValue.Type = DatabaseType.InlineRelationshipType; } } ResourceListArgument resourceList = argument.As <ResourceListArgument>(); if (resourceList.ResourceListParameterValues == null || resourceList.ResourceListParameterValues.Count <= 0) { typedValues.Add(typedValue); } else { typedValues.AddRange(resourceList.ResourceListParameterValues.Select(parameterValue => new TypedValue { Type = typedValue.Type, SourceEntityTypeId = resourceList.ConformsToType.Id, Value = parameterValue.Id })); } } else { // Throw as we cannot convert type:( throw new Exception("Unhandled expression result type"); } return(typedValues); }
/// <summary>Given an arg inst and a value create and activityArgument ready for storage.</summary> /// <param name="activity">The <see cref="WfActivity"/> being run. This cannot be null.</param> /// <param name="arg">The argument for the activity. This cannot be null.</param> /// <param name="value">The value of the argument. Its type is determined by <paramref name="arg"/>. This may be null for certain argument types.</param> /// <returns>The converted value.</returns> /// <exception cref="ArgumentNullException">Neither <paramref name="activity"/> nor <paramref name="arg"/> can be null.</exception> /// <exception cref="ArgumentException"><paramref name="arg"/> must be of a supported type.</exception> public static ActivityArgument ConvertArgInstValue(WfActivity activity, ActivityArgument arg, object value) { if (activity == null) { throw new ArgumentNullException(nameof(activity)); } if (arg == null) { throw new ArgumentNullException(nameof(arg)); } ActivityArgument result; string name = $"{activity.Name ?? "[Unnamed]"}.{arg.Name ?? "[Unnamed]"}"; if (arg.Is <ResourceListArgument>()) { var list = (IEnumerable <IEntity>)value; var created = new ResourceListArgument { Name = name }; if (list != null) { var cleanList = list.Where(e => e != null).Select(e => e.As <Resource>()); created.ResourceListParameterValues.AddRange(cleanList); } result = created.As <ActivityArgument>(); } else if (arg.Is <ResourceArgument>()) { var res = (IEntity)value; var created = new ResourceArgument { Name = name, ResourceParameterValue = res?.As <Resource>() }; result = created.As <ActivityArgument>(); } else if (arg.Is <StringArgument>()) { var created = new StringArgument { Name = name, StringParameterValue = (string)value }; result = created.As <ActivityArgument>(); } else if (arg.Is <BoolArgument>()) { var created = new BoolArgument { Name = name, BoolParameterValue = (bool?)value ?? false }; result = created.As <ActivityArgument>(); } else if (arg.Is <IntegerArgument>()) { var created = new IntegerArgument { Name = name, IntParameterValue = (int?)value ?? 0 }; result = created.As <ActivityArgument>(); } else if (arg.Is <DecimalArgument>()) { var created = new DecimalArgument { Name = name, DecimalParameterValue = (decimal?)value ?? 0 }; result = created.As <ActivityArgument>(); } else if (arg.Is <CurrencyArgument>()) { var created = new CurrencyArgument { Name = name, DecimalParameterValue = (decimal?)value ?? 0 }; result = created.As <ActivityArgument>(); } else if (arg.Is <DateTimeArgument>()) { var created = new DateTimeArgument { Name = name, DateTimeParameterValue = (DateTime?)value }; result = created.As <ActivityArgument>(); } else if (arg.Is <DateArgument>()) { var created = new DateArgument { Name = name, DateParameterValue = (DateTime?)value }; result = created.As <ActivityArgument>(); } else if (arg.Is <TimeArgument>()) { // ensure that timeparametervalue only ever holds a datetime var dt = value is TimeSpan?TimeType.NewTime((TimeSpan)value) : (DateTime?)value; var created = new TimeArgument { Name = name, TimeParameterValue = dt }; result = created.As <ActivityArgument>(); } else if (arg.Is <GuidArgument>()) { var created = new GuidArgument { Name = name, GuidParameterValue = (Guid?)value ?? Guid.Empty }; result = created.As <ActivityArgument>(); } else { throw new ArgumentException($"Unsupported ActivityArgument '{arg.IsOfType.First().Name}' in '{name}'", "arg"); } return(result); }