home *** CD-ROM | disk | FTP | other *** search
/ High Voltage Shareware / high1.zip / high1 / DIR36 / C7101.ZIP / CHILD.TPX < prev    next >
Text File  |  1994-01-12  |  23KB  |  450 lines

  1. #!┌───────────────────────────┤Template Segment├───────────┬─────────────────┐
  2. #!│                               CHILD.TPX                │Version: 3007.000│
  3. #!├───────────────────────────────┤Contents├───────────────┴─────────────────┤
  4. #!│Structure             Type       Description                              │
  5. #!│────────────────────  ─────────  ─────────────────────────────────────────│
  6. #!│Child                 PROCEDURE  Update a batch of child records          │
  7. #!├───────────────────────────────┤Comments├─────────────────────────────────┤
  8. #!│Version   Comments                                                        │
  9. #!│────────  ────────────────────────────────────────────────────────────────│
  10. #!│3007.000  Release of CDD3 version 3007 templates                          │
  11. #!└──────────────────────────────────────────────────────────────────────────┘
  12. #!
  13. #PROCEDURE(Child,'Update child records from a parent'),SCREEN,PULLDOWN
  14. #!
  15. #!┌──────────────────────────┤Procedure Template├──────────┬─────────────────┐
  16. #!│                                 Child                  │Version: 3007.000│
  17. #!├──────────────────────────────┤Description├─────────────┴─────────────────┤
  18. #!│ The Child template scrolls child records from a file on the screen       │
  19. #!│ and allows updating the records on the same screen.  A parent            │
  20. #!│ file must be identified and a valid parent record must be in memory      │
  21. #!│ when this procedure is called.                                           │
  22. #!│                                                                          │
  23. #!│ The QUEUE will contain all children records for a particular             │
  24. #!│ Parent record.  Changes to the children records are made to the          │
  25. #!│ QUEUE and are only written to disk upon completion of the OK             │
  26. #!│ button.  The update process is framed within a logged transaction.       │
  27. #!│ The child file must use a file driver which supports transaction         │
  28. #!│ processing in order to use this template.                                │
  29. #!│                                                                          │
  30. #!│ The Child template's screen will contain a scrolling listbox             │
  31. #!│ With Add, Change, Delete, Ok and Cancel pushbuttons and a fixed          │
  32. #!│ field entry area with Save and Exit Buttons.                             │
  33. #!│                                                                          │
  34. #!│ The Child template does not support autonumbering of keys.               │
  35. #!├───────────────────────────────┤Comments├─────────────────────────────────┤
  36. #!│Version   Comments                                                        │
  37. #!│────────  ────────────────────────────────────────────────────────────────│
  38. #!│3007.000  Release of CDD3 version 3007 templates                          │
  39. #!└──────────────────────────────────────────────────────────────────────────┘
  40. #!
  41. #DISPLAY('')
  42. #PROMPT('First Upd&ate Field',FIELD),%FirstUpdateField
  43. #PROMPT('Parent F&ile',FILE),%ParentFile
  44. #PROMPT('Exit on &Null Parent',CHECK),%NullParentExit
  45. #PROMPT('Upd&ate Parent on OK',CHECK),%PutParent
  46. #INSERT(%SetChildSymbols)
  47. #PROTOTYPE('')
  48.  
  49. %Procedure       PROCEDURE
  50.  
  51. #INSERT(%FileControl)
  52. #FIX(%File,%Primary)
  53. RecordQueue      QUEUE,PRE(SAV)
  54. Line              STRING(%ScreenFieldQueueSize) #<! Line to be scrolled
  55. SaveRecord        LIKE(%FilePre:Record),PRE(SAV)
  56. SkipRecord        BYTE
  57. #FIX(%Key,%PrimaryKey)
  58. #SET(%FirstField, %Null)
  59. #SET(%SortString,%Null)
  60. #FOR(%KeyField)
  61.   #FIX(%Field,%KeyField)
  62.   #IF(%FirstField = %Null)
  63.     #SET(%FirstField, %KeyField)
  64.     #SET(%FirstFieldSequence, %KeyFieldSequence)
  65.   #ENDIF
  66.   #IF(%KeyFieldSequence <> 'DESCENDING')
  67.     #SET(%SortString, (CLIP(LEFT(%SortString)) & ',+SAV:' & %FieldID))
  68.   #ELSE
  69.     #SET(%SortString, (CLIP(LEFT(%SortString)) & ',-SAV:' & %FieldID))
  70.   #ENDIF
  71. #ENDFOR
  72. RecordPosition    STRING(256)
  73. #FOR(%Field)
  74.   #IF(%FieldType = 'MEMO')
  75.     #SET(%MemoField,%FieldID)
  76. %FieldID          STRING(SIZE(%Field))         #<! Restore the Memos
  77.   #ENDIF
  78. #ENDFOR
  79.                  .                             #<!End Queue structure
  80.  
  81. FirstPage        BYTE(1)                         ! Page display variable
  82. EntryMode        BYTE(0)                         ! Toggles for entry mode
  83. ScrollMode       EQUATE(1)                       !   ScrollMode or
  84. UpdateMode       EQUATE(2)                       !   UpdateMode
  85. DRecs            SHORT                           ! Number of Child records
  86. QRecs            SHORT                           ! Number of QUEUE records
  87. I                BYTE                            ! QUEUE record pointer
  88. ChildAction      BYTE(0)                         ! Update mode
  89. NoMoreFields     BYTE(0)                         ! No more fields
  90. TransactionError BYTE(0)                         ! Transaction Error
  91. RecordEntryOne   BYTE(0)                         ! Starting record in QUEUE
  92.  
  93. %LocalData
  94. %ScreenStructure
  95. %PulldownStructure
  96. #EMBED('Data Section')
  97.  
  98.   CODE
  99.   #EMBED('Setup Procedure')
  100.   #INSERT(%NullParentCheck)                     #!Return if blank parent
  101.   #INSERT(%FileControl)
  102.   #INSERT(%HoldParentRecord)                    #!Hold the parent record
  103.   OPEN(SCREEN)                                   !Open the screen
  104.   #EMBED('Setup Screen')
  105.   #IF(%Pulldown)                                #!If a Pulldown exists
  106.   OPEN(%Pulldown)                              #<!Open the Pulldown
  107.   #ENDIF
  108.   DO EnterScrollMode                             !Select Scrolling mode
  109.   DO FillQueues                                  !Fill the Queues
  110.   DRecs = RECORDS(RecordQueue)                   !Save the number of children
  111.   #FIX(%File,%Primary)
  112.   DISPLAY                                        !Show the listbox
  113.   LOOP
  114.     #INSERT(%GenerateFormulas)                  #!Generate all formulas
  115.     #EMBED('Top of Accept Loop')
  116.     CASE SELECTED()                              !Jump to setup routine
  117.       OF NoMoreFields
  118.         SELECT(?Save)
  119.         DO EnterScrollMode                       ! Switch modes
  120.       #INSERT(%ScreenSetupRoutines)
  121.     END                                          !End CASE
  122.     ACCEPT                                       !Accept user input
  123.  
  124.     CASE KEYCODE()
  125.     #FOR(%HotKey)
  126.     OF %HotKey                                 #<!User defined HotKey
  127.       %HotKeyProc                              #<!HotKey Procedure
  128.     #ENDFOR
  129.     END
  130.  
  131.     IF EntryMode = ScrollMode                    !If processing the ScrollMode
  132.       IF RECORDS(RecordQueue) = %FixRows       #<!  If deleted last record.
  133.         DISABLE(?Change)                         !    Disable the change button
  134.         DISABLE(?Delete)                         !    Disable the delete button
  135.       END                                        !  End IF
  136.       CASE FIELD()                               ! Jump to edit routine
  137.       OF ?List                                   ! Process the List box
  138.         GET(RecordQueue,CHOICE())              #<! Get the Record Data
  139.         IF ERRORCODE() THEN CYCLE.               ! Cycle if no records
  140.     #FIX(%File,%Primary)
  141.         %FilePre:Record = SAV:SaveRecord       #<! Fill the fields
  142.     #FOR(%Field)
  143.       #IF(%FieldType = 'MEMO')
  144.         #SET(%MemoField,%FieldID)
  145.         %Field = SAV:%FieldID                  #<!  Restore the Memos
  146.       #ENDIF
  147.     #ENDFOR
  148.         #INSERT(%GetChildSecondary)
  149.         DISPLAY                                  ! and re-display
  150.         IF KEYCODE() = MouseLeft2                ! On Mouse double click
  151.           PRESS(EnterKey)                        !  Press the EnterKey
  152.         END                                      ! End IF
  153.       OF ?Insert                                 ! Process the Insert Button
  154.         #FIX(%File,%Primary)
  155.         ChildAction = AddRecord                  !  Set to adding a record
  156.         #INSERT(%ClearFileFields)              #<!  Clear the record for entry
  157.         #INSERT(%GetChildSecondary)
  158.         DO EnterUpdateMode                       ! Switch to update mode
  159.         #IF(%InitRoutine)                        #<!Field(s) initial value
  160.         DO InitializeFields                      !Initial values from dictionary
  161.         #ENDIF
  162.       OF ?Change                                 ! Process the Change Button
  163.         ChildAction = ChangeRecord               !  Set to Changing a record
  164.         DO EnterUpdateMode                       !  Switch modes
  165.       OF ?Delete                                 ! Process the Delete Button
  166.         ChildAction = DeleteRecord               !  Set to Deleting a record
  167.         DELETE(RecordQueue)                      !  Delete Record Queue Entry
  168.         #INSERT(%ClearFileFields)                #<! Clear the record for entry
  169.         DISPLAY                                  !  Redisplay the list box
  170.         IF RECORDS(RecordQueue) = %FixRows       #<! If deleted last record.
  171.           DISABLE(?Change)                       !    Disable the change button
  172.           DISABLE(?Delete)                       !    Disable the delete button
  173.           SELECT(?Insert)                        !    Select the insert button
  174.         ELSE                                     !  Else
  175.           SELECT(?List)                          !    Select the list box
  176.         END                                      !  End IF
  177.         ChildAction = 0                          !  Reset the Action
  178.         CYCLE                                    !  Cycle to accept input
  179.       OF ?Ok                                     ! Process the Ok Button
  180.         QRecs = RECORDS(RecordQueue)             !  Save the queue record count
  181.   #FIX(%File, %Primary)
  182.         #INSERT(%ClearFileFields)              #<!Clear the record buffer
  183.         TransactionError = 0                     !Clear Transaction error
  184.   #FIX(%File, %ParentFile)
  185.   #FIX(%Relation,%Primary)
  186.   #IF(%RelationType = '1:MANY')
  187.     #FOR(%RelationKeyField)
  188.       #IF(%RelationKeyFieldLink)
  189.         %RelationKeyField = %RelationKeyFieldLink #<!Assign linking field value
  190.       #ENDIF
  191.     #ENDFOR
  192.   #ENDIF
  193.         SET(%RelationKey,%RelationKey)         #<!Set to the matching record
  194.         LOGOUT(2,%Primary)                     #<!Enable transaction logging
  195.         IF ERRORCODE() = BadTranErr              !If transaction error occurs
  196.           #INSERT(%BadTrxPrimaryMsg)
  197.           SELECT(?Ok)                            ! Reselect the Ok button
  198.           CYCLE                                  ! Cycle to ACCEPT input
  199.         END                                      !End IF
  200.         #INSERT(%UpdateChildRecords)
  201.         IF TransactionError                      !If transaction error occurs
  202.           #INSERT(%BadTrxChildMsg)
  203.           ROLLBACK                               ! Rollback the changes.
  204.           SELECT(?List,I)                        ! Reselect the List box
  205.           PRESS(EnterKey)                        ! Setup to change record
  206.           CYCLE                                  ! Cycle to ACCEPT input
  207.         ELSE                                     !Else
  208.           COMMIT                                 ! Commit the changes to disk
  209.         END                                      !End IF
  210.         #INSERT(%PutParentFile)
  211.         DO ProcedureReturn                       ! Break to Return to Caller
  212.       OF ?Cancel                                 !Process the Cancel Button
  213.         DO ProcedureReturn                       ! Break to Return to Caller
  214.       END                                        !End CASE
  215.     ELSE                                         ! Else if update mode
  216.       #INSERT(%GetChildSecondary)
  217.       DISPLAY                                    ! Display the new record
  218.       CASE FIELD()                                   !
  219.   #FOR(%ScreenField)
  220.     #IF(%ScreenField='?OK')
  221.     #ELSIF(%ScreenField='?Cancel')
  222.     #ELSIF(%ScreenField='?Save')
  223.       OF ?Save                                   ! Process the Save Button
  224.         #FIX(%ScreenField,'?Save')               #! Set current active to ?Save
  225.         #IF(%ScreenFieldEdit)                    #! Check for Field Edits
  226.         %ScreenFieldEdit
  227.         #ENDIF
  228.         CASE ChildAction                         ! Adding or Changing?
  229.         OF AddRecord                             ! When adding a new record.
  230.           #INSERT(%FillQueueFields)
  231.           ADD(RecordQueue %SortString)         #<! Add to the sorted queue
  232.           ChildAction = 0                        ! Reset the Action value
  233.         OF ChangeRecord                          ! When changing a record
  234.           #INSERT(%FillQueueFields)
  235.           PUT(RecordQueue %SortString)         #<! Add to the queue
  236.           ChildAction = 0                        ! Reset the Action value
  237.         END                                      ! Case
  238.         SELECT(1)                                !Start with the first field
  239.         SELECT                                   !and cycle non-stop
  240.         CYCLE                                    !restart main process loop
  241.     #ELSIF(%ScreenField='?Exit')
  242.       OF ?Exit                                   ! Process the Exit Button
  243.         #FIX(%ScreenField,'?Exit')               #! Set current active to ?Exit
  244.         #IF(%ScreenFieldEdit)                    #! Check for Field Edits
  245.         %ScreenFieldEdit
  246.         #ENDIF
  247.         DO EnterScrollMode                       ! Switch modes
  248.       END
  249.     #ELSE
  250.       #INSERT(%ScreenEditRoutines)
  251.     #ENDIF
  252.   #ENDFOR
  253.     END                                          ! End IF
  254.     CASE FIELD()
  255.   #FOR(%PulldownField)
  256.     #IF(%PulldownFieldProc <> %NULL)
  257.     OF %PulldownField                          #<!For a selected menu item
  258.   %PulldownFieldProc                           #<!Call the procedure
  259.     #ENDIF
  260.   #ENDFOR
  261.     END                                          !End CASE for Pulldowns
  262.   END                                            !End LOOP
  263. !─────────────────────────────────────────────────────────────────────────────
  264. ProcedureReturn ROUTINE
  265.   #EMBED('Prior to Return')
  266.   #IF(%Pulldown)                                #!If a Pulldown exists
  267.   CLOSE(%Pulldown)                             #<!Open the Pulldown
  268.   #ENDIF
  269.   #IF(%SharedFiles)
  270.   RELEASE(%ParentFile)                         #<!Release held parent record
  271.   #ENDIF
  272.   FREE(RecordQueue)                              !Free the QUEUE memory
  273.   #EMBED('Before Closing Screen')
  274.   CLOSE(%Screen)
  275.   #EMBED('Before Closing Files')
  276.   #INSERT(%FileControl)
  277.   DO EndOfProcedureEmbed
  278.   RETURN
  279. !─────────────────────────────────────────────────────────────────────────────
  280. EndOfProcedureEmbed ROUTINE
  281. #EMBED('End of Procedure')
  282. #EMBED('Custom Routines')
  283.  
  284. !─────────────────────────────────────────────────────────────────────────────
  285. #INSERT(%ChildInitFields)
  286.  
  287. EnterScrollMode ROUTINE                          !Switch screen mode routine
  288.  
  289.   DISABLE(1,FIELDS())                            ! Disable listbox and buttons
  290.   ENABLE(?List)                                  ! Enable the list box
  291.   ENABLE(?Insert, ?Cancel)                       ! Enable the Buttons
  292.   IF RECORDS(RecordQueue) = %FixRows           #<!  If no records to scroll
  293.     IF EntryMode                                 !   If not before FillQueues
  294.       DISABLE(?Change)                           !    Disable the change button
  295.       DISABLE(?Delete)                           !    Disable the delete button
  296.       SELECT(?Insert)                            !    Select the insert button
  297.     END                                          !   End IF
  298.   ELSE                                           !  Else
  299.     SELECT(?List)                                !    Select the list box
  300.   END                                            !  End IF
  301.   EntryMode = ScrollMode                         ! Switch to scroll mode
  302.   #EMBED('Enter Scroll Mode Routine')
  303.  
  304. EnterUpdateMode ROUTINE
  305.  
  306.   EntryMode = UpdateMode                         !Switch screen mode routine
  307.   DISABLE(1,FIELDS())                            ! Disable listbox and buttons
  308.   ENABLE(?%FirstUpdateField, ?Exit)              #<! Enable the entry fields
  309.   Select(?%FirstUpdateField)                     #<! Select the first entry field
  310.   #EMBED('Enter Update Mode Routine')
  311.  
  312. FillQueues ROUTINE
  313.  
  314.   FREE(RecordQueue)                            #<!Clear the Record queue
  315.   #SET(%FixRows, '0')
  316.   #SET(%ListField,'?List')
  317.   #FIX(%ScreenField,%ListField)
  318.   #FOR(%ScreenFieldFix)
  319.     #SET(%FixRows, (%FixRows + 1))
  320.   SAV:Line = %ScreenFieldFix                   #<!Add list box fixed fields
  321.   ADD(RecordQueue %SortString)                 #<! Add to the sorted queue
  322.   DISPLAY(?List)                               #<!Blank the listbox
  323.   #ENDFOR
  324.   #FIX(%File, %Primary)
  325.   #INSERT(%ClearFileFields)                    #<!Clear the Child record
  326.   #FIX(%File, %ParentFile)
  327.   #FIX(%Relation,%Primary)
  328.   #IF(%RelationType = '1:MANY')
  329.     #FOR(%RelationKeyField)
  330.       #IF(%RelationKeyFieldLink)
  331.   %RelationKeyField = %RelationKeyFieldLink    #<!Assign linking field value
  332.       #ENDIF
  333.     #ENDFOR
  334.   #ENDIF
  335.   SET(%RelationKey,%RelationKey)               #<!Set to keyed order
  336.   LOOP                                           !Get all selected records
  337.     NEXT(%Primary)                             #<!Get the next record.
  338.     IF ERRORCODE() THEN BREAK.                   !Quit if an error occurs
  339.     #INSERT(%GetChildSecondary)
  340.   #FIX(%File,%Primary)
  341.   #FIX(%Key,%PrimaryKey)
  342.   #IF(%ChildRelationField)                      #!If using a Range
  343.     #SET(%FieldCounter,%Null)
  344.     #FOR(%RelationKeyField)
  345.       #IF(%RelationKeyFieldLink)
  346.         #SET(%FieldCounter,(%FieldCounter+1))
  347.       #ENDIF
  348.     #ENDFOR
  349.     #SET(%IfWritten,%Null)
  350.     #FOR(%RelationKeyField)
  351.       #IF(%RelationKeyFieldLink)
  352.         #IF(%FieldCounter = '1')
  353.           #IF(%IfWritten)
  354.     OR %RelationKeyFieldLink <> %RelationKeyField #<!If not in Range
  355.           #ELSE
  356.     IF %RelationKeyFieldLink <> %RelationKeyField #<!If not in Range
  357.           #ENDIF
  358.           #BREAK
  359.         #ELSE
  360.           #IF(%IfWritten)
  361.     OR %RelationKeyFieldLink <> %RelationKeyField |#<!If not in Range
  362.           #ELSE
  363.     IF %RelationKeyFieldLink <> %RelationKeyField |#<!If not in Range
  364.           #ENDIF
  365.         #ENDIF
  366.       #ENDIF
  367.     #ENDFOR
  368.       IF RECORDS(RecordQueue) <> %FixRows      #<!  If records were added
  369.     #SET(%FirstNonFixedRecord,(%FixRows+1))
  370.         GET(RecordQueue,%FirstNonFixedRecord)  #<!   Get first non-fixed row
  371.         RESET(%RelationKey,SAV:RecordPosition) #<!  Reset to the last entry
  372.         NEXT(%Primary)                         #<!  Reread the last entry
  373.         BREAK                                  #<!  Break out of the Loop
  374.       ELSE                                       ! Else no children found
  375.         #INSERT(%ClearFileFields)              #<!  Clear the record
  376.         BREAK                                  #<!  Break out of the Loop
  377.       END                                        ! End IF
  378.     END                                          !End IF
  379.   #ENDIF
  380.   #IF(%RecordFilterFormula)
  381.     IF ~(%RecordFilterFormula)                 #<!If Filter condition not met
  382.       CYCLE                                      ! Try another record
  383.     END                                          !End IF
  384.   #ELSE
  385.     #FOR(%Formula)
  386.       #IF(UPPER(%FormulaClass) = 'FILTER')
  387.         #IF(%FormulaType <> 'COMPUTED')
  388.     IF ~(%FormulaCondition)                    #<!If Filter condition not met
  389.       CYCLE                                      ! Try another record
  390.     END                                          !End IF
  391.         #ELSE
  392.     IF ~(%FormulaComputation)                  #<!If Filter condition not met
  393.       CYCLE                                      ! Try another record
  394.     END                                          !End IF
  395.         #ENDIF
  396.       #ENDIF
  397.     #ENDFOR
  398.   #ENDIF
  399.   #FOR(%Formula)
  400.     #IF(UPPER(%FormulaClass) = 'LIST')
  401.     #INSERT(%GenerateFormula)                   #!Generate LIST formulas
  402.     #ENDIF
  403.   #ENDFOR
  404.   #FIX(%File,%Primary)
  405.   #SET(%ListField,'?LIST')
  406.   #FIX(%ScreenField,%ListField)
  407.     SAV:Line = %ScreenFieldExpression          #<! Fill the DisplayQueue line
  408.     SAV:SaveRecord = %FilePre:Record             ! Save the record data
  409.     SAV:RecordPosition = POSITION(%RelationKey)  ! Save the record position
  410.   #FOR(%Field)
  411.     #IF(%FieldType = 'MEMO')
  412.       #SET(%MemoField,%FieldID)
  413.     SAV:%FieldID = %Field                      #<!  Restore the Memos
  414.     #ENDIF
  415.   #ENDFOR
  416.     ADD(RecordQueue %SortString)               #<! Add to the sorted queue
  417.     IF ERRORCODE() THEN BREAK.                   ! Quit out if error
  418.     IF FirstPage                                 ! If page 1
  419.       IF RECORDS(RecordQueue) = ROWS(?List)      ! If we have a full screen
  420.         FirstPage = 0                            !   turn off the page flag
  421.       END                                        !  End IF
  422.       DISPLAY(?List)                             !  Display page 1
  423.     END                                          ! End IF
  424.   END                                            !End LOOP
  425.   IF RECORDS(RecordQueue) = %FixRows           #<!If the queue is empty
  426.     IF RECORDS(%Primary)                       #<! If file is not empty
  427.       IF ?List <> %FirstEntryField             #<!  And list is not first
  428.         SELECT(1)                                !   Select the first field
  429.       ELSE                                       !  Else
  430.         DISABLE(1,FIELDS())                      !   Disable all fields
  431.         ENABLE(?Insert)                          !   Enable the Insert and
  432.         ENABLE(?Cancel)                          !   the cancel buttons
  433.         SELECT(?Insert)                          !   Select the Insert Button
  434.       END                                        !  End IF
  435.     ELSE                                         ! Else the file is empty
  436.       DISABLE(1,FIELDS())                        !   Disable all fields
  437.       ENABLE(?Insert)                            !   Enable the Insert and
  438.       ENABLE(?Cancel)                            !   the cancel buttons
  439.       SELECT(?Insert)                            !   Select the Insert Button
  440.     END                                          ! End IF
  441.   ELSE                                           !Else records exist
  442.     GET(RecordQueue,%FirstNonFixedRecord)      #<! Get first non-fixed row
  443.     RESET(%RelationKey,SAV:RecordPosition)     #<! Reset to the last entry
  444.     NEXT(%Primary)                             #<! Reread the last entry
  445.     #INSERT(%GetChildSecondary)
  446.   END                                            !End IF
  447.   DISPLAY
  448. #!
  449. #CHAIN('Report.tpx')
  450.