[4311] | 1 | using System;
|
---|
| 2 | using System.Collections.Generic;
|
---|
| 3 | using System.Linq;
|
---|
| 4 | using System.Text;
|
---|
| 5 | using System.Collections.ObjectModel;
|
---|
| 6 | using System.Text.RegularExpressions;
|
---|
| 7 |
|
---|
| 8 | namespace HeuristicLab.OKB.Cockpit.Query.OKBQuery {
|
---|
| 9 |
|
---|
| 10 | public class TypeConverter {
|
---|
| 11 |
|
---|
| 12 | public static object Parse(object value, Type type) {
|
---|
| 13 | if (value == null)
|
---|
| 14 | return null;
|
---|
| 15 | string s = value as string;
|
---|
| 16 | if (s == null || type == null)
|
---|
| 17 | return value;
|
---|
| 18 | if (string.IsNullOrEmpty(s))
|
---|
| 19 | return null;
|
---|
| 20 | if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>)) {
|
---|
| 21 | type = type.GetGenericArguments()[0];
|
---|
| 22 | }
|
---|
| 23 | try {
|
---|
| 24 | if (type == typeof(string)) return value;
|
---|
| 25 | if (type == typeof(int)) return int.Parse(s);
|
---|
| 26 | if (type == typeof(double)) return double.Parse(s);
|
---|
| 27 | if (type == typeof(DateTime)) return DateTime.Parse(s);
|
---|
| 28 | if (type == typeof(Guid)) return new Guid(s);
|
---|
| 29 | } catch (Exception x) {
|
---|
| 30 | throw new ArgumentException(string.Format(
|
---|
| 31 | "conversion of value '{0}' of type '{1}' to type '{2}' failed",
|
---|
| 32 | value, value.GetType(), type), x);
|
---|
| 33 | }
|
---|
| 34 | throw new ArgumentException(string.Format(
|
---|
| 35 | "conversion of value '{0}' of type '{1}' to type '{2}' is not supported",
|
---|
| 36 | value, value.GetType(), type));
|
---|
| 37 | }
|
---|
| 38 |
|
---|
| 39 | public static List<object> ParseList(IEnumerable<object> values, Type type) {
|
---|
| 40 | if (values == null)
|
---|
| 41 | return null;
|
---|
| 42 | return values
|
---|
| 43 | .Select(v => Parse(v, type))
|
---|
| 44 | .ToList();
|
---|
| 45 | }
|
---|
| 46 | }
|
---|
| 47 |
|
---|
| 48 |
|
---|
| 49 | public partial class AttributeSelector {
|
---|
| 50 |
|
---|
| 51 | public AttributeSelector() {
|
---|
| 52 | this.PropertyChanged += (s, a) => {
|
---|
| 53 | switch (a.PropertyName) {
|
---|
| 54 | case "IsValid":
|
---|
| 55 | case "ValidationErrorString":
|
---|
| 56 | break;
|
---|
| 57 | case "DataTypeName":
|
---|
| 58 | DataType = DataTypeName != null ? Type.GetType(DataTypeName) : null;
|
---|
| 59 | goto default;
|
---|
| 60 | case "TableName":
|
---|
| 61 | case "FieldName":
|
---|
| 62 | columnName = null;
|
---|
| 63 | OnPropertyChanged("ColumnName");
|
---|
| 64 | goto default;
|
---|
| 65 | default:
|
---|
| 66 | Validate();
|
---|
| 67 | break;
|
---|
| 68 | }
|
---|
| 69 | };
|
---|
| 70 | ValidationErrors.CollectionChanged += (s, a) => {
|
---|
| 71 | OnPropertyChanged("ValidationErrorString");
|
---|
| 72 | };
|
---|
| 73 | }
|
---|
| 74 |
|
---|
| 75 | public AttributeSelector(string tableName, string fieldName, Type type)
|
---|
| 76 | : this() {
|
---|
| 77 | TableName = tableName;
|
---|
| 78 | FieldName = fieldName;
|
---|
| 79 | DataTypeName = type.FullName;
|
---|
| 80 | }
|
---|
| 81 |
|
---|
| 82 | private Type dataType;
|
---|
| 83 | public Type DataType {
|
---|
| 84 | get {
|
---|
| 85 | return dataType;
|
---|
| 86 | }
|
---|
| 87 | private set {
|
---|
| 88 | if (value == dataType)
|
---|
| 89 | return;
|
---|
| 90 | dataType = value;
|
---|
| 91 | OnPropertyChanged("DataType");
|
---|
| 92 | }
|
---|
| 93 | }
|
---|
| 94 |
|
---|
| 95 | protected void OnPropertyChanged(string propertyName) {
|
---|
| 96 | if (PropertyChanged != null)
|
---|
| 97 | PropertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
|
---|
| 98 | }
|
---|
| 99 |
|
---|
| 100 | private string columnName;
|
---|
| 101 | public string ColumnName {
|
---|
| 102 | get {
|
---|
| 103 | if (columnName == null)
|
---|
| 104 | columnName = TableName + "_" + FieldName;
|
---|
| 105 | return columnName;
|
---|
| 106 | }
|
---|
| 107 | set {
|
---|
| 108 | if (value == columnName)
|
---|
| 109 | return;
|
---|
| 110 | columnName = value;
|
---|
| 111 | OnPropertyChanged("ColumnName");
|
---|
| 112 | }
|
---|
| 113 | }
|
---|
| 114 |
|
---|
| 115 | private bool isValid;
|
---|
| 116 | public bool IsValid {
|
---|
| 117 | get {
|
---|
| 118 | return isValid;
|
---|
| 119 | }
|
---|
| 120 | set {
|
---|
| 121 | if (isValid == value)
|
---|
| 122 | return;
|
---|
| 123 | isValid = value;
|
---|
| 124 | OnPropertyChanged("IsValid");
|
---|
| 125 | }
|
---|
| 126 | }
|
---|
| 127 |
|
---|
| 128 | private ObservableCollection<string> ValidationErrors = new ObservableCollection<string>();
|
---|
| 129 | public string ValidationErrorString {
|
---|
| 130 | get {
|
---|
| 131 | return string.Join("\n", ValidationErrors.ToArray());
|
---|
| 132 | }
|
---|
| 133 | }
|
---|
| 134 |
|
---|
| 135 | protected bool validating = false;
|
---|
| 136 | protected void Validate() {
|
---|
| 137 | if (validating)
|
---|
| 138 | return;
|
---|
| 139 | object minValue = MinValue;
|
---|
| 140 | object maxValue = MaxValue;
|
---|
| 141 | List<object> allowedValues = AllowedValues;
|
---|
| 142 | ValidationErrors.Clear();
|
---|
| 143 | IsValid =
|
---|
| 144 | ValidateNotNull(TableName, "table name") &
|
---|
| 145 | ValidateNotNull(FieldName, "field name") &
|
---|
| 146 | ValidateAndParse(ref minValue, "min value") &
|
---|
| 147 | ValidateAndParse(ref maxValue, "max value") &
|
---|
| 148 | ValidateAndParse(ref allowedValues, "allowed values");
|
---|
| 149 | if (IsValid) {
|
---|
| 150 | validating = true;
|
---|
| 151 | MinValue = minValue;
|
---|
| 152 | MaxValue = maxValue;
|
---|
| 153 | AllowedValues = allowedValues;
|
---|
| 154 | validating = false;
|
---|
| 155 | }
|
---|
| 156 | }
|
---|
| 157 |
|
---|
| 158 | private bool ValidateNotNull(string value, string name) {
|
---|
| 159 | if (value == null) {
|
---|
| 160 | ValidationErrors.Add(String.Format("No {0}", name));
|
---|
| 161 | return false;
|
---|
| 162 | }
|
---|
| 163 | return true;
|
---|
| 164 | }
|
---|
| 165 |
|
---|
| 166 |
|
---|
| 167 | protected TypeNameConverter TypeNameConverter = new TypeNameConverter();
|
---|
| 168 | private bool ValidateAndParse(ref object value, string name) {
|
---|
| 169 | try {
|
---|
| 170 | value = TypeConverter.Parse(value, DataType);
|
---|
| 171 | return true;
|
---|
| 172 | } catch (Exception) {
|
---|
| 173 | ValidationErrors.Add(string.Format("Cannot parse {0}: '{1}' as {2}",
|
---|
| 174 | name, value, TypeNameConverter.FormatType(DataType)));
|
---|
| 175 | return false;
|
---|
| 176 | }
|
---|
| 177 | }
|
---|
| 178 |
|
---|
| 179 | private bool ValidateAndParse(ref List<object> values, string name) {
|
---|
| 180 | try {
|
---|
| 181 | if (values != null)
|
---|
| 182 | values = values.Select(v => TypeConverter.Parse(v, DataType)).ToList();
|
---|
| 183 | return true;
|
---|
| 184 | } catch (Exception) {
|
---|
| 185 | ValidationErrors.Add(string.Format(
|
---|
| 186 | "Cannot parse {0}: [{1}] as list of {2}",
|
---|
| 187 | name,
|
---|
| 188 | string.Join(", ", values.Select(v => v.ToString()).ToArray()),
|
---|
| 189 | TypeNameConverter.FormatType(DataType)));
|
---|
| 190 | return false;
|
---|
| 191 | }
|
---|
| 192 | }
|
---|
| 193 |
|
---|
| 194 | public override string ToString() {
|
---|
| 195 | StringBuilder sb = new StringBuilder()
|
---|
| 196 | .Append(TableName)
|
---|
| 197 | .Append('.')
|
---|
| 198 | .Append(FieldName);
|
---|
| 199 | if (MinValue != null)
|
---|
| 200 | sb.Append("\n >").Append(MinValue);
|
---|
| 201 | if (MaxValue != null)
|
---|
| 202 | sb.Append("\n <").Append(MaxValue);
|
---|
| 203 | if (AllowedValues != null)
|
---|
| 204 | sb.Append("\n in [")
|
---|
| 205 | .Append(string.Join(", ",
|
---|
| 206 | AllowedValues
|
---|
| 207 | .Select(v => v.ToString())
|
---|
| 208 | .ToArray()))
|
---|
| 209 | .Append(']');
|
---|
| 210 | if (IsHidden)
|
---|
| 211 | return String.Format("({0})", sb.ToString());
|
---|
| 212 | else
|
---|
| 213 | return sb.ToString();
|
---|
| 214 | }
|
---|
| 215 | }
|
---|
| 216 |
|
---|
| 217 | public class AttributeSelectors : ObservableCollection<AttributeSelector> { }
|
---|
| 218 | }
|
---|