/** * Creates a {@link Representation} instance by using the parsed ID or * {@link RepresentationTest#DEFAULT_REPRESENTATION_ID} if <code>null</code> is parsed as ID. * * @param id * The ID or <code>null</code> to use the default ID * @return the Representation. */ protected Representation createRepresentation(String id) { if (id == null) { id = DEFAULT_REPRESENTATION_ID; } return getValueFactory().createRepresentation(id); }
@Override public Representation process(Representation source) { if(mapper == null){ throw new IllegalStateException("The mapper is not initialised. One must call setConfiguration to configure the FieldMapper!"); } if(source == null){ return null; } else { return mapper.applyMappings(source, vf.createRepresentation(source.getId()), vf); } } /**
@Override public Representation process(Representation source) { if(mapper == null){ throw new IllegalStateException("The mapper is not initialised. One must call setConfiguration to configure the FieldMapper!"); } if(source == null){ return null; } else { return mapper.applyMappings(source, vf.createRepresentation(source.getId()), vf); } } /**
protected Representation create(String id, boolean store) throws YardException { Representation r; if (store) { r = getYard().create(id); } else if (id != null && !id.isEmpty()) { r = getYard().getValueFactory().createRepresentation(id); } else { throw new IllegalArgumentException("If store is FALSE the id MUST NOT be NULL nor EMPTY!"); } representationIds.add(r.getId()); return r; }
/** * Used to create the Representation the first time * {@link #getRepresentation()} is called for the current entity. The * information for the Representation are already stored in {@link #data} */ private Representation createRepresentation() { final String uri; if(currentEntity.isBlank()){ //STANBOL-765: support bNodes StringBuilder sb = new StringBuilder(bnodePrefix); sb.append(currentEntity.getBlankNodeId().getLabelString()); uri = sb.toString(); } else { uri = currentEntity.getURI(); } Representation representation = vf.createRepresentation(uri); Iterator<Node> it = data.iterator(); while(it.hasNext()){ //data contains field,value pairs //because of that we call two times next for String field = it.next().getURI(); //the field Node value = it.next();//and the value processValue(value, representation, field); } return representation; } }
/** * Used to create the Representation the first time * {@link #getRepresentation()} is called for the current entity. The * information for the Representation are already stored in {@link #data} */ private Representation createRepresentation() { final String uri; if(currentEntity.isBlank()){ //STANBOL-765: support bNodes StringBuilder sb = new StringBuilder(bnodePrefix); sb.append(currentEntity.getBlankNodeId().getLabelString()); uri = sb.toString(); } else { uri = currentEntity.getURI(); } Representation representation = vf.createRepresentation(uri); Iterator<Node> it = data.iterator(); while(it.hasNext()){ //data contains field,value pairs //because of that we call two times next for String field = it.next().getURI(); //the field Node value = it.next();//and the value processValue(value, representation, field); } return representation; } }
/** * Creates a representation with the parsed ID. If <code>null</code> is * parsed a random UUID is generated as describe in {@link #create()}.<p> * Note that {@link #store(Representation)} is called for the newly created * representation and the Representation returned by this Method is returned. * @param id The id or <code>null</code> to create a random uuid. * @return The newly created, empty and stored representation * @see Yard#create(String) * @see Yard#store(Representation) */ @Override public final Representation create(String id) throws IllegalArgumentException,YardException { if(config == null){ throw new IllegalStateException("This Yard is not activated"); } if(id == null){ //create a new ID do { id = createRandomEntityUri(); } while(isRepresentation(id)); } else if(isRepresentation(id)){ throw new IllegalArgumentException( String.format("An representation with the parsed ID %s is already present in this Yard",id)); } return store(valueFactory.createRepresentation(id)); }
/** * Creates a representation with the parsed ID. If <code>null</code> is * parsed a random UUID is generated as describe in {@link #create()}.<p> * Note that {@link #store(Representation)} is called for the newly created * representation and the Representation returned by this Method is returned. * @param id The id or <code>null</code> to create a random uuid. * @return The newly created, empty and stored representation * @see Yard#create(String) * @see Yard#store(Representation) */ @Override public final Representation create(String id) throws IllegalArgumentException,YardException { if(config == null){ throw new IllegalStateException("This Yard is not activated"); } if(id == null){ //create a new ID do { id = createRandomEntityUri(); } while(isRepresentation(id)); } else if(isRepresentation(id)){ throw new IllegalArgumentException( String.format("An representation with the parsed ID %s is already present in this Yard",id)); } return store(valueFactory.createRepresentation(id)); }
/** * Applies the mappings defined by the {@link #baseMapper} and the {@link #additionalMapper} * to the parsed Representation. * * @param yard The yard (local reference to avoid syncronization) * @param representation The representation to map * @return the mapped representation */ private Representation applyCacheMappings(Yard yard, Representation representation) { long start = System.currentTimeMillis(); Representation mapped = null; ValueFactory valueFactory = getValueFactory(); if (baseMapper != null) { mapped = yard.getValueFactory().createRepresentation(representation.getId()); baseMapper.applyMappings(representation, mapped,valueFactory); } if (additionalMapper != null) { if (mapped == null) { mapped = yard.getValueFactory().createRepresentation(representation.getId()); } additionalMapper.applyMappings(representation, mapped,valueFactory); } log.info(" -- applied mappings in " + (System.currentTimeMillis() - start) + "ms"); return mapped != null ? mapped : representation; }
/** * Applies the mappings defined by the {@link #baseMapper} and the {@link #additionalMapper} * to the parsed Representation. * * @param yard The yard (local reference to avoid syncronization) * @param representation The representation to map * @return the mapped representation */ private Representation applyCacheMappings(Yard yard, Representation representation) { long start = System.currentTimeMillis(); Representation mapped = null; ValueFactory valueFactory = getValueFactory(); if (baseMapper != null) { mapped = yard.getValueFactory().createRepresentation(representation.getId()); baseMapper.applyMappings(representation, mapped,valueFactory); } if (additionalMapper != null) { if (mapped == null) { mapped = yard.getValueFactory().createRepresentation(representation.getId()); } additionalMapper.applyMappings(representation, mapped,valueFactory); } log.info(" -- applied mappings in " + (System.currentTimeMillis() - start) + "ms"); return mapped != null ? mapped : representation; }
/** * Create a sub-representation by considering the base {@link Representation}, * IDs already taken by other sub representations. The Id addon the caller * would like to add to the id of the base representation. In addition it * adds the relation between the base and the sub-representation as well as * the type and the inverse links to the sub-representation. * @param base the base (parent) representation * @param addon the string addon to the id of the base * @param takenIds set of IDs that are already taken * @param mapping the mapping used to get the information needed to correctly * initialise the sub-relation */ private Representation createSubRepresentation(Representation base, String addon, Set<String> takenIds, Mapping mapping) { Representation current = vf.createRepresentation( checkId(base.getId()+addon, takenIds)); initSubRepresentation(current, base, mapping); return current; }
/** * Stores the baseMappings to the {@link Yard}. This may cause unexpected * behaviour for subsequest calls of the stored configuration does not * correspond with the actual data stored within the cache.<p> * Typically this is only used at the start or end of the creation of a * full Cache ({@link CacheStrategy#all}) of an referenced site (entity source).<p> * Note also that if the {@link #baseMapper} is <code>null</code> this * method removes any existing configuration from the yard. * @throws YardException an any error while storing the config to the yard. * @throws IllegalArgumentException if <code>null</code> is parsed as {@link Yard}. */ public static void storeBaseMappingsConfiguration(Yard yard,FieldMapper baseMapper) throws YardException,IllegalArgumentException { if(yard == null){ throw new IllegalArgumentException("The parsed Yard MUST NOT be NULL!"); } if(baseMapper == null){ yard.remove(Cache.BASE_CONFIGURATION_URI); } else { Representation config = yard.getValueFactory().createRepresentation(Cache.BASE_CONFIGURATION_URI); writeFieldConfig(config,baseMapper); yard.store(config); } } /**
/** * Stores the current configuration used for caching documents back to the * {@link Yard}. This configuration is present in the {@link #additionalMapper}). * If this field is <code>null</code> than any existing configuration is * removed form the index. * @throws YardException on any error while changing the configuration in the * yard. * @throws IllegalArgumentException if <code>null</code> is parsed as {@link Yard}. */ protected static void storeAdditionalMappingsConfiguration(Yard yard,FieldMapper additionalMapper) throws YardException,IllegalArgumentException { if(yard == null){ throw new IllegalArgumentException("The parsed Yard MUST NOT be NULL!"); } if(additionalMapper == null){ yard.remove(Cache.ADDITIONAL_CONFIGURATION_URI); } else { Representation config = yard.getValueFactory().createRepresentation(Cache.ADDITIONAL_CONFIGURATION_URI); writeFieldConfig(config,additionalMapper); yard.store(config); } } /**
/** * Executes the parsed {@link Program} and stores the * {@link Program#getFields() fields} in a {@link Representation}. The actual * implementation used for the {@link Representation} depends on the * {@link ValueFactory} of this EntityhubLDPath instance * @param context the context * @param program the program * @return the {@link Representation} holding the results of the execution * @throws IllegalArgumentException if the parsed context or the program is * <code>null</code> */ public Representation execute(Reference context,Program<Object> program){ if(context == null){ throw new IllegalArgumentException("The parsed context MUST NOT be NULL!"); } if(program == null){ throw new IllegalArgumentException("The parsed program MUST NOT be NULL!"); } Representation result = vf.createRepresentation(context.getReference()); for(FieldMapping<?,Object> mapping : program.getFields()) { Collection<?> values = mapping.getValues(backend,context); if(values !=null && !values.isEmpty()){ result.add(mapping.getFieldName(),values); } } return result; } /**
/** * Stores the current configuration used for caching documents back to the * {@link Yard}. This configuration is present in the {@link #additionalMapper}). * If this field is <code>null</code> than any existing configuration is * removed form the index. * @throws YardException on any error while changing the configuration in the * yard. * @throws IllegalArgumentException if <code>null</code> is parsed as {@link Yard}. */ protected static void storeAdditionalMappingsConfiguration(Yard yard,FieldMapper additionalMapper) throws YardException,IllegalArgumentException { if(yard == null){ throw new IllegalArgumentException("The parsed Yard MUST NOT be NULL!"); } if(additionalMapper == null){ yard.remove(Cache.ADDITIONAL_CONFIGURATION_URI); } else { Representation config = yard.getValueFactory().createRepresentation(Cache.ADDITIONAL_CONFIGURATION_URI); writeFieldConfig(config,additionalMapper); yard.store(config); } } /**
/** * Stores the baseMappings to the {@link Yard}. This may cause unexpected * behaviour for subsequest calls of the stored configuration does not * correspond with the actual data stored within the cache.<p> * Typically this is only used at the start or end of the creation of a * full Cache ({@link CacheStrategy#all}) of an referenced site (entity source).<p> * Note also that if the {@link #baseMapper} is <code>null</code> this * method removes any existing configuration from the yard. * @throws YardException an any error while storing the config to the yard. * @throws IllegalArgumentException if <code>null</code> is parsed as {@link Yard}. */ public static void storeBaseMappingsConfiguration(Yard yard,FieldMapper baseMapper) throws YardException,IllegalArgumentException { if(yard == null){ throw new IllegalArgumentException("The parsed Yard MUST NOT be NULL!"); } if(baseMapper == null){ yard.remove(Cache.BASE_CONFIGURATION_URI); } else { Representation config = yard.getValueFactory().createRepresentation(Cache.BASE_CONFIGURATION_URI); writeFieldConfig(config,baseMapper); yard.store(config); } } /**
private Representation testRepresentation(String id) { ValueFactory vf = getValueFactory(); Representation rep = vf.createRepresentation(id); assertNotNull(rep); assertNotNull(rep.getId()); if (id != null) { assertEquals(rep.getId(), id); } return rep; }
/** * Tests that multiple Representations are removed. * * @throws YardException */ @Test public void testRemoveRepresentations() throws YardException { // NOTE: This test needs not to use the create(..) method, because we // remove the created representation form the store anyway as part of the // test String id = "urn:yard.test.testRemoveRepresentationsWithNullValue:representation.id1"; String id2 = "urn:yard.test.testRemoveRepresentationsWithNullValue:representation.id2"; String field = "urn:the.field:used.for.this.Test"; String testValue = "This is a test"; Yard yard = getYard(); // use both ways to add the two Representations (should make no differences, // but one never can know ... Representation test1 = yard.create(id); // create and add Representation test2 = yard.getValueFactory().createRepresentation(id2); // create test2.add(field, testValue); // add value yard.store(test2);// store assertTrue(yard.isRepresentation(test1.getId())); // test if stored assertTrue(yard.isRepresentation(test2.getId())); yard.remove(Arrays.asList(test1.getId(), test2.getId())); // remove assertFalse(yard.isRepresentation(test1.getId())); // test if removed assertFalse(yard.isRepresentation(test2.getId())); } /**
String testValue = "This is a test"; Yard yard = getYard(); Representation test = yard.getValueFactory().createRepresentation(id); test.add(field, testValue);
Representation test2 = yard.getValueFactory().createRepresentation(id2); // create