Exemplo n.º 1
0
        private string BuildLine(IList <object> fields, FieldDefinitionList fieldDefinitions)
        {
            string line        = "";
            int    fieldIndex  = 0;
            string paddedField = "";

            // loop through all defintions of import/exportfields and build the line of fixed text
            foreach (var fieldDef in fieldDefinitions)
            {
                if (fieldDef.IsActive)
                {
                    // build a padded field with the right length (take the field, pad to the max. length, and then cut of at the given position)
                    paddedField = fields[fieldIndex].ToStringOrEmpty()
                                  .PadRight(fieldDef.DataSourceField.Length)
                                  .Truncate(fieldDef.DataSourceField.Length);

                    // add to the line
                    line += paddedField;
                }

                fieldIndex++;
            }

            return(line);
        }
        /// <summary>
        /// Splits a line based on the length in the field definitions.
        /// </summary>
        /// <param name="line">The line.</param>
        /// <param name="fieldDefinitions">The field definitions with the length info.</param>
        /// <returns>The fields</returns>
        private List <string> SplitLine(string line, FieldDefinitionList fieldDefinitions)
        {
            var fieldArray = new List <string>();

            int startIndex = 0;

            foreach (var fieldDef in fieldDefinitions)
            {
                // cancel, when line shorter than the next field
                if (line.Length < startIndex)
                {
                    break;
                }

                if (fieldDef.IsActive)
                {
                    // get the unpadded field (without the spaces and tabs at the end)
                    string unpaddedField = line.Truncate(startIndex, fieldDef.DataSourceField.Length)
                                           .TrimEnd(new char[] { ' ', '\t' });

                    fieldArray.Add(unpaddedField);
                }

                startIndex += fieldDef.DataSourceField.Length;
            }

            return(fieldArray);
        }
        /// <summary>
        /// Get mapping.
        /// </summary>
        /// <param name="type">Type name.</param>
        /// <returns>Current mapping.</returns>
        protected FieldDefinitionList GetMapping(String type)
        {
            ElasticsearchResponse <DynamicResponse> response;
            FieldDefinition     fieldDefinition;
            FieldDefinitionList fieldDefinitions;
            String fieldName, mappingJson;

            String[] splitDataType, fieldProperties, splitFields;

            fieldDefinitions = null;
            response         = _client.IndicesGetMapping <DynamicResponse>(IndexName, type);
            CheckResponse(response);
            if (response.Body.IsNotNull() &&
                (response.Body.Count > 0))
            {
                fieldDefinitions = new FieldDefinitionList();
                mappingJson      = (String)(response.Body.Values.ElementAt(0));
                mappingJson      = mappingJson.Substring(mappingJson.IndexOf('{') + 1);
                mappingJson      = mappingJson.Substring(mappingJson.IndexOf('{') + 1);
                mappingJson      = mappingJson.Substring(mappingJson.IndexOf('{') + 1);
                mappingJson      = mappingJson.Substring(mappingJson.IndexOf("properties") + 1);
                mappingJson      = mappingJson.Substring(mappingJson.IndexOf('{') + 1);

                //// If Elasticsearch 2.1 is used run next line.
                //mappingJson = mappingJson.Substring(mappingJson.IndexOf('{') + 1);

                splitFields = mappingJson.Split('}');

                foreach (String fieldMappingJson in splitFields)
                {
                    if (fieldMappingJson.IsNotEmpty() &&
                        (2 <= fieldMappingJson.IndexOf(':')))
                    {
                        fieldName = fieldMappingJson.Substring(1, fieldMappingJson.IndexOf(':') - 2);
                        if (fieldName[0] == '"')
                        {
                            fieldName = fieldName.Substring(1);
                        }

                        fieldDefinition       = new FieldDefinition();
                        fieldDefinition.Index = IndexName;
                        fieldDefinition.Json  = fieldMappingJson + "}";
                        if (fieldDefinition.Json[0] == ',')
                        {
                            fieldDefinition.Json = fieldDefinition.Json.Substring(1);
                        }

                        fieldDefinition.Name = fieldName;
                        fieldDefinition.Type = type;
                        fieldProperties      = fieldMappingJson.Substring(fieldMappingJson.IndexOf('{') + 1).Split(',');
                        foreach (String fieldProperty in fieldProperties)
                        {
                            splitDataType    = fieldProperty.Split(':');
                            splitDataType[0] = splitDataType[0].Substring(1, splitDataType[0].Length - 2);
                            switch (splitDataType[0])
                            {
                            case "format":
                                fieldDefinition.Format = splitDataType[1].Substring(1, splitDataType[1].Length - 2);
                                break;

                            case "index":
                                fieldDefinition.FieldIndex = splitDataType[1].Substring(1, splitDataType[1].Length - 2);
                                break;

                            case "tree_levels":
                                fieldDefinition.TreeLevel = splitDataType[1].WebParseInt32();
                                break;

                            case "type":
                                fieldDefinition.DataType = splitDataType[1].Substring(1, splitDataType[1].Length - 2);
                                break;
                            }
                        }

                        fieldDefinitions.Add(fieldDefinition);
                    }
                }
            }

            return(fieldDefinitions);
        }