public static string GetWqxOrgCountsString(OrganizationDataType org) { StringBuilder sb = new StringBuilder(); sb.AppendFormat("WQX data for organization \"{0}\" contains ", org.OrganizationDescription.OrganizationFormalName); int collectionActivityCount = CollectionUtils.Count(org.Activity); int biologicalHabitatCount = CollectionUtils.Count(org.BiologicalHabitatIndex); int addCount = 0, commaIndex = -1; AppendCountString(sb, collectionActivityCount, "Collection Activities", null, ref addCount, ref commaIndex); AppendCountString(sb, biologicalHabitatCount, "Biological Habitat Indices", null, ref addCount, ref commaIndex); if (addCount == 0) { sb.Append("no collection activites or biological habitat indices"); } else if (addCount > 1) { sb.Replace(", ", " and ", commaIndex, sb.Length - commaIndex); } return(sb.ToString()); }
public static string StoreWqxDataToDatabase(IAppendAuditLogEvent appendAuditLogEvent, WQXDataType data, IObjectsToDatabase objectsToDatabase, SpringBaseDao baseDao, string attachmentsFolderPath) { appendAuditLogEvent.AppendAuditLogEvent("Storing WQX data into the database ..."); string countsString = string.Empty; baseDao.TransactionTemplate.Execute(delegate(Spring.Transaction.ITransactionStatus status) { OrganizationDataType org = data.Organization; appendAuditLogEvent.AppendAuditLogEvent("Storing WQX data into database for organization \"{0}\" ...", org.OrganizationDescription.OrganizationFormalName); appendAuditLogEvent.AppendAuditLogEvent(DatabaseHelper.GetWqxOrgCountsString(org)); Dictionary <string, int> insertCounts = objectsToDatabase.SaveToDatabase(data, baseDao); DatabaseHelper.StoreAttachmentFilesFromFolder(objectsToDatabase, baseDao, data.Organization, attachmentsFolderPath); countsString += string.Format("Stored WQX data for organization \"{0}\" into the database with the following table row counts:{1}{2}", org.OrganizationDescription.OrganizationFormalName, Environment.NewLine, CreateTableRowCountsString(insertCounts)); appendAuditLogEvent.AppendAuditLogEvent(countsString); return(null); }); return(countsString); }
public static void StoreAttachmentFilesFromFolder(IObjectsToDatabase objectsToDatabase, SpringBaseDao baseDao, OrganizationDataType data, string folderPath) { if (data.Activity != null) { foreach (ActivityDataType activity in data.Activity) { if (activity.AttachedBinaryObject != null) { foreach (AttachedBinaryObjectDataType attachment in activity.AttachedBinaryObject) { if (string.IsNullOrEmpty(attachment.BinaryObjectFileName)) { throw new ArgException("An attachment for the activity with id \"{0}\" does not have an attachment name.", activity.ActivityDescription.ActivityIdentifier); } string filePath = Path.Combine(folderPath, attachment.BinaryObjectFileName); if (!File.Exists(filePath)) { throw new ArgException("Failed to locate an attachment with the name \"{0}\" for the activity with id \"{1}\" in the temporary folder: \"{2}\"", attachment.BinaryObjectFileName, activity.ActivityDescription.ActivityIdentifier, filePath); } byte[] content = CompressFile(folderPath, filePath); int updateCount = baseDao.DoJDBCExecuteNonQuery("UPDATE WQX_ACTATTACHEDBINARYOBJECT SET BINARYOBJECTCONTENT = ? WHERE PARENTID = ? AND BINARYOBJECTFILE = ?", content, activity.RecordId, attachment.BinaryObjectFileName); if (updateCount == 0) { throw new ArgException("Failed to update the content for an attachment with the name \"{0}\" for the activity with id \"{1}\"", attachment.BinaryObjectFileName, activity.ActivityDescription.ActivityIdentifier); } } } } } if (data.MonitoringLocation != null) { foreach (MonitoringLocationDataType monitoringLocation in data.MonitoringLocation) { if (monitoringLocation.AttachedBinaryObject != null) { foreach (AttachedBinaryObjectDataType attachment in monitoringLocation.AttachedBinaryObject) { if (string.IsNullOrEmpty(attachment.BinaryObjectFileName)) { throw new ArgException("An attachment for the monitoring location \"{0}\" with id \"{1}\" does not have an attachment name.", monitoringLocation.WellInformation, monitoringLocation.MonitoringLocationIdentity.MonitoringLocationIdentifier); } string filePath = Path.Combine(folderPath, attachment.BinaryObjectFileName); if (!File.Exists(filePath)) { throw new ArgException("Failed to locate an attachment with the name \"{0}\" for the monitoring location \"{1}\" with id \"{2}\" in the temporary folder: \"{3}\"", attachment.BinaryObjectFileName, monitoringLocation.WellInformation, monitoringLocation.MonitoringLocationIdentity.MonitoringLocationIdentifier, filePath); } byte[] content = CompressFile(folderPath, filePath); int updateCount = baseDao.DoJDBCExecuteNonQuery("UPDATE WQX_MONLOCATTACHEDBINARYOBJECT SET BINARYOBJECTCONTENT = ? WHERE PARENTID = ? AND BINARYOBJECTFILE = ?", content, monitoringLocation.RecordId, attachment.BinaryObjectFileName); if (updateCount == 0) { throw new ArgException("Failed to update the content for an attachment with the name \"{0}\" for the monitoring location with id \"{1}\"", attachment.BinaryObjectFileName, monitoringLocation.MonitoringLocationIdentity.MonitoringLocationIdentifier); } } } } } if (data.Project != null) { foreach (ProjectDataType project in data.Project) { if (project.AttachedBinaryObject != null) { foreach (AttachedBinaryObjectDataType attachment in project.AttachedBinaryObject) { if (string.IsNullOrEmpty(attachment.BinaryObjectFileName)) { throw new ArgException("An attachment for the project \"{0}\" with id \"{1}\" does not have an attachment name.", project.ProjectDescriptionText, project.ProjectIdentifier); } string filePath = Path.Combine(folderPath, attachment.BinaryObjectFileName); if (!File.Exists(filePath)) { throw new ArgException("Failed to locate an attachment with the name \"{0}\" for the project \"{1}\" with id \"{2}\" in the temporary folder: \"{3}\"", attachment.BinaryObjectFileName, project.ProjectDescriptionText, project.ProjectIdentifier, filePath); } byte[] content = CompressFile(folderPath, filePath); int updateCount = baseDao.DoJDBCExecuteNonQuery("UPDATE WQX_PROJATTACHEDBINARYOBJECT SET BINARYOBJECTCONTENT = ? WHERE PARENTID = ? AND BINARYOBJECTFILE = ?", content, project.RecordId, attachment.BinaryObjectFileName); if (updateCount == 0) { throw new ArgException("Failed to update the content for an attachment with the name \"{0}\" for the project with id \"{1}\"", attachment.BinaryObjectFileName, project.ProjectIdentifier); } } } } } }
public static void WriteAttachmentFilesToFolder(SpringBaseDao baseDao, WQXDataType data, string folderPath) { OrganizationDataType org = data.Organization; if (org.Activity != null) { CollectionUtils.ForEach(org.Activity, delegate(ActivityDataType activity) { if (activity.AttachedBinaryObject != null) { CollectionUtils.ForEach(activity.AttachedBinaryObject, delegate(ActivityAttachedBinaryObjectDataType attachment) { if (string.IsNullOrEmpty(attachment.BinaryObjectFileName)) { throw new ArgException("An attachment for the activity with id \"{0}\" does not have an attachment name.", activity.ActivityDescription.ActivityIdentifier); } string filePath = Path.Combine(folderPath, attachment.BinaryObjectFileName); if (File.Exists(filePath)) { throw new ArgException("Failed to write the attachment \"{0}\" for the activity with id \"{1}\" because another file with the same name already exists in the temporary folder: \"{2}\"", attachment.BinaryObjectFileName, activity.ActivityDescription.ActivityIdentifier, filePath); } byte[] content = null; baseDao.DoJDBCQueryWithRowCallbackDelegate("SELECT BINARYOBJECTCONTENT FROM WQX_ACTATTACHEDBINARYOBJECT WHERE PARENTID = ? AND BINARYOBJECTFILE = ?", delegate(IDataReader dataReader) { content = (byte[])dataReader.GetValue(0); }, activity.ActivityDescription.ActivityIdentifier, attachment.BinaryObjectFileName); if (content == null) { throw new ArgException("Failed to load attachment content for the file \"{0}\" from the database for the activity with id \"{1}\"", attachment.BinaryObjectFileName, activity.ActivityDescription.ActivityIdentifier); } UncompressFile(content, filePath); }); } }); } ; if (org.Project != null) { CollectionUtils.ForEach(org.Project, delegate(ProjectDataType project) { if (project.AttachedBinaryObject != null) { CollectionUtils.ForEach(project.AttachedBinaryObject, delegate(ProjectAttachedBinaryObjectDataType attachment) { if (string.IsNullOrEmpty(attachment.BinaryObjectFileName)) { throw new ArgException("An attachment for the project \"{0}\" with id \"{1}\" does not have an attachment name.", project.ProjectName, project.ProjectIdentifier); } string filePath = Path.Combine(folderPath, attachment.BinaryObjectFileName); if (File.Exists(filePath)) { throw new ArgException("Failed to write the attachment \"{0}\" for the project \"{1}\" with id \"{2}\" because another file with the same name already exists in the temporary folder: \"{3}\"", attachment.BinaryObjectFileName, project.ProjectName, project.ProjectIdentifier, filePath); } byte[] content = null; baseDao.DoJDBCQueryWithRowCallbackDelegate("SELECT BINARYOBJECTCONTENT FROM WQX_PROJATTACHEDBINARYOBJECT WHERE PARENTID = ? AND BINARYOBJECTFILE = ?", delegate(IDataReader dataReader) { content = (byte[])dataReader.GetValue(0); }, project.ProjectIdentifier, attachment.BinaryObjectFileName); if (content == null) { throw new ArgException("Failed to load attachment content for the file \"{0}\" from the database for the project \"{1}\" with id \"{2}\"", attachment.BinaryObjectFileName, project.ProjectName, project.ProjectIdentifier); } UncompressFile(content, filePath); }); } }); } if (org.MonitoringLocation != null) { CollectionUtils.ForEach(org.MonitoringLocation, delegate(MonitoringLocationDataType monitoringLocation) { if (monitoringLocation.AttachedBinaryObject != null) { CollectionUtils.ForEach(monitoringLocation.AttachedBinaryObject, delegate(MonitoringLocationAttachedBinaryObjectDataType attachment) { if (string.IsNullOrEmpty(attachment.BinaryObjectFileName)) { throw new ArgException("An attachment for the monitoring location \"{0}\" with id \"{1}\" does not have an attachment name.", monitoringLocation.WellInformation, monitoringLocation.MonitoringLocationIdentity.MonitoringLocationIdentifier); } string filePath = Path.Combine(folderPath, attachment.BinaryObjectFileName); if (File.Exists(filePath)) { throw new ArgException("Failed to write the attachment \"{0}\" for the monitoring location \"{1}\" with id \"{2}\" because another file with the same name already exists in the temporary folder: \"{3}\"", attachment.BinaryObjectFileName, monitoringLocation.WellInformation, monitoringLocation.MonitoringLocationIdentity.MonitoringLocationIdentifier, filePath); } byte[] content = null; baseDao.DoJDBCQueryWithRowCallbackDelegate("SELECT BINARYOBJECTCONTENT FROM WQX_MONLOCATTACHEDBINARYOBJECT WHERE PARENTID = ? AND BINARYOBJECTFILE = ?", delegate(IDataReader dataReader) { content = (byte[])dataReader.GetValue(0); }, monitoringLocation.MonitoringLocationIdentity.MonitoringLocationIdentifier, attachment.BinaryObjectFileName); if (content == null) { throw new ArgException("Failed to load attachment content for the file \"{0}\" from the database for the monitoring location \"{1}\" with id \"{2}\"", attachment.BinaryObjectFileName, monitoringLocation.WellInformation, monitoringLocation.MonitoringLocationIdentity.MonitoringLocationIdentifier); } UncompressFile(content, filePath); }); } }); } }