Search in sources :

Example 1 with Property

use of net.fortuna.ical4j.vcard.Property in project bw-calendar-engine by Bedework.

the class BwPrincipalInfo method setPropertiesFromVCard.

/**
 * @param cardStr
 * @throws CalFacadeException
 */
public void setPropertiesFromVCard(final String cardStr, final String addrDataCtype) throws CalFacadeException {
    if (cardStr == null) {
        return;
    }
    this.cardStr = cardStr;
    addProperty(new PrincipalProperty<String>("vcard", cardStr));
    try {
        if ("application/vcard+json".equals(addrDataCtype)) {
            card = new JsonCardBuilder(null).build(new StringReader(cardStr));
        } else {
            card = new VCardBuilder(new StringReader(cardStr)).build();
        }
        Property piprop = card.getExtendedProperty("X-BW-PRINCIPALHREF");
        if (piprop != null) {
            setPrincipalHref(piprop.getValue());
        }
        piprop = card.getExtendedProperty("X-ICAL4J-TOV3-KIND");
        if (piprop != null) {
            setKind(piprop.getValue());
        }
        if (getKind() == null) {
            // Check for member attributes
            piprop = card.getProperty(Id.MEMBER);
            if (piprop != null) {
                setKind(Kind.GROUP.getValue());
            }
        }
        for (PrincipalPropertyInfo ppi : BwPrincipalInfo.getPrincipalPropertyInfoSet()) {
            Property.Id pname = ppi.getVcardPname();
            if (pname == null) {
                // Not a vcard property
                continue;
            }
            if (!ppi.getMulti()) {
                // Single valued
                Property prop = card.getProperty(pname);
                if (prop == null) {
                    continue;
                }
                addProperty(ppi, prop);
            } else {
                List<Property> ps = card.getProperties(pname);
                if (Util.isEmpty(ps)) {
                    continue;
                }
                for (Property prop : ps) {
                    addProperty(ppi, prop);
                }
            }
        }
    } catch (final Throwable t) {
        if (debug) {
            debug("CardStr was " + cardStr);
        }
        throw new CalFacadeException(t);
    }
}
Also used : Id(net.fortuna.ical4j.vcard.Property.Id) VCardBuilder(net.fortuna.ical4j.vcard.VCardBuilder) StringReader(java.io.StringReader) ToString(org.bedework.util.misc.ToString) Property(net.fortuna.ical4j.vcard.Property) JsonCardBuilder(org.bedework.util.vcard.JsonCardBuilder) CalFacadeException(org.bedework.calfacade.exc.CalFacadeException)

Example 2 with Property

use of net.fortuna.ical4j.vcard.Property in project bw-calendar-engine by Bedework.

the class AbstractDirImpl method find.

@Override
public List<BwPrincipalInfo> find(final String cua, final String cutype, final boolean expand, final Holder<Boolean> truncated) throws CalFacadeException {
    final CardDavInfo cdi = getCardDavInfo(false);
    if ((cdi == null) || (cdi.getHost() == null)) {
        return null;
    }
    BasicHttpClient cdc = null;
    try {
        cdc = new BasicHttpClient(cdi.getHost(), cdi.getPort(), null, 15 * 1000);
        final List<BwPrincipalInfo> pis = find(cdc, cdi, cua, cutype);
        if (!expand) {
            return pis;
        }
        for (final BwPrincipalInfo pi : pis) {
            if (!Kind.GROUP.getValue().equalsIgnoreCase(pi.getKind())) {
                continue;
            }
            final List<BwPrincipalInfo> memberPis = new ArrayList<>();
            VCard card = pi.getCard();
            List<Property> members = card.getProperties(Id.MEMBER);
            if (members == null) {
                continue;
            }
            for (final Property p : members) {
                BwPrincipalInfo memberPi = fetch(cdc, cdi, p.getValue());
                if (memberPi != null) {
                    memberPis.add(memberPi);
                }
            }
            pi.setMembers(memberPis);
        }
        return pis;
    } catch (final Throwable t) {
        if (getLogger().isDebugEnabled()) {
            error(t);
        }
        throw new CalFacadeException(t);
    } finally {
        if (cdc != null) {
            try {
                cdc.release();
            } catch (final Throwable ignored) {
            }
        }
    }
}
Also used : ArrayList(java.util.ArrayList) BasicHttpClient(org.bedework.util.http.BasicHttpClient) CardDavInfo(org.bedework.calfacade.configs.CardDavInfo) BwPrincipalInfo(org.bedework.calfacade.BwPrincipalInfo) VCard(net.fortuna.ical4j.vcard.VCard) BooleanPrincipalProperty(org.bedework.calfacade.BwPrincipalInfo.BooleanPrincipalProperty) IntPrincipalProperty(org.bedework.calfacade.BwPrincipalInfo.IntPrincipalProperty) Property(net.fortuna.ical4j.vcard.Property) WebdavProperty(org.bedework.webdav.servlet.shared.WebdavProperty) BwProperty(org.bedework.calfacade.BwProperty) CalFacadeException(org.bedework.calfacade.exc.CalFacadeException)

Example 3 with Property

use of net.fortuna.ical4j.vcard.Property 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();
}
Also used : Address(net.fortuna.ical4j.vcard.property.Address) Org(net.fortuna.ical4j.vcard.property.Org) HashMap(java.util.HashMap) Representation(org.apache.stanbol.entityhub.servicesapi.model.Representation) Mapping(org.apache.stanbol.entityhub.indexing.source.vcard.OntologyMappings.Mapping) VCARD_ORGANIZATION(org.apache.stanbol.entityhub.indexing.source.vcard.OntologyMappings.VCARD_ORGANIZATION) N_GIVEN(org.apache.stanbol.entityhub.indexing.source.vcard.OntologyMappings.N_GIVEN) N(net.fortuna.ical4j.vcard.property.N) ADR_REGION(org.apache.stanbol.entityhub.indexing.source.vcard.OntologyMappings.ADR_REGION) VCARD_PERSON(org.apache.stanbol.entityhub.indexing.source.vcard.OntologyMappings.VCARD_PERSON) Parameter(net.fortuna.ical4j.vcard.Parameter) Property(net.fortuna.ical4j.vcard.Property)

Aggregations

Property (net.fortuna.ical4j.vcard.Property)3 CalFacadeException (org.bedework.calfacade.exc.CalFacadeException)2 StringReader (java.io.StringReader)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Parameter (net.fortuna.ical4j.vcard.Parameter)1 Id (net.fortuna.ical4j.vcard.Property.Id)1 VCard (net.fortuna.ical4j.vcard.VCard)1 VCardBuilder (net.fortuna.ical4j.vcard.VCardBuilder)1 Address (net.fortuna.ical4j.vcard.property.Address)1 N (net.fortuna.ical4j.vcard.property.N)1 Org (net.fortuna.ical4j.vcard.property.Org)1 ADR_REGION (org.apache.stanbol.entityhub.indexing.source.vcard.OntologyMappings.ADR_REGION)1 Mapping (org.apache.stanbol.entityhub.indexing.source.vcard.OntologyMappings.Mapping)1 N_GIVEN (org.apache.stanbol.entityhub.indexing.source.vcard.OntologyMappings.N_GIVEN)1 VCARD_ORGANIZATION (org.apache.stanbol.entityhub.indexing.source.vcard.OntologyMappings.VCARD_ORGANIZATION)1 VCARD_PERSON (org.apache.stanbol.entityhub.indexing.source.vcard.OntologyMappings.VCARD_PERSON)1 Representation (org.apache.stanbol.entityhub.servicesapi.model.Representation)1 BwPrincipalInfo (org.bedework.calfacade.BwPrincipalInfo)1 BooleanPrincipalProperty (org.bedework.calfacade.BwPrincipalInfo.BooleanPrincipalProperty)1