"-------------------------------------------------------------------------------------------------------------------------------------------------------------------------"
" TO COMPARE AND SYNCHRONIZE CURRENT ALLOCATIONS AND DESIRED ALLOCATIONS "
" INPUT FILE SHOULD BE COMMA SEPARATED AND WITHOUT HEADER "
"<asset internalNumber>,<assetoperator employeeNumber>"
"--------------------------------------------------------------------------------------------------------------------------------------------------------------------------"
"VARIABLES TO BE CHANGED ACCORDINGLY"
ioPath := 'C:\temp\'.
inputFileName := 'driverAllocation.csv'.
progressWriter := (ioPath, 'output_Allocations.txt') asFilename writeStream.
deallocateWriter := (ioPath, 'output_Deallocate.csv') asFilename writeStream.
allocateWriter := (ioPath, 'output_Allocate.csv') asFilename writeStream.
currentAllocationWriter := (ioPath, 'output_CurrentAllocation.csv') asFilename writeStream.
myCustomer := ApplicationCustomer findUniqueId: 16478639.
isSimulation := true. "IF TRUE, DATA WILL NOT CHANGE, ONLY GENERATES OUTPUT FOR VALIDATION PURPOSES."
assetsCollection := myCustomer allAssets.
assetOperatorCollection := myCustomer allAssetOperators.
currentAllocatedCollection := OrderedCollection new.
deallocateCollection := OrderedCollection new.
allocateCollection := OrderedCollection new.
[
assetsCollection do: [ :eachAsset |
allAssetOperators := eachAsset allowedOperators2 asOrderedCollection.
allAssetOperators do:[ :eachAO |
currentAllocation := OrderedCollection new.
currentAllocation add: eachAsset.
currentAllocation add: eachAO.
currentAllocatedCollection add: currentAllocation.
currentAllocationWriter nextPutAll: (eachAsset internalNumber trimBlanks , ',' , eachAO employeeNumber trimBlanks);
nextPut: Character cr.
].
].
] ensure: [ currentAllocationWriter flush; close. ].
assetIdentifierBlock := [ :myAsset | myAsset internalNumber asString trimBlanks ].
assetOperatorIdentifierBlock := [ :myDriver | myDriver employeeNumber asString trimBlanks ].
"--------------------------------------------------------------------------------------------------------------------------------------------------------------------------"
" COLLECTING ALLOCATION AND DEALOCATION DATA"
"--------------------------------------------------------------------------------------------------------------------------------------------------------------------------"
[
inputCollection := OrderedCollection new.
[
inputReader := (ioPath, inputFileName) asFilename readStream.
[ inputReader atEnd ] whileFalse: [
line := inputReader nextLine.
"tokenize each line"
inputCollection add: ((line tokensBasedOn: $,)
collect: [:x | x trimBlanks ]).
].
] ensure: [
inputReader close.
].
canIContinue := true.
inputCollection doWithIndex: [:eachTokenizedLine :lineNumber |
(eachTokenizedLine size > 2) ifTrue: [
progressWriter nextPutAll: ('found invalid column size at line: ',
lineNumber asString);
nextPut: Character cr.
canIContinue := false.
].
].
canIContinue ifFalse: [
progressWriter nextPutAll: ('invalid columns. cannot continue. aborting...');
nextPut: Character cr.
] ifTrue: [
currentAllocatedCollection doWithIndex: [ :eachAllocation :index |
tempCollection := inputCollection select: [ :x | (x first trimBlanks = (eachAllocation first internalNumber trimBlanks)) and: [ x second trimBlanks = (eachAllocation second employeeNumber trimBlanks) ]].
tempCollection isEmpty ifTrue: [
deallocateCollection add: eachAllocation.
progressWriter nextPutAll: ('DEALLOCATION: ' , eachAllocation first internalNumber trimBlanks , ',' , eachAllocation second employeeNumber trimBlanks);
nextPut: Character cr.
deallocateWriter nextPutAll: (eachAllocation first internalNumber trimBlanks , ',' , eachAllocation second employeeNumber trimBlanks);
nextPut: Character cr.
].
].
inputCollection doWithIndex: [ :eachAllocation :index |
tempCollection := currentAllocatedCollection select: [ :x | (x first internalNumber trimBlanks = (eachAllocation first trimBlanks)) and: [ x second employeeNumber trimBlanks = (eachAllocation second trimBlanks) ]].
tempCollection isEmpty ifTrue: [
allocateCollection add: eachAllocation.
progressWriter nextPutAll: ('ALLOCATION: ' ,eachAllocation first trimBlanks , ',' , eachAllocation second trimBlanks);
nextPut: Character cr.
allocateWriter nextPutAll: (eachAllocation first trimBlanks , ',' , eachAllocation second trimBlanks);
nextPut: Character cr.
].
].
].
"--------------------------------------------------------------------------------------------------------------------------------------------------------------------------"
" DEALLOCATE "
"--------------------------------------------------------------------------------------------------------------------------------------------------------------------------"
progressWriter nextPutAll: ('Deallocation in progress.....'); nextPut: Character cr.
deallocateWriter nextPutAll: ('START DEALLOCATING'); nextPut: Character cr.
deallocateCollection do: [ :each |
deallocateWriter nextPutAll:(each first internalNumber trimBlanks , ',' , each second employeeNumber trimBlanks).
isSimulation ifFalse: [
Transaction acidDo: [ :trans |
txAsset := trans transact: each first.
txAsset removeAllowedOperator: (each second).
] ifSuccessfulDo: [
deallocateWriter nextPutAll: ('...done').
((each first allowedOperators2) asOrderedCollection includes: (each second)) ifTrue: [
deallocateWriter nextPutAll: ('...validation failed').
] ifFalse: [
deallocateWriter nextPutAll: ('...validation success').
].
] ifFailedDo: [ :ex |
deallocateWriter nextPutAll: ('...failed').
].
].
deallocateWriter nextPut: Character cr.
].
deallocateWriter nextPutAll: ('DEALLOCATING COMPLETED'); nextPut: Character cr.
progressWriter nextPutAll: ('Deallocation done. Allocation in progress.....'); nextPut: Character cr.
allocateWriter nextPutAll: ('START ALLOCATING'); nextPut: Character cr.
"--------------------------------------------------------------------------------------------------------------------------------------------------------------------------"
" ALLOCATE "
"--------------------------------------------------------------------------------------------------------------------------------------------------------------------------"
allocateCollection do: [ :each |
allocateWriter nextPutAll: (each first trimBlanks , ',' , each second trimBlanks, ': ').
foundAssets := assetsCollection select: [ :thisAsset |
(assetIdentifierBlock value: thisAsset) asString
= each first.
].
(foundAssets size = 0) ifTrue: [
allocateWriter nextPutAll:
('no asset found. ').
canIContinue := false.
].
(foundAssets size > 1) ifTrue: [
allocateWriter nextPutAll:
('more than one asset. ').
canIContinue := false.
].
foundAssetOperators := assetOperatorCollection select: [ :thisOperator |
(assetOperatorIdentifierBlock value: thisOperator) asString
= each second.
].
(foundAssetOperators size = 0) ifTrue: [
allocateWriter nextPutAll:
('no driver found. ').
canIContinue := false.
].
(foundAssetOperators size > 1) ifTrue: [
allocateWriter nextPutAll:
('more than one driver found. ').
canIContinue := false.
].
canIContinue ifFalse: [
allocateWriter nextPut: Character cr.
] ifTrue: [
allocateWriter nextPutAll: ('allocating').
"asset and operator found, updating..."
"do actual asset operator allocation"
isSimulation ifFalse: [
Wildcat.Transaction acidDo: [ :tx |
txAsset := tx transact: foundAssets first.
txOperator := tx transact: foundAssetOperators first.
txAsset insertAllowedOperator: txOperator.
] ifSuccessfulDo: [
allocateWriter nextPutAll: ('...done').
((foundAssets first allowedOperators2) asOrderedCollection includes: (foundAssetOperators first)) ifTrue: [
allocateWriter nextPutAll: ('...validation success').
] ifFalse: [
allocateWriter nextPutAll: ('...validation failed').
].
] ifFailedDo: [ :ex |
allocateWriter nextPutAll: ('...failed').
].
].
allocateWriter nextPut: Character cr.
].
].
allocateWriter nextPutAll: ('ALLOCATION COMPLETED'); nextPut: Character cr.
progressWriter nextPutAll: ('Allocation done.'); nextPut: Character cr.
] ensure: [
progressWriter flush; close.
deallocateWriter flush; close.
allocateWriter flush; close.
].