Commit bf48e810 authored by Ricki Hirner's avatar Ricki Hirner

Better handling for empty FN/N

* allow empty FN and N from received VCards
* always write FN and N to outgoing VCards
* trim empty FN/N/phonetic name/job title/note components to null
* debugging: log built data rows
parent 37a8021a
Pipeline #4641912 passed with stage
in 11 minutes and 4 seconds
......@@ -729,6 +729,13 @@ public class AndroidContact {
}
protected void insertStructuredName(BatchOperation batch) {
if (contact.displayName == null &&
contact.prefix == null &&
contact.givenName == null && contact.middleName == null && contact.familyName == null &&
contact.suffix == null &&
contact.phoneticGivenName == null && contact.phoneticMiddleName == null && contact.phoneticFamilyName == null)
return;
final BatchOperation.Operation op;
final ContentProviderOperation.Builder builder = ContentProviderOperation.newInsert(dataSyncURI());
if (id == null)
......@@ -738,8 +745,8 @@ public class AndroidContact {
builder.withValue(StructuredName.RAW_CONTACT_ID, id);
}
builder .withValue(RawContacts.Data.MIMETYPE, CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
.withValue(StructuredName.PREFIX, contact.prefix)
.withValue(StructuredName.DISPLAY_NAME, contact.displayName)
.withValue(StructuredName.PREFIX, contact.prefix)
.withValue(StructuredName.GIVEN_NAME, contact.givenName)
.withValue(StructuredName.MIDDLE_NAME, contact.middleName)
.withValue(StructuredName.FAMILY_NAME, contact.familyName)
......@@ -747,6 +754,7 @@ public class AndroidContact {
.withValue(StructuredName.PHONETIC_GIVEN_NAME, contact.phoneticGivenName)
.withValue(StructuredName.PHONETIC_MIDDLE_NAME, contact.phoneticMiddleName)
.withValue(StructuredName.PHONETIC_FAMILY_NAME, contact.phoneticFamilyName);
Constants.log.log(Level.FINER, "Built StructuredName data row", builder.build());
batch.enqueue(op);
}
......@@ -833,6 +841,7 @@ public class AndroidContact {
.withValue(Phone.LABEL, typeLabel)
.withValue(Phone.IS_PRIMARY, is_primary ? 1 : 0)
.withValue(Phone.IS_SUPER_PRIMARY, is_primary ? 1 : 0);
Constants.log.log(Level.FINER, "Built Phone data row", builder.build());
batch.enqueue(op);
}
......@@ -886,6 +895,7 @@ public class AndroidContact {
.withValue(Email.LABEL, typeLabel)
.withValue(Email.IS_PRIMARY, is_primary ? 1 : 0)
.withValue(Phone.IS_SUPER_PRIMARY, is_primary ? 1 : 0);
Constants.log.log(Level.FINER, "Built Email data row", builder.build());
batch.enqueue(op);
}
......@@ -916,6 +926,7 @@ public class AndroidContact {
.withValue(Organization.DEPARTMENT, department)
.withValue(Organization.TITLE, contact.jobTitle)
.withValue(Organization.JOB_DESCRIPTION, contact.jobDescription);
Constants.log.log(Level.FINER, "Built Organization data row", builder.build());
batch.enqueue(op);
}
......@@ -990,13 +1001,14 @@ public class AndroidContact {
op = new BatchOperation.Operation(builder);
builder.withValue(Im.RAW_CONTACT_ID, id);
}
if (sipAddress)
if (sipAddress) {
// save as SIP address
builder .withValue(SipAddress.MIMETYPE, SipAddress.CONTENT_ITEM_TYPE)
.withValue(SipAddress.DATA, impp.getHandle())
.withValue(SipAddress.TYPE, typeCode)
.withValue(SipAddress.LABEL, typeLabel);
else {
Constants.log.log(Level.FINER, "Built SipAddress data row", builder.build());
} else {
// save as IM address
builder .withValue(Im.MIMETYPE, Im.CONTENT_ITEM_TYPE)
.withValue(Im.DATA, impp.getHandle())
......@@ -1004,6 +1016,7 @@ public class AndroidContact {
.withValue(Im.LABEL, typeLabel)
.withValue(Im.PROTOCOL, protocolCode)
.withValue(Im.CUSTOM_PROTOCOL, protocolLabel);
Constants.log.log(Level.FINER, "Built Im data row", builder.build());
}
batch.enqueue(op);
}
......@@ -1046,6 +1059,7 @@ public class AndroidContact {
.withValue(Nickname.NAME, nick.getValues().get(0))
.withValue(Nickname.TYPE, typeCode)
.withValue(Nickname.LABEL, typeLabel);
Constants.log.log(Level.FINER, "Built Nickname data row", builder.build());
batch.enqueue(op);
}
}
......@@ -1062,6 +1076,7 @@ public class AndroidContact {
}
builder .withValue(Note.MIMETYPE, Note.CONTENT_ITEM_TYPE)
.withValue(Note.NOTE, contact.note);
Constants.log.log(Level.FINER, "Built Note data row", builder.build());
batch.enqueue(op);
}
}
......@@ -1133,6 +1148,7 @@ public class AndroidContact {
.withValue(StructuredPostal.REGION, address.getRegion())
.withValue(StructuredPostal.POSTCODE, address.getPostalCode())
.withValue(StructuredPostal.COUNTRY, address.getCountry());
Constants.log.log(Level.FINER, "Built StructuredPostal data row", builder.build());
batch.enqueue(op);
}
......@@ -1184,6 +1200,7 @@ public class AndroidContact {
.withValue(Website.URL, url.getValue())
.withValue(Website.TYPE, typeCode)
.withValue(Website.LABEL, typeLabel);
Constants.log.log(Level.FINER, "Built Website data row", builder.build());
batch.enqueue(op);
}
......@@ -1206,6 +1223,7 @@ public class AndroidContact {
.withValue(Event.TYPE, type)
.withValue(Event.START_DATE, formatter.format(dateOrTime.getDate()));
batch.enqueue(op);
Constants.log.log(Level.FINER, "Built Event data row", builder.build());
}
protected void insertRelation(BatchOperation batch, Related related) {
......@@ -1244,6 +1262,7 @@ public class AndroidContact {
.withValue(Relation.NAME, related.getText())
.withValue(Relation.TYPE, typeCode)
.withValue(Relation.LABEL, StringUtils.join(labels, "/"));
Constants.log.log(Level.FINER, "Built Relation data row", builder.build());
batch.enqueue(op);
}
......
......@@ -206,37 +206,35 @@ public class Contact {
// FN
FormattedName fn = vCard.getFormattedName();
if (fn != null) {
c.displayName = fn.getValue();
c.displayName = StringUtils.trimToNull(fn.getValue());
vCard.removeProperties(FormattedName.class);
} else
Constants.log.warning("Received VCard without FN (formatted name)");
}
// N
StructuredName n = vCard.getStructuredName();
if (n != null) {
c.prefix = StringUtils.join(n.getPrefixes(), ' ');
c.givenName = n.getGiven();
c.middleName = StringUtils.join(n.getAdditionalNames(), ' ');
c.familyName = n.getFamily();
c.suffix = StringUtils.join(n.getSuffixes(), ' ');
c.prefix = StringUtils.trimToNull(StringUtils.join(n.getPrefixes(), ' '));
c.givenName = StringUtils.trimToNull(n.getGiven());
c.middleName = StringUtils.trimToNull(StringUtils.join(n.getAdditionalNames(), ' '));
c.familyName = StringUtils.trimToNull(n.getFamily());
c.suffix = StringUtils.trimToNull(StringUtils.join(n.getSuffixes(), ' '));
vCard.removeProperties(StructuredName.class);
} else
Constants.log.warning("Received VCard without N (structured name)");
}
// phonetic names
RawProperty phoneticFirstName = vCard.getExtendedProperty(PROPERTY_PHONETIC_FIRST_NAME),
phoneticMiddleName = vCard.getExtendedProperty(PROPERTY_PHONETIC_MIDDLE_NAME),
phoneticLastName = vCard.getExtendedProperty(PROPERTY_PHONETIC_LAST_NAME);
if (phoneticFirstName != null) {
c.phoneticGivenName = phoneticFirstName.getValue();
c.phoneticGivenName = StringUtils.trimToNull(phoneticFirstName.getValue());
vCard.removeExtendedProperty(PROPERTY_PHONETIC_FIRST_NAME);
}
if (phoneticMiddleName != null) {
c.phoneticMiddleName = phoneticMiddleName.getValue();
c.phoneticMiddleName = StringUtils.trimToNull(phoneticMiddleName.getValue());
vCard.removeExtendedProperty(PROPERTY_PHONETIC_MIDDLE_NAME);
}
if (phoneticLastName != null) {
c.phoneticFamilyName = phoneticLastName.getValue();
c.phoneticFamilyName = StringUtils.trimToNull(phoneticLastName.getValue());
vCard.removeExtendedProperty(PROPERTY_PHONETIC_LAST_NAME);
}
......@@ -259,10 +257,11 @@ public class Contact {
vCard.removeProperties(Organization.class);
// TITLE
for (Title title : vCard.getTitles()) {
c.jobTitle = title.getValue();
c.jobTitle = StringUtils.trimToNull(title.getValue());
vCard.removeProperties(Title.class);
break;
}
// ROLE
for (Role role : vCard.getRoles()) {
c.jobDescription = role.getValue();
......@@ -293,7 +292,7 @@ public class Contact {
for (Note note : vCard.getNotes())
notes.add(note.getValue());
if (!notes.isEmpty())
c.note = StringUtils.join(notes, "\n\n\n");
c.note = StringUtils.trimToNull(StringUtils.join(notes, "\n\n\n"));
vCard.removeProperties(Note.class);
// CATEGORY
......@@ -396,21 +395,18 @@ public class Contact {
String fn = null;
if (displayName != null)
fn = displayName;
else if (organization != null && organization.getValues() != null && organization.getValues().get(0) != null)
if (StringUtils.isEmpty(fn) && organization != null && organization.getValues() != null && !organization.getValues().isEmpty())
fn = organization.getValues().get(0);
else if (nickName != null)
if (StringUtils.isEmpty(fn) && nickName != null)
fn = nickName.getValues().get(0);
else {
if (!phoneNumbers.isEmpty())
fn = phoneNumbers.get(0).property.getText();
else if (!emails.isEmpty())
if (StringUtils.isEmpty(fn) && !emails.isEmpty())
fn = emails.get(0).property.getValue();
Constants.log.warning("No FN (formatted name) available, using " + fn);
}
if (StringUtils.isEmpty(fn)) {
fn = "-";
Constants.log.warning("No FN (formatted name) available, using \"-\"");
}
if (StringUtils.isEmpty(fn) && !phoneNumbers.isEmpty())
fn = phoneNumbers.get(0).property.getText();
if (StringUtils.isEmpty(fn))
fn = uid;
if (StringUtils.isEmpty(fn))
fn = "";
vCard.setFormattedName(fn);
// N
......@@ -432,11 +428,11 @@ public class Contact {
} else if (vCardVersion == VCardVersion.V3_0) {
// (only) VCard 3 requires N [RFC 2426 3.1.2]
if (group && groupMethod == GroupMethod.GROUP_VCARDS) {
Constants.log.info("No structured name available, using formatted name as family name");
StructuredName n = new StructuredName();
n.setFamily(fn);
vCard.setStructuredName(n);
}
} else
vCard.setStructuredName(new StructuredName());
}
// phonetic names
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment