/// <summary>
        /// ProcessSolicit
        /// </summary>
        /// <param name="requestId"></param>
        public virtual void ProcessSolicit(string requestId)
        {
            ProcessQuerySolicitInit(requestId, false);

            CheckForPendingSubmissions(Submission_Type.Delete);

            string recordId = SetPendingSubmission(Submission_Type.Delete);

            try
            {
                WQXDeleteDataType data = GetDeleteData();

                string transactionId = GenerateSubmissionFileAndSubmit(Submission_Type.Delete, data);

                UpdatePendingSubmissionInfo(recordId, transactionId);
            }
            catch (Exception)
            {
                SetSubmissionFailed(recordId);
                throw;
            }
        }
Beispiel #2
0
        protected virtual WQXDataType GetWQXData(string transactionId, string docId,
                                                 out Windsor.Node2008.WNOSPlugin.WQX1XsdOrm.WQXDataType data1,
                                                 out WQXDeleteDataType deleteData, out string attachmentsFolderPath)
        {
            WQXDataType data = null;

            data1      = null;
            deleteData = null;
            string tempXmlFilePath = _settingsProvider.NewTempFilePath();

            attachmentsFolderPath = null;
            string tempFolder = null;

            try
            {
                IHeaderDocumentHelper headerDocumentHelper;
                GetServiceImplementation(out headerDocumentHelper);

                AppendAuditLogEvent("Getting data for document with id \"{0}\"", docId);
                Document document = _documentManager.GetDocument(transactionId, docId, true);
                if (document.IsZipFile)
                {
                    tempFolder = _settingsProvider.CreateNewTempFolderPath();
                    AppendAuditLogEvent("Decompressing document to temporary folder");
                    _compressionHelper.UncompressDirectory(document.Content, tempFolder);
                    string[] xmlFiles = Directory.GetFiles(tempFolder, "*.xml");
                    if (xmlFiles.Length == 0)
                    {
                        throw new ArgException("Failed to locate a WQX xml file in the WQX data");
                    }
                    else if (xmlFiles.Length > 1)
                    {
                        throw new ArgException("More than one xml file was found in the WQX data");
                    }
                    tempXmlFilePath = xmlFiles[0];
                }
                else
                {
                    AppendAuditLogEvent("Writing document data to temporary file");
                    File.WriteAllBytes(tempXmlFilePath, document.Content);
                }

                XmlElement loadElement = null;
                try
                {
                    AppendAuditLogEvent("Attempting to load document with Exchange Header");
                    headerDocumentHelper.Load(tempXmlFilePath);
                    loadElement = headerDocumentHelper.GetPayload("Update-Insert");
                    if (loadElement == null)
                    {
                        loadElement = headerDocumentHelper.GetPayload("Delete");
                        if (loadElement == null)
                        {
                            throw new ArgumentException("The submitted document does not contain an \"Update-Insert\" or \"Delete\" payload");
                        }
                    }
                }
                catch (Exception)
                {
                    AppendAuditLogEvent("Document does not contain an Exchange Header");
                    // Assume, for now, that document does not have a header
                }

                AppendAuditLogEvent("Deserializing document data to WQX data");
                if (loadElement != null)
                {
                    try
                    {
                        data = _serializationHelper.Deserialize <WQXDataType>(loadElement);
                        attachmentsFolderPath = tempFolder;
                    }
                    catch (Exception)
                    {
                        AppendAuditLogEvent("Failed to deserialize WQX v2, trying WQX v1 ...");
                        try
                        {
                            data1 = _serializationHelper.Deserialize <Windsor.Node2008.WNOSPlugin.WQX1XsdOrm.WQXDataType>(loadElement);
                        }
                        catch (Exception)
                        {
                            AppendAuditLogEvent("Failed to deserialize WQX v1, trying WQX v2 Delete data ...");
                            deleteData = _serializationHelper.Deserialize <WQXDeleteDataType>(loadElement);
                        }
                    }
                }
                else
                {
                    try
                    {
                        data = _serializationHelper.Deserialize <WQXDataType>(tempXmlFilePath);
                        attachmentsFolderPath = tempFolder;
                    }
                    catch (Exception)
                    {
                        AppendAuditLogEvent("Failed to deserialize WQX v2, trying WQX v1 ...");
                        try
                        {
                            data1 = _serializationHelper.Deserialize <Windsor.Node2008.WNOSPlugin.WQX1XsdOrm.WQXDataType>(tempXmlFilePath);
                        }
                        catch (Exception)
                        {
                            AppendAuditLogEvent("Failed to deserialize WQX v1, trying WQX v2 Delete data ...");
                            deleteData = _serializationHelper.Deserialize <WQXDeleteDataType>(tempXmlFilePath);
                        }
                    }
                }
            }
            catch (Exception)
            {
                FileUtils.SafeDeleteAllFilesAndFoldersInFolder(tempFolder);
                throw;
            }
            finally
            {
                FileUtils.SafeDeleteFile(tempXmlFilePath);
            }
            return(data);
        }
Beispiel #3
0
        protected virtual void ProcessSubmitDocument(string transactionId, string docId)
        {
            string attachmentsFolderPath = null;

            try
            {
                WQXDataType       data       = null;
                WQXDeleteDataType deleteData = null;
                Windsor.Node2008.WNOSPlugin.WQX1XsdOrm.WQXDataType data1 = null;

                data = GetWQXData(transactionId, docId, out data1, out deleteData, out attachmentsFolderPath);

                AppendAuditLogEvent("Storing WQX data into database");

                string orgId;
                object addObject;

                if (data != null)
                {
                    if (data.Organization == null)
                    {
                        throw new InvalidDataException("Deserialized WQX data does not contain an Organization element");
                    }
                    if ((data.Organization.OrganizationDescription == null) ||
                        string.IsNullOrEmpty(data.Organization.OrganizationDescription.OrganizationIdentifier))
                    {
                        throw new InvalidDataException("WQX data does not contain an OrganizationIdentifier element");
                    }
                    orgId     = data.Organization.OrganizationDescription.OrganizationIdentifier;
                    addObject = data;

                    AppendAuditLogEvent("WQX data contains {0} Projects, {1} Activities, {2} Biological Habitat Indexes, {3} Monitoring Locations, and {4} Activity Groups for Organization Id \"{5}\"",
                                        CollectionUtils.Count(data.Organization.Project).ToString(),
                                        CollectionUtils.Count(data.Organization.Activity).ToString(),
                                        CollectionUtils.Count(data.Organization.BiologicalHabitatIndex).ToString(),
                                        CollectionUtils.Count(data.Organization.MonitoringLocation).ToString(),
                                        CollectionUtils.Count(data.Organization.ActivityGroup).ToString(),
                                        orgId);
                }
                else if (data1 != null)
                {
                    if (data1.Organization == null)
                    {
                        throw new InvalidDataException("Deserialized WQX data does not contain an Organization element");
                    }
                    if ((data1.Organization.OrganizationDescription == null) ||
                        string.IsNullOrEmpty(data1.Organization.OrganizationDescription.OrganizationIdentifier))
                    {
                        throw new InvalidDataException("WQX data does not contain an OrganizationIdentifier element");
                    }

                    orgId     = data1.Organization.OrganizationDescription.OrganizationIdentifier;
                    addObject = data1;

                    AppendAuditLogEvent("WQX data contains {0} Projects, {1} Activities, {2} Biological Habitat Indexes, {3} Monitoring Locations, and {4} Activity Groups for Organization Id \"{5}\"",
                                        CollectionUtils.Count(data1.Organization.Project).ToString(),
                                        CollectionUtils.Count(data1.Organization.Activity).ToString(),
                                        "0",
                                        CollectionUtils.Count(data1.Organization.MonitoringLocation).ToString(),
                                        CollectionUtils.Count(data1.Organization.ActivityGroup).ToString(),
                                        data1.Organization.OrganizationDescription.OrganizationIdentifier);
                }
                else
                {
                    if (deleteData.OrganizationDelete == null)
                    {
                        throw new InvalidDataException("Deserialized WQX data does not contain an Organization element");
                    }
                    if (string.IsNullOrEmpty(deleteData.OrganizationDelete.OrganizationIdentifier))
                    {
                        throw new InvalidDataException("WQX data does not contain an OrganizationIdentifier element");
                    }

                    orgId     = deleteData.OrganizationDelete.OrganizationIdentifier;
                    addObject = deleteData;

                    AppendAuditLogEvent("WQX delete data contains {0} Projects, {1} Activities, {2} Biological Habitat Indexes, {3} Monitoring Locations, and {4} Activity Groups for Organization Id \"{5}\"",
                                        CollectionUtils.Count(deleteData.OrganizationDelete.ProjectIdentifier).ToString(),
                                        CollectionUtils.Count(deleteData.OrganizationDelete.ActivityIdentifier).ToString(),
                                        CollectionUtils.Count(deleteData.OrganizationDelete.IndexIdentifier).ToString(),
                                        CollectionUtils.Count(deleteData.OrganizationDelete.MonitoringLocationIdentifier).ToString(),
                                        CollectionUtils.Count(deleteData.OrganizationDelete.ActivityGroupIdentifier).ToString(),
                                        deleteData.OrganizationDelete.OrganizationIdentifier);
                }
                if (_authorizedWqxUsers != null)
                {
                    AppendAuditLogEvent("Validating that User \"{0}\" is authorized to submit WQX data for Organization Id \"{1}\" ...", _submitUsername, orgId);
                    List <string> usernames = null;
                    if (_authorizedWqxUsers.TryGetValue(orgId.ToUpper(), out usernames))
                    {
                        if (!usernames.Contains("*") && !usernames.Contains(_submitUsername.ToUpper()))
                        {
                            string orgName = _settingsProvider.NodeOrganizationName;
                            throw new UnauthorizedAccessException(string.Format("The User \"{0}\" is not authorized to provide data to the {1} for the Organization ID \"{2}.\"  If you feel you have received this message in error, please contact the {1} for further assistance.",
                                                                                _submitUsername, orgName, orgId));
                        }
                    }
                    else if (_authorizedWqxUsers.TryGetValue("*", out usernames))
                    {
                        if (!usernames.Contains("*") && !usernames.Contains(_submitUsername.ToUpper()))
                        {
                            string orgName = _settingsProvider.NodeOrganizationName;
                            throw new UnauthorizedAccessException(string.Format("The User \"{0}\" is not authorized to provide data to the {1} for the Organization ID \"{2}.\"  If you feel you have received this message in error, please contact the {1} for further assistance.",
                                                                                _submitUsername, orgName, orgId));
                        }
                    }
                    else
                    {
                        string orgName = _settingsProvider.NodeOrganizationName;
                        throw new UnauthorizedAccessException(string.Format("Organization ID \"{0}\" has not been authorized to provide data to the {1}.  If you feel you have received this message in error, please contact the {1} for further assistance.",
                                                                            orgId, orgName));
                    }
                }
                _baseDao.TransactionTemplate.Execute(delegate
                {
                    if (_deleteExistingDataBeforeInsert)
                    {
                        AppendAuditLogEvent("Deleting existing WQX data from the data store for organization \"{0}\" ...", orgId);
                        int numRowsDeleted = 0;

                        string groupWhere = string.Format("PARENTID IN (SELECT RECORDID FROM WQX_ORGANIZATION WHERE UPPER(ORGID) = UPPER('{0}'))",
                                                          orgId);
                        _baseDao.DoSimpleDelete("WQX_ACTIVITYGROUP", groupWhere, null);
                        numRowsDeleted = _baseDao.DoSimpleDelete("WQX_ORGANIZATION", "ORGID", orgId);

                        if (numRowsDeleted > 0)
                        {
                            AppendAuditLogEvent("Deleted {0} existing WQX data rows from the data store for organization \"{1}\"",
                                                numRowsDeleted.ToString(), orgId);
                        }
                        else
                        {
                            AppendAuditLogEvent("Did not find any existing WQX data to delete from the data store for organization \"{0}\"",
                                                orgId);
                        }
                    }
                    AppendAuditLogEvent("Storing WQX data for organization \"{0}\" into data store ...",
                                        orgId);
                    Dictionary <string, int> tableRowCounts = _objectsToDatabase.SaveToDatabase(addObject, _baseDao);

                    if (attachmentsFolderPath != null)
                    {
                        DatabaseHelper.StoreAttachmentFilesFromFolder(_objectsToDatabase, _baseDao, data.Organization, attachmentsFolderPath);
                    }

                    string recordId = ReflectionUtils.GetFieldOrPropertyValueByName <string>(addObject, "RecordId");

                    AppendAuditLogEvent("Stored WQX data content with organization primary key \"{0}\" into data store with the following table row counts: {1}",
                                        recordId, CreateTableRowCountsString(tableRowCounts));
                    return(null);
                });
            }
            catch (Exception e)
            {
                AppendAuditLogEvent("Failed to process document with id \"{0}.\"  EXCEPTION: {1}",
                                    docId.ToString(), ExceptionUtils.ToShortString(e));
                throw;
            }
            finally
            {
                FileUtils.SafeDeleteAllFilesAndFoldersInFolder(attachmentsFolderPath);
            }
        }
        protected override WQXDataType GetWQXData(string transactionId, string docId,
                                                  out Windsor.Node2008.WNOSPlugin.WQX1XsdOrm.WQXDataType data1,
                                                  out WQXDeleteDataType deleteData, out string attachmentsFolderPath)
        {
            WQXDataType data = null;

            data1                 = null;
            deleteData            = null;
            attachmentsFolderPath = null;
            string tempFolderPath = Path.Combine(_settingsProvider.TempFolderPath, Guid.NewGuid().ToString());

            try
            {
                AppendAuditLogEvent("Extracting contents of document with id \"{0}\" to temporary folder", docId);
                _compressionHelper.UncompressDirectory(_documentManager.GetContent(transactionId, docId), tempFolderPath);

                string projectsFile, monitoringFile, resultsFile;
                GetFlatFiles(tempFolderPath, out projectsFile, out monitoringFile, out resultsFile);

                AppendAuditLogEvent("Generating WQX data from flat files for Organization Id \"{0}\" and Name \"{1}\"",
                                    _organizationId, _organizationName);

                Dictionary <string, MonitoringLocationDataType> monitoringLocations =
                    WQXFlatFileParser.ParseMonitoringLocations(monitoringFile);
                if (CollectionUtils.IsNullOrEmpty(monitoringLocations))
                {
                    throw new InvalidDataException(string.Format("The document does not contain any monitoring locations inside the flat file"));
                }

                Dictionary <string, ProjectDataType> projects =
                    WQXFlatFileParser.ParseProjects(projectsFile);
                if (CollectionUtils.IsNullOrEmpty(projects))
                {
                    throw new InvalidDataException(string.Format("The document does not contain any projects inside the flat file"));
                }

                Dictionary <string, ActivityDataType> activities =
                    WQXFlatFileParser.ParseResults(resultsFile, projects, monitoringLocations, _resultAnalyticalMethodLookups,
                                                   _sampleCollectionMethodLookups, false);

                data = new WQXDataType();
                data.Organization = new OrganizationDataType();
                data.Organization.OrganizationDescription = new OrganizationDescriptionDataType();
                data.Organization.OrganizationDescription.OrganizationIdentifier = _organizationId;
                data.Organization.OrganizationDescription.OrganizationFormalName = _organizationName;

                if (!CollectionUtils.IsNullOrEmpty(monitoringLocations))
                {
                    data.Organization.MonitoringLocation = new List <MonitoringLocationDataType>(monitoringLocations.Values).ToArray();
                }
                if (!CollectionUtils.IsNullOrEmpty(projects))
                {
                    data.Organization.Project = new List <ProjectDataType>(projects.Values).ToArray();
                }
                if (!CollectionUtils.IsNullOrEmpty(activities))
                {
                    data.Organization.Activity = new List <ActivityDataType>(activities.Values).ToArray();
                }
                SaveDataFileToTransaction(transactionId, data);
            }
            finally
            {
                FileUtils.SafeDeleteDirectory(tempFolderPath);
            }
            return(data);
        }