Module: ADIWG::Mdtranslator::Readers::SbJson::RelatedItem

Defined in:
lib/adiwg/mdtranslator/readers/sbJson/modules/module_relatedItem.rb

Class Method Summary collapse

Class Method Details

.unpack(hSbJson, hMetadata, hResponseObj) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/adiwg/mdtranslator/readers/sbJson/modules/module_relatedItem.rb', line 22

def self.unpack(hSbJson, , hResponseObj)

   # instance classes needed in script
   intMetadataClass = .new

   # relationship direction of related items depends on the main record id
   sbId = nil
   if hSbJson.has_key?('id')
      sbId = hSbJson['id']
   end
   if sbId.nil? || sbId == ''
      hResponseObj[:readerExecutionMessages] << 'ScienceBase id missing'
      return 
   end

   # get the link to the related items array
   hRelatedItems = hSbJson['relatedItems']
   itemsLink = nil
   if hRelatedItems.has_key?('link')
      hLink = hRelatedItems['link']
      unless hLink.empty?
         itemsLink = hLink['url']
      end
   end
   return  if itemsLink.nil?

   # fetch the related items array in json format
   itemsLink = itemsLink + '&format=json'
   begin
      web_contents = open(itemsLink, :read_timeout => 30) { |f| f.read }
   rescue => readErr
      hResponseObj[:readerExecutionMessages] << "Failed reading ScienceBase relatedItems #{itemsLink} - see following message(s):\n"
      hResponseObj[:readerExecutionMessages] << readErr.to_s.slice(0, 300)
      return 
   else
      # parse related items array
      aItems = []
      begin
         aItems = JSON.parse(web_contents)
      rescue JSON::JSONError => parseErr
         hResponseObj[:readerExecutionMessages] << 'Parsing related links failed - see following message(s):\n'
         hResponseObj[:readerExecutionMessages] << parseErr.to_s.slice(0, 300)
         return 
      end

      # process each returned related item as a separate associated resource
      aItems.each do |hItem|
         unless hItem.empty?

            # determine relationship direction
            # forward: how the associated resource relates to the main resource
            # ... in other words - the relationship is defined in terms of the associated resource
            # ... example: the associated resource is a 'subProject' of the main resource
            # reverse: how the main resource relates to the associated resource
            # ... in other words - the relationship is defined in terms of the main resource
            # ... example: the main resource is the 'parentProject' of the associated resource
            # all mdJson/mdTranslator relationships must be expressed as forward
            forward = nil
            if hItem.has_key?('itemId')
               forward = false if sbId == hItem['itemId']
            end
            if hItem.has_key?('relatedItemId')
               forward = true if sbId == hItem['relatedItemId']
            end
            if forward.nil?
               hResponseObj[:readerExecutionMessages] << 'Main ScienceBase id was not referenced in related item'
               return 
            end

            # fetch resourceTypes from related item's record
            if forward
               resourceId = hItem['itemId']
            else
               resourceId = hItem['relatedItemId']
            end
            resourceLink = "https://www.sciencebase.gov/catalog/item/#{resourceId}?format=json"
            begin
               web_contents = open(resourceLink, :read_timeout => 30) { |f| f.read }
            rescue => readErr
               hResponseObj[:readerExecutionMessages] << "Failed reading ScienceBase relatedItem #{resourceId} - see following message(s)"
               hResponseObj[:readerExecutionMessages] << readErr.to_s.slice(0, 300)
               hResponseObj[:readerExecutionMessages] << 'Either the item does not exist or the item is secured and requires authentication to access.'
               break
            else

               # parse related item
               begin
                  hRelatedItem = JSON.parse(web_contents)
               rescue JSON::JSONError => parseErr
                  hResponseObj[:readerExecutionMessages] << 'Parsing related item failed - see following message(s):\n'
                  hResponseObj[:readerExecutionMessages] << parseErr.to_s.slice(0, 300)
                  break
               end

               # create mew associated resource
               hResource = intMetadataClass.newAssociatedResource

               # add resource types
               BrowseCategory.unpack(hRelatedItem, hResource[:resourceTypes], hResponseObj)

               # add association type
               if hItem.has_key?('type')
                  sbAssocType = hItem['type']
                  unless sbAssocType.nil? || sbAssocType == ''
                     assocType = nil
                     if forward
                        assocType = Codelists.codelist_sb2adiwg('association_sb2adiwg_assoc2main', sbAssocType)
                     else
                        assocType = Codelists.codelist_sb2adiwg('association_sb2adiwg_main2assoc', sbAssocType)
                     end
                     if assocType.nil?
                        hResource[:associationType] = sbAssocType
                     else
                        hResource[:associationType] = assocType
                     end
                  end
               end
               if hResource[:associationType].nil?
                  hResource[:associationType] = 'missing'
               end

               # fill in associated resource citation
               hCitation = intMetadataClass.newCitation
               citationTitle = 'associated resource title'
               if forward
                  if hItem.has_key?('title')
                     citationTitle = hItem['title']
                  end
               else
                  if hItem.has_key?('relatedItemTitle')
                     citationTitle = hItem['relatedItemTitle']
                  end
               end
               hCitation[:title] = citationTitle

               # create an identifier for the related item
               # use resourceId computed above
               hIdentifier = intMetadataClass.newIdentifier
               hIdentifier[:identifier] = resourceId
               hIdentifier[:namespace] = 'gov.sciencebase.catalog'
               hIdentifier[:description] = 'relatedItemId'
               hCitation[:identifiers] << hIdentifier

               hResource[:resourceCitation] = hCitation
               [:associatedResources] << hResource

            end

         end
      end

      return 
   end

end