use of org.geotoolkit.storage.event.AggregationEvent in project geotoolkit by Geomatys.
the class AggregatedCoverageResource method add.
/**
* Add a new resource in the aggregation.
* The resource is not copied.
*
* @param resource
* @return the same resource
* @throws DataStoreException if new resource is incompatible with other resources.
*/
@Override
public synchronized Resource add(Resource resource) throws DataStoreException {
if (resource instanceof GridCoverageResource) {
final GridCoverageResource gcr = (GridCoverageResource) resource;
if (bands.isEmpty()) {
// first resource in the aggregation
bands.addAll(toMapping(Arrays.asList(gcr)));
eraseCaches();
listeners.fire(new AggregationEvent(this, AggregationEvent.TYPE_ADD, resource), AggregationEvent.class);
return resource;
}
final List<SampleDimension> sampleDimensions = gcr.getSampleDimensions();
if (sampleDimensions.size() != bands.size()) {
throw new DataStoreException("Coverage sample dimension size " + sampleDimensions.size() + "do not match bands size " + bands.size());
}
for (int i = 0, n = sampleDimensions.size(); i < n; i++) {
final VirtualBand band = bands.get(i);
band.sources.add(new Source(gcr, i));
}
eraseCaches();
try {
initModel();
} catch (DataStoreException ex) {
// restore to an usable state
remove(resource, false);
throw ex;
}
listeners.fire(new AggregationEvent(this, AggregationEvent.TYPE_ADD, resource), AggregationEvent.class);
return resource;
} else {
throw new DataStoreException("Resource must be an instance of GridCoverageResource");
}
}
use of org.geotoolkit.storage.event.AggregationEvent in project geotoolkit by Geomatys.
the class AggregatedCoverageResource method remove.
private synchronized void remove(Resource resource, boolean sendEvent) throws DataStoreException {
ArgumentChecks.ensureNonNull("resource", resource);
boolean found = false;
for (VirtualBand band : bands) {
final ListIterator<Source> ite = band.sources.listIterator();
while (ite.hasNext()) {
Source source = ite.next();
if (source.resource == resource) {
found = true;
ite.remove();
}
}
}
if (!found) {
throw new DataStoreException("Resource not found");
}
if (sendEvent) {
listeners.fire(new AggregationEvent(this, AggregationEvent.TYPE_REMOVE, resource), AggregationEvent.class);
}
eraseCaches();
}
use of org.geotoolkit.storage.event.AggregationEvent in project geotoolkit by Geomatys.
the class AggregatedCoverageResource method removeAll.
/**
* Remove all resources from aggregation.
* This method does not delete any data.
*/
public synchronized void removeAll() {
final Set<Resource> removed = new HashSet<>();
for (VirtualBand band : bands) {
for (Source source : band.sources) {
removed.add(source.resource);
}
band.sources.clear();
}
listeners.fire(new AggregationEvent(this, AggregationEvent.TYPE_REMOVE, removed.toArray(new Resource[0])), AggregationEvent.class);
eraseCaches();
}
use of org.geotoolkit.storage.event.AggregationEvent in project geotoolkit by Geomatys.
the class InMemoryAggregate method add.
@Override
public synchronized Resource add(Resource resource) throws DataStoreException {
Resource newr;
if (resource instanceof FeatureSet) {
final FeatureSet fs = (FeatureSet) resource;
final InMemoryFeatureSet newres = new InMemoryFeatureSet(fs.getType());
try (Stream<Feature> stream = fs.features(false)) {
newres.add(stream.iterator());
}
newr = newres;
} else if (resource instanceof DefiningTiledGridCoverageResource) {
final DefiningTiledGridCoverageResource cr = (DefiningTiledGridCoverageResource) resource;
final GenericName name = cr.getIdentifier().orElse(null);
newr = new InMemoryTiledGridCoverageResource(name);
} else if (resource instanceof GridCoverageResource && resource instanceof TiledResource) {
final GridCoverageResource cr = (GridCoverageResource) resource;
final GenericName name = cr.getIdentifier().orElse(null);
newr = new InMemoryTiledGridCoverageResource(name);
} else if (resource instanceof GridCoverageResource) {
final GridCoverageResource cr = (GridCoverageResource) resource;
final GenericName name = cr.getIdentifier().orElse(null);
final InMemoryGridCoverageResource newres = new InMemoryGridCoverageResource(name);
newres.write(cr.read(null));
newr = newres;
} else if (resource instanceof DefiningGridCoverageResource) {
final DefiningGridCoverageResource cr = (DefiningGridCoverageResource) resource;
final GenericName name = cr.getIdentifier().orElse(null);
newr = new InMemoryGridCoverageResource(name);
} else if (resource instanceof Aggregate) {
final Aggregate agg = (Aggregate) resource;
final InMemoryAggregate newres = new InMemoryAggregate(agg.getIdentifier().orElse(null));
for (Resource r : agg.components()) {
newres.add(r);
}
newr = newres;
} else {
throw new DataStoreException("Unsupported resource type " + resource);
}
resources.add(newr);
listeners.fire(new AggregationEvent(this, AggregationEvent.TYPE_ADD, newr), AggregationEvent.class);
return newr;
}
Aggregations