@@ -112,10 +112,10 @@ class EmlImportService {
112
112
* @param connParams
113
113
* @return
114
114
*/
115
- def extractContactsFromEml (eml , dataResource ){
116
-
115
+ def extractContactsFromEml (eml , dataResource ) {
117
116
def contacts = []
118
117
def primaryContacts = []
118
+ def processedContacts = []
119
119
120
120
emlFields. each { name , accessor ->
121
121
def val = accessor(eml)
@@ -124,45 +124,45 @@ class EmlImportService {
124
124
}
125
125
}
126
126
127
- // add contacts...
128
- if (eml. dataset. creator){
129
- eml. dataset. creator. each {
130
- def contact = addOrUpdateContact(it)
131
- if (contact){
127
+ def addUniqueContact = { provider ->
128
+ def providerEmail = provider. electronicMailAddress?. text()?. trim()
129
+ def providerName = " ${ provider.individualName?.givenName?.text()?.trim()} _${ provider.individualName?.surName?.text()?.trim()} "
130
+ def providerOrg = provider. organizationName?. text()?. trim()
131
+ def providerPosition = provider. positionName?. text()?. trim()
132
+
133
+ def uniqueKey = providerEmail ?: providerName ?: providerOrg ?: providerPosition
134
+ if (uniqueKey && ! processedContacts. contains(uniqueKey)) {
135
+ def contact = addOrUpdateContact(provider)
136
+ if (contact) {
132
137
contacts << contact
138
+ processedContacts << uniqueKey
139
+ return contact
133
140
}
134
141
}
135
142
}
136
143
137
- if (eml . dataset . metadataProvider
138
- && eml. dataset . metadataProvider . electronicMailAddress != eml. dataset . creator . electronicMailAddress){
144
+ // EML Schema
145
+ // https:// eml.ecoinformatics.org/images/ eml-party.png
139
146
140
- eml. dataset. metadataProvider. each {
141
- def contact = addOrUpdateContact(it)
142
- if (contact){
143
- contacts << contact
144
- }
145
- }
147
+ if (eml. dataset. creator) {
148
+ eml. dataset. creator. each { addUniqueContact(it) }
146
149
}
147
150
148
- // Add additional contacts
149
- if (eml. dataset. contact){
151
+ if (eml. dataset. contact) {
150
152
eml. dataset. contact. each {
151
- def contact = addOrUpdateContact(it)
152
- if (contact){
153
- contacts << contact
153
+ def contact = addUniqueContact(it)
154
+ if (contact) {
154
155
primaryContacts << contact
155
156
}
156
157
}
157
158
}
158
159
159
- if (eml. dataset. associatedParty){
160
- eml. dataset. associatedParty. each {
161
- def contact = addOrUpdateContact(it)
162
- if (contact){
163
- contacts << contact
164
- }
165
- }
160
+ if (eml. dataset. metadataProvider) {
161
+ eml. dataset. metadataProvider. each { addUniqueContact(it) }
162
+ }
163
+
164
+ if (eml. dataset. associatedParty) {
165
+ eml. dataset. associatedParty. each { addUniqueContact(it) }
166
166
}
167
167
168
168
[contacts : contacts, primaryContacts : primaryContacts]
@@ -173,42 +173,44 @@ class EmlImportService {
173
173
if (emlElement. electronicMailAddress && ! emlElement. electronicMailAddress. isEmpty()) {
174
174
String email = emlElement. electronicMailAddress. text(). trim()
175
175
contact = Contact . findByEmail(email)
176
- } else if (emlElement. individualName. givenName && emlElement. individualName. surName) {
177
- contact = Contact . findByFirstNameAndLastName(emlElement. individualName. givenName, emlElement. individualName. surName)
178
- } else if (emlElement. individualName. surName) {
179
- // surName is mandatory
180
- contact = Contact . findByLastName(emlElement. individualName. surName)
176
+ } else if (emlElement. individualName?. givenName && emlElement. individualName?. surName) {
177
+ contact = Contact . findByFirstNameAndLastName(
178
+ emlElement. individualName. givenName. text()?. trim(),
179
+ emlElement. individualName. surName. text()?. trim()
180
+ )
181
+ } else if (emlElement. individualName?. surName) {
182
+ contact = Contact . findByLastName(emlElement. individualName. surName. text()?. trim())
183
+ } else if (emlElement. organizationName) {
184
+ contact = Contact . findByFirstName(emlElement. organizationName. text()?. trim())
185
+ } else if (emlElement. positionName) {
186
+ contact = Contact . findByFirstName(emlElement. positionName. text()?. trim())
181
187
}
182
188
183
- // Create the contact if it doesn't exist and it's a individualName with email or surName
184
- // to prevent empty contacts (e.g. with emlElement.organizationName only)
185
189
boolean hasEmail = emlElement?. electronicMailAddress?. text()?. trim()?. isEmpty() == false
186
190
boolean hasName = emlElement?. individualName?. surName?. text()?. trim()?. isEmpty() == false
191
+ boolean hasOrg = emlElement?. organizationName?. text()?. trim()?. isEmpty() == false
192
+ boolean hasPosition = emlElement?. positionName?. text()?. trim()?. isEmpty() == false
187
193
188
- if (! contact && (hasEmail || hasName)) {
194
+ if (! contact && (hasEmail || hasName || hasOrg || hasPosition )) {
189
195
contact = new Contact ()
190
- } else {
191
- return null
192
- }
193
-
194
- // Update the contact details
195
- contact. firstName = emlElement. individualName. givenName
196
- contact. lastName = emlElement. individualName. surName
197
- // some email has leading/trailing spaces causing the email constrain regexp to fail, lets trim
198
- contact. email = emlElement. electronicMailAddress. text(). trim()
199
- contact. setUserLastModified(collectoryAuthService. username())
200
- Contact . withTransaction {
201
- if (contact. validate()) {
202
- contact. save(flush : true , failOnError : true )
203
- return contact
204
- } else {
205
- contact. errors. each {
206
- log. error(" Problem creating contact: " + it)
196
+ // For now we are just going to set the first name to the organization name
197
+ contact. firstName = emlElement. individualName?. givenName?. text()?. trim()
198
+ if (hasOrg)
199
+ contact. firstName = emlElement. organizationName?. text()?. trim()
200
+ if (hasPosition)
201
+ contact. firstName = emlElement. positionName?. text()?. trim()
202
+ contact. lastName = emlElement. individualName?. surName?. text()?. trim()
203
+ contact. email = emlElement. electronicMailAddress?. text()?. trim()
204
+ contact. setUserLastModified(collectoryAuthService. username())
205
+ Contact . withTransaction {
206
+ if (contact. validate()) {
207
+ contact. save(flush : true , failOnError : true )
208
+ } else {
209
+ log. error(" Validation errors creating contact: ${ contact.errors} " )
210
+ return null
207
211
}
208
- return null
209
212
}
210
213
}
211
-
212
- contact
214
+ return contact
213
215
}
214
216
}
0 commit comments