public void UpdateSpeciesFactsNullSpeciesFactsError()
 {
     Data.ArtDatabankenService.SpeciesFactManager.UpdateSpeciesFacts(null, ReferenceManagerTest.GetAReference());
 }
        public void UpdateSpeciesFacts()
        {
            DateTime oldUpdateDate;
            Int32    fieldIndex, oldReferenceId, oldQualityId;

            Data.ArtDatabankenService.Reference        reference;
            Data.ArtDatabankenService.SpeciesFact      changedSpeciesFact;
            Data.ArtDatabankenService.SpeciesFactField field;
            Data.ArtDatabankenService.SpeciesFactList  changedSpeciesFacts, speciesFacts;
            String oldUpdateUser;
            String stringValue;
            UserParameterSelection userParameterSelection;

            // Test delete of species fact.
            using (WebTransaction transaction = new WebTransaction(5))
            {
                // Delete some species facts.
                speciesFacts = GetSomeSpeciesFacts();
                foreach (Data.ArtDatabankenService.SpeciesFact speciesFact in speciesFacts)
                {
                    if (speciesFact.AllowManualUpdate)
                    {
                        for (fieldIndex = 0; fieldIndex < speciesFact.Fields.Count; fieldIndex++)
                        {
                            speciesFact.Fields[fieldIndex].Value = null;
                        }
                    }
                }
                Data.ArtDatabankenService.SpeciesFactManager.UpdateSpeciesFacts(speciesFacts, ReferenceManagerTest.GetAReference());

                // Verify that delete has been done.
                Data.ArtDatabankenService.SpeciesFactManager.UpdateSpeciesFacts(speciesFacts);
                foreach (Data.ArtDatabankenService.SpeciesFact speciesFact in speciesFacts)
                {
                    if (speciesFact.AllowManualUpdate)
                    {
                        Assert.IsTrue(!speciesFact.HasId ||
                                      (speciesFact.MainField.Type.DataType == FactorFieldDataTypeId.Enum));
                    }
                }
            }
            Thread.Sleep(6000);

            // Test creation of species fact.
            using (WebTransaction transaction = new WebTransaction(5))
            {
                // Create some species facts.
                changedSpeciesFacts    = new Data.ArtDatabankenService.SpeciesFactList();
                userParameterSelection = new UserParameterSelection();
                userParameterSelection.Taxa.Merge(ArtDatabanken.Data.ArtDatabankenService.TaxonManager.GetTaxa(TaxonManagerTest.GetTaxaIds(), TaxonInformationType.Basic));
                userParameterSelection.Factors.Merge(Data.ArtDatabankenService.FactorManager.GetFactors(FactorManagerTest.GetFactorIds()));
                speciesFacts = Data.ArtDatabankenService.SpeciesFactManager.GetSpeciesFactsByUserParameterSelection(userParameterSelection);
                foreach (Data.ArtDatabankenService.SpeciesFact speciesFact in speciesFacts)
                {
                    if (speciesFact.AllowManualUpdate &&
                        !speciesFact.HasId)
                    {
                        switch (speciesFact.MainField.Type.DataType)
                        {
                        case FactorFieldDataTypeId.Boolean:
                            speciesFact.MainField.Value = true;
                            break;

                        case FactorFieldDataTypeId.Double:
                            speciesFact.MainField.Value = 1;
                            break;

                        case FactorFieldDataTypeId.Int32:
                            speciesFact.MainField.Value = 1;
                            break;

                        case FactorFieldDataTypeId.String:
                            speciesFact.MainField.Value = "1";
                            break;

                        default:
                            continue;
                        }
                        changedSpeciesFacts.Add(speciesFact);
                    }
                }
                Data.ArtDatabankenService.SpeciesFactManager.UpdateSpeciesFacts(speciesFacts, ReferenceManagerTest.GetAReference());

                // Verify that creation of species facts has been done.
                Data.ArtDatabankenService.SpeciesFactManager.UpdateSpeciesFacts(speciesFacts);
                foreach (Data.ArtDatabankenService.SpeciesFact speciesFact in speciesFacts)
                {
                    if (changedSpeciesFacts.Exists(speciesFact))
                    {
                        Assert.IsTrue(speciesFact.HasId);
                        Assert.IsTrue(speciesFact.HasUpdateDate);
                        Assert.IsTrue(speciesFact.UpdateUserFullName.IsNotEmpty());
                        Assert.AreEqual(1, Int32.Parse(speciesFact.MainField.Value.ToString()));
                    }
                }
            }
            Thread.Sleep(6000);

            // Test update of data.
            using (WebTransaction transaction = new WebTransaction(5))
            {
                // Get species fact.
                changedSpeciesFacts = new Data.ArtDatabankenService.SpeciesFactList();
                foreach (Data.ArtDatabankenService.SpeciesFact speciesFact in GetSomeSpeciesFacts())
                {
                    if (speciesFact.AllowManualUpdate &&
                        speciesFact.HasId)
                    {
                        changedSpeciesFacts.Add(speciesFact);
                        break;
                    }
                }
                changedSpeciesFact = changedSpeciesFacts[0];

                // Test change of reference id.
                oldReferenceId = changedSpeciesFact.Reference.Id;
                oldUpdateUser  = changedSpeciesFact.UpdateUserFullName;
                reference      = Data.ArtDatabankenService.ReferenceManager.GetReferences()[3];
                changedSpeciesFact.Reference = reference;
                Data.ArtDatabankenService.SpeciesFactManager.UpdateSpeciesFacts(changedSpeciesFacts, ReferenceManagerTest.GetAReference());
                Data.ArtDatabankenService.SpeciesFactManager.UpdateSpeciesFacts(changedSpeciesFacts);
                Assert.AreNotEqual(oldReferenceId, changedSpeciesFact.Reference.Id);
                Assert.AreEqual(reference.Id, changedSpeciesFact.Reference.Id);

                // Test change of update date.
                oldUpdateDate = changedSpeciesFact.UpdateDate;
                changedSpeciesFact.Reference = Data.ArtDatabankenService.ReferenceManager.GetReferences()[4];
                Data.ArtDatabankenService.SpeciesFactManager.UpdateSpeciesFacts(changedSpeciesFacts, ReferenceManagerTest.GetAReference());
                Data.ArtDatabankenService.SpeciesFactManager.UpdateSpeciesFacts(changedSpeciesFacts);
                Assert.IsTrue(oldUpdateDate <= changedSpeciesFact.UpdateDate);

                // Test change of update user.
                Assert.AreNotEqual(oldUpdateUser, changedSpeciesFact.UpdateUserFullName);

                // Test change of field values.
                field       = changedSpeciesFact.Field5;
                stringValue = "4232";
                Assert.AreNotEqual(stringValue, field.Value.ToString());
                field.Value = stringValue;
                Data.ArtDatabankenService.SpeciesFactManager.UpdateSpeciesFacts(changedSpeciesFacts, ReferenceManagerTest.GetAReference());
                Data.ArtDatabankenService.SpeciesFactManager.UpdateSpeciesFacts(changedSpeciesFacts);
                Assert.IsTrue(field.HasValue);
                Assert.AreEqual(stringValue, field.Value.ToString());

                // Test change of quality.
                oldQualityId = changedSpeciesFact.Quality.Id;
                changedSpeciesFact.Quality = Data.ArtDatabankenService.SpeciesFactManager.GetSpeciesFactQualities()[5];
                Data.ArtDatabankenService.SpeciesFactManager.UpdateSpeciesFacts(changedSpeciesFacts, ReferenceManagerTest.GetAReference());
                Data.ArtDatabankenService.SpeciesFactManager.UpdateSpeciesFacts(changedSpeciesFacts);
                Assert.AreNotEqual(oldQualityId, changedSpeciesFact.Quality.Id);
                Assert.AreEqual(Data.ArtDatabankenService.SpeciesFactManager.GetSpeciesFactQualities()[5].Id, changedSpeciesFact.Quality.Id);
            }
        }