@SuppressWarnings("unchecked") @Override public void remove(String field, Object parsedValue) { if(field == null){ throw new IllegalArgumentException("The parsed field MUST NOT be NULL"); } else if(field.isEmpty()){ throw new IllegalArgumentException("The parsed field MUST NOT be Empty"); } Collection<Object> removeValues = new ArrayList<Object>(); ModelUtils.checkValues(valueFactory, parsedValue, removeValues); Object values = representation.get(field); if(values == null) { return; } else if(removeValues.contains(values)){ //in case this field has a single value and this values is part of //the values to remove -> remove the whole field representation.remove(field); } else if(values instanceof Collection<?>){ if(((Collection<Object>)values).removeAll(removeValues) && //remove all Elements ((Collection<Object>)values).size()<2){ //if removed check for size if(((Collection<Object>)values).size()==1){ //only one element remaining -> replace the collection with a Object representation.put(field, ((Collection<Object>)values).iterator().next()); } else { //if no element remains, remove the field representation.remove(field); } } } //else ignore (single value for field && value not to be removed) }
/** * Loads the additional field mappings used by this cache from the yard. * This method sets the {@link #baseMapper} field during initialisation. * @param yard The yard * @param nsPrefixService if present '{prefix}:{localname}' configurations are * @return The parsed mappings or <code>null</code> if no found * @throws YardException on any Error while reading the {@link Representation} * holding the configuration from the {@link Yard}. * @throws IllegalArgumentException if <code>null</code> is parsed as {@link Yard}. */ protected static FieldMapper loadAdditionalMappings(Yard yard, NamespacePrefixService nsPrefixService) throws YardException { if(yard == null){ throw new IllegalArgumentException("The parsed Yard MUST NOT be NULL!"); } Representation addConfig = yard.getRepresentation(Cache.ADDITIONAL_CONFIGURATION_URI); if(addConfig != null){ FieldMapper mapper = readFieldConfig(yard,addConfig, nsPrefixService); if(mapper == null){ log.warn("Invalid Additinal Configuration: Unable to parse FieldMappings from Field "+Cache.FIELD_MAPPING_CONFIG_FIELD+"-> return NULL (no additional Configuration)"); if(log.isWarnEnabled()){ log.warn(ModelUtils.getRepresentationInfo(addConfig)); } } return mapper; } else { return null; } } /**
/** * Copies all elements of the parsed Iterator to a {@link ArrayList}. * To use other Set implementations that {@link ArrayList} you can use * {@link #addToCollection(Iterator, Collection) * @param <T> the generic type of the returned Collection * @param it the Iterator with elements compatible to T * @return the collection containing all elements of the iterator * @throws IllegalArgumentException if the parsed {@link Iterator} is <code>null</code> */ public static <T> Collection<T> asCollection(Iterator<? extends T> it){ return addToCollection(it, new ArrayList<T>()); } /**
results = ModelUtils.asCollection(r.get(property.toString())); } else { results = new LinkedHashSet<Object>(); for(Iterator<String> properties = r.getFieldNames();properties.hasNext();){ results.addAll(ModelUtils.addToCollection(r.get(properties.next()), results));
/** * Getter for the id of the Entity the parsed {@link Representation metadata} * are {@link RdfResourceEnum#aboutRepresentation about}. * @param metadata the metadata * @return the id of the entity or <code>null</code> if the parsed {@link Representation} * is <code>null</code> or does not define a value for * {@link RdfResourceEnum#aboutRepresentation} */ public static String getAboutRepresentation(Representation metadata) throws IllegalStateException{ if(metadata == null){ return null; } Iterator<Reference> refs = metadata.getReferences(RdfResourceEnum.aboutRepresentation.getUri()); if(refs.hasNext()){ Reference about = refs.next(); if(refs.hasNext()){ log.warn("The parsed Representation {} claims to be the metadata of" + "multiple Entities (entities: {})", metadata.getId(), asCollection(metadata.getReferences(RdfResourceEnum.aboutRepresentation.getUri()))); } return about.getReference(); } else { return null; } }
/** * Appends an URI if possible by using prefix:localName * @param builder the StringBuilder to add the URI MUST NOT be NULL * @param uri the URI to add MUST NOT be NULL */ private static void appendUri(StringBuilder builder, String uri) { String[] namespaceLocal = ModelUtils.getNamespaceLocalName(uri); if(namespaceLocal[0]!=null){ NamespaceEnum namespace = NamespaceEnum.forNamespace(namespaceLocal[0]); if(namespace != null){ builder.append(namespace.getPrefix()).append(':'); } else { builder.append(namespaceLocal[0]); } } //else no name space to add (e.g. if the pattern is "*") builder.append(namespaceLocal[1]); } }
/** * Checks if the parsed Entity can be wrapped by this EntityMapping wrapper. * Currently it checks only if the rdf:type entityhub:EntityMapping is * present * @param entity the entity to check * @return */ public static boolean isValid(Entity entity){ if(entity != null){ Set<Reference> types = ModelUtils.asSet(entity.getRepresentation().getReferences(RDF_TYPE)); return types.contains(ENTITY_MAPPING_TYPE); } else { return false; } } /**
/** * Copies all elements of the parsed Iterator to a {@link HashSet}. * To use other Set implementations that {@link HashSet} you can use * {@link #addToSet(Iterator, Set)} * @param <T> the generic type of the returned set * @param it the Iterator with elements compatible to T * @return the set containing all elements of the iterator * @throws IllegalArgumentException if the parsed {@link Iterator} is <code>null</code> */ public static <T> Set<T> asSet(Iterator<? extends T> it){ if(it == null){ throw new IllegalArgumentException("The parsed Iterator MUST NOT be NULL!"); } return addToSet(it, new HashSet<T>()); } /**
/** * Getter for the id of the Entity the parsed {@link Representation metadata} * are {@link RdfResourceEnum#aboutRepresentation about}. * @param metadata the metadata * @return the id of the entity or <code>null</code> if the parsed {@link Representation} * is <code>null</code> or does not define a value for * {@link RdfResourceEnum#aboutRepresentation} */ public static String getAboutRepresentation(Representation metadata) throws IllegalStateException{ if(metadata == null){ return null; } Iterator<Reference> refs = metadata.getReferences(RdfResourceEnum.aboutRepresentation.getUri()); if(refs.hasNext()){ Reference about = refs.next(); if(refs.hasNext()){ log.warn("The parsed Representation {} claims to be the metadata of" + "multiple Entities (entities: {})", metadata.getId(), asCollection(metadata.getReferences(RdfResourceEnum.aboutRepresentation.getUri()))); } return about.getReference(); } else { return null; } }
/** * Appends an URI if possible by using prefix:localName * @param builder the StringBuilder to add the URI MUST NOT be NULL * @param uri the URI to add MUST NOT be NULL */ private static void appendUri(StringBuilder builder, String uri) { String[] namespaceLocal = ModelUtils.getNamespaceLocalName(uri); if(namespaceLocal[0]!=null){ NamespaceEnum namespace = NamespaceEnum.forNamespace(namespaceLocal[0]); if(namespace != null){ builder.append(namespace.getPrefix()).append(':'); } else { builder.append(namespaceLocal[0]); } } //else no name space to add (e.g. if the pattern is "*") builder.append(namespaceLocal[1]); } }
/** * Checks if the parsed Entity can be wrapped by this EntityMapping wrapper. * Currently it checks only if the rdf:type entityhub:EntityMapping is * present * @param entity the entity to check * @return */ public static boolean isValid(Entity entity){ if(entity != null){ Set<Reference> types = ModelUtils.asSet(entity.getRepresentation().getReferences(RDF_TYPE)); return types.contains(ENTITY_MAPPING_TYPE); } else { return false; } } /**
/** * Copies all elements of the parsed Iterator to a {@link HashSet}. * To use other Set implementations that {@link HashSet} you can use * {@link #addToSet(Iterator, Set)} * @param <T> the generic type of the returned set * @param it the Iterator with elements compatible to T * @return the set containing all elements of the iterator * @throws IllegalArgumentException if the parsed {@link Iterator} is <code>null</code> */ public static <T> Set<T> asSet(Iterator<? extends T> it){ if(it == null){ throw new IllegalArgumentException("The parsed Iterator MUST NOT be NULL!"); } return addToSet(it, new HashSet<T>()); } /**
@SuppressWarnings("unchecked") @Override public void remove(String field, Object parsedValue) { if(field == null){ throw new IllegalArgumentException("The parsed field MUST NOT be NULL"); } else if(field.isEmpty()){ throw new IllegalArgumentException("The parsed field MUST NOT be Empty"); } Collection<Object> removeValues = new ArrayList<Object>(); ModelUtils.checkValues(valueFactory, parsedValue, removeValues); Object values = representation.get(field); if(values == null) { return; } else if(removeValues.contains(values)){ //in case this field has a single value and this values is part of //the values to remove -> remove the whole field representation.remove(field); } else if(values instanceof Collection<?>){ if(((Collection<Object>)values).removeAll(removeValues) && //remove all Elements ((Collection<Object>)values).size()<2){ //if removed check for size if(((Collection<Object>)values).size()==1){ //only one element remaining -> replace the collection with a Object representation.put(field, ((Collection<Object>)values).iterator().next()); } else { //if no element remains, remove the field representation.remove(field); } } } //else ignore (single value for field && value not to be removed) }
/** * Loads the additional field mappings used by this cache from the yard. * This method sets the {@link #baseMapper} field during initialisation. * @param yard The yard * @param nsPrefixService if present '{prefix}:{localname}' configurations are * @return The parsed mappings or <code>null</code> if no found * @throws YardException on any Error while reading the {@link Representation} * holding the configuration from the {@link Yard}. * @throws IllegalArgumentException if <code>null</code> is parsed as {@link Yard}. */ protected static FieldMapper loadAdditionalMappings(Yard yard, NamespacePrefixService nsPrefixService) throws YardException { if(yard == null){ throw new IllegalArgumentException("The parsed Yard MUST NOT be NULL!"); } Representation addConfig = yard.getRepresentation(Cache.ADDITIONAL_CONFIGURATION_URI); if(addConfig != null){ FieldMapper mapper = readFieldConfig(yard,addConfig, nsPrefixService); if(mapper == null){ log.warn("Invalid Additinal Configuration: Unable to parse FieldMappings from Field "+Cache.FIELD_MAPPING_CONFIG_FIELD+"-> return NULL (no additional Configuration)"); if(log.isWarnEnabled()){ log.warn(ModelUtils.getRepresentationInfo(addConfig)); } } return mapper; } else { return null; } } /**
private void deleteMappingsbyTarget(String id) throws YardException { if(id != null && !id.isEmpty()){ FieldQuery fieldQuery = getQueryFactory().createFieldQuery(); fieldQuery.setConstraint(RdfResourceEnum.mappingTarget.getUri(), new ReferenceConstraint(id)); deleteEntities(ModelUtils.asCollection( entityhubYard.findReferences(fieldQuery).iterator())); } }
/** * This Method uses {@link #getNamespaceLocalName(String)} to split up * namespace and local name. It uses also the Data in the * {@link NamespaceEnum} to retrieve prefixes for Namespaces. * @param uri the URI * @return the QName */ public static QName getQName(String uri){ String[] nsln = getNamespaceLocalName(uri); if(nsln[0] != null){ NamespaceEnum entry = NamespaceEnum.forNamespace(nsln[0]); if(entry != null){ return new QName(nsln[0], nsln[1],entry.getPrefix()); } else { return new QName(nsln[0], nsln[1]); } } else { return new QName(nsln[1]); } } /**
/** * Copies all elements of the parsed Iterator to a {@link ArrayList}. * To use other Set implementations that {@link ArrayList} you can use * {@link #addToCollection(Iterator, Collection) * @param <T> the generic type of the returned Collection * @param it the Iterator with elements compatible to T * @return the collection containing all elements of the iterator * @throws IllegalArgumentException if the parsed {@link Iterator} is <code>null</code> */ public static <T> Collection<T> asCollection(Iterator<? extends T> it){ return addToCollection(it, new ArrayList<T>()); } /**
if(expectedFields != null){ //validate fields for(String field : expectedFields){ Set<Object> expectedFieldValues = ModelUtils.asSet( data.representations.get(result.getId()).get(field)); Iterator<Object> fieldValues = result.get(field);
ModelUtils.checkValues(valueFactory, parsedValue, newValues); Object values = representation.get(field); if(values != null){
fieldMapper.applyMappings(rep, clerezzaRep, valueFactory); if(log.isTraceEnabled()){ log.trace("dereferenced via Mappings {}", ModelUtils.getRepresentationInfo(clerezzaRep));