×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: Text
Posted by: user82d7a
Added: Jul 28, 2015 6:14 PM
Views: 6
Tags: no tags
something
  1. "-------------------------------------------------------------------------------------------------------------------------------------------------------------------------"
  2. " TO COMPARE AND SYNCHRONIZE CURRENT ALLOCATIONS AND DESIRED ALLOCATIONS "
  3. " INPUT FILE SHOULD BE COMMA SEPARATED AND WITHOUT HEADER "
  4. "<asset internalNumber>,<assetoperator employeeNumber>"
  5. "--------------------------------------------------------------------------------------------------------------------------------------------------------------------------"
  6.  
  7. "VARIABLES TO BE CHANGED ACCORDINGLY"
  8. ioPath := 'C:\temp\'.
  9. inputFileName := 'driverAllocation.csv'.
  10. progressWriter := (ioPath, 'output_Allocations.txt') asFilename writeStream.
  11. deallocateWriter := (ioPath, 'output_Deallocate.csv') asFilename writeStream.
  12. allocateWriter := (ioPath, 'output_Allocate.csv') asFilename writeStream.
  13. currentAllocationWriter := (ioPath, 'output_CurrentAllocation.csv') asFilename writeStream.
  14. myCustomer := ApplicationCustomer findUniqueId: 16478639.
  15. isSimulation := true. "IF TRUE, DATA WILL NOT CHANGE, ONLY GENERATES OUTPUT FOR VALIDATION PURPOSES."
  16.  
  17. assetsCollection := myCustomer allAssets.
  18. assetOperatorCollection := myCustomer allAssetOperators.
  19. currentAllocatedCollection := OrderedCollection new.
  20. deallocateCollection := OrderedCollection new.
  21. allocateCollection := OrderedCollection new.
  22.  
  23. [
  24.         assetsCollection do: [ :eachAsset |
  25.                 allAssetOperators := eachAsset allowedOperators2 asOrderedCollection.
  26.                 allAssetOperators do:[ :eachAO |
  27.                         currentAllocation := OrderedCollection new.
  28.                         currentAllocation add: eachAsset.
  29.                         currentAllocation add: eachAO.
  30.                         currentAllocatedCollection add: currentAllocation.
  31.                         currentAllocationWriter nextPutAll:  (eachAsset internalNumber trimBlanks , ',' , eachAO employeeNumber trimBlanks);
  32.                                         nextPut: Character cr.
  33.                 ].
  34.         ].
  35. ] ensure: [ currentAllocationWriter flush; close. ].
  36.  
  37. assetIdentifierBlock := [ :myAsset | myAsset internalNumber asString trimBlanks ].
  38. assetOperatorIdentifierBlock := [ :myDriver | myDriver employeeNumber asString trimBlanks ].
  39.  
  40.  
  41. "--------------------------------------------------------------------------------------------------------------------------------------------------------------------------"
  42. " COLLECTING ALLOCATION AND DEALOCATION DATA"
  43. "--------------------------------------------------------------------------------------------------------------------------------------------------------------------------"
  44. [
  45.         inputCollection := OrderedCollection new.
  46.         [
  47.                 inputReader := (ioPath, inputFileName) asFilename readStream.
  48.                 [ inputReader atEnd ] whileFalse: [
  49.                         line := inputReader nextLine.
  50.                         "tokenize each line"
  51.                         inputCollection add: ((line tokensBasedOn: $,)
  52.                                 collect: [:x  | x trimBlanks ]).
  53.                 ].
  54.         ] ensure: [
  55.                 inputReader close.
  56.         ].
  57.         canIContinue := true.
  58.         inputCollection doWithIndex: [:eachTokenizedLine :lineNumber |
  59.                 (eachTokenizedLine size > 2) ifTrue: [
  60.                         progressWriter nextPutAll: ('found invalid column size at line: ',
  61.                                 lineNumber asString);
  62.                                 nextPut: Character cr.
  63.                         canIContinue := false.
  64.                 ].
  65.         ].
  66.         canIContinue ifFalse: [
  67.                 progressWriter nextPutAll: ('invalid columns. cannot continue. aborting...');
  68.                         nextPut: Character cr.
  69.         ] ifTrue: [
  70.                 currentAllocatedCollection doWithIndex: [ :eachAllocation  :index |  
  71.                         tempCollection := inputCollection select: [ :x | (x first trimBlanks = (eachAllocation first internalNumber trimBlanks)) and: [ x second trimBlanks = (eachAllocation second employeeNumber trimBlanks) ]].
  72.                         tempCollection isEmpty ifTrue: [
  73.                                 deallocateCollection add: eachAllocation.
  74.                                 progressWriter nextPutAll: ('DEALLOCATION: ' , eachAllocation first internalNumber trimBlanks , ',' , eachAllocation second employeeNumber trimBlanks);
  75.                                         nextPut: Character cr.
  76.                                 deallocateWriter nextPutAll: (eachAllocation first internalNumber trimBlanks , ',' , eachAllocation second employeeNumber trimBlanks);
  77.                                         nextPut: Character cr.
  78.                         ].
  79.                 ].
  80.                 inputCollection doWithIndex: [ :eachAllocation  :index |  
  81.                         tempCollection := currentAllocatedCollection select: [ :x | (x first internalNumber trimBlanks = (eachAllocation first trimBlanks)) and: [ x second employeeNumber trimBlanks = (eachAllocation second trimBlanks) ]].
  82.                         tempCollection isEmpty ifTrue: [
  83.                                 allocateCollection add: eachAllocation.
  84.                                 progressWriter nextPutAll: ('ALLOCATION: ' ,eachAllocation first trimBlanks , ',' , eachAllocation second trimBlanks);
  85.                                         nextPut: Character cr.
  86.                                 allocateWriter nextPutAll: (eachAllocation first trimBlanks , ',' , eachAllocation second trimBlanks);
  87.                                         nextPut: Character cr.
  88.                         ].
  89.                 ].
  90.         ].
  91.        
  92. "--------------------------------------------------------------------------------------------------------------------------------------------------------------------------"
  93. " DEALLOCATE "
  94. "--------------------------------------------------------------------------------------------------------------------------------------------------------------------------"
  95.         progressWriter nextPutAll: ('Deallocation in progress.....'); nextPut: Character cr.
  96.         deallocateWriter nextPutAll: ('START DEALLOCATING'); nextPut: Character cr.
  97.  
  98.         deallocateCollection do: [ :each |
  99.                 deallocateWriter nextPutAll:(each first internalNumber trimBlanks , ',' , each second employeeNumber trimBlanks).
  100.                 isSimulation ifFalse: [
  101.                         Transaction acidDo: [ :trans |
  102.                                 txAsset := trans transact: each first.
  103.                                 txAsset removeAllowedOperator: (each second).
  104.                         ] ifSuccessfulDo: [
  105.                                 deallocateWriter nextPutAll: ('...done').
  106.                                 ((each first allowedOperators2) asOrderedCollection includes: (each second)) ifTrue: [
  107.                                         deallocateWriter nextPutAll: ('...validation failed').
  108.                                 ] ifFalse: [
  109.                                         deallocateWriter nextPutAll: ('...validation success').
  110.                                 ].
  111.                         ] ifFailedDo: [ :ex |
  112.                                 deallocateWriter nextPutAll: ('...failed').
  113.                         ].
  114.                 ].
  115.                 deallocateWriter nextPut: Character cr.
  116.         ].
  117.  
  118.         deallocateWriter nextPutAll: ('DEALLOCATING COMPLETED'); nextPut: Character cr.
  119.         progressWriter nextPutAll: ('Deallocation done. Allocation in progress.....'); nextPut: Character cr.
  120.         allocateWriter nextPutAll: ('START ALLOCATING'); nextPut: Character cr.
  121.  
  122. "--------------------------------------------------------------------------------------------------------------------------------------------------------------------------"
  123. " ALLOCATE "
  124. "--------------------------------------------------------------------------------------------------------------------------------------------------------------------------"
  125.         allocateCollection do: [ :each |
  126.                 allocateWriter nextPutAll: (each first trimBlanks , ',' , each second trimBlanks, ': ').
  127.                 foundAssets := assetsCollection select: [ :thisAsset |
  128.                         (assetIdentifierBlock value: thisAsset) asString
  129.                                         = each first.
  130.                         ].
  131.                         (foundAssets size = 0) ifTrue: [
  132.                                 allocateWriter nextPutAll:
  133.                                         ('no asset found. ').
  134.                                 canIContinue := false.
  135.                         ].
  136.                         (foundAssets size > 1) ifTrue: [
  137.                                 allocateWriter nextPutAll:
  138.                                         ('more than one asset. ').
  139.                                 canIContinue := false.
  140.                         ].
  141.                         foundAssetOperators := assetOperatorCollection select: [ :thisOperator |
  142.                                 (assetOperatorIdentifierBlock value: thisOperator) asString
  143.                                         = each second.
  144.                         ].
  145.                         (foundAssetOperators size = 0) ifTrue: [
  146.                                 allocateWriter nextPutAll:
  147.                                         ('no driver found. ').
  148.                                 canIContinue := false.
  149.                         ].
  150.                         (foundAssetOperators size > 1) ifTrue: [
  151.                                 allocateWriter nextPutAll:
  152.                                         ('more than one driver found. ').
  153.                                 canIContinue := false.
  154.                         ].             
  155.                         canIContinue ifFalse: [
  156.                                 allocateWriter nextPut: Character cr.
  157.                         ] ifTrue: [
  158.                                 allocateWriter nextPutAll: ('allocating').
  159.                                 "asset and operator found, updating..."
  160.                                 "do actual asset operator allocation"
  161.                                 isSimulation ifFalse: [
  162.                                         Wildcat.Transaction acidDo: [ :tx |
  163.                                                 txAsset := tx transact: foundAssets first.
  164.                                                 txOperator := tx transact: foundAssetOperators first.
  165.                                                 txAsset insertAllowedOperator: txOperator.
  166.                                         ] ifSuccessfulDo: [
  167.                                                 allocateWriter nextPutAll: ('...done').
  168.                                                 ((foundAssets first allowedOperators2) asOrderedCollection includes: (foundAssetOperators first)) ifTrue: [
  169.                                                         allocateWriter nextPutAll: ('...validation success').
  170.                                                 ] ifFalse: [
  171.                                                         allocateWriter nextPutAll: ('...validation failed').
  172.                                                 ].
  173.                                         ] ifFailedDo: [ :ex |
  174.                                                 allocateWriter nextPutAll: ('...failed').
  175.                                         ].
  176.                                 ].
  177.                                 allocateWriter nextPut: Character cr.
  178.                         ].
  179.         ].
  180.         allocateWriter nextPutAll: ('ALLOCATION COMPLETED'); nextPut: Character cr.
  181.         progressWriter nextPutAll: ('Allocation done.'); nextPut: Character cr.
  182. ] ensure: [
  183.         progressWriter flush; close.
  184.         deallocateWriter flush; close.
  185.         allocateWriter flush; close.
  186. ].
  187.