* This program will take a select list input by the user and change either an attribute, multivalue, or subvalue
* on a file specified by the user.  The user identifies which attribute,multivalue, or subvalue to chnange and
* the new value to be written.

* This program will alter your data.  You accept all responsibility when using this program.  Be careful when using
* it.  Be absolutely sure to test the program in a test account.  Especially if you make changes to the source code.

*********** LEGAL SECTION *****************
*BY INSTALLING, COMPILING, COPYING OR OTHERWISE USING THE FOLLOWING SOURCE CODE AND/OR THE RESULTING PROGRAM FROM THE 
*COMPILATION OF SOURCE CODE, YOU AGREE TO BE BOUND BY THE TERMS AND CONDITIONS OF THE 
*FOLLOWING.  IF YOU DO NOT AGREE TO THESE TERMS AND CONDITIONS, DO NOT INSTALL, COPY OR USE THE FOLLOWING SOURCE CODE.

*Limitation of Liability.  In the event the Product malfunctions and/or Roehsner or his contractors and subcontractors 
*are negligent, and such malfunction or negligence is the sole and direct cause of the inaccurate or inadequate results 
*obtained by Client or the loss, alteration, or improper access of Client's programs or data, or any other event or 
*circumstance, the collective liability of Roehsner and his contractors and subcontractors shall be limited to 
*general money damages in an amount not to exceed the Purchase Price listed above.  Such limitation shall be the 
*extent of liability of Roehsner and his contractors and subcontractors in the event of any alleged defaults by 
*Roehsner and/or his contractors and subcontractors under this Agreement, including alleged acts of negligence or 
*breach of contract and regardless of the form in which any legal or equitable action may be brought against Roehsner 
*and/or his contractors and subcontractors, and the foregoing shall constitute Client's exclusive remedy.  Under no 
*circumstances shall Roehsner and/or his contractors and subcontractors be liable for any loss of profits or for special, 
*consequential, or exemplary damages, even if Roehsner and/or his contractors and subcontractors has been advised of 
*the possibility of such damages.  Additionally, no action regardless of form arising out of the services under this 
*Agreement may be brought by either party more than one (1) year after the cause of action has accrued, except that 
*an action for nonpayment may be brought within the applicable statute of limitations for contracts under seal, based 
*upon the laws of the State of Maryland.
************ END OF LEGAL SECTION *************


******* Initialization ********
FileToChange=""
NameOfListOfRecordsToChange=""
ListOfRecordsToChange=""
LoopDone=0
AttributeToChange=0
MultiValueToChange=0
SubValueToChange=0
NewDataToWrite=""
RecordNumber=0
NumberOfRecordsToChange=0
* DataArray is an array that will be defined once we now the number of items in the user provided list

*******  Main ******

* Go get the name of the list and file, also which value to change and the new data
GOSUB GetUserInfo:


* Read the data to be changed
GOSUB ReadOldData:


* Write the changes to data change tracking if you have purchased that software from GHR Concepts.
*GOSUB WriteDataChange:


* Write the new data to the record
GOSUB WriteNewData:


* End of program
GOSUB EndProgram:



******************************* GetUserInfo ****************************************
GetUserInfo:

   * Clear the screen and print the title
   PRINT @(-1)
   PRINT @(27,1):"File Data Changing Program"

   PRINT @(2,3):"Enter Saved List to use":
   INPUT NameOfListOfRecordsToChange
   	
   * If the users quits then end the program
   IF UPCASE(NameOfListOfRecordsToChange)="END" THEN STOP
   	
   * If the list isn't valid, exit.  When automating there might be an empty list
   * so we need to stop if this is the case
   GETLIST NameOfListOfRecordsToChange TO ListOfRecordsToChange SETTING NumberOfRecordsToChange ELSE
   	PRINT @(2,4):"INVALID LIST OR LIST IS EMPTY, PROGRAM STOPPED"
   	STOP
   END
   
   

   * Reset the loop control variable LoopDone
   LoopDone=0
    
   LOOP
   	PRINT @(2,5):"Enter the file name:":
   	INPUT FileToChange

   	* If the users quits then end the program
   	IF UPCASE(FileToChange)="END" THEN STOP
   	
   	* Until the file can be opened, keep asking for it
   	OPEN FileToChange TO F.FileToChange THEN LoopDone = 1
   
   UNTIL LoopDone DO REPEAT

   * Reset the loop control variable LoopDone
   
   LoopDone=0
   
   * Input the attribute that will be changed, if it is not a number or if they type end then stop the program
   PRINT @(2,7):"Enter the Attribute to change":
   INPUT AttributeToChange

   IF UPCASE(AttributeToChange)="END" THEN
        STOP
   END ELSE 
   	IF NOT(NUM(AttributeToChange)) THEN
        	PRINT @(2,8):AttributeToChange:" is not a number! Try again"
   		STOP
   	END
   END
   
   	
   * Input the multivalue that will be changed, if it is not a number or if they type end then stop the program
   PRINT @(2,9):"Enter the Multivalue to change or (0) to change the whole Attribute":
   INPUT MultiValueToChange

   IF UPCASE(MultiValueToChange)="END" THEN
        STOP
   END ELSE 
   	IF NOT(NUM(MultiValueToChange)) THEN
        	PRINT @(2,10):MultiValueToChange:" is not a number! Try again"
        	STOP
        END
   END
   
   
   * If the multivalue isn't 0 then they do want to change a subvalue so input the subvalue that will be 
   * changed, if it is not a number or if they type end then stop the program 
   
   IF MultiValueToChange NE 0 THEN
	   PRINT @(2,11):"Enter the Subvalue to change or (0) to change the whole Multivalue":
	   INPUT SubValueToChange
   
           IF UPCASE(SubValueToChange)="END" THEN
        	STOP
   	   END ELSE 
   	   	IF NOT(NUM(SubValueToChange)) THEN
        		PRINT @(2,12):SubValueToChange:" is not a number! Try again"
        		STOP
        	END
   	   END
   END

   
   * Now get the new data to be written
   PRINT @(2,13):"Enter the new data you want written or (NULL) for nothing":
   INPUT NewDataToWrite

   IF UPCASE(NewDataToWrite)="END" THEN
   	PRINT @(2,14):"YOU CHOSE TO END, WE WILL NOW QUIT THE PROGRAM"
   	STOP
   END
   
   IF UPCASE(NewDataToWrite)="NULL" THEN NewDataToWrite = ""
   
   * If the Data Change Tracking subroutine has been purchased then input the description of the change being made
   * PRINT @(2,15):"Enter the description of the change to be written to Data Change Tracking":
   * INPUT ChangeDescription
   
   
   * Display what we are changing and ask if ok to continue
   PRINT @(2,16):"Change Attribute ":AttributeToChange:", MultiValue ":MultiValueToChange:", SubValue ":SubValueToChange:", in file ":FileToChange
   PRINT @(2,17):"to ":NewDataToWrite:" for list ":NameOfListOfRecordsToChange:" - Continue (Y/N)":
   INPUT OkToContinue
   
   * If anything other than Y is entered then stop the program
   IF UPCASE(OkToContinue) NE "Y" THEN 
   	PRINT @(2,19):"YOU CHOSE TO END, WE WILL NOW QUIT THE PROGRAM"
   	STOP
   END


* Now that we have the info we will return to where we came from
RETURN


******************************** Read the data currently on file **********************************
ReadOldData:

   * Dimension the OldDataArray array now that we know the number of items in the user provided list
   
   DIM DataArray(NumberOfRecordsToChange,2)
   
   
   * Go through each item in the list and read in the data to be changed from each record storing the record id and data in 
   * the DataArray variable
   
   FOR RecordNumber = 1 TO NumberOfRecordsToChange
        
        * Read the record ids from the user supplied list
        READNEXT DataArray(RecordNumber,1) FROM ListOfRecordsToChange ELSE CONTINUE
           
        
        * Read the data that will be changed from the file, if we can't then stop because the user
        * may have not used the right list with the right file
        
        READV DataArray(RecordNumber,2) FROM F.FileToChange,DataArray(RecordNumber,1),AttributeToChange ELSE
            PRINT @(2,25):"CAN'T READ RECORD ":DataArray(RecordNumber,1):" FROM FILE ":FileToChange:", CHECK YOUR INFORMATION AND RERUN"
            STOP
        END
        
   NEXT RecordNumber        
           

RETURN

******************************** Data Change Tracking *******************************************
*WriteDataChange:

* If you purchase the Data Change Tracking subroutine from GHR Concepts you get the updated version of
* this program that will write these changes to data change tracking.



******************************** Write New Data ****************************************************
WriteNewData:

   * Go through each item in the DataArray and assign the new data to the appropriate value then write 
   * the changed data to the file
   
   FOR RecordNumber = 1 TO NumberOfRecordsToChange
   	
   	* Assign the new data
   	DataArray(RecordNumber,2)<1,MultiValueToChange,SubValueToChange> = NewDataToWrite
   	
   	* Write the data to file
   	WRITEV DataArray(RecordNumber,2) ON F.FileToChange,DataArray(RecordNumber,1),AttributeToChange
   
   NEXT RecordNumber
   
   
   

RETURN

************************************ End Of Program ******************************************
EndProgram:
