/** * Helper method that consumes a catalog objects iterator keeping only the ones visible in the * current context. * * @param objects iterator over catalog objects * @param filter filter that checks if an element should be visible * @return an iterator over the catalog objects visible in the current context */ private <T extends CatalogInfo> CloseableIterator<T> filterIsolated( CloseableIterator<T> objects, Function<T, T> filter) { List<T> iterable = new ArrayList<>(); // consume the iterator while (objects.hasNext()) { T object = objects.next(); if (filter.apply(object) != null) { // this catalog object is visible in the current context iterable.add(object); } } // create an iterator for the visible catalog objects return new CloseableIteratorAdapter<>(iterable.iterator()); }
@Override public <T extends CatalogInfo> int count(Class<T> of, Filter filter) { CloseableIterator<T> found = facade.list(of, filter, null, null); try (CloseableIterator<T> filtered = filterIsolated(of, found)) { int count = 0; while (filtered.hasNext()) { count++; filtered.next(); } return count; } }
@Override public <T extends CatalogInfo> T get(Class<T> type, Filter filter) throws IllegalArgumentException { final Integer limit = Integer.valueOf(2); CloseableIterator<T> it = list(type, filter, null, limit, null); T result = null; try { if (it.hasNext()) { result = it.next(); if (it.hasNext()) { throw new IllegalArgumentException( "Specified query predicate resulted in more than one object"); } } } finally { it.close(); } return result; }
public void visit(LayerGroupInfo layerGroupToRemove) { Filter associatedTo = Predicates.equal("layers", layerGroupToRemove, MatchAction.ANY); try (CloseableIterator<LayerGroupInfo> it = catalog.list(LayerGroupInfo.class, associatedTo)) { while (it.hasNext()) { LayerGroupInfo group = it.next(); if (group.getLayers().contains(layerGroupToRemove)) { final List<PublishedInfo> layers = new ArrayList<>(group.getLayers()); layers.removeAll(objects.keySet()); if (layers.size() == 0) { visit(group); } else { add(group, ModificationType.GROUP_CHANGED); } } } } add(layerGroupToRemove, ModificationType.DELETE); } }
public void visit(LayerInfo layer) { // mark layer and resource as removed add(layer.getResource(), ModificationType.DELETE); add(layer, ModificationType.DELETE); // scan the layer groups and find those that do use the // current layer Filter groupContainsLayer = Predicates.equal("layers", layer, MatchAction.ANY); try (CloseableIterator<LayerGroupInfo> it = catalog.list(LayerGroupInfo.class, groupContainsLayer)) { while (it.hasNext()) { LayerGroupInfo group = it.next(); // mark the layer as one that will be removed Set<LayerInfo> layers = groups.get(group); if (layers == null) { layers = new HashSet<LayerInfo>(); groups.put(group, layers); } layers.add(layer); // a group can contain the same layer multiple times. We want to // make sure to mark the group as removed if all the layers inside of // it are going to be removed, just changed otherwise if (layers.size() == new HashSet<PublishedInfo>(group.getLayers()).size()) { visit(group); } else { add(group, ModificationType.GROUP_CHANGED); } } } }
LayerInfo li = it.next(); if (style.equals(li.getDefaultStyle())) add(li, ModificationType.STYLE_RESET); else if (li.getStyles().contains(style)) catalog.list(LayerGroupInfo.class, groupAssociated)) { while (it.hasNext()) { LayerGroupInfo group = it.next(); if (style.equals(group.getRootLayerStyle())) { add(group, ModificationType.GROUP_CHANGED);
public void visit(LayerGroupInfo layerGroupToRemove) { // remove layerGroupToRemove references from other groups Filter associatedTo = Predicates.equal("layers.id", layerGroupToRemove.getId(), MatchAction.ANY); try (CloseableIterator<LayerGroupInfo> it = catalog.list(LayerGroupInfo.class, associatedTo)) { while (it.hasNext()) { LayerGroupInfo group = it.next(); // parallel remove of layer and styles int index = getLayerGroupIndex(layerGroupToRemove, group); while (index != -1) { group.getLayers().remove(index); group.getStyles().remove(index); index = getLayerGroupIndex(layerGroupToRemove, group); } if (group.getLayers().size() == 0) { // if group is empty, delete it visit(group); } else { catalog.save(group); } } } // finally remove the group catalog.remove(layerGroupToRemove); }
public void visit(LayerInfo layer) { // first update the groups, remove the layer, and if no // other layers remained, remove the group as well Filter groupContainsLayer = Predicates.equal("layers.id", layer.getId(), MatchAction.ANY); try (CloseableIterator<LayerGroupInfo> groups = catalog.list(LayerGroupInfo.class, groupContainsLayer)) { while (groups.hasNext()) { LayerGroupInfo group = groups.next(); // parallel remove of layer and styles int index = group.getLayers().indexOf(layer); while (index != -1) { group.getLayers().remove(index); group.getStyles().remove(index); index = group.getLayers().indexOf(layer); } // either update or remove the group if (group.getLayers().size() == 0) { visit(catalog.getLayerGroup(group.getId())); } else { catalog.save(group); } } } // remove the layer and (for the moment) its resource as well // TODO: change this to just remove the resource once the // resource/publish split is done ResourceInfo resource = layer.getResource(); catalog.remove(layer); catalog.remove(resource); }
public void visit(StyleInfo style) { // find the layers having this style as primary or secondary Filter anyStyle = Predicates.equal("styles.id", style.getId(), MatchAction.ANY); Filter layersAssociated = Predicates.or(Predicates.equal("defaultStyle.id", style.getId()), anyStyle); // remove style references in layers try (CloseableIterator<LayerInfo> it = catalog.list(LayerInfo.class, layersAssociated)) { while (it.hasNext()) { LayerInfo layer = it.next(); removeStyleInLayer(layer, style); } } // groups can also refer to style, reset each reference to the // associated layer default style Filter groupAssociated = Predicates.or(Predicates.equal("rootLayerStyle.id", style.getId()), anyStyle); try (CloseableIterator<LayerGroupInfo> it = catalog.list(LayerGroupInfo.class, groupAssociated)) { while (it.hasNext()) { LayerGroupInfo group = it.next(); removeStyleInLayerGroup(group, style); } } // finally remove the style catalog.remove(style); }
private <T extends CatalogInfo> void testOrderBy( Class<T> clazz, Filter filter, Integer offset, Integer limit, SortBy sortOrder, List<T> expected) { CatalogPropertyAccessor pe = new CatalogPropertyAccessor(); List<Object> props = new ArrayList<Object>(); List<Object> actual = new ArrayList<Object>(); String sortProperty = sortOrder.getPropertyName().getPropertyName(); for (T info : expected) { Object pval = pe.getProperty(info, sortProperty); props.add(pval); } CloseableIterator<T> it = catalog.list(clazz, filter, offset, limit, sortOrder); try { while (it.hasNext()) { Object property = pe.getProperty(it.next(), sortProperty); actual.add(property); } } finally { it.close(); } assertEquals(props, actual); }
@Override public Object nextElement() { return it.next().getPrefix(); } };
private void updateLayers(CloseableIterator<LayerInfo> affectedLayers) { try { while (affectedLayers.hasNext()) { LayerInfo geogigLayer = affectedLayers.next(); setIdentifier(geogigLayer); } } finally { affectedLayers.close(); } }
/** * Returns a sorted list of layer names in the specified workspace (or * if the workspace is *) */ ArrayList<String> getLayerNames(String rootName) { ArrayList<String> result = new ArrayList<String>(); if (!rootName.equals("*")) { Filter wsResources = Predicates.equal("store.workspace.name", rootName); try (CloseableIterator<ResourceInfo> it = getCatalog().list(ResourceInfo.class, wsResources)) { while (it.hasNext()) { result.add(it.next().getName()); } } // collect also layer groups getCatalog() .getLayerGroupsByWorkspace(rootName) .stream() .map(lg -> lg.getName()) .forEach( name -> { if (!result.contains(name)) { result.add(name); } }); Collections.sort(result); } result.add(0, "*"); return result; }
static <T> List<T> collectAndClose(CloseableIterator<T> it) throws IOException { if (it == null) return null; try { LinkedList<T> list = new LinkedList<T>(); while (it.hasNext()) { list.add(it.next()); } return list; } finally { it.close(); } }
static <T> List<T> collectAndClose(CloseableIterator<T> it) throws IOException { if (it == null) return null; try { LinkedList<T> list = new LinkedList<T>(); while (it.hasNext()) { list.add(it.next()); } return list; } finally { it.close(); } }
private void handleWorkspaceChange(WorkspaceInfo ws) { final CatalogModifyEvent preModifyEvent = PRE_MODIFY_EVENT.get(); PRE_MODIFY_EVENT.remove(); final List<String> changedProperties = preModifyEvent.getPropertyNames(); // was the workspace name modified? this implies a name change in workspace local styles int nameIdx = changedProperties.indexOf("name"); if (nameIdx == -1) { return; } String oldWorkspaceName = (String) preModifyEvent.getOldValues().get(nameIdx); String newWorkspaceName = (String) preModifyEvent.getNewValues().get(nameIdx); // grab the styles CloseableIterator<StyleInfo> styles = catalog.list(StyleInfo.class, Predicates.equal("workspace.name", newWorkspaceName)); try { while (styles.hasNext()) { StyleInfo style = styles.next(); String oldStyleName = oldWorkspaceName + ":" + style.getName(); String newStyleName = newWorkspaceName + ":" + style.getName(); handleStyleRenamed(oldStyleName, newStyleName); } } finally { styles.close(); } }
/** * Returns a sorted list of layer names in the specified workspace (or * if the workspace is *) */ protected List<String> getLayerNames(String workspaceName) { List<String> resultSet = new ArrayList<String>(); if (workspaceName != null) { FilterFactory2 ff = CommonFactoryFinder.getFilterFactory2(); try (CloseableIterator<ResourceInfo> it = getCatalog() .getFacade() .list( ResourceInfo.class, Predicates.equal("store.workspace.name", workspaceName), null, null, ff.sort("name", SortOrder.ASCENDING))) { while (it.hasNext()) { resultSet.add(it.next().getName()); } } } return resultSet; }
private synchronized int getConnectionCount() { int count = 0; CloseableIterator<DataStoreInfo> i = getDataStores(); try { for (; i.hasNext(); ) { DataStoreInfo meta = i.next(); if (!meta.isEnabled()) { // Don't count connections from disabled datastores. continue; } try { meta.getDataStore(null); } catch (Throwable notAvailable) { // TODO: Logging. continue; } count += 1; } } finally { i.close(); } return count; }
@Override public boolean hasNext() { if (next != null) { return true; } boolean hasNext = featureTypes.hasNext(); if (!hasNext) { featureTypes.close(); return false; } else { try { FeatureTypeInfo featureType = featureTypes.next(); CollectionDocument collection = new CollectionDocument(geoServer, request, featureType); next = collection; return true; } catch (Exception e) { featureTypes.close(); throw new ServiceException( "Failed to iterate over the feature types in the catalog", e); } } }
@Test public void testGetDomain() throws IOException { Name name = new NameImpl(DC.NAMESPACE, "type"); CloseableIterator<String> domain = store.getDomain(new NameImpl(CSW.NAMESPACE, "Record"), name); assertTrue(domain.hasNext()); assertEquals("http://purl.org/dc/dcmitype/Dataset", domain.next()); assertEquals("http://purl.org/dc/dcmitype/Image", domain.next()); assertEquals("http://purl.org/dc/dcmitype/Service", domain.next()); assertEquals("http://purl.org/dc/dcmitype/Text", domain.next()); assertFalse(domain.hasNext()); domain.close(); }