MODULE osm$job_template_management;
?? PUSH (LISTEXT := ON) ??
*copyc osd$default_pragmats
*copyc ost$name
*copyc osp$set_status_abnormal
*copyc fsp$open_file
*copyc fsp$close_file
*copyc amp$get_segment_pointer
*copyc clp$scan_parameter_list
*copyc clp$get_value
*copyc clp$get_set_count
*copyc jmp$determine_job_class
*copyc jmp$determine_job_class_name
*copyc syp$activate_job_template
*copyc syp$deactivate_job_template
*copyc osd$virtual_address
*copyc mmp$set_access_selections
*copyc jmp$system_job
*copyc sye$job_template_conditions
*copyc syv$user_templates
*copyc jme$queued_file_conditions
*copyc syp$set_job_debug_ring
?? POP ??

 {The purpose of this module is to provide job template support
 {for multiple job templates.  The interfaces are only available
 {to the system job.


?? TITLE := 'PROCEDURE osp$activate_job_template' ??
?? EJECT ??

  PROCEDURE [XDCL, #GATE] osp$activate_job_template
    (    parameter_list: clt$parameter_list;
     VAR status: ost$status);

{ PDT     activate_job_template (template_file, tf: file = $required
{           template_name, tn: name = $required
{           job_classes, jc: list of name = $required
{           job_unique_segments, jus: list of integer 0 .. 4095 = (4, 6)
{           task_unique_segments, tus: list of integer 0 .. 4095 = 5
{           status: var of status)

?? PUSH (LISTEXT := ON) ??
?? FMT (FORMAT := OFF) ??

  VAR
    activate_job_template: [STATIC, READ, cls$pdt] clt$parameter_descriptor_table := [
  ^activate_job_template_names, ^activate_job_template_params];

  VAR
    activate_job_template_names: [STATIC, READ, cls$pdt_names_and_defaults] array [1 .. 11] of
  clt$parameter_name_descriptor := [['TEMPLATE_FILE', 1], ['TF', 1], ['TEMPLATE_NAME', 2], ['TN', 2], [
  'JOB_CLASSES', 3], ['JC', 3], ['JOB_UNIQUE_SEGMENTS', 4], ['JUS', 4], ['TASK_UNIQUE_SEGMENTS', 5], ['TUS', 5
  ], ['STATUS', 6]];

  VAR
    activate_job_template_params: [STATIC, READ, cls$pdt_parameters] array [1 .. 6] of
  clt$parameter_descriptor := [

{ TEMPLATE_FILE TF }
    [[clc$required], 1, 1, 1, 1, clc$value_range_not_allowed, [NIL, clc$file_value]],

{ TEMPLATE_NAME TN }
    [[clc$required], 1, 1, 1, 1, clc$value_range_not_allowed, [NIL, clc$name_value, 1, osc$max_name_size]],

{ JOB_CLASSES JC }
    [[clc$required], 1, clc$max_value_sets,1, 1, clc$value_range_not_allowed, [NIL, clc$name_value, 1,
  osc$max_name_size]],

{ JOB_UNIQUE_SEGMENTS JUS }
    [[clc$optional_with_default, ^activate_job_template_dv4], 1, clc$max_value_sets,1, 1,
  clc$value_range_not_allowed, [NIL, clc$integer_value, 0, 4095]],

{ TASK_UNIQUE_SEGMENTS TUS }
    [[clc$optional_with_default, ^activate_job_template_dv5], 1, clc$max_value_sets,1, 1,
  clc$value_range_not_allowed, [NIL, clc$integer_value, 0, 4095]],

{ STATUS }
    [[clc$optional], 1, 1, 1, 1, clc$value_range_not_allowed, [NIL, clc$variable_reference,
  clc$array_not_allowed, clc$status_value]]];

  VAR
    activate_job_template_dv4: [STATIC, READ, cls$pdt_names_and_defaults] string (6) := '(4, 6)';

  VAR
    activate_job_template_dv5: [STATIC, READ, cls$pdt_names_and_defaults] string (1) := '5';

?? FMT (FORMAT := ON) ??
?? POP ??

    VAR
      ao: ^fst$attachment_options,
      i,
      j,
      k: integer,
      number_of_classes,
      number_of_segments: 0 .. clc$max_value_sets,
      job_classes: ^array [1 .. * ] of ost$name,
      job_class: jmt$job_class,
      job_class_name: jmt$job_class_name,
      file_identifier: amt$file_identifier,
      segment_pointer: amt$segment_pointer,
      template_file: ^SEQ ( * ),
      local_status: ost$status,
      job_unique_segments,
      task_unique_segments: ost$segment_set,
      template_name: ost$name,
      value: clt$value;

    clp$scan_parameter_list (parameter_list, activate_job_template, status);
    IF NOT status.normal THEN
      RETURN;
    IFEND;

    IF (NOT jmp$system_job ()) AND (NOT syv$user_templates) THEN
      osp$set_status_abnormal ('JM', jme$must_be_system_job, 'osp$activate_job_template', status);
      RETURN;
    IFEND;

    clp$get_value ('TEMPLATE_FILE', 1, 1, clc$low, value, status);
    IF NOT status.normal THEN
      RETURN;
    IFEND;

    PUSH ao: [1 .. 2];
    ao^ [1].selector := fsc$access_and_share_modes;
    ao^ [1].access_modes.selector := fsc$specific_access_modes;
    ao^ [1].access_modes.value := $fst$file_access_options [fsc$read];
    ao^ [1].share_modes.selector := fsc$specific_share_modes;
    ao^ [1].share_modes.value := $fst$file_access_options [fsc$read];
    ao^ [2].selector := fsc$create_file;
    ao^ [2].create_file := FALSE;

    fsp$open_file (value.file.local_file_name, amc$segment, ao, NIL, NIL, NIL,
          NIL, file_identifier, status);
    IF NOT status.normal THEN
      RETURN;
    IFEND;

  /file_open/
    BEGIN
      amp$get_segment_pointer (file_identifier, amc$sequence_pointer, segment_pointer,
            status);
      IF NOT status.normal THEN
        EXIT /file_open/;
      IFEND;
      template_file := segment_pointer.sequence_pointer;

      clp$get_value ('TEMPLATE_NAME', 1, 1, clc$low, value, status);
      IF NOT status.normal THEN
        EXIT /file_open/;
      IFEND;
      template_name := value.name.value;

      clp$get_set_count ('JOB_CLASSES', number_of_classes, status);
      IF NOT status.normal THEN
        EXIT /file_open/;
      IFEND;

      IF number_of_classes <= 0 THEN
        osp$set_status_abnormal (syc$system_core_id, sye$not_enough_classes, '', status);
        EXIT /file_open/;
      IFEND;

      PUSH job_classes: [1 .. number_of_classes];

      FOR i := 1 TO number_of_classes DO
        clp$get_value ('JOB_CLASSES', i, 1, clc$low, value, status);
        IF NOT status.normal THEN
          EXIT /file_open/;
        IFEND;
        jmp$determine_job_class (value.name.value, job_class, status);
        IF NOT status.normal THEN
          RETURN;
        IFEND;

        jmp$determine_job_class_name (job_class, job_class_name, status);
        IF NOT status.normal THEN
          RETURN;
        IFEND;
        job_classes^ [i] := job_class_name;
      FOREND;

      clp$get_set_count ('JOB_UNIQUE_SEGMENTS', number_of_segments, status);
      IF NOT status.normal THEN
        EXIT /file_open/;
      IFEND;

      job_unique_segments := $ost$segment_set [];
      FOR i := 1 TO number_of_segments DO
        clp$get_value ('JOB_UNIQUE_SEGMENTS', i, 1, clc$low, value, status);
        IF NOT status.normal THEN
          EXIT /file_open/;
        IFEND;
        job_unique_segments := job_unique_segments + $ost$segment_set
              [value.int.value];
      FOREND;

      clp$get_set_count ('TASK_UNIQUE_SEGMENTS', number_of_segments, status);
      IF NOT status.normal THEN
        EXIT /file_open/;
      IFEND;

      task_unique_segments := $ost$segment_set [];
      FOR i := 1 TO number_of_segments DO
        clp$get_value ('TASK_UNIQUE_SEGMENTS', i, 1, clc$low, value, status);
        IF NOT status.normal THEN
          EXIT /file_open/;
        IFEND;
        task_unique_segments := task_unique_segments + $ost$segment_set
              [value.int.value];
      FOREND;

      mmp$set_access_selections (#ADDRESS (3, #SEGMENT (template_file), 0),
            mmc$as_sequential, status);

      syp$activate_job_template (template_file, template_name,
            job_unique_segments, task_unique_segments, job_classes, status);

    END /file_open/;

    fsp$close_file (file_identifier, local_status);
    IF status.normal THEN
      status := local_status;
    IFEND;

  PROCEND osp$activate_job_template;
?? TITLE := 'PROCEDURE osp$deactivate_job_template' ??
?? EJECT ??

  PROCEDURE [XDCL, #GATE] osp$deactivate_job_template
    (    parameter_list: clt$parameter_list;
     VAR status: ost$status);

{   PDT     deactivate_job_template (template_name, tn: name = $required
{             status: var of status)

?? PUSH (LISTEXT := ON) ??

  VAR
    deactivate_job_template: [STATIC, READ, cls$pdt] clt$parameter_descriptor_table := [
      ^deactivate_job_template_names, ^deactivate_job_template_params];

  VAR
    deactivate_job_template_names: [STATIC, READ, cls$pdt_names_and_defaults] array [1 .. 3] of
      clt$parameter_name_descriptor := [['TEMPLATE_NAME', 1], ['TN', 1], ['STATUS', 2]];

  VAR
    deactivate_job_template_params: [STATIC, READ, cls$pdt_parameters] array [1 .. 2] of
      clt$parameter_descriptor := [

{ TEMPLATE_NAME TN }
    [[clc$required], 1, 1, 1, 1, clc$value_range_not_allowed, [NIL, clc$name_value, 1, osc$max_name_size]],

{ STATUS }
    [[clc$optional], 1, 1, 1, 1, clc$value_range_not_allowed, [NIL, clc$variable_reference,
      clc$array_not_allowed, clc$status_value]]];

?? POP ??
    VAR
      template_name: ost$name,
      value: clt$value;

    clp$scan_parameter_list (parameter_list, deactivate_job_template, status);
    IF NOT status.normal THEN
      RETURN;
    IFEND;

    IF (NOT jmp$system_job ()) AND (NOT syv$user_templates) THEN
      osp$set_status_abnormal ('JM', jme$must_be_system_job, 'osp$deactivate_job_template', status);
      RETURN;
    IFEND;

    clp$get_value ('TEMPLATE_NAME', 1, 1, clc$low, value, status);
    IF NOT status.normal THEN
      RETURN;
    IFEND;
    template_name := value.name.value;

    syp$deactivate_job_template (template_name, status);

  PROCEND osp$deactivate_job_template;

?? TITLE := 'PROCEDURE osp$set_job_debug_ring_cmd' ??
?? EJECT ??

  PROCEDURE [XDCL, #GATE] osp$set_job_debug_ring_cmd
    (    parameter_list: clt$parameter_list;
     VAR status: ost$status);

{ PDT setjdr_pdt (job_debug_ring, jdr: integer 0 .. 15 = $required
{       status)

?? PUSH (LISTEXT := ON) ??

    VAR
      setjdr_pdt: [STATIC, READ, cls$pdt] clt$parameter_descriptor_table :=
            [^setjdr_pdt_names, ^setjdr_pdt_params];

    VAR
      setjdr_pdt_names: [STATIC, READ, cls$pdt_names_and_defaults] array
            [1 .. 3] of clt$parameter_name_descriptor :=
            [['JOB_DEBUG_RING', 1], ['JDR', 1], ['STATUS', 2]];

    VAR
      setjdr_pdt_params: [STATIC, READ, cls$pdt_parameters] array [1 .. 2] of
            clt$parameter_descriptor := [

{ JOB_DEBUG_RING JDR }
      [[clc$required], 1, 1, 1, 1, clc$value_range_not_allowed,
            [NIL, clc$integer_value, 0, 15]],

{ STATUS }
      [[clc$optional], 1, 1, 1, 1, clc$value_range_not_allowed,
            [NIL, clc$variable_reference, clc$array_not_allowed,
            clc$status_value]]];

?? POP ??

    VAR
      value: clt$value;

    clp$scan_parameter_list (parameter_list, setjdr_pdt, status);
    IF NOT status.normal THEN
      RETURN;
    IFEND;

    status.normal := TRUE;
    IF NOT syv$user_templates THEN
      RETURN;
    IFEND;

    clp$get_value ('JOB_DEBUG_RING', 1, 1, clc$low, value, status);
    IF NOT status.normal THEN
      RETURN;
    IFEND;

    syp$set_job_debug_ring (value.int.value);

  PROCEND osp$set_job_debug_ring_cmd;
MODEND osm$job_template_management
