55 "regexp"
66 "strings"
77
8+ "github.com/indexdata/crosslink/broker/common"
89 "github.com/indexdata/crosslink/broker/ill_db"
910 "github.com/indexdata/crosslink/directory"
1011 "github.com/indexdata/crosslink/iso18626"
@@ -88,6 +89,11 @@ func (i *Iso18626AlmaShim) ApplyToIncomingRequest(message *iso18626.ISO18626Mess
8889 copyMessage .RequestingAgencyMessage .Header .SupplyingAgencyId .AgencyIdValue = symbol [1 ]
8990 }
9091 }
92+ if message .SupplyingAgencyMessage != nil {
93+ copySam := * message .SupplyingAgencyMessage
94+ copyMessage .SupplyingAgencyMessage = & copySam
95+ i .unifyItem (copyMessage .SupplyingAgencyMessage )
96+ }
9197 return & copyMessage
9298}
9399
@@ -107,6 +113,7 @@ func (i *Iso18626AlmaShim) ApplyToOutgoingRequest(message *iso18626.ISO18626Mess
107113 i .appendReturnAddressToSuppMsgNote (suppMsg )
108114 }
109115 i .appendUnfilledStatusAndReasonUnfilled (suppMsg )
116+ i .setItemId (suppMsg )
110117 }
111118 if message .Request != nil {
112119 request := message .Request
@@ -198,6 +205,20 @@ func (i *Iso18626AlmaShim) appendUnfilledStatusAndReasonUnfilled(suppMsg *iso186
198205 }
199206}
200207
208+ func (i * Iso18626AlmaShim ) setItemId (sam * iso18626.SupplyingAgencyMessage ) {
209+ if common .SamHasItems (* sam ) {
210+ result , startIdx , endIdx := common .GetItemParams (sam .MessageInfo .Note )
211+ var items []string
212+ for _ , item := range result {
213+ items = append (items , item [0 ])
214+ }
215+ sam .DeliveryInfo .ItemId = strings .Join (items , "," )
216+
217+ sam .MessageInfo .Note = sam .MessageInfo .Note [0 :startIdx - 1 ] + // -1 because we remove new line symbol
218+ sam .MessageInfo .Note [endIdx + len (common .MULTIPLE_ITEMS_END ):]
219+ }
220+ }
221+
201222func (i * Iso18626AlmaShim ) transferOfferedCostsToDeliveryCosts (suppMsg * iso18626.SupplyingAgencyMessage ) {
202223 //alma doesn't care about the delivery costs unless the status is Loaned or CopyCompleted
203224 if suppMsg .StatusInfo .Status != iso18626 .TypeStatusLoaned && suppMsg .StatusInfo .Status != iso18626 .TypeStatusCopyCompleted {
@@ -516,13 +537,34 @@ func (i *Iso18626AlmaShim) fixRequesterConditionNote(requestingAgencyMessage *is
516537 }
517538}
518539
540+ func (i * Iso18626AlmaShim ) unifyItem (sam * iso18626.SupplyingAgencyMessage ) {
541+ if sam .DeliveryInfo != nil && sam .DeliveryInfo .ItemId != "" {
542+ var sb strings.Builder
543+ //retain original note
544+ if sam .MessageInfo .Note != "" {
545+ sb .WriteString (sam .MessageInfo .Note )
546+ sb .WriteString ("\n " )
547+ }
548+ sb .WriteString (common .MULTIPLE_ITEMS )
549+ sb .WriteString ("\n " )
550+ list := strings .Split (sam .DeliveryInfo .ItemId , "," )
551+ for _ , item := range list {
552+ sb .WriteString (common .PackItemsNote ([]string {item }))
553+ sb .WriteString ("\n " )
554+ }
555+ sb .WriteString (common .MULTIPLE_ITEMS_END )
556+ sam .MessageInfo .Note = sb .String ()
557+ }
558+ }
559+
519560type Iso18626ReShareShim struct {
520561 Iso18626DefaultShim
521562}
522563
523564func (i * Iso18626ReShareShim ) ApplyToOutgoingRequest (message * iso18626.ISO18626Message ) ([]byte , error ) {
524565 if message .SupplyingAgencyMessage != nil {
525566 i .transferDeliveryCostsToOfferedCosts (message .SupplyingAgencyMessage )
567+ i .setItemId (message .SupplyingAgencyMessage )
526568 }
527569 return xml .Marshal (message )
528570}
@@ -543,3 +585,72 @@ func (i *Iso18626ReShareShim) transferDeliveryCostsToOfferedCosts(suppMsg *iso18
543585 }
544586 }
545587}
588+
589+ func (i * Iso18626ReShareShim ) ApplyToIncomingRequest (message * iso18626.ISO18626Message , requester * ill_db.Peer , supplier * ill_db.LocatedSupplier ) * iso18626.ISO18626Message {
590+ if message == nil {
591+ return message
592+ }
593+ copyMessage := * message
594+ if message .SupplyingAgencyMessage != nil {
595+ copySam := * message .SupplyingAgencyMessage
596+ copyMessage .SupplyingAgencyMessage = & copySam
597+ i .unifyItem (copyMessage .SupplyingAgencyMessage )
598+ }
599+ return & copyMessage
600+ }
601+
602+ func (i * Iso18626ReShareShim ) setItemId (sam * iso18626.SupplyingAgencyMessage ) {
603+ if common .SamHasItems (* sam ) {
604+ result , startIdx , endIdx := common .GetItemParams (sam .MessageInfo .Note )
605+ if len (result ) == 1 {
606+ sam .DeliveryInfo .ItemId = strings .Join (result [0 ], "," )
607+ } else {
608+ var items []string
609+ for _ , item := range result {
610+ items = append (items , strings .Join (item , "," ))
611+ }
612+ sam .DeliveryInfo .ItemId = "multivol:" + strings .Join (items , ",multivol:" )
613+ }
614+
615+ sam .MessageInfo .Note = sam .MessageInfo .Note [0 :startIdx - 1 ] + // -1 because we remove new line symbol
616+ sam .MessageInfo .Note [endIdx + len (common .MULTIPLE_ITEMS_END ):]
617+ }
618+ }
619+
620+ func (i * Iso18626ReShareShim ) unifyItem (sam * iso18626.SupplyingAgencyMessage ) {
621+ if sam .DeliveryInfo != nil && sam .DeliveryInfo .ItemId != "" {
622+ var sb strings.Builder
623+ //retain original note
624+ if sam .MessageInfo .Note != "" {
625+ sb .WriteString (sam .MessageInfo .Note )
626+ sb .WriteString ("\n " )
627+ }
628+ sb .WriteString (common .MULTIPLE_ITEMS )
629+ sb .WriteString ("\n " )
630+ if strings .Contains (sam .DeliveryInfo .ItemId , "multivol:" ) {
631+ list := strings .Split (sam .DeliveryInfo .ItemId , ",multivol:" )
632+ for _ , item := range list {
633+ item = strings .Replace (item , "multivol:" , "" , 1 )
634+ writeItemValues (& sb , item )
635+ sb .WriteString ("\n " )
636+ }
637+ } else {
638+ writeItemValues (& sb , sam .DeliveryInfo .ItemId )
639+ sb .WriteString ("\n " )
640+ }
641+ sb .WriteString (common .MULTIPLE_ITEMS_END )
642+ sam .MessageInfo .Note = sb .String ()
643+ }
644+ }
645+
646+ func writeItemValues (sb * strings.Builder , itemId string ) {
647+ re := regexp .MustCompile (`(.*),(.*),(.*)` )
648+ match := re .FindStringSubmatch (itemId )
649+ var row string
650+ if len (match ) > 0 {
651+ row = common .PackItemsNote ([]string {match [1 ], match [2 ], match [3 ]})
652+ } else {
653+ row = common .PackItemsNote ([]string {itemId })
654+ }
655+ sb .WriteString (row )
656+ }
0 commit comments