use of net.fortuna.ical4j.vcard.property.N in project stanbol by apache.
the class VcardIndexingSource method processVcard.
/**
* Converts a vCard object to Representations.
* @param vCard the vCard object to process
* @param mappings the Mappings to use
* @param entityMap the Map holding the ids of already processed vCards. This
* is used to avoid id conflicts
* @return Iterator over the processed Representation
*/
protected Iterator<Representation> processVcard(VCard vCard, Map<String, Mapping> mappings, Map<EntityType, Map<String, Set<String>>> entityMap) {
// NOTE: this is protected to allow direct access from the VCardIterator
String name = null;
EntityType entityType = null;
Property nameProperty = vCard.getProperty(Property.Id.FN);
if (nameProperty != null && nameProperty.getValue() != null && !nameProperty.getValue().isEmpty()) {
entityType = EntityType.person;
name = nameProperty.getValue();
} else {
// FN name -> maybe a ORG was exported
Property orgProperty = vCard.getProperty(Property.Id.ORG);
if (orgProperty != null && ((Org) orgProperty).getValues() != null && ((Org) orgProperty).getValues().length > 0) {
entityType = EntityType.organization;
name = ((Org) orgProperty).getValues()[0];
}
}
if (entityType == null) {
log.warn("Unable to index vCard object without values for FN or ORG parameter (vCard: {})", vCard);
return Collections.emptyList().iterator();
}
String id = null;
Property uid = vCard.getProperty(Property.Id.UID);
if (uid != null) {
id = uid.getValue();
} else {
id = name;
}
id = entityByName(entityMap, entityType, name, id, true);
// we have a name and an id (local name of the URI/URN)
// ... now parse the vCard
Representation rep = vf.createRepresentation(id);
Map<String, Representation> representations = new HashMap<String, Representation>();
representations.put(rep.getId(), rep);
// add the type
Mapping typeMapping = mappings.get(entityType == EntityType.person ? VCARD_PERSON : VCARD_ORGANIZATION);
if (typeMapping != null) {
rep.add(NamespaceEnum.rdf + "type", typeMapping.uri);
}
log.debug("vCard [type: {} | name: '{}' | id: '{}']", new Object[] { entityType, name, rep.getId() });
for (Property property : vCard.getProperties()) {
Property.Id propertyId = property.getId();
String propName = propertyId.getPropertyName();
if (mappings.containsKey(propName)) {
// there is a mapping for this property
// the Representation to write the Information of the current Property
Representation current;
// the Map with the mappings to be used for processing the current
// Property
Map<String, Mapping> currentMappings;
// May be null!!
Mapping mapping = mappings.get(propName);
if (mapping == null || mapping.subMappings == null) {
// add to the base Representation
current = rep;
// and use the parsed mappings
currentMappings = mappings;
} else {
// indicates we need to create a new Representation
current = null;
// and use the sub mappings
currentMappings = mapping.subMappings;
}
switch(propertyId) {
case N:
N n = (N) property;
String given = n.getGivenName();
String family = n.getFamilyName();
if ((given == null || given.isEmpty()) && (family == null || family.isEmpty())) {
log.warn("'N' property '{}'does not define given nor family name -> ignored", n.getValue());
} else {
if (current == null) {
// create new Representation
current = createSubRepresentation(rep, ".name", representations.keySet(), mapping);
representations.put(current.getId(), current);
}
Mapping subPropertyMapping = currentMappings.get(N_GIVEN);
if (subPropertyMapping != null && given != null && !given.isEmpty()) {
current.addNaturalText(subPropertyMapping.uri, StringUtils.chomp(given).trim());
}
subPropertyMapping = currentMappings.get(N_FAMILY);
if (subPropertyMapping != null & family != null && !family.isEmpty()) {
current.addNaturalText(subPropertyMapping.uri, StringUtils.chomp(family).trim());
}
String[] additional = n.getAdditionalNames();
subPropertyMapping = currentMappings.get(N_ADDITIONAL);
if (subPropertyMapping != null & additional != null && additional.length > 0) {
for (String value : additional) {
if (value != null && !value.isEmpty()) {
current.addNaturalText(subPropertyMapping.uri, StringUtils.chomp(value).trim());
}
}
}
String[] prefixes = n.getPrefixes();
subPropertyMapping = currentMappings.get(N_PREFIX);
if (subPropertyMapping != null & prefixes != null && prefixes.length > 0) {
for (String value : prefixes) {
if (value != null && !value.isEmpty()) {
current.addNaturalText(subPropertyMapping.uri, StringUtils.chomp(value).trim());
}
}
}
String[] suffixes = n.getSuffixes();
subPropertyMapping = currentMappings.get(N_SUFFIX);
if (subPropertyMapping != null & suffixes != null && suffixes.length > 0) {
for (String value : suffixes) {
if (value != null && !value.isEmpty()) {
current.addNaturalText(subPropertyMapping.uri, StringUtils.chomp(value).trim());
}
}
}
}
break;
case ADR:
Address address = (Address) property;
if (address.getValue() != null && // check of the value does not only contain seperators (',')
!address.getValue().replace(';', ' ').trim().isEmpty()) {
if (current == null) {
// create new Representation
current = createSubRepresentation(rep, ".adr", representations.keySet(), mapping);
representations.put(current.getId(), current);
}
Mapping subPropertyMapping = currentMappings.get(ADR_POST_OFFICE_ADDRESS);
String value = address.getPoBox();
if (subPropertyMapping != null && value != null && !value.isEmpty()) {
// add string -> this is no natural language text
current.add(subPropertyMapping.uri, StringUtils.chomp(value).trim());
}
value = address.getExtended();
subPropertyMapping = currentMappings.get(ADR_EXTENDED);
if (subPropertyMapping != null && value != null && !value.isEmpty()) {
current.addNaturalText(subPropertyMapping.uri, StringUtils.chomp(value).trim());
}
value = address.getStreet();
subPropertyMapping = currentMappings.get(ADR_STREET);
if (subPropertyMapping != null && value != null && !value.isEmpty()) {
current.addNaturalText(subPropertyMapping.uri, StringUtils.chomp(value).trim());
}
value = address.getLocality();
subPropertyMapping = currentMappings.get(ADR_LOCALITY);
if (subPropertyMapping != null && value != null && !value.isEmpty()) {
current.addNaturalText(subPropertyMapping.uri, StringUtils.chomp(value).trim());
}
value = address.getRegion();
subPropertyMapping = currentMappings.get(ADR_REGION);
if (subPropertyMapping != null && value != null && !value.isEmpty()) {
current.addNaturalText(subPropertyMapping.uri, StringUtils.chomp(value).trim());
}
value = address.getPostcode();
subPropertyMapping = currentMappings.get(ADR_POSTAL_CODE);
if (subPropertyMapping != null && value != null && !value.isEmpty()) {
// add string -> this is no natural language text
current.add(subPropertyMapping.uri, StringUtils.chomp(value).trim());
}
value = address.getCountry();
subPropertyMapping = currentMappings.get(ADR_COUNTRY);
if (subPropertyMapping != null && value != null && !value.isEmpty()) {
// add string -> based on the standard this should be the two letter code
current.add(subPropertyMapping.uri, StringUtils.chomp(value).trim());
}
}
// else empty ADR field -> ignore
break;
case ORG:
Org org = (Org) property;
String[] unitHierarchy = org.getValues();
Mapping orgNameMapping = currentMappings.get(OntologyMappings.ORG_NAME);
if (unitHierarchy.length > 0 && orgNameMapping != null && unitHierarchy[0] != null && unitHierarchy[0].trim().length() > 0) {
String orgName = unitHierarchy[0];
if (current == null) {
// create new Representation for the Organisation
// Note: this is an Entity and no sub-RDFTerm!
String orgEntityId = entityByName(entityMap, EntityType.organization, orgName, null, false);
if (orgEntityId == null) {
// create new Entity for this Organization
orgEntityId = entityByName(entityMap, EntityType.organization, orgName, null, true);
current = vf.createRepresentation(orgEntityId);
initSubRepresentation(current, rep, mapping);
representations.put(current.getId(), current);
current.addNaturalText(orgNameMapping.uri, StringUtils.chomp(orgName).trim());
// organisations. Therefore delete this relation for now
if (mapping.invUri != null) {
current.removeAll(mapping.invUri);
}
// TODO: Organisation units are not supported
} else {
rep.addReference(mapping.uri, orgEntityId);
}
}
}
break;
default:
if (current != null && mapping != null) {
String value = property.getValue();
if (value != null) {
value = StringUtils.chomp(property.getValue()).trim();
}
if (value.isEmpty()) {
log.warn("Unable to index empty value for property {} of vCard {}", property.getId().getPropertyName(), rep.getId());
} else {
current.addNaturalText(mapping.uri, value);
}
} else if (mapping != null) {
log.warn("Sub-Resources are not supported for Property {} (mapping to {} ignored)!", propName, mapping);
}
// else no mapping defined
break;
}
String value = property.getValue();
log.debug(" - {}: {}", propertyId.getPropertyName(), value);
for (Parameter param : property.getParameters()) {
Parameter.Id paramId = param.getId();
String paramValue = param.getValue();
log.debug(" {}:{}", paramId.getPname(), paramValue);
}
} else {
log.debug("No mapping for Property {} with value {}", propertyId, property.getValue());
}
}
log.debug(" > Mapped Data;");
if (log.isDebugEnabled()) {
for (Representation tmp : representations.values()) {
log.info(ModelUtils.getRepresentationInfo(tmp));
}
}
log.debug("--- end ---");
return representations.values().iterator();
}
Aggregations