-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileSystemEconomicQuoteProvider.cls
More file actions
479 lines (379 loc) · 15.6 KB
/
Copy pathFileSystemEconomicQuoteProvider.cls
File metadata and controls
479 lines (379 loc) · 15.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
VERSION 1.0 CLASS
BEGIN
MultiUse = -1 'True
END
Attribute VB_Name = "FileSystemEconomicQuoteProvider"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
' ==================================================================
' IMPLEMENTACION DE INFRAESTRUCTURA: FileSystemEconomicQuoteProvider
' ==================================================================
' Implementa IEconomicQuoteProvider usando el sistema de archivos.
' Contiene toda la logica de:
' - Busqueda de ficheros de valoracion en carpeta de oportunidad
' - Identificacion por patrones regex
' - Parseo de nombres de fichero
' - Gestion de subcarpetas de revision
'
' Patron de VBScript:
' strQuoteNrPattern = "\d{9}(?:[\-_]\d+)?"
' strQuoteNrRevPattern = "(" & strQuoteNrPattern & ")(?:[ \-_]*rev\.?[ \-_]*\d+\b)?"
' ==================================================================
'@Folder "2-Infraestructura.5-2-Providers.FS"
Option Explicit
Implements IEconomicQuoteProvider
Private Const MODULE_NAME As String = "FileSystemEconomicQuoteProvider"
' Nombre de la subcarpeta de valoraciones economicas
Private Const QUOTE_FOLDER_NAME As String = "3.VALORACION ECONOMICA"
' Patrones de expresion regular
Private Const QUOTE_NR_PATTERN As String = "\d{9}(?:[\-_]\d+)?"
Private Const QUOTE_FILE_PATTERN As String = "^(\d{9})(?:[\-_](\d+))?(?:[ \-_]*rev\.?[ \-_]*(\d+))?.*\.xls[xm]$"
Private Const REV_FOLDER_PATTERN As String = "^rev\.?\s*(\d+)$"
' ==================================================================
' DEPENDENCIAS
' ==================================================================
Private mFileManager As clsFileManager
' ==================================================================
' INICIALIZACION
' ==================================================================
Private Sub Class_Initialize()
LogInfo MODULE_NAME, "[Class_Initialize]"
End Sub
Private Sub Class_Terminate()
Set mFileManager = Nothing
LogInfo MODULE_NAME, "[Class_Terminate]"
End Sub
'@Description: Inyecta la dependencia de FileManager
Public Sub Initialize(fm As clsFileManager)
Set mFileManager = fm
LogDebug MODULE_NAME, "[Initialize] FileManager inyectado"
End Sub
' ==================================================================
' IMPLEMENTACION DE IEconomicQuoteProvider
' ==================================================================
Private Function IEconomicQuoteProvider_GetQuotes(ByVal opportunityPath As String) As Collection
Set IEconomicQuoteProvider_GetQuotes = GetQuotes(opportunityPath)
End Function
Private Function IEconomicQuoteProvider_GetQuotesByNumber(ByVal opportunityPath As String, ByVal quoteNumber As String) As Collection
Set IEconomicQuoteProvider_GetQuotesByNumber = GetQuotesByNumber(opportunityPath, quoteNumber)
End Function
Private Function IEconomicQuoteProvider_GetQuoteById(ByVal opportunityPath As String, ByVal quoteId As String) As IEconomicQuote
Set IEconomicQuoteProvider_GetQuoteById = GetQuoteById(opportunityPath, quoteId)
End Function
Private Function IEconomicQuoteProvider_GetQuotesByRevision(ByVal opportunityPath As String) As Object
Set IEconomicQuoteProvider_GetQuotesByRevision = GetQuotesByRevision(opportunityPath)
End Function
Private Function IEconomicQuoteProvider_GetLatestQuotes(ByVal opportunityPath As String) As Collection
Set IEconomicQuoteProvider_GetLatestQuotes = GetLatestQuotes(opportunityPath)
End Function
Private Function IEconomicQuoteProvider_IdentifyQuoteFiles(ByVal opportunityPath As String) As Collection
Set IEconomicQuoteProvider_IdentifyQuoteFiles = IdentifyQuoteFiles(opportunityPath)
End Function
Private Function IEconomicQuoteProvider_IsValidQuoteFile(ByVal fileName As String) As Boolean
IEconomicQuoteProvider_IsValidQuoteFile = IsValidQuoteFile(fileName)
End Function
Private Function IEconomicQuoteProvider_ParseQuoteFileName(ByVal fileName As String) As Object
Set IEconomicQuoteProvider_ParseQuoteFileName = ParseQuoteFileName(fileName)
End Function
Private Function IEconomicQuoteProvider_GetQuotesFolder(ByVal opportunityPath As String) As String
IEconomicQuoteProvider_GetQuotesFolder = GetQuotesFolder(opportunityPath)
End Function
Private Function IEconomicQuoteProvider_QuotesFolderExists(ByVal opportunityPath As String) As Boolean
IEconomicQuoteProvider_QuotesFolderExists = QuotesFolderExists(opportunityPath)
End Function
Private Function IEconomicQuoteProvider_GetRevisionFolders(ByVal opportunityPath As String) As Collection
Set IEconomicQuoteProvider_GetRevisionFolders = GetRevisionFolders(opportunityPath)
End Function
Private Function IEconomicQuoteProvider_AssociateWithCalculation(ByVal quotePath As String, ByVal calcOption As String) As Boolean
IEconomicQuoteProvider_AssociateWithCalculation = AssociateWithCalculation(quotePath, calcOption)
End Function
' ==================================================================
' METODOS PUBLICOS
' ==================================================================
'@Description: Obtiene todas las valoraciones de una oportunidad
Public Function GetQuotes(ByVal opportunityPath As String) As Collection
Dim result As New Collection
On Error GoTo ErrHandler
Dim quoteFolder As String
quoteFolder = GetQuotesFolder(opportunityPath)
If Not mFileManager.ExisteCarpeta(quoteFolder) Then
LogWarning MODULE_NAME, "[GetQuotes] Carpeta no existe: " & quoteFolder
Set GetQuotes = result
Exit Function
End If
' Obtener ficheros de valoracion (raiz y subcarpetas de revision)
Dim files As Collection
Set files = IdentifyQuoteFiles(opportunityPath)
' Crear objetos IEconomicQuote para cada fichero
Dim filePath As Variant
Dim parsed As Object
Dim quoteSource As ExcelEconomicQuoteSource
Dim fso As Object
Set fso = CreateObject("Scripting.FileSystemObject")
For Each filePath In files
Set parsed = ParseQuoteFileName(fso.GetFileName(CStr(filePath)))
If Not parsed Is Nothing Then
' Crear ExcelEconomicQuoteSource
Set quoteSource = New ExcelEconomicQuoteSource
quoteSource.Initialize CStr(filePath), parsed
' Agregar a la coleccion usando QuoteId + revision como key
Dim key As String
key = parsed("QuoteId") & "_rev" & parsed("Revision")
On Error Resume Next
result.Add quoteSource, key
If Err.Number <> 0 Then
' Ya existe, ignorar duplicado
LogDebug MODULE_NAME, "[GetQuotes] Valoracion duplicada: " & key
Err.Clear
End If
On Error GoTo ErrHandler
End If
Next filePath
LogDebug MODULE_NAME, "[GetQuotes] Encontradas " & result.Count & " valoraciones"
Set GetQuotes = result
Exit Function
ErrHandler:
LogCurrentError MODULE_NAME, "[GetQuotes]"
Set GetQuotes = result
End Function
'@Description: Obtiene valoraciones por numero de oferta
Public Function GetQuotesByNumber(ByVal opportunityPath As String, ByVal quoteNumber As String) As Collection
Dim result As New Collection
On Error GoTo ErrHandler
Dim allQuotes As Collection
Set allQuotes = GetQuotes(opportunityPath)
Dim quote As IEconomicQuote
For Each quote In allQuotes
If quote.quoteNumber = quoteNumber Then
result.Add quote
End If
Next quote
Set GetQuotesByNumber = result
Exit Function
ErrHandler:
LogCurrentError MODULE_NAME, "[GetQuotesByNumber]"
Set GetQuotesByNumber = result
End Function
'@Description: Obtiene una valoracion por ID
Public Function GetQuoteById(ByVal opportunityPath As String, ByVal quoteId As String) As IEconomicQuote
On Error GoTo ErrHandler
Dim allQuotes As Collection
Set allQuotes = GetQuotes(opportunityPath)
Dim quote As IEconomicQuote
For Each quote In allQuotes
If quote.quoteId = quoteId Then
Set GetQuoteById = quote
Exit Function
End If
Next quote
Exit Function
ErrHandler:
LogCurrentError MODULE_NAME, "[GetQuoteById]"
End Function
'@Description: Agrupa valoraciones por revision
Public Function GetQuotesByRevision(ByVal opportunityPath As String) As Object
Dim result As Object
Set result = CreateObject("Scripting.Dictionary")
On Error GoTo ErrHandler
Dim allQuotes As Collection
Set allQuotes = GetQuotes(opportunityPath)
Dim quote As IEconomicQuote
Dim rev As Long
For Each quote In allQuotes
rev = quote.Revision
If Not result.Exists(rev) Then
result.Add rev, New Collection
End If
result(rev).Add quote
Next quote
Set GetQuotesByRevision = result
Exit Function
ErrHandler:
LogCurrentError MODULE_NAME, "[GetQuotesByRevision]"
Set GetQuotesByRevision = result
End Function
'@Description: Obtiene las valoraciones mas recientes
Public Function GetLatestQuotes(ByVal opportunityPath As String) As Collection
Dim result As New Collection
On Error GoTo ErrHandler
Dim byQuoteId As Object
Set byQuoteId = CreateObject("Scripting.Dictionary")
Dim allQuotes As Collection
Set allQuotes = GetQuotes(opportunityPath)
' Agrupar por QuoteId y quedarse con la mayor revision
Dim quote As IEconomicQuote
For Each quote In allQuotes
If Not byQuoteId.Exists(quote.quoteId) Then
byQuoteId.Add quote.quoteId, quote
Else
If quote.Revision > byQuoteId(quote.quoteId).Revision Then
Set byQuoteId(quote.quoteId) = quote
End If
End If
Next quote
' Convertir a Collection
Dim key As Variant
For Each key In byQuoteId.Keys
result.Add byQuoteId(key)
Next key
Set GetLatestQuotes = result
Exit Function
ErrHandler:
LogCurrentError MODULE_NAME, "[GetLatestQuotes]"
Set GetLatestQuotes = result
End Function
'@Description: Identifica ficheros de valoracion
Public Function IdentifyQuoteFiles(ByVal opportunityPath As String) As Collection
Dim result As New Collection
On Error GoTo ErrHandler
Dim quoteFolder As String
quoteFolder = GetQuotesFolder(opportunityPath)
If Not mFileManager.ExisteCarpeta(quoteFolder) Then
Set IdentifyQuoteFiles = result
Exit Function
End If
Dim fso As Object
Set fso = CreateObject("Scripting.FileSystemObject")
If Not fso.FolderExists(quoteFolder) Then
Set IdentifyQuoteFiles = result
Exit Function
End If
Dim folder As Object
Set folder = fso.GetFolder(quoteFolder)
' Buscar en carpeta raiz
Dim file As Object
For Each file In folder.files
If IsValidQuoteFile(file.Name) Then
result.Add file.path
End If
Next file
' Buscar en subcarpetas de revision
Dim subfolder As Object
For Each subfolder In folder.SubFolders
If IsRevisionFolder(subfolder.Name) Then
For Each file In subfolder.files
If IsValidQuoteFile(file.Name) Then
result.Add file.path
End If
Next file
End If
Next subfolder
LogDebug MODULE_NAME, "[IdentifyQuoteFiles] Encontrados " & result.Count & " ficheros"
Set IdentifyQuoteFiles = result
Exit Function
ErrHandler:
LogCurrentError MODULE_NAME, "[IdentifyQuoteFiles]"
Set IdentifyQuoteFiles = result
End Function
'@Description: Verifica si un fichero es una valoracion valida
Public Function IsValidQuoteFile(ByVal fileName As String) As Boolean
On Error GoTo ErrHandler
' Ignorar ficheros temporales
If Left(fileName, 2) = "~$" Then Exit Function
' Ignorar plantillas
If InStr(1, fileName, "plantilla", vbTextCompare) > 0 Then Exit Function
Dim regEx As Object
Set regEx = CreateObject("VBScript.RegExp")
regEx.IgnoreCase = True
regEx.Pattern = QUOTE_FILE_PATTERN
IsValidQuoteFile = regEx.Test(fileName)
Exit Function
ErrHandler:
LogCurrentError MODULE_NAME, "[IsValidQuoteFile]"
End Function
'@Description: Verifica si una carpeta es de revision
Private Function IsRevisionFolder(ByVal folderName As String) As Boolean
On Error GoTo ErrHandler
Dim regEx As Object
Set regEx = CreateObject("VBScript.RegExp")
regEx.IgnoreCase = True
regEx.Pattern = REV_FOLDER_PATTERN
IsRevisionFolder = regEx.Test(folderName)
Exit Function
ErrHandler:
LogCurrentError MODULE_NAME, "[IsRevisionFolder]"
End Function
'@Description: Parsea el nombre de un fichero de valoracion
Public Function ParseQuoteFileName(ByVal fileName As String) As Object
Dim result As Object
Set result = CreateObject("Scripting.Dictionary")
On Error GoTo ErrHandler
Dim regEx As Object
Set regEx = CreateObject("VBScript.RegExp")
regEx.IgnoreCase = True
regEx.Pattern = QUOTE_FILE_PATTERN
If Not regEx.Test(fileName) Then
Set ParseQuoteFileName = Nothing
Exit Function
End If
Dim matches As Object
Set matches = regEx.Execute(fileName)
If matches.Count > 0 Then
Dim m As Object
Set m = matches(0)
result.Add "QuoteNumber", m.SubMatches(0) ' 123456789
result.Add "CompressorIndex", IIf(Len(m.SubMatches(1)) > 0, m.SubMatches(1), "01")
result.Add "Revision", IIf(Len(m.SubMatches(2)) > 0, CLng(m.SubMatches(2)), 0)
result.Add "QuoteId", m.SubMatches(0) & IIf(Len(m.SubMatches(1)) > 0, "-" & m.SubMatches(1), "")
Set ParseQuoteFileName = result
Else
Set ParseQuoteFileName = Nothing
End If
Exit Function
ErrHandler:
LogCurrentError MODULE_NAME, "[ParseQuoteFileName]"
Set ParseQuoteFileName = Nothing
End Function
'@Description: Obtiene la ruta de la carpeta de valoraciones
Public Function GetQuotesFolder(ByVal opportunityPath As String) As String
GetQuotesFolder = opportunityPath & "\" & QUOTE_FOLDER_NAME
End Function
'@Description: Verifica si existe la carpeta de valoraciones
Public Function QuotesFolderExists(ByVal opportunityPath As String) As Boolean
If mFileManager Is Nothing Then Exit Function
QuotesFolderExists = mFileManager.ExisteCarpeta(GetQuotesFolder(opportunityPath))
End Function
'@Description: Obtiene las subcarpetas de revision
Public Function GetRevisionFolders(ByVal opportunityPath As String) As Collection
Dim result As New Collection
On Error GoTo ErrHandler
Dim quoteFolder As String
quoteFolder = GetQuotesFolder(opportunityPath)
If Not mFileManager.ExisteCarpeta(quoteFolder) Then
Set GetRevisionFolders = result
Exit Function
End If
Dim fso As Object
Set fso = CreateObject("Scripting.FileSystemObject")
Dim folder As Object
Set folder = fso.GetFolder(quoteFolder)
Dim regEx As Object
Set regEx = CreateObject("VBScript.RegExp")
regEx.IgnoreCase = True
regEx.Pattern = REV_FOLDER_PATTERN
Dim subfolder As Object
Dim revNum As Long
For Each subfolder In folder.SubFolders
If regEx.Test(subfolder.Name) Then
revNum = CLng(regEx.Execute(subfolder.Name)(0).SubMatches(0))
result.Add revNum
End If
Next subfolder
' TODO: Ordenar la coleccion
Set GetRevisionFolders = result
Exit Function
ErrHandler:
LogCurrentError MODULE_NAME, "[GetRevisionFolders]"
Set GetRevisionFolders = result
End Function
'@Description: Asocia una valoracion con un calculo
Public Function AssociateWithCalculation(ByVal quotePath As String, ByVal calcOption As String) As Boolean
On Error GoTo ErrHandler
' TODO: Implementar logica de asociacion
LogWarning MODULE_NAME, "[AssociateWithCalculation] No implementado aun"
Exit Function
ErrHandler:
LogCurrentError MODULE_NAME, "[AssociateWithCalculation]"
End Function