Module: ADIWG::Mdtranslator::Readers::Fgdc::Contact

Defined in:
lib/adiwg/mdtranslator/readers/fgdc/modules/module_contact.rb

Class Method Summary collapse

Class Method Details

.add_phone(hContact, number, type) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/adiwg/mdtranslator/readers/fgdc/modules/module_contact.rb', line 19

def self.add_phone(hContact, number, type)

   # instance classes needed in script
   intMetadataClass = .new

   # test if phone number already exists for this contact
   hPhone = intMetadataClass.newPhone
   newPhone = true
   hContact[:phones].each do |hOld|
      if hOld[:phoneNumber] == number
         newPhone = false
         hPhone = hOld
      end
   end
   if newPhone
      hContact[:phones] << hPhone
   end
   hPhone[:phoneNumber] = number
   unless hPhone[:phoneServiceTypes].include?(type)
      hPhone[:phoneServiceTypes] << type
   end

end

.unpack(xContact, hResponseObj) ⇒ Object



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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
# File 'lib/adiwg/mdtranslator/readers/fgdc/modules/module_contact.rb', line 43

def self.unpack(xContact, hResponseObj)

   # instance classes needed in script
   intMetadataClass = .new

   # contact 10 (cntinfo) - contact information
   contactType = nil
   personName = ''
   orgName = ''
   personId = nil
   orgId = nil
   xContactInfo = xContact.xpath('./cntinfo')
   unless xContactInfo.empty?

      # contact 10.1 (cntperp) - contact person primary
      xPerson = xContactInfo.xpath('./cntperp')
      unless xPerson.empty?
         contactType = 'person'

         # person primary 10.1.1 (cntper) - name of person
         # person primary 10.1.2 (cntorg) - name of organization
         personName = xPerson.xpath('./cntper').text
         orgName = xPerson.xpath('./cntorg').text

      end

      # contact 10.2 (cntorgp) - contact organization primary
      xOrg = xContactInfo.xpath('./cntorgp')
      unless xOrg.empty?
         contactType = 'organization'

         # organization primary 10.1.1 (cntper) - name of person
         # organization primary 10.1.2 (cntorg) - name of organization
         personName = xOrg.xpath('./cntper').text
         orgName = xOrg.xpath('./cntorg').text

      end

      # error handling
      if contactType.nil?
         hResponseObj[:readerExecutionMessages] << 'ERROR: FGDC reader: contact is neither person or organization'
         return nil
      end
      if contactType == 'person' && personName == ''
         hResponseObj[:readerExecutionMessages] << 'WARNING: FGDC reader: contact person name is missing'
      end
      if contactType == 'organization' && orgName == ''
         hResponseObj[:readerExecutionMessages] << 'WARNING: FGDC reader: contact organization name is missing'
      end

      # add new contacts to contact array
      unless contactType.nil?
         personId = Fgdc.add_contact(personName, false) unless personName.empty?
         orgId = Fgdc.add_contact(orgName, true) unless orgName.empty?
         hContact = Fgdc.get_contact_by_id(personId) if contactType == 'person'
         hContact = Fgdc.get_contact_by_id(orgId) if contactType == 'organization'

         # CAUTION: the contact being processed may have been added previously.
         # check that addresses, phones, emails, org members, hours of service
         # were not previous defined for this contact.

         # contact - member of organization
         if contactType == 'person'
            unless orgId.nil?
               unless hContact.include?(orgId)
                  hContact[:memberOfOrgs] << orgId
               end
            end
         end

         # contact 10.3 (cntpos) - contact position
         position = xContactInfo.xpath('./cntpos').text
         if contactType == 'person'
            hContact[:positionName] = position
         end

         # contact 10.4 (cntaddr) - contact address [] (required)
         axAddress = xContactInfo.xpath('./cntaddr')
         unless axAddress.empty?
            axAddress.each do |xAddress|
               hAddress = intMetadataClass.newAddress

               # address 10.4.1 (addrtype) - address type (required)
               addType = xAddress.xpath('./addrtype').text
               hAddress[:addressTypes] << addType unless addType.empty?
               if addType.empty?
                  hResponseObj[:readerExecutionMessages] << 'WARNING: FGDC reader: contact address type is missing'
               end

               # address 10.4.2 (address) - address lines []
               axAddLines = xAddress.xpath('./address')
               unless axAddLines.empty?
                  axAddLines.each do |xLine|
                     addLine = xLine.text
                     hAddress[:deliveryPoints] << addLine unless addLine.empty?
                  end
               end

               # address 10.4.3 (city) - city (required)
               city = xAddress.xpath('./city').text
               hAddress[:city] = city unless city.empty?
               if city.empty?
                  hResponseObj[:readerExecutionMessages] << 'WARNING: FGDC reader: contact address city is missing'
               end

               # address 10.4.4 (state) - state (required)
               state = xAddress.xpath('./state').text
               hAddress[:adminArea] = state unless state.empty?
               if state.empty?
                  hResponseObj[:readerExecutionMessages] << 'WARNING: FGDC reader: contact address state is missing'
               end

               # address 10.4.5 (postal) - postal code (required)
               postal = xAddress.xpath('./postal').text
               hAddress[:postalCode] = postal unless postal.empty?
               if postal.empty?
                  hResponseObj[:readerExecutionMessages] << 'WARNING: FGDC reader: contact address zip code is missing'
               end

               # address 10.4.6 (country) - country
               country = xAddress.xpath('./country').text
               hAddress[:country] = country unless country.empty?

               # test if new address before adding to contact
               saveAddress = true
               hContact[:addresses].each do |hOld|
                  isSame = true
                  isSame = false unless hAddress[:deliveryPoints].length == hOld[:deliveryPoints].length
                  if hAddress[:deliveryPoints].length == hOld[:deliveryPoints].length
                     (1..hAddress[:deliveryPoints].length).each do |x|
                        isSame = false unless hAddress[:deliveryPoints][x] == hOld[:deliveryPoints][x]
                     end
                  end
                  isSame = false unless hAddress[:city] == hOld[:city]
                  isSame = false unless hAddress[:adminArea] == hOld[:adminArea]
                  isSame = false unless hAddress[:postalCode] == hOld[:postalCode]
                  isSame = false unless hAddress[:country] == hOld[:country]
                  if isSame
                     saveAddress = false
                     break
                  end
               end

               hContact[:addresses] << hAddress if saveAddress

            end
         end
         if axAddress.empty?
            hResponseObj[:readerExecutionMessages] << 'WARNING: FGDC reader: contact address is missing'
         end

         # contact 10.5 (cntvoice) - contact voice phone number [] (required)
         axVoice = xContactInfo.xpath('./cntvoice')
         unless axVoice.empty?
            axVoice.each do |xPhone|
               phone = xPhone.text
               unless phone.empty?
                  add_phone(hContact, phone, 'voice')
               end
            end
         end
         if axVoice.empty?
            hResponseObj[:readerExecutionMessages] << 'WARNING: FGDC reader: contact voice phone is missing'
         end

         # contact 10.6 (cnttdd) - contact TDD/TTY phone number []
         axTDD = xContactInfo.xpath('./cnttdd')
         unless axTDD.empty?
            axTDD.each do |xPhone|
               phone = xPhone.text
               unless phone.empty?
                  add_phone(hContact, phone, 'tty')
               end
            end
         end

         # contact 10.7 (cntfax) - contact fax phone number []
         axFax = xContactInfo.xpath('./cntfax')
         unless axFax.empty?
            axFax.each do |xPhone|
               phone = xPhone.text
               unless phone.empty?
                  add_phone(hContact, phone, 'facsimile')
               end
            end
         end

         # contact 10.8 (cntemail) - contact email address []
         axEmail = xContactInfo.xpath('./cntemail')
         unless axEmail.empty?
            axEmail.each do |xEmail|
               email = xEmail.text
               unless email.empty?
                  unless hContact[:eMailList].include?(email)
                     hContact[:eMailList] << email
                  end
               end
            end
         end

         # contact 10.9 (hours) - hours of service
         hours = xContactInfo.xpath('./hours').text
         unless hours.empty?
            unless hContact[:hoursOfService].include?(hours)
               hContact[:hoursOfService] << hours
            end
         end

         # contact 10.10 (cntinst) - contact instructions
         instruct = xContactInfo.xpath('./cntinst').text
         hContact[:contactInstructions] = instruct unless instruct.empty?

         Fgdc.set_contact(hContact)

         # setup responsibility object
         # let routine that called this module set responsibility roleName
         hResponsibility = intMetadataClass.newResponsibility
         hParty = intMetadataClass.newParty
         hParty[:contactId] = hContact[:contactId]
         aContact = Fgdc.find_contact_by_id(hContact[:contactId])
         hParty[:contactIndex] = aContact[0]
         hParty[:contactType] = aContact[1]
         hResponsibility[:parties] << hParty

         return hResponsibility

      end
   end

   return nil

end