Imports DigitalData.Modules.Database Imports DigitalData.Modules.Logging Imports DigitalData.Modules.Language Imports DigitalData.Modules Imports System.IO Imports System.ServiceModel Imports System.Data.SqlClient Imports System.ServiceModel.Description Imports DigitalData.Services.EDMIService.Messages Imports DigitalData.Modules.EDMI.API.Rights Imports DigitalData.Services.EDMIService.Exceptions Public Class EDMIService Implements IEDMIService Public Shared LogConfig As LogConfig Public Shared MSSQL_ECM As MSSQLServer Public Shared MSSQL_IDB As MSSQLServer Public Shared Firebird As Firebird Public Shared AppConfig As Config Public Shared Filesystem As Filesystem.File Public Shared EDMIArchive As EDMI.File.Archive Public Shared GlobalState As GlobalState Public Shared Scheduler As Scheduler Public Const TBIDB_DOC_INFO = "TBIDB_DOC_INFO" Public Const TBIDB_ACCESSRIGHT = "TBIDB_ACCESSRIGHT" Private ReadOnly _Logger As Logger Private ReadOnly _Debug As Boolean = False Private ReadOnly _Username As String Public Shared Sub Configure(Config As ServiceConfiguration) Dim oBaseAddress = Config.BaseAddresses.Item(0) Dim oBinding = EDMI.API.Channel.GetBinding() Dim oAddress = New EndpointAddress(oBaseAddress) ' See: https://stackoverflow.com/questions/42327988/addserviceendpoint-throws-key-is-null Dim oDescription = ContractDescription.GetContract(GetType(IEDMIService), GetType(EDMIService)) Dim oEndpoint As New ServiceEndpoint(oDescription, oBinding, oAddress) Config.AddServiceEndpoint(oEndpoint) Config.Description.Behaviors.Add(New ServiceDebugBehavior With {.IncludeExceptionDetailInFaults = True}) End Sub Public Sub New() Dim oOperationContext As OperationContext = OperationContext.Current Dim oInstanceContext As InstanceContext = oOperationContext.InstanceContext Dim oUsername = StripDomainFromUsername(oOperationContext.ServiceSecurityContext.WindowsIdentity.Name) _Username = oUsername _Logger = LogConfig.GetLogger() _Logger.Debug("New Request by User [{0}]", _Username) End Sub Private Function StripDomainFromUsername(UserName As String) If UserName.Contains("\") Then Return UserName.Split("\")(1) ElseIf UserName.Contains("@") Then Return UserName.Split("@")(0) Else Return UserName End If End Function #Region "=== Heartbeat ===" Public Function Heartbeat() As Boolean Implements IEDMIService.Heartbeat Return True End Function #End Region #Region "=== Database ===" Public Function ReturnDatatableFromCache(Name As String, FilterExpression As String, SortByColumn As String) As TableResult Implements IEDMIService.ReturnDatatableFromCache Try _Logger.Debug($"ReturnDatatableFromCache: Datatable: {Name}") Dim oDataset As DataSet = GlobalState.TableStore Dim oDataTable As DataTable = Nothing _Logger.Debug("ReturnDatatableFromCache: DataSet contains [{0}] datatables", oDataset.Tables.Count) If oDataset.Tables.Contains(Name) Then oDataTable = oDataset.Tables.Item(Name).Copy() ' Apply filter and sorting to data Dim oFilterExpression As String = Utils.NotNull(FilterExpression, String.Empty) Dim oSortByColumn As String = Utils.NotNull(SortByColumn, String.Empty) Dim oFilteredRows = oDataTable.Select(oFilterExpression, oSortByColumn) Dim oFilteredTable As DataTable = Nothing If oFilteredRows.Count > 0 Then oFilteredTable = oFilteredRows.CopyToDataTable() oFilteredTable.TableName = Name Else ' Produce empty table oFilteredTable = oDataTable.Clone() oFilteredTable.TableName = Name End If _Logger.Debug("ReturnDatatableFromCache: Datatable Stats for [{0}]:", Name) _Logger.Debug("Unfiltered: [{0}] rows", oDataTable.Rows.Count) _Logger.Debug("Filtered: [{0}] rows", oFilteredTable.Rows.Count) Return New TableResult(oFilteredTable) Else _Logger.Warn($"ReturnDatatableFromCache: Datatable {Name} does not exist") Throw GetFault($"ReturnDatatableFromCache: Datatable {Name} does not exist") End If Catch ex As FaultException _Logger.Error(ex) Throw ex Catch ex As Exception _Logger.Error(ex) Throw GetFault(ex) End Try End Function #End Region #Region "=== Database (MSSQL IDB) ===" Public Function ReturnDatatable_MSSQL_IDB(SQL As String) As TableResult Implements IEDMIService.ReturnDatatable_MSSQL_IDB Try _Logger.Info($"ReturnDatatable_MSSQL_IDB, SQL: {SQL}") Dim oResult As DataTable = MSSQL_IDB.GetDatatable(SQL) oResult.TableName = "DD_RESULT" Return New TableResult(oResult) Catch ex As FaultException _Logger.Error(ex) Throw ex Catch ex As Exception _Logger.Error(ex) Throw GetFault(ex) End Try End Function Public Function ReturnScalar_MSSQL_IDB(SQL As String) As ScalarResult Implements IEDMIService.ReturnScalar_MSSQL_IDB Try _Logger.Info($"ReturnScalar_MSSQL_IDB, SQL: {SQL}") Dim oResult As Object = MSSQL_IDB.GetScalarValue(SQL) Return New ScalarResult(oResult) Catch ex As FaultException _Logger.Error(ex) Throw ex Catch ex As Exception _Logger.Error(ex) Throw GetFault(ex) End Try End Function Public Function ExecuteNonQuery_MSSQL_IDB(SQL As String) As NonQueryResult Implements IEDMIService.ExecuteNonQuery_MSSQL_IDB Try _Logger.Info($"ExecuteNonQuery_MSSQL_IDB, SQL: {SQL}") Dim oResult As Boolean = MSSQL_IDB.ExecuteNonQuery(SQL) Return New NonQueryResult() Catch ex As FaultException _Logger.Error(ex) Throw ex Catch ex As Exception _Logger.Error(ex) Throw GetFault(ex) End Try End Function #End Region #Region "=== Database (MSSQL ECM) ===" Public Function ReturnDatatable_MSSQL_ECM(SQL As String) As TableResult Implements IEDMIService.ReturnDatatable_MSSQL_ECM Try _Logger.Info($"ReturnDatatable_MSSQL_ECM, SQL: {SQL}") Dim oResult As DataTable = MSSQL_ECM.GetDatatable(SQL) oResult.TableName = "DD_RESULT" Return New TableResult(oResult) Catch ex As FaultException _Logger.Error(ex) Throw ex Catch ex As Exception _Logger.Error(ex) Throw GetFault(ex) End Try End Function Public Function ReturnScalar_MSSQL_ECM(SQL As String) As ScalarResult Implements IEDMIService.ReturnScalar_MSSQL_ECM Try _Logger.Info($"ReturnScalar_MSSQL_ECM, SQL: {SQL}") Dim oResult As Object = MSSQL_ECM.GetScalarValue(SQL) Return New ScalarResult(oResult) Catch ex As FaultException _Logger.Error(ex) Throw ex Catch ex As Exception _Logger.Error(ex) Throw GetFault(ex) End Try End Function Public Function ExecuteNonQuery_MSSQL_ECM(SQL As String) As NonQueryResult Implements IEDMIService.ExecuteNonQuery_MSSQL_ECM Try _Logger.Info($"ExecuteNonQuery_MSSQL_ECM, SQL: {SQL}") Dim oResult As Boolean = MSSQL_ECM.ExecuteNonQuery(SQL) Return New NonQueryResult() Catch ex As FaultException _Logger.Error(ex) Throw ex Catch ex As Exception _Logger.Error(ex) Throw GetFault(ex) End Try End Function #End Region #Region "=== Database (Firebird) ===" Public Function ReturnDatatable_Firebird(SQL As String) As TableResult Implements IEDMIService.ReturnDatatable_Firebird Try _Logger.Info($"ReturnDatatable, SQL: {SQL}") Dim oResult As DataTable = Firebird.GetDatatable(SQL) oResult.TableName = "DD_RESULT" Return New TableResult(oResult) Catch ex As FaultException _Logger.Error(ex) Throw ex Catch ex As Exception _Logger.Error(ex) Throw GetFault(ex) End Try End Function Public Function ReturnScalar_Firebird(SQL As String) As ScalarResult Implements IEDMIService.ReturnScalar_Firebird Try _Logger.Info($"ReturnScalar, SQL: {SQL}") Dim oResult As Object = Firebird.GetScalarValue(SQL) Return New ScalarResult(oResult) Catch ex As FaultException _Logger.Error(ex) Throw ex Catch ex As Exception _Logger.Error(ex) Throw GetFault(ex) End Try End Function Public Function ExecuteNonQuery_Firebird(SQL As String) As NonQueryResult Implements IEDMIService.ExecuteNonQuery_Firebird Try _Logger.Info($"ExecuteNonQuery, SQL: {SQL}") Dim oResult As Boolean = Firebird.ExecuteNonQuery(SQL) Return New NonQueryResult() Catch ex As FaultException _Logger.Error(ex) Throw ex Catch ex As Exception _Logger.Error(ex) Throw GetFault(ex) End Try End Function #End Region #Region "=== Document ===" ''' ''' Imports a file according to ObjectStoreId ''' ''' Public Function ImportFile(Data As DocumentImportRequest) As DocumentImportResponse Implements IEDMIService.ImportFile Dim oObjectStore = GlobalState.ObjectStores.First() Dim EDMIPath = New EDMI.File.Path(LogConfig, oObjectStore.Path) ' TODO: ' - Get Object Store -> Object Catalog -> Catalog Path ' - If IS_ARCHIVE = True And RetentionDays <> Nothing: ' DoArchive! ' - Refactor EDMIPath to get ObjectStore at service start up ' and return ObjectStore Path from ObjectStoreId + RelativePath ' VWIDB_OBJECTSTORE Dim oRelativePath As String = EDMIPath.GetRelativePath(Data.DocumentType, Data.FileName) Dim oAbsolutePath As String = EDMIPath.GetFullPath(Data.DocumentType, Data.FileName) Dim oDirectoryPath = EDMIPath.GetFullPath(Data.DocumentType) Try Directory.CreateDirectory(oDirectoryPath) Dim oVersionedFileName As String = Filesystem.GetVersionedFilename(oAbsolutePath) _Logger.Info("ImportFile: Saving file [{0}] to path [{1}]", Data.FileName, oVersionedFileName) Using oStream = New FileStream(oVersionedFileName, FileMode.CreateNew) oStream.Write(Data.Contents, 0, Data.Contents.Length) oStream.Flush(True) oStream.Close() End Using ' insert into db Dim oCommand As New SqlCommand("PRIDB_NEW_DOCUMENT") oCommand.Parameters.AddWithValue("@OBJ_ST_ID", 1) oCommand.Parameters.AddWithValue("@REL_PATH", oRelativePath) oCommand.Parameters.AddWithValue("@WHO", _Username) oCommand.Parameters.AddWithValue("@REF_DOCID", 0) oCommand.Parameters.Add(New SqlParameter("@IDB_OBJ_ID", SqlDbType.BigInt)) Dim oObjectId = MSSQL_IDB.GetScalarValue(oCommand, "@IDB_OBJ_ID") Return New DocumentImportResponse() With {.ObjectId = oObjectId} Catch ex As FaultException _Logger.Error(ex) Throw ex Catch ex As Exception _Logger.Error(ex) Throw GetFault(ex) End Try End Function Public Function GetFileByObjectId(Data As DocumentStreamRequest) As DocumentStreamResponse Implements IEDMIService.GetFileByObjectId Try Dim oFullPath = GetFullPathForObjectId(Data.ObjectId) If oFullPath = String.Empty Then Dim oDetails As New ObjectDoesNotExistFault(Data.ObjectId) _Logger.Warn("GetFileByObjectId: " & oDetails.ErrorMessage) Throw New FaultException(Of ObjectDoesNotExistFault)(oDetails, oDetails.ErrorMessage) End If _Logger.Debug("GetFileByObjectId: Loading file [{0}]", oFullPath) Dim oFileInfo As New FileInfo(oFullPath) If Not oFileInfo.Exists Then Dim oDetails As New ObjectDoesNotExistFault(Data.ObjectId) _Logger.Warn("GetFileByObjectId: " & oDetails.ErrorMessage) Throw New FaultException(Of ObjectDoesNotExistFault)(oDetails, oDetails.ErrorMessage) End If Dim oDestination As New MemoryStream() Using oSource As FileStream = IO.File.OpenRead(oFullPath) oSource.CopyTo(oDestination) End Using oDestination.Seek(0, SeekOrigin.Begin) Dim oMessage As New DocumentStreamResponse() With { .FileName = oFileInfo.Name, .FileContents = oDestination } Return oMessage Catch ex As FaultException _Logger.Error(ex) Throw ex Catch ex As Exception _Logger.Error(ex) Throw GetFault(ex) End Try End Function Public Function GetFileInfoByObjectId(Data As DocumentInfoRequest) As DocumentInfoResponse Implements IEDMIService.GetFileInfoByObjectId Try Dim oAccessRight = GetAccessRightForObjectId(Data.UserId, Data.ObjectId) Dim oFullPath = GetFullPathForObjectId(Data.ObjectId) If oFullPath = String.Empty Then Dim oFault As New ObjectDoesNotExistFault(Data.ObjectId) Throw New FaultException(Of ObjectDoesNotExistFault)(oFault, oFault.ErrorMessage) End If Return New DocumentInfoResponse With { .FileRight = oAccessRight, .FullPath = oFullPath } Catch ex As FaultException _Logger.Warn("GetFileInfoByObjectId: " & ex.Message) _Logger.Error(ex) Throw ex Catch ex As Exception _Logger.Warn("GetFileInfoByObjectId: " & ex.Message) _Logger.Error(ex) Throw GetFault(ex) End Try End Function Public Function ListFilesForUser() As DocumentListResponse Implements IEDMIService.ListFilesForUser Try Dim oDatatable = GetFileList() Return New DocumentListResponse() With { .Datatable = oDatatable } Catch ex As FaultException _Logger.Error(ex) Throw ex Catch ex As Exception _Logger.Error(ex) Throw GetFault(ex) End Try End Function Public Function GetFileList() As DataTable Try If Not GlobalState.TableStore.Tables.Contains(TBIDB_DOC_INFO) Then _Logger.Warn("GetFileList: Document info table does not exist!") Return Nothing End If Dim oTable As DataTable = GlobalState.TableStore.Tables.Item(TBIDB_DOC_INFO) Return oTable Catch ex As Exception _Logger.Warn("GetFileList: Unexpected Error while getting file list.") _Logger.Error(ex) Return Nothing End Try End Function Private Function GetFullPathForObjectId(ObjectId As Long) As String Try If Not GlobalState.TableStore.Tables.Contains(TBIDB_DOC_INFO) Then _Logger.Warn("GetFullPathForObjectId: Document info table does not exist!") Return String.Empty End If Dim oTable As DataTable = GlobalState.TableStore.Tables.Item(TBIDB_DOC_INFO) Dim oRows As List(Of DataRow) = oTable.Select($"IDB_OBJ_ID = {ObjectId}").ToList() Dim oFullPath As String If oRows.Count = 0 Then _Logger.Warn("GetFullPathForObjectId: Full path does not exist for object [{0}]", ObjectId) oFullPath = String.Empty Else Dim oRow As DataRow = oRows.First() oFullPath = oRow.Item("FULL_PATH") End If Return oFullPath Catch ex As Exception _Logger.Warn("GetFullPathForObjectId: Unexpected Error while getting full path for object [{0}].", ObjectId) _Logger.Error(ex) Return String.Empty End Try End Function Private Function GetAccessRightForObjectId(UserId As Long, ObjectId As Long) As AccessRight Try If Not GlobalState.TableStore.Tables.Contains(TBIDB_ACCESSRIGHT) Then _Logger.Warn("GetAccessRightForObjectId: Access right table does not exist!") Return AccessRight.VIEW_ONLY End If Dim oTable As DataTable = GlobalState.TableStore.Tables.Item(TBIDB_ACCESSRIGHT) Dim oRows As List(Of DataRow) = oTable.Select($"IDB_OBJ_ID = {ObjectId} AND USR_ID = {UserId}").ToList() Dim oRight As AccessRight If oRows.Count = 0 Then _Logger.Warn("GetAccessRightForObjectId: Access right assignment does not exist for user [{0}] on object [{1}]", UserId, ObjectId) Return AccessRight.VIEW_ONLY Else If oRows.Count > 1 Then _Logger.Warn("GetAccessRightForObjectId: More than one access right assignment found for user [{0}] on object [{1}]", UserId, ObjectId) End If Dim oRow As DataRow = oRows.First() Dim oRightAsInt = oRow.Item("ACCESSRIGHT") oRight = Utils.ToEnum(Of AccessRight)(oRightAsInt) End If Return oRight Catch ex As Exception _Logger.Warn("GetAccessRightForObjectId: Unexpected Error while getting access right for object [{0}].", ObjectId) _Logger.Error(ex) Return AccessRight.VIEW_ONLY End Try End Function Public Function NewObjectId(Data As NewObjectIdRequest) As NewObjectIdResponse Implements IEDMIService.NewObjectId Try Dim oSQL As String = $"DECLARE @NEW_IDB_OBJ_ID BIGINT EXEC PRIDB_NEW_OBJECT '{Data.KindType}','{Data.Who}','{Data.BusinessEntity}',0, @IDB_OBJ_ID = @NEW_IDB_OBJ_ID OUTPUT; SELECT @NEW_IDB_OBJ_ID" Dim oObjectId = MSSQL_IDB.GetScalarValue(oSQL) If oObjectId Is Nothing Then Throw GetFault("NewObjectId: Could not create new ObjectId!") End If Return New NewObjectIdResponse With {.ObjectId = oObjectId} Catch ex As FaultException _Logger.Error(ex) Throw ex Catch ex As Exception _Logger.Error(ex) Throw GetFault(ex) End Try End Function Public Function NewFileObject(Data As NewFileObjectRequest) As NewFileObjectResponse Implements IEDMIService.NewFileObject Try Dim oStoreType As String = Data.StoreType _Logger.Debug("DataStore type is [{0}]", oStoreType) If oStoreType = String.Empty Then _Logger.Debug("DataStore empty, set to [{0}]", ClassConstants.FileStoreWork) oStoreType = ClassConstants.FileStoreWork End If Dim oRelpath As String = GetFileStorePraefix(oStoreType) Dim oSubDirectory As String If IsNothing(Data.DateImported) Then oSubDirectory = GetDateSubDirectory(Now) Else oSubDirectory = GetDateSubDirectory(Data.DateImported) End If _Logger.Debug("Subdirectory is [{0}]", oSubDirectory) oRelpath = Path.Combine(oRelpath, oSubDirectory) If Not Directory.Exists(oRelpath) Then Try _Logger.Debug("Path does not exist, creating: [{0}]", oRelpath) Directory.CreateDirectory(oRelpath) _Logger.Debug("Created folder [{0}]", oRelpath) Catch ex As Exception Throw GetFault(ex) End Try End If _Logger.Debug("Relative Path is [{0}]", oRelpath) Dim oFileObjectPath As String = Path.Combine(oRelpath, GetFileObjectFileName(Data.ObjectId, Data.Extension, Data.KeepExtension)) _Logger.Debug("Final Path is [{0}]", oFileObjectPath) Return New NewFileObjectResponse With {.FileObjectPath = oFileObjectPath} Catch ex As FaultException _Logger.Error(ex) Throw ex Catch ex As Exception _Logger.Error(ex) Throw GetFault(ex) End Try End Function Public Function ImportFileIntoFileObject(Data As ImportFileIntoFileObjectRequest) As ImportFileIntoFileObjectResponse Implements IEDMIService.ImportFileIntoFileObject Try Dim oObjectStore = GlobalState.ObjectStores. Where(Function(o) o.Title = Data.ObjectStoreType). FirstOrDefault() If oObjectStore Is Nothing Then Throw New KeyNotFoundException($"ObjectStore [{Data.ObjectStoreType}] was not found.") End If Using oStream = New FileStream(Data.FilePath, FileMode.Create, FileAccess.Write) _Logger.Info("ImportFile: Saving file to path [{0}]", Data.FilePath) _Logger.Info("ImportFile: Content Length: {0}", Data.Contents.Length) oStream.Write(Data.Contents, 0, Data.Contents.Length) oStream.Flush(True) oStream.Close() End Using ' insert into db Dim oSQL As String = $"EXEC PRIDB_NEW_IDBFO '{Data.FilePath}','{Data.Who}','{Data.ObjectId}',{oObjectStore.Id}" Dim oResult As Boolean = MSSQL_IDB.ExecuteNonQuery(oSQL) Return New ImportFileIntoFileObjectResponse() With {.Result = oResult} Catch ex As FaultException _Logger.Error(ex) Throw ex Catch ex As Exception _Logger.Error(ex) Throw GetFault(ex) End Try End Function #End Region Private Function GetFileObjectFileName(IDB_OBJ_ID As Long, pExtension As String, pKeepExtension As Boolean) As String If Not pExtension.StartsWith("."c) Then pExtension &= "."c End If If pKeepExtension Then Return $"{IDB_OBJ_ID}{pExtension}" Else Return $"{IDB_OBJ_ID}.ddfo" End If End Function Private Function GetFileStorePraefix(pStoreType As String) As String Dim oObjectStore If pStoreType = ClassConstants.FileStoreArchive Then oObjectStore = GlobalState.ObjectStores.Item(0) Else ' pStoreType = ClassConstants.FileStoreWork Then oObjectStore = GlobalState.ObjectStores.Item(1) End If _Logger.Debug($"oObjectStore is [{oObjectStore.Path}]") Return oObjectStore.Path End Function Private Function GetDateSubDirectory(pDate As Date) As String Return Path.Combine(pDate.ToString("yyyy"), pDate.ToString("MM"), pDate.ToString("dd")) End Function Private Function GetFault(Exception As Exception) As FaultException Dim oFault As New UnexpectedErrorFault(Exception) Return New FaultException(Of UnexpectedErrorFault)(oFault, New FaultReason(oFault.InnerException.Message)) End Function Private Function GetFault(ErrorMessage As String) As FaultException Dim oFault As New UnexpectedErrorFault(New ApplicationException(ErrorMessage)) Return New FaultException(Of UnexpectedErrorFault)(oFault, New FaultReason(oFault.InnerException.Message)) End Function End Class