All Downloads are FREE. Search and download functionalities are using the official Maven repository.

nl.pdok.workspacebuilder.model.layer.GroupLayer Maven / Gradle / Ivy

The newest version!
package nl.pdok.workspacebuilder.model.layer;

import it.geosolutions.geoserver.rest.encoder.GSLayerGroupEncoder23;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import nl.pdok.catalogus.Catalogus;
import nl.pdok.workspacebuilder.GeoserverRestProxy;
import nl.pdok.workspacebuilder.model.Service;

/**
 * Created with IntelliJ IDEA.
 * User: nijhur
 * Date: 4-3-14
 * Time: 14:58
 * To change this template use File | Settings | File Templates.
 */
public class GroupLayer extends Layer {

    public enum GrouplLayerType { CONTAINER, SINGLE };
    private List layers;
    private GrouplLayerType grouplLayerType;

    public GroupLayer(Catalogus catalogus){
        super(Type.GROUP, catalogus);
        layers = new ArrayList();
    }
    
    public GroupLayer(Catalogus catalogus, String name){
        super(Type.GROUP, catalogus);
        layers = new ArrayList();
        this.setName(name);
    	
    }    

    public void addLayer(Layer layer){
        layers.add(layer);
    }

    public List getLayers() {
        return layers;
    }

    public GrouplLayerType getGrouplLayerType() {
        return grouplLayerType;
    }

    public void setGrouplLayerType(GrouplLayerType grouplLayerType) {
        this.grouplLayerType = grouplLayerType;
    }

	@Override
	public void addLayerToGeoserver(Service service, GeoserverRestProxy geoserverRestProxy) {
     
		logger.debug("Creating grouplayer: " + getName());
        GSLayerGroupEncoder23 layerGroupEncoder = new GSLayerGroupEncoder23();
        layerGroupEncoder.setName(getName());
        layerGroupEncoder.setTitle(getTitle());
        layerGroupEncoder.setAbstract(getAbstractText());
        layerGroupEncoder.setWorkspace(service.getWorkspaceName());
        layerGroupEncoder.setMode(getGrouplLayerType().toString());
        Collections.reverse(getLayers());
        for (Layer layer : getLayers()){
        	
            String nameLayer = layer.getName();
            
            if (layer.existsLayerInGeoserver(service, geoserverRestProxy)) {

            	if (layer instanceof DbLayer) {
            		String style =  layer.getDefaultStyle().getName();
            		layerGroupEncoder.addLayer(service.getWorkspaceName() + ":" + nameLayer, style,
                            service.getWorkspaceName());
            		
            		logger.info("Layer " + nameLayer + " in " + getName() + " created");
            	} else if (layer instanceof GroupLayer) {
            		layerGroupEncoder.addLayerGroup(service.getWorkspaceName() + ":" + nameLayer);
            		
            		logger.info("GroupLayer " + nameLayer + " in " + getName() + " created");
            	}
            } else {
            	logger.info("(Group-)Layer " + nameLayer + " not exists; will not be created in " + getName());
            }
        }
        
        boolean createdOrUpdated = false;

        if (geoserverRestProxy.getReader().existsLayerGroup(service.getWorkspaceName(), getName())) {
        	createdOrUpdated = geoserverRestProxy.getPublisher().configureLayerGroup(service.getWorkspaceName(), getName(), layerGroupEncoder);
        } else {
        	createdOrUpdated = geoserverRestProxy.getPublisher().createLayerGroup(service.getWorkspaceName(),  getName(), layerGroupEncoder);
        }

        if(!createdOrUpdated) {
        	throw new RuntimeException("Error creating layergroup " +  getName());
        }
	}
	
	@Override
	public boolean existsLayerInGeoserver(Service service, GeoserverRestProxy geoserverRestProxy) {
		return geoserverRestProxy.getReader().existsLayerGroup(service.getWorkspaceName(), getName());
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy