/// <summary> /// Convert a WebSpeciesObservationFieldProjectParameter instance into /// a WebSpeciesObservationField instance. /// </summary> /// <returns>A WebSpeciesObservationField instance.</returns> public WebSpeciesObservationField GetSpeciesObservationField() { WebSpeciesObservationField field; field = new WebSpeciesObservationField(); field.ClassIdentifier = ClassIdentifier; field.PropertyIdentifier = PropertyIdentifier; field.DataFields = DataFields; field.Type = Type; field.Value = Value; return(field); }
/// <summary> /// Get field name in Elasticsearch. /// </summary> /// <param name="field">Search criteria.</param> /// <returns>Field name in Elasticsearch.</returns> public static String GetFieldName(this WebSpeciesObservationField field) { if ((field.ClassIdentifier == "Project") && field.PropertyIdentifier.StartsWith("ProjectParameter")) { return(field.PropertyIdentifier); } else { return(field.ClassIdentifier + "_" + field.PropertyIdentifier); } }
/// <summary> /// Add a field to the species observation. /// </summary> /// <param name="speciesObservation">The species observation.</param> /// <param name="speciesObservationClassId">Species observation class id.</param> /// <param name="speciesObservationPropertyId">Species observation property id.</param> /// <param name="value">A Double value.</param> public static void AddField(this WebSpeciesObservation speciesObservation, SpeciesObservationClassId speciesObservationClassId, SpeciesObservationPropertyId speciesObservationPropertyId, Double value) { WebSpeciesObservationField field; if (speciesObservation.Fields.IsNull()) { speciesObservation.Fields = new List <WebSpeciesObservationField>(); } field = new WebSpeciesObservationField(); field.ClassIdentifier = speciesObservationClassId.ToString(); field.PropertyIdentifier = speciesObservationPropertyId.ToString(); field.Type = WebDataType.Float64; field.Value = value.WebToString(); speciesObservation.Fields.Add(field); }
/// <summary> /// Convert one species observation field from JSON to /// class WebSpeciesObservationField. /// </summary> /// <param name="speciesObservationField">Field is returned in this parameter.</param> /// <param name="speciesObservationsJson">Species observation field in JSON format.</param> /// <param name="mapping">Species observation field information mapping.</param> /// <param name="startIndex">Current position in the species observation JSON string.</param> /// <returns>Updated current position in the species observation JSON string.</returns> private Int32 GetSpeciesObservationField(out WebSpeciesObservationField speciesObservationField, String speciesObservationsJson, Dictionary <String, WebSpeciesObservationField> mapping, Int32 startIndex) { Int32 stopIndex; String fieldKey, value; String[] splitKey; WebSpeciesObservationField mappingField; // Get field key. if (speciesObservationsJson[startIndex] != '}') { stopIndex = speciesObservationsJson.IndexOf(':', startIndex); fieldKey = speciesObservationsJson.Substring(startIndex + 1, stopIndex - startIndex - 2); startIndex = stopIndex + 1; if (mapping.ContainsKey(fieldKey)) { mappingField = mapping[fieldKey]; splitKey = fieldKey.Split('_'); if (splitKey.Length > 2) { // Get field in order to advance startIndex to next position. startIndex = GetSpeciesObservationFieldValue(out value, mappingField.Type, speciesObservationsJson, startIndex); // This field should not be returned. // Return next field instead. return(GetSpeciesObservationField(out speciesObservationField, speciesObservationsJson, mapping, startIndex)); } else { // Create field instance. speciesObservationField = new WebSpeciesObservationField(); speciesObservationField.ClassIdentifier = mappingField.ClassIdentifier; speciesObservationField.PropertyIdentifier = mappingField.PropertyIdentifier; speciesObservationField.Type = mappingField.Type; startIndex = GetSpeciesObservationFieldValue(out value, speciesObservationField.Type, speciesObservationsJson, startIndex); speciesObservationField.Value = value; } } else { if (fieldKey == "\"sort") { // No more fields in current species observation. // Read to next species observation. stopIndex = speciesObservationsJson.IndexOf('}', startIndex); startIndex = stopIndex + 1; speciesObservationField = null; } else { throw new Exception("Unknown field name = " + fieldKey); } } } else { // No more fields in current species observation. // Read to next species observation. startIndex += 2; speciesObservationField = null; } return(startIndex); }
/// <summary> /// Get mapping for species observation fields. /// </summary> /// <param name="context">Web service request context. </param> /// <param name="elasticsearch">Proxy to Elasticsearch.</param> /// <returns>Mapping for species observation fields.</returns> public Dictionary <String, WebSpeciesObservationField> GetMapping(WebServiceContext context, ElasticsearchSpeciesObservationProxy elasticsearch) { Dictionary <String, WebSpeciesObservationField> mapping; FieldDefinitionList fieldDefinitions; String cacheKey; String[] splitField; WebSpeciesObservationField field; // Get data from cache. cacheKey = Settings.Default.SpeciesObservationFieldMappingCacheKey; mapping = (Dictionary <String, WebSpeciesObservationField>)context.GetCachedObject(cacheKey); if (mapping.IsNull()) { // Get data from Elasticsearch. mapping = new Dictionary <String, WebSpeciesObservationField>(); fieldDefinitions = elasticsearch.GetSpeciesObservationMapping(); if (fieldDefinitions.IsNotEmpty()) { foreach (FieldDefinition fieldDefinition in fieldDefinitions) { if (1 <= fieldDefinition.Name.IndexOf('_')) { splitField = fieldDefinition.Name.Split('_'); field = new WebSpeciesObservationField(); field.ClassIdentifier = splitField[0]; field.PropertyIdentifier = splitField[1]; switch (fieldDefinition.DataType) { case "boolean": field.Type = WebDataType.Boolean; break; case "byte": field.Type = WebDataType.Int32; break; case "date": field.Type = WebDataType.DateTime; break; case "double": field.Type = WebDataType.Float64; break; case "integer": field.Type = WebDataType.Int32; break; case "long": field.Type = WebDataType.Int64; break; case "short": field.Type = WebDataType.Int32; break; case "string": field.Type = WebDataType.String; break; default: throw new Exception("Not handled data type = " + fieldDefinition.DataType); } mapping[fieldDefinition.Name] = field; ////Debug.WriteLine(fieldDefinition.Name + " " + //// field.ClassIdentifier + " " + //// field.PropertyIdentifier + " " + //// field.Type); } } } // Store data in cache. context.AddCachedObject(cacheKey, mapping, DateTime.Now + new TimeSpan(1, 0, 0, 0), CacheItemPriority.High); } return(mapping); }
/// <summary> /// Get species observation field of data type /// String in Json representation. /// </summary> /// <param name="context">Web service context.</param> /// <param name="field">Species observation field.</param> private static String GetStringValueJson(WebServiceContext context, WebSpeciesObservationField field) { String errorMessage, stringValue; // Handle accepted special characters. stringValue = field.Value.CheckJsonInjection(); // Remove unwanted special characters. if (stringValue.Contains("\x6") || stringValue.Contains("\x7") || stringValue.Contains("\x8") || stringValue.Contains("\x9") || stringValue.Contains("\xA") || stringValue.Contains("\xB") || stringValue.Contains("\xC") || stringValue.Contains("\xD")) { if (stringValue.Contains("\x6")) { errorMessage = "Species observation field: " + field.ClassIdentifier + " " + field.PropertyIdentifier + " " + " contains character Hex:6."; WebServiceData.LogManager.Log(context, errorMessage, LogType.Error, null); stringValue = stringValue.Replace("\x6", ""); } if (stringValue.Contains("\x7")) { errorMessage = "Species observation field: " + field.ClassIdentifier + " " + field.PropertyIdentifier + " " + " contains character Hex:7."; WebServiceData.LogManager.Log(context, errorMessage, LogType.Error, null); stringValue = stringValue.Replace("\x7", ""); } if (stringValue.Contains("\x8")) { errorMessage = "Species observation field: " + field.ClassIdentifier + " " + field.PropertyIdentifier + " " + " contains character Hex:8."; WebServiceData.LogManager.Log(context, errorMessage, LogType.Error, null); stringValue = stringValue.Replace("\x8", ""); } if (stringValue.Contains("\x9")) { //errorMessage = "Species observation field: " + // field.ClassIdentifier + " " + // field.PropertyIdentifier + " " + // " contains character Hex:9."; //WebServiceData.LogManager.Log(context, errorMessage, LogType.Error, null); stringValue = stringValue.Replace("\x9", ""); } if (stringValue.Contains("\xA")) { //errorMessage = "Species observation field: " + // field.ClassIdentifier + " " + // field.PropertyIdentifier + " " + // " contains character Hex:A."; //WebServiceData.LogManager.Log(context, errorMessage, LogType.Error, null); stringValue = stringValue.Replace("\xA", ""); } if (stringValue.Contains("\xB")) { errorMessage = "Species observation field: " + field.ClassIdentifier + " " + field.PropertyIdentifier + " " + " contains character Hex:B."; WebServiceData.LogManager.Log(context, errorMessage, LogType.Error, null); stringValue = stringValue.Replace("\xB", ""); } if (stringValue.Contains("\xC")) { errorMessage = "Species observation field: " + field.ClassIdentifier + " " + field.PropertyIdentifier + " " + " contains character Hex:C."; WebServiceData.LogManager.Log(context, errorMessage, LogType.Error, null); stringValue = stringValue.Replace("\xC", ""); } if (stringValue.Contains("\xD")) { //errorMessage = "Species observation field: " + // field.ClassIdentifier + " " + // field.PropertyIdentifier + " " + // " contains character Hex:D."; //WebServiceData.LogManager.Log(context, errorMessage, LogType.Error, null); stringValue = stringValue.Replace("\xD", ""); } } return(stringValue); }
/// <summary> /// Get species observation field in Json representation. /// </summary> /// <param name="context">Web service context.</param> /// <param name="json">Species observation in Json format.</param> /// <param name="field">Species observation field.</param> private static void GetFieldJson(WebServiceContext context, StringBuilder json, WebSpeciesObservationField field) { DateTime dateTime; String fieldName; if (json.ToString().Length > 10) { // Not first column. json.Append(", "); } // Add field name. if ((field.ClassIdentifier == "Project") && field.PropertyIdentifier.StartsWith("ProjectParameter")) { // Special handling of species observation project parameters. fieldName = field.PropertyIdentifier; } else { fieldName = field.ClassIdentifier + "_" + field.PropertyIdentifier; } json.Append("\"" + fieldName + "\": "); // Add value. switch (field.Type) { case WebDataType.Boolean: json.Append(field.Value.ToLower()); break; case WebDataType.DateTime: json.Append("\""); json.Append(field.Value); json.Append("\""); // Add date time special fields. dateTime = field.Value.WebParseDateTime(); json.Append(", \"" + fieldName + "_DatePartOnly" + "\": \"" + dateTime.Date.ToString("yyyy-MM-dd") + "\""); json.Append(", \"" + fieldName + "_DayOfMonth" + "\": " + dateTime.Day); json.Append(", \"" + fieldName + "_DayOfYear" + "\": " + dateTime.DayOfYear); json.Append(", \"" + fieldName + "_MonthOfYear" + "\": " + dateTime.Month); json.Append(", \"" + fieldName + "_WeekOfYear" + "\": " + CultureInfo.CurrentCulture.Calendar.GetWeekOfYear(dateTime, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday)); json.Append(", \"" + fieldName + "_Year" + "\": " + dateTime.Year); json.Append(", \"" + fieldName + "_YearAndMonth" + "\": \"" + dateTime.Date.ToString("yyyy-MM") + "\""); json.Append(", \"" + fieldName + "_YearAndWeek" + "\": \"" + string.Format("{0}-{1}", dateTime.Date.ToString("yyyy"), CultureInfo.CurrentCulture.Calendar.GetWeekOfYear(dateTime, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday)) + "\""); break; case WebDataType.Float64: case WebDataType.Int32: case WebDataType.Int64: json.Append(field.Value); break; case WebDataType.String: json.Append("\""); json.Append(GetStringValueJson(context, field)); json.Append("\""); json.Append(", \"" + fieldName + "_Lowercase" + "\": "); json.Append("\""); json.Append(GetStringValueJson(context, field).ToLower()); json.Append("\""); break; default: throw new Exception("Not handled data type, type = " + field.Type); } }