@@ -115,6 +115,7 @@ class EmlImportService {
115
115
def extractContactsFromEml (eml , dataResource ){
116
116
117
117
def contacts = []
118
+ def primaryContacts = []
118
119
119
120
emlFields. each { name , accessor ->
120
121
def val = accessor(eml)
@@ -123,10 +124,10 @@ class EmlImportService {
123
124
}
124
125
}
125
126
126
- // add a contacts...
127
+ // add contacts...
127
128
if (eml. dataset. creator){
128
129
eml. dataset. creator. each {
129
- def contact = addContact (it)
130
+ def contact = addOrUpdateContact (it)
130
131
if (contact){
131
132
contacts << contact
132
133
}
@@ -137,36 +138,77 @@ class EmlImportService {
137
138
&& eml. dataset. metadataProvider. electronicMailAddress != eml. dataset. creator. electronicMailAddress){
138
139
139
140
eml. dataset. metadataProvider. each {
140
- def contact = addContact (it)
141
+ def contact = addOrUpdateContact (it)
141
142
if (contact){
142
143
contacts << contact
143
144
}
144
145
}
145
146
}
146
147
147
- contacts
148
+ // Add additional contacts
149
+ if (eml. dataset. contact){
150
+ eml. dataset. contact. each {
151
+ def contact = addOrUpdateContact(it)
152
+ if (contact){
153
+ contacts << contact
154
+ primaryContacts << contact
155
+ }
156
+ }
157
+ }
158
+
159
+ if (eml. dataset. associatedParty){
160
+ eml. dataset. associatedParty. each {
161
+ def contact = addOrUpdateContact(it)
162
+ if (contact){
163
+ contacts << contact
164
+ }
165
+ }
166
+ }
167
+
168
+ [contacts : contacts, primaryContacts : primaryContacts]
148
169
}
149
170
150
- private def addContact (emlElement ){
151
- def contact = Contact . findByEmail(emlElement. electronicMailAddress)
152
- if (! contact){
171
+ private def addOrUpdateContact (emlElement ) {
172
+ def contact = null
173
+ if (emlElement. electronicMailAddress && ! emlElement. electronicMailAddress. isEmpty()) {
174
+ String email = emlElement. electronicMailAddress. text(). trim()
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)
181
+ }
182
+
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
+ boolean hasEmail = emlElement?. electronicMailAddress?. text()?. trim()?. isEmpty() == false
186
+ boolean hasName = emlElement?. individualName?. surName?. text()?. trim()?. isEmpty() == false
187
+
188
+ if (! contact && (hasEmail || hasName)) {
153
189
contact = new Contact ()
154
- contact. firstName = emlElement. individualName. givenName
155
- contact. lastName = emlElement. individualName. surName
156
- contact. email = emlElement. electronicMailAddress
157
- contact. setUserLastModified(collectoryAuthService. username())
158
- Contact . withTransaction {
159
- if (contact. validate()) {
160
- contact. save(flush : true , failOnError : true )
161
- return contact
162
- } else {
163
- contact. errors. each {
164
- log. error(" Problem creating contact: " + it)
165
- }
166
- return null
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)
167
207
}
208
+ return null
168
209
}
169
210
}
211
+
170
212
contact
171
213
}
172
214
}
0 commit comments