EDMIService: Update for Annette GmbH

This commit is contained in:
Jonathan Jenne 2021-11-25 15:55:45 +01:00
parent 6df72b07ad
commit 8846a5bf1c
10 changed files with 268 additions and 250 deletions

View File

@ -9,7 +9,8 @@ Imports System.ServiceModel.Description
Imports DigitalData.Services.EDMIService.Messages
Imports DigitalData.Modules.EDMI.API.Rights
Imports DigitalData.Services.EDMIService.Exceptions
Imports DigitalData.Services.EDMIService.GlobalState
Imports DigitalData.Services.EDMIService.FileStorage
<ServiceBehavior(InstanceContextMode:=InstanceContextMode.PerSession)>
Public Class EDMIService
@ -54,16 +55,110 @@ Public Class EDMIService
_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
Public Function NewFile(Data As NewFile.NewFileRequest) As NewFile.NewFileResponse Implements IEDMIService.NewFile
_Logger.Debug("Start of Method [NewFile]")
Try
Dim oNewObjectIdSQL = $"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 As Long = MSSQL_IDB.GetScalarValue(oNewObjectIdSQL)
_Logger.Info("New Object with Id [{0}] created!", oObjectId)
If IsNothing(oObjectId) Then
LogAndThrow("Could not create new ObjectId!")
End If
'---------------------------------------------------------------------------
' Find ObjectStore by Title
_Logger.Debug("Checking for DataStore [{0}].", Data.StoreName)
Dim oStore = GlobalState.ObjectStores.
Where(Function(store) store.Path.Equals(Data.StoreName, StringComparison.OrdinalIgnoreCase)).
SingleOrDefault()
If oStore Is Nothing Then
LogAndThrow($"DataStore [{Data.StoreName}] does not exist. Exiting.")
End If
' Get Store base path
Dim oBasePath As String = oStore.Path
_Logger.Debug("Store BasePath is [{0}]", oBasePath)
' Get directory by DateImported or, if not supplied, by current date
Dim oSubDirectory As String
If IsNothing(Data.FileImportedAt) Then
oSubDirectory = GetDateSubDirectory(Now)
Else
oSubDirectory = GetDateSubDirectory(Data.FileImportedAt)
End If
_Logger.Debug("Subdirectory is [{0}]", oSubDirectory)
' Check and create final path, if necessary
Dim oFinalPath = Path.Combine(oBasePath, oSubDirectory)
If Not Directory.Exists(oFinalPath) Then
Try
_Logger.Debug("Path does not exist, creating: [{0}]", oFinalPath)
Directory.CreateDirectory(oFinalPath)
_Logger.Debug("Created folder [{0}]", oFinalPath)
Catch ex As Exception
Throw GetFault(ex)
End Try
End If
_Logger.Debug("Final Directory is [{0}]", oFinalPath)
' Get filename
Dim oKeepFileName As Boolean = False
If oStore.IsArchive Then
_Logger.Debug("Object Store is an archive: [{0}]", oStore.IsArchive)
oKeepFileName = True
End If
Dim oFileName As String = GetFileObjectFileName(oObjectId, Data.FileName, oKeepFileName)
_Logger.Debug("Filename is [{0}]", oFileName)
Dim oFileObjectPath As String = Path.Combine(oFinalPath, oFileName)
Dim oFileObjectInfo As FileInfo = New FileInfo(oFileObjectPath)
Dim oFileObjectSize As Long = Data.FileContents.Length
Dim oFileObjectExtension As String = oFileObjectInfo.Extension.Substring(1)
Dim oFileObjectName As String = oFileObjectInfo.Name
_Logger.Debug("File Information for [{0}]:", oFileObjectName)
_Logger.Debug("Size: [{0}]", oFileObjectSize)
_Logger.Debug("Extension: [{0}]", oFileObjectExtension)
_Logger.Debug("Checksum: [{0}]", Data.FileChecksum)
Try
Using oStream = New FileStream(oFileObjectPath, FileMode.Create, FileAccess.Write)
_Logger.Info("ImportFile: Saving file to path [{0}]", oFileObjectPath)
_Logger.Info("ImportFile: Content Length: {0}", oFileObjectSize)
oStream.Write(Data.FileContents, 0, oFileObjectSize)
oStream.Flush(True)
oStream.Close()
End Using
Catch ex As Exception
_Logger.Error(ex)
LogAndThrow("Could not write file to disk!")
End Try
' Insert into DB
Dim oSQL As String = $"EXEC PRIDB_NEW_IDBFO '{oFileObjectPath}', '{oFileObjectName}', '{oFileObjectExtension}',{oFileObjectSize},{Data.FileChecksum} ,'{Data.Who}','{oObjectId}',{oStore.Id}"
Dim oResult As Boolean = MSSQL_IDB.ExecuteNonQuery(oSQL)
If oResult = False Then
LogAndThrow("IDB FileObject could not be created!")
End If
Return New NewFile.NewFileResponse(oObjectId)
Catch ex As FaultException
Return New NewFile.NewFileResponse(ex)
End Try
End Function
#Region "=== Heartbeat ==="
Public Function Heartbeat() As Boolean Implements IEDMIService.Heartbeat
Return True
@ -288,56 +383,56 @@ Public Class EDMIService
''' Imports a file according to ObjectStoreId
''' </summary>
''' <returns></returns>
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)
'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
' ' 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)
' 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)
' Try
' Directory.CreateDirectory(oDirectoryPath)
Dim oVersionedFileName As String = Filesystem.GetVersionedFilename(oAbsolutePath)
' 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
' _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))
' ' 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")
' Dim oObjectId = MSSQL_IDB.GetScalarValue(oCommand, "@IDB_OBJ_ID")
Return New DocumentImportResponse() With {.ObjectId = oObjectId}
' Return New DocumentImportResponse() With {.ObjectId = oObjectId}
Catch ex As FaultException
_Logger.Error(ex)
Throw ex
' Catch ex As FaultException
' _Logger.Error(ex)
' Throw ex
Catch ex As Exception
_Logger.Error(ex)
Throw GetFault(ex)
' Catch ex As Exception
' _Logger.Error(ex)
' Throw GetFault(ex)
End Try
End Function
' End Try
'End Function
Public Function GetFileByObjectId(Data As DocumentStreamRequest) As DocumentStreamResponse Implements IEDMIService.GetFileByObjectId
Try
@ -447,29 +542,6 @@ Public Class EDMIService
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 TestObjectIdExists(Data As TestObjectIdExistsRequest) As TestObjectIdExistsResponse Implements IEDMIService.TestObjectIdExists
Try
Dim oSQL As String = $"SELECT IDB_OBJ_ID, ACTIVE, DELETED FROM TBIDB_OBJECT WHERE IDB_OBJ_ID = {Data.ObjectId}"
@ -496,119 +568,20 @@ Public Class EDMIService
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 oBasePath 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)
Dim oFinalPath = Path.Combine(oBasePath, oSubDirectory)
If Not Directory.Exists(oFinalPath) Then
Try
_Logger.Debug("Path does not exist, creating: [{0}]", oFinalPath)
Directory.CreateDirectory(oFinalPath)
_Logger.Debug("Created folder [{0}]", oFinalPath)
Catch ex As Exception
Throw GetFault(ex)
End Try
End If
_Logger.Debug("Final Directory is [{0}]", oFinalPath)
Dim oFileName As String = GetFileObjectFileName(Data.ObjectId, Data.Extension, Data.KeepExtension)
Dim oFileObjectPath As String = Path.Combine(oFinalPath, oFileName)
_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.GetObjectStore(Data.ObjectStoreType)
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.Contents.Length},'{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
#Region "=== Private ==="
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}"
Private Function GetFileObjectFileName(IDB_OBJ_ID As Long, pFilename As String, pKeepFilename As Boolean) As String
If pKeepFilename Then
Return pFilename
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
@ -690,6 +663,21 @@ Public Class EDMIService
Return AccessRight.VIEW_ONLY
End Try
End Function
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
Private Function LogAndThrow(pMessage As String)
_Logger.Warn(pMessage)
Throw New ApplicationException(pMessage)
End Function
#End Region

View File

@ -129,10 +129,12 @@
<ItemGroup>
<Compile Include="ClassConstants.vb" />
<Compile Include="Config.vb" />
<Compile Include="Filestorage\NewFile\NewFileRequest.vb" />
<Compile Include="Filestorage\NewFile\NewFileResponse.vb" />
<Compile Include="GlobalState.vb" />
<Compile Include="Helpers\AccessRightResult.vb" />
<Compile Include="Helpers\Messages.vb" />
<Compile Include="Helpers\BaseResult.vb" />
<Compile Include="Helpers\BaseResponse.vb" />
<Compile Include="Helpers\Exceptions.vb" />
<Compile Include="Helpers\DatabaseResult.vb" />
<Compile Include="EDMIService.vb" />

View File

@ -0,0 +1,69 @@
Imports System.Runtime.Serialization
Namespace FileStorage.NewFile
<Serializable>
<DataContract>
Public Class NewFileRequest
''' <summary>
''' Absolute filename of the file to be imported
''' </summary>
<DataMember>
Public Property FileName As String
''' <summary>
''' Creation date of the original file from the filesystem
''' </summary>
<DataMember>
Public Property FileCreatedAt As String
''' <summary>
''' Modification date of the original file from the filesystem
''' </summary>
<DataMember>
Public Property FileChangedAt As String
''' <summary>
''' Date for which the file should be show as imported
''' </summary>
<DataMember>
Public Property FileImportedAt As Date
''' <summary>
''' The byte array representing the file contents
''' </summary>
<DataMember>
Public Property FileContents As Byte()
''' <summary>
''' The SHA256 Hash of the file contents
''' </summary>
<DataMember>
Public Property FileChecksum As String
''' <summary>
''' Name/title of the ObjectStore to save the file to, ex. Work
''' </summary>
<DataMember>
Public Property StoreName As String
''' <summary>
''' The business entity of the file, ex DEFAULT
''' </summary>
<DataMember>
Public Property BusinessEntity As String
''' <summary>
''' The kind of object to be created, ex. DOC
''' </summary>
<DataMember>
Public Property KindType As String
''' <summary>
''' The name of the user importing the file, ex. JenneJ
''' </summary>
<DataMember>
Public Property Who As String
End Class
End Namespace

View File

@ -0,0 +1,22 @@
Imports System.Runtime.Serialization
Namespace FileStorage.NewFile
<Serializable>
<DataContract>
Public Class NewFileResponse
Inherits Messages.BaseResponse
<DataMember>
Public Property ObjectId As Long
Public Sub New(pObjectId As Long)
MyBase.New()
ObjectId = pObjectId
End Sub
Public Sub New(pException As Exception)
MyBase.New(pException)
End Sub
End Class
End Namespace

View File

@ -6,7 +6,7 @@ Namespace Messages
<DataContract>
<Serializable>
Public Class AccessRightResult
Inherits BaseResult
Inherits BaseResponse
<DataMember>
Public Property Right As AccessRight = AccessRight.VIEW_ONLY

View File

@ -4,7 +4,7 @@ Namespace Messages
<Serializable>
<DataContract>
<KnownType(GetType(DBNull))>
Public MustInherit Class BaseResult
Public MustInherit Class BaseResponse
<DataMember>
Public Property OK As Boolean
<DataMember>

View File

@ -6,7 +6,7 @@ Namespace Messages
<DataContract>
<KnownType(GetType(DBNull))>
Public Class TableResult
Inherits BaseResult
Inherits BaseResponse
<DataMember>
Public Property Table As DataTable
@ -25,7 +25,7 @@ Namespace Messages
<DataContract>
<KnownType(GetType(DBNull))>
Public Class ScalarResult
Inherits BaseResult
Inherits BaseResponse
<DataMember>
Public Property Scalar As Object
@ -44,7 +44,7 @@ Namespace Messages
<DataContract>
<KnownType(GetType(DBNull))>
Public Class NonQueryResult
Inherits BaseResult
Inherits BaseResponse
Public Sub New()
MyBase.New()

View File

@ -26,64 +26,6 @@ Namespace Messages
Public ObjectId As Long
End Class
#End Region
#Region "FileImport 2021"
<MessageContract>
Public Class ImportFileIntoFileObjectRequest
<MessageBodyMember>
Public Contents() As Byte
<MessageBodyMember>
Public Who As String
<MessageBodyMember>
Public FilePath As String
<MessageBodyMember>
Public ObjectId As Long
<MessageBodyMember>
Public ObjectStoreType As String
End Class
<MessageContract>
Public Class ImportFileIntoFileObjectResponse
<MessageBodyMember>
Public Result As Boolean
End Class
<MessageContract>
Public Class NewObjectIdRequest
<MessageBodyMember>
Public KindType As String
<MessageBodyMember>
Public Who As String
<MessageBodyMember>
Public BusinessEntity As String
End Class
<MessageContract>
Public Class NewObjectIdResponse
<MessageBodyMember>
Public ObjectId As Long
End Class
<MessageContract>
Public Class NewFileObjectRequest
<MessageBodyMember>
Public ObjectId As Long
<MessageBodyMember>
Public StoreType As String
<MessageBodyMember>
Public DateImported As Date
<MessageBodyMember>
Public Extension As String
<MessageBodyMember>
Public KeepExtension As Boolean
End Class
<MessageContract>
Public Class NewFileObjectResponse
<MessageBodyMember>
Public FileObjectPath As String
End Class
#End Region
#Region "DocumentStream"
<MessageContract>

View File

@ -3,6 +3,7 @@ Imports System.ServiceModel
Imports DigitalData.Modules.Filesystem
Imports DigitalData.Services.EDMIService.Exceptions
Imports DigitalData.Services.EDMIService.Messages
Imports DigitalData.Services.EDMIService.FileStorage
<ServiceContract(Name:="IEDMIService", [Namespace]:="http://DigitalData.Services.EDMIService")>
Interface IEDMIService
@ -63,8 +64,11 @@ Interface IEDMIService
#Region "Document (New)"
<OperationContract>
<FaultContract(GetType(UnexpectedErrorFault))>
Function ImportFile(Data As DocumentImportRequest) As DocumentImportResponse
Function NewFile(Data As NewFile.NewFileRequest) As NewFile.NewFileResponse
'-----------------------------------------------------
' Everything below this line is subject to change!
'-----------------------------------------------------
<OperationContract>
<FaultContract(GetType(ObjectDoesNotExistFault))>
@ -79,17 +83,7 @@ Interface IEDMIService
<FaultContract(GetType(UnexpectedErrorFault))>
Function ListFilesForUser() As DocumentListResponse
<OperationContract>
<FaultContract(GetType(UnexpectedErrorFault))>
Function NewObjectId(Data As NewObjectIdRequest) As NewObjectIdResponse
<OperationContract>
<FaultContract(GetType(UnexpectedErrorFault))>
Function NewFileObject(Data As NewFileObjectRequest) As NewFileObjectResponse
<OperationContract>
<FaultContract(GetType(UnexpectedErrorFault))>
Function ImportFileIntoFileObject(Data As ImportFileIntoFileObjectRequest) As ImportFileIntoFileObjectResponse
#End Region
#Region "Helpers"

View File

@ -44,8 +44,9 @@ Public Class DatatableJob
oLogger.Debug("Connection Id: {0}", oConnectionId)
Dim oConnectionString = oMSSQL.Get_ConnectionStringforID(oConnectionId)
Dim oDecryptedConnectionString = MSSQLServer.DecryptConnectionString(oConnectionString)
Dim oTable = oMSSQL.GetDatatableWithConnection(oSQL, oConnectionString, COMMAND_SQL_TIMEOUT)
Dim oTable = oMSSQL.GetDatatableWithConnection(oSQL, oDecryptedConnectionString, COMMAND_SQL_TIMEOUT)
oTable.TableName = oDatatableName
Dim oView As DataView = Nothing