Example #1
0
        private static void RunQuery(ClarizenUtils utils, string csvReadFile, string targetType)
        {
            using (var csv = new CsvParser(new StreamReader(csvReadFile)))
            {
                int rowIndex = 0;
                while (csv.ReadNextRecord())
                {
                    var conditions = new List<Compare>();
                    foreach (string field in csv.HeaderFields)
                    {
                        if (string.IsNullOrEmpty(csv.CurrentDic[field])) continue;
                        var condition = new Compare
                                            {
                                                LeftExpression = new FieldExpression {FieldName = field},
                                                Operator = Operator.Equal,
                                                RightExpression = new ConstantExpression
                                                                      {
                                                                          Value =
                                                                              ParseValue(csv.CurrentDic[field],
                                                                                         field)
                                                                      }
                                            };
                        conditions.Add(condition);
                    }

                    var qry = new EntityQuery
                                  {
                                      TypeName = targetType,
                                      Where = GetWhereCondition(conditions),
                                      Fields = GetFieldsNames(utils.GetMetaDataFields(targetType).Fields)
                                  };

                    QueryResult queryResult = utils.RunQuery(qry);

                    if (!queryResult.Success) continue;
                    string dir = Path.GetDirectoryName(csvReadFile);
                    if (dir == null) throw new ArgumentNullException("csvReadFile");
                    dir = Path.Combine(dir, "Handled");
                    if (!Directory.Exists(dir))
                        Directory.CreateDirectory(dir);
                    string resultFileName = Path.Combine(dir,
                                                         string.Format("{0}_Result_{1}.csv",
                                                                       Path.GetFileNameWithoutExtension(csvReadFile),
                                                                       rowIndex));
                    string heder = string.Empty;
                    string result = string.Empty;
                    string combineText;
                    if (queryResult.Entities.Length == 0)
                    {
                        combineText = "No Results";
                    }
                    else
                    {
                        for (int i = 0; i < queryResult.Entities.Length; i++)
                        {
                            foreach (FieldValue val in ((GenericEntity) queryResult.Entities[i]).Values)
                            {
                                if (i == 0)
                                    heder += val.FieldName + ",";
                                if (val.Value == null || val.Value.GetType() == typeof (GenericEntity))
                                    result += ",";

                                else if (val.Value.GetType() == typeof (Money))
                                    result += ((Money) val.Value).Value + " (" + ((Money) val.Value).Currency + "),";
                                else if (val.Value.GetType() == typeof (Duration))
                                    result += ((Duration) val.Value).Value + " (" + ((Duration) val.Value).Unit + "),";
                                else
                                    result += ClearNewLine(val.Value.ToString()) + ",";
                            }
                            if ((i + 1) < queryResult.Entities.Length)
                                result += "\r\n";
                        }

                        combineText = heder + "\r\n" + result;
                    }
                    File.WriteAllText(resultFileName, combineText);
                    rowIndex++;
                }
            }
            string p = Path.GetDirectoryName(csvReadFile);
            string d = Path.GetFileName(csvReadFile);
            if (p != null)
            {
                if (d != null)
                {
                    string destFileName = Path.Combine(Path.Combine(p, "Handled"), d);
                    if (File.Exists(destFileName))
                        File.Delete(destFileName);
                    File.Move(csvReadFile, destFileName);
                }
            }
        }