/// <summary> /// Get specified species observation field. /// </summary> /// <param name="speciesObservationFields">Species observation fields.</param> /// <param name="speciesObservationClass">Web service request context.</param> /// <param name="speciesObservationProperty">Species observation search criteria.</param> /// <returns>Specified species observation field.</returns> public static WebSpeciesObservationField GetField(this List <WebSpeciesObservationField> speciesObservationFields, SpeciesObservationClassId speciesObservationClass, SpeciesObservationPropertyId speciesObservationProperty) { return(speciesObservationFields.GetField(speciesObservationClass.ToString(), speciesObservationProperty.ToString())); }
/// <summary> /// Merge a species observation field specification into the list. /// </summary> /// <param name='fieldSpecifications'>Species observation field specifiactions.</param> /// <param name='classId'>Species observation class.</param> /// <param name='propertyId'>Species observation property.</param> public static void Merge(this List <WebSpeciesObservationFieldSpecification> fieldSpecifications, SpeciesObservationClassId classId, SpeciesObservationPropertyId propertyId) { WebSpeciesObservationFieldSpecification fieldSpecification; fieldSpecification = new WebSpeciesObservationFieldSpecification(); fieldSpecification.Class = new WebSpeciesObservationClass(); fieldSpecification.Class.Id = classId; fieldSpecification.Property = new WebSpeciesObservationProperty(); fieldSpecification.Property.Id = propertyId; Merge(fieldSpecifications, fieldSpecification); }
/// <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> /// The process observation. /// </summary> /// <param name="webData"> /// The web data. /// </param> /// <param name="mappings"> /// The mappings. /// </param> /// <param name="context"> /// The context. /// </param> /// <returns> /// The harvestSpeciesObservation.<see cref="HarvestSpeciesObservation"/>. /// </returns> /// <exception cref="ArgumentNullException">If some properties where not found. /// </exception> public virtual HarvestSpeciesObservation ProcessObservation(WebData webData, List <HarvestMapping> mappings, WebServiceContext context) { // Create a dictionary object from the WebData object (Not cachable, needs to be done for each row) // Can be a problem if same name exists in more than one class... Dictionary <string, WebDataField> dictionaryWebData = webData.DataFields.ToDictionary(row => row.Name.ToLower(), row => row); WebLocale webLocale = GetWebLocale(); HarvestSpeciesObservation harvestSpeciesObservation = new HarvestSpeciesObservation(); harvestSpeciesObservation.Fields = new List <HarvestSpeciesObservationField>(); if (mappings.IsNotNull()) { // Loop through the mappings + create objects for every field foreach (HarvestMapping mapping in mappings) { String mapProperty; if (!ReadElement(mapping.Property, out mapProperty)) { throw new ArgumentNullException(String.Format("Error in mapping. Element <Property>, {0}, not found.", mapping.Property)); } SpeciesObservationPropertyId speciesObservationPropertyId = SpeciesObservationPropertyId.None; // This validation should not be performed on fields of ProjectParameter type if (mapping.GUID == null || !mapping.GUID.Contains("projectparameter")) { if (!Enum.TryParse(mapProperty, true, out speciesObservationPropertyId)) { throw new ArgumentNullException(String.Format("Property value: {0} not a valid property.", mapProperty)); } } Boolean isMandatory; String isMandatoryString; if (!ReadElement(mapping.Mandatory.ToString(), out isMandatoryString)) { throw new ArgumentNullException(String.Format("Error in mapping. Element <IsMandatoryFromProvider>, {0}, not found.", mapping.IsMandatoryFromProvider)); } isMandatory = mapping.Mandatory; String mapClass; if (!ReadElement(mapping.Class, out mapClass)) { throw new ArgumentNullException(String.Format("Error in mapping. Element <Class>, {0}, not found.", mapping.Class)); } SpeciesObservationClassId speciesObservationClassId; if (!Enum.TryParse(mapClass, true, out speciesObservationClassId)) { throw new ArgumentNullException(String.Format("Class value: {0} not a valid class.", mapClass)); } String mapType; if (!ReadElement(mapping.Type, out mapType)) { throw new ArgumentNullException(String.Format("Error in mapping. Element <Type>, {0}, not found.", mapping.Type)); } WebDataType webDataType; if (!Enum.TryParse(mapType, true, out webDataType)) { throw new ArgumentNullException(String.Format("Type value: {0} not a valid type.", mapType)); } String mapName; if (mapping.IsProjectParameter()) { mapName = mapping.PropertyIdentifier; } else { ReadElement(mapping.Name, out mapName); } String mapMethod; ReadElement(mapping.Method, out mapMethod); String mapDefault; ReadElement(mapping.Default, out mapDefault); String fieldValue = String.Empty; String fieldUnit = null; if (mapName.IsNotEmpty()) { try { if (dictionaryWebData.ContainsKey(mapName.ToLower())) { fieldValue = dictionaryWebData[mapName.ToLower()].Value; fieldUnit = ReplaceInvalidCharacters(dictionaryWebData[mapName.ToLower()].Unit); if (webDataType == WebDataType.String) { fieldValue = ReplaceInvalidCharacters(fieldValue); } } } catch (KeyNotFoundException) { throw new ArgumentNullException(String.Format("Field:{0} not found.", mapName)); } } if (mapMethod.IsNotEmpty()) { fieldValue = MethodWrapper(mapMethod, dictionaryWebData, context, mapping); } if (fieldValue.IsNull() || fieldValue.IsEmpty()) { // field is empty - set to default value ? if (mapDefault.IsNotEmpty()) { fieldValue = mapDefault; } } if (fieldValue.IsNotEmpty() || isMandatory) { HarvestSpeciesObservationField harvestSpeciesObservationField = new HarvestSpeciesObservationField(); harvestSpeciesObservationField.Class = new WebSpeciesObservationClass(speciesObservationClassId); // The type SpeciesObservationPropertyId.None indicates the use of the Identifier instead harvestSpeciesObservationField.Property = new WebSpeciesObservationProperty(); if (mapping.IsProjectParameter()) { harvestSpeciesObservationField.Property.Id = SpeciesObservationPropertyId.None; harvestSpeciesObservationField.Property.Identifier = mapping.PropertyIdentifier; } else { harvestSpeciesObservationField.Property.Id = speciesObservationPropertyId; if (speciesObservationPropertyId == SpeciesObservationPropertyId.None) { harvestSpeciesObservationField.Property.Identifier = mapping.Name; } } harvestSpeciesObservationField.Type = webDataType; harvestSpeciesObservationField.Unit = fieldUnit; harvestSpeciesObservationField.Value = fieldValue.CheckInjection(); harvestSpeciesObservationField.Locale = webLocale; harvestSpeciesObservationField.IsDarwinCore = mapping.IsDarwinCore; harvestSpeciesObservationField.IsSearchable = mapping.IsSearchable; harvestSpeciesObservationField.IsMandatory = isMandatory; // måste med till nästa steg harvestSpeciesObservationField.IsMandatoryFromProvider = mapping.IsMandatoryFromProvider; harvestSpeciesObservationField.IsObtainedFromProvider = mapping.IsObtainedFromProvider; harvestSpeciesObservationField.PersistedInTable = mapping.PersistedInTable; // add the field to the observation harvestSpeciesObservation.Fields.Add(harvestSpeciesObservationField); } } } // return one observation return(harvestSpeciesObservation); }
/// <summary> /// /// </summary> /// <param name="speciesObservationPropertyId"></param> public WebSpeciesObservationProperty(SpeciesObservationPropertyId speciesObservationPropertyId) { Id = speciesObservationPropertyId; }
/// <summary> /// Get name of specified field in the database. /// </summary> /// <param name="classId">Species observation class.</param> /// <param name="propertyId">Species observation property.</param> /// <returns>Name of specified field in the database.</returns> public String GetFieldName(SpeciesObservationClassId classId, SpeciesObservationPropertyId propertyId) { return(classId + "_" + propertyId); }