rule "combine 949 based on $8"
salience 1
when
(TRUE)
then
# prepare combined TMP field
# needs to be first
# supply placeholder values to all possible subfields
addSubField "949.a._" if (not exists "949.a")
addSubField "949.b._" if (not exists "949.b")
addSubField "949.c._" if (not exists "949.c")
addSubField "949.s._" if (not exists "949.s")
addSubField "949.8._" if (not exists "949.8")
combineFields "949" excluding ""
changeField "949" to "TMP"
# extract first set from TMP
addField "TMA.a._"
addSubField "TMA.b._"
addSubField "TMA.c._"
addSubField "TMA.s._"
addSubField "TMA.8._"
replaceSubFieldContents "TMA.a" with "TMP.a"
replaceSubFieldContents "TMA.b" with "TMP.b"
replaceSubFieldContents "TMA.c" with "TMP.c"
replaceSubFieldContents "TMA.s" with "TMP.s"
replaceSubFieldContents "TMA.8" with "TMP.8"
changeField "TMA" to "TMB" #####
changeSubFieldOnlyFirst "TMP.a" to "Z"
changeSubFieldOnlyFirst "TMP.b" to "Z"
changeSubFieldOnlyFirst "TMP.c" to "Z"
changeSubFieldOnlyFirst "TMP.s" to "Z"
changeSubFieldOnlyFirst "TMP.8" to "Z"
# extract next set from TMP
# duplicate as many times as separate 949 can appear in a single record
addField "TMA.A._"
addSubField "TMA.B._"
addSubField "TMA.C._"
addSubField "TMA.S._"
addSubField "TMA.8._"
replaceSubFieldContents "TMA.A" with "TMP.a"
replaceSubFieldContents "TMA.B" with "TMP.b"
replaceSubFieldContents "TMA.C" with "TMP.c"
replaceSubFieldContents "TMA.S" with "TMP.s"
replaceSubFieldContents "TMA.8" with "TMP.8"
changeSubFieldOnlyFirst "TMP.a" to "Z"
changeSubFieldOnlyFirst "TMP.b" to "Z"
changeSubFieldOnlyFirst "TMP.c" to "Z"
changeSubFieldOnlyFirst "TMP.s" to "Z"
changeSubFieldOnlyFirst "TMP.8" to "Z"
changeField "TMA" to "TMB" #####
combineFields "TMB" excluding "" #####
# check if sets are of the same holding (X=different)
correctDuplicateSubfields "TMB" "8"
changeSubFieldExceptFirst "TMB.8" to "X"
# change A>a etc. if $8 is the same
changeSubField "TMB.A" to "a" if (not exists "TMB.X")
changeSubField "TMB.B" to "b" if (not exists "TMB.X")
changeSubField "TMB.C" to "c" if (not exists "TMB.X")
changeSubField "TMB.S" to "s" if (not exists "TMB.X")
# move TMB to 949 if $8 is different
copyField "TMB" to "949"
removeField "949" if (not exists "949.X")
# remove copied fields if $8 is different
removeSubField "TMB.a" if (exists "TMB.X")
removeSubField "TMB.b" if (exists "TMB.X")
removeSubField "TMB.c" if (exists "TMB.X")
removeSubField "TMB.s" if (exists "TMB.X")
removeSubField "TMB.8" if (exists "TMB.X")
# convert next set if $8 is different
changeSubField "TMB.A" to "a" if (exists "TMB.X")
changeSubField "TMB.B" to "b" if (exists "TMB.X")
changeSubField "TMB.C" to "c" if (exists "TMB.X")
changeSubField "TMB.S" to "s" if (exists "TMB.X")
changeSubField "TMB.X" to "8" if (exists "TMB.X")
# remove temporary fields and subfields
# keep at the end of the NR
removeField "TMP"
removeField "TMB"
removeSubfield "949.A"
removeSubfield "949.B"
removeSubfield "949.C"
removeSubfield "949.S"
removeSubfield "949.X"
replaceContents "949.*._" with ""
end