From abf4020a185b1de6e4c3b242c1fbc302c9192f86 Mon Sep 17 00:00:00 2001 From: Digital Data - Marlon Schreiber Date: Wed, 5 Sep 2018 15:37:00 +0200 Subject: [PATCH] MS Added Stream File --- Modules.Windream/Windream2.vb | 212 +++++++++++++++++++++++++++++++++- 1 file changed, 211 insertions(+), 1 deletion(-) diff --git a/Modules.Windream/Windream2.vb b/Modules.Windream/Windream2.vb index 1a4b1ee2..f7374a10 100644 --- a/Modules.Windream/Windream2.vb +++ b/Modules.Windream/Windream2.vb @@ -2,6 +2,7 @@ Imports WINDREAMLib.WMCOMEvent Imports WINDREAMLib.WMEntity Imports WINDREAMLib.WMObjectEditMode + Imports WINDREAMLib.WMSearchOperator Imports WINDREAMLib.WMSearchRelation Imports WMOBRWSLib @@ -59,6 +60,7 @@ Imports System.IO ''' REMARKS: This class should not be instanciated directly. Instead, ConnectionBuilder should be used. ''' Public Class Windream2 + Const WMObjectStreamOpenModeReadWrite = 2 #Region "Private Properties" Private ReadOnly _logger As Logger Private ReadOnly _loggerFactory As LogFactory @@ -266,6 +268,109 @@ Public Class Windream2 End Try End Function + Public Function NewStream_File(ByVal FilenameSource As String, ByVal filenameTarget As String) + Try + Dim oExtension As String = Path.GetExtension(FilenameSource) + _logger.Debug("Stream_File was started...") + If Not TestSessionLoggedIn() Then + Return False + End If + + Dim oTargetDrive As String = Path.GetDirectoryName(filenameTarget) + filenameTarget = GetNormalizedPath(filenameTarget) + Const STREAM_BinaryObject = "BinaryObject" + + _logger.Debug($"Import file from {FilenameSource } to {filenameTarget}") + + Dim WMObject + Dim aFileIO + Dim aWMStream + + aFileIO = New WMOTOOLLib.WMFileIO + _logger.Debug("Try to access file and lock it...") + Err.Clear() + Const WMCOMEventWMSessionNeedIndex = 1 + + 'Indexierungsdialog der Session unterdrücken + Session.SwitchEvents(WMCOMEventWMSessionNeedIndex, False) + 'clsWindream.MY_WDSESSION.switchEvents(WMCOMEventWMSessionNeedIndex, False) + '================================================================== + ' create an object + '================================================================== + WMObject = Session.GetNewWMObjectFS(WMEntity.WMEntityDocument, filenameTarget, WMObjectEditModeObject) + If Err.Number > 0 Then + _logger.Warn($"WMObject could not be created - Error: ' {Err.Description}'") + End If + If WMObject IsNot Nothing Then + ' lock object for file system access (to change the file itself) + LockObject(WMObject) + ' set fileIO the local source file + aFileIO.bstrOriginalFileName = FilenameSource + If Err.Number > 0 Then + 'MsgBox(Err.Number.ToString) + _logger.Warn($"fileIO could not be set - Error: '{Err.Description}'") + If UnlockObject(WMObject) Then RemoveFile(filenameTarget) + Return False + End If + ' open the windream object's file stream for writing + aWMStream = WMObject.OpenStream(STREAM_BinaryObject, WMObjectStreamOpenModeReadWrite) + If Err.Number > 0 Then + _logger.Warn($"Error whhile creating stream - Error: '{Err.Description}'") + If UnlockObject(WMObject) Then RemoveFile(filenameTarget) + Return False + End If + _logger.Debug("oWMStream created!") + ' give fileIO helper object the windream stream + aFileIO.aWMStream = aWMStream + If Err.Number > 0 Then + _logger.Warn($"Error while adding aWMStream to aFileIO - Error: '{Err.Description}'") + If UnlockObject(WMObject) Then RemoveFile(filenameTarget) + Return False + End If + ' let fileIO object import the original file into windream + aFileIO.ImportOriginal(True) + If Err.Number > 0 Then + _logger.Warn($"Error while FileIO.ImportOriginal(True) Error: '{Err.Description}'") + If UnlockObject(WMObject) Then RemoveFile(filenameTarget) + + Return False + End If + _logger.Debug("Content of file was transferred!") + ' close the windream file stream + aWMStream.Close() + If Err.Number > 0 Then + _logger.Warn($"Error in aWMStream.Close() - Error: '{Err.Description}'") + If UnlockObject(WMObject) Then RemoveFile(filenameTarget) + Return False + 'MsgBox(Err.Description) + End If + ' save new windream object + WMObject.save() + If Err.Number > 0 Then + _logger.Warn($"Error while WMObject.save() - Error: '{Err.Description}'") + If UnlockObject(WMObject) Then RemoveFile(filenameTarget) + Return False + 'MsgBox(Err.Description) + End If + _logger.Debug("File was saved correctly.") + ' unlock the windream object + If WMObject.unlock() = False Then + RemoveFile(filenameTarget) + Return False + End If + _logger.Info($"File '{filenameTarget}' was imported.") + Return True + Else + _logger.Warn($"WMObject could not be created!") + Return False + End If + Catch ex As Exception + _logger.Warn("Unexpected Error in NewStream_File:") + _logger.Error(ex) + Return False + End Try + End Function + Public Function LockObject(WMObject As WMObject, Optional EditMode As WMObjectEditMode = WMObjectEditModeNoEdit) As Boolean Try WMObject.LockFor(EditMode) @@ -526,7 +631,112 @@ Public Class Windream2 Return oResult End Try End Function - + ''' + ''' Gets an array of the actual vektorvalues of index, collated with the passed values + ''' + ''' windream-file as WMObject + ''' Indexname as String + ''' The new values as Array + ''' True if duplicates shall be prevented + ''' + Public Function GetVektorData_Combined(ByVal WMDoc As WMObject, IndexName As String, NewValues As Object, CheckDuplikat As Boolean) + Try + Dim oAnzahl As Integer = 0 + Dim oValueArray() + 'Jeden Wert des Vektorfeldes durchlaufen + Dim oWMValue = WMDoc.GetVariableValue(IndexName) + If oWMValue Is Nothing = False Then + 'Nochmals prüfen ob wirklich Array + If oWMValue.GetType.ToString.Contains("System.Object") Then + 'Keine Duplikatprüfung also einfach neues Array füllen + If CheckDuplikat = False Then + For Each ovalue As Object In oWMValue + 'Das Array anpassen + ReDim Preserve oValueArray(oAnzahl) + 'Den Wert im Array speichern + oValueArray(oAnzahl) = ovalue.ToString + oAnzahl += 1 + Next + 'Und jetzt den/die Neuen Wert(e) anfügen + For Each oNewValue As Object In NewValues + If oNewValue Is Nothing = False Then + 'Das Array anpassen + ReDim Preserve oValueArray(oAnzahl) + 'Den Wert im Array speichern + oValueArray(oAnzahl) = oNewValue.ToString + oAnzahl += 1 + End If + Next + Else + _logger.Debug("Duplicates shall be checked...") + 'Duplikat Prüfung an, also nur anhängen wenn Wert <> + For Each oValue As Object In oWMValue + If oValue Is Nothing = False Then + 'Erst einmal die ALten Werte schreiben + ReDim Preserve oValueArray(oAnzahl) + 'Den Wert im Array speichern + oValueArray(oAnzahl) = oValue.ToString + _logger.Debug("Value (" & oAnzahl & ") " & oValue.ToString) + oAnzahl += 1 + End If + Next + 'Jetzt die Neuen Werte auf Duplikate überprüfen + For Each NewValue As Object In NewValues + If NewValue Is Nothing = False Then + If oValueArray.Contains(NewValue) = False Then + _logger.Debug("New Value (" & oAnzahl & ") " & NewValue.ToString) + 'Das Array anpassen + ReDim Preserve oValueArray(oAnzahl) + 'Den Wert im Array speichern + oValueArray(oAnzahl) = NewValue.ToString + oAnzahl += 1 + Else + _logger.Debug("Value '" & NewValue.ToString & "' bereits in Vektorfeld enthalten") + End If + End If + Next + End If + End If + Else + _logger.Debug("Vektorfeld ist noch leer....") + 'Den/die Neuen Wert(e) anfügen + For Each oNewValue As Object In NewValues + If oNewValue Is Nothing = False Then + If CheckDuplikat = True Then + If oValueArray Is Nothing = False Then + If oValueArray.Contains(oNewValue) = False Then + 'Das Array anpassen + ReDim Preserve oValueArray(oAnzahl) + 'Den Wert im Array speichern + oValueArray(oAnzahl) = oNewValue.ToString + oAnzahl += 1 + Else + _logger.Debug("Value '" & oNewValue.ToString & "' bereits in Array enthalten") + End If + Else 'Dererste Wert, also hinzufügen + 'Das Array anpassen + ReDim Preserve oValueArray(oAnzahl) + 'Den Wert im Array speichern + oValueArray(oAnzahl) = oNewValue.ToString + oAnzahl += 1 + End If + Else + 'Das Array anpassen + ReDim Preserve oValueArray(oAnzahl) + 'Den Wert im Array speichern + oValueArray(oAnzahl) = oNewValue.ToString + oAnzahl += 1 + End If + End If + Next + End If + _logger.Debug("Return ValueArray: length " & oValueArray.Length) + Return oValueArray + Catch ex As Exception + _logger.Error(ex) + Return Nothing + End Try + End Function Public Function SetFolderObjecttype(FolderPath As String, Objecttype As String) As Boolean If TestSessionLoggedIn() = False Then Return False