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

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

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


import it.geosolutions.geoserver.rest.decoder.RESTFeatureTypeList;
import it.geosolutions.geoserver.rest.decoder.utils.NameLinkElem;
import it.geosolutions.geoserver.rest.encoder.GSLayerEncoder;
import it.geosolutions.geoserver.rest.encoder.GSResourceEncoder;
import it.geosolutions.geoserver.rest.encoder.authorityurl.GSAuthorityURLInfoEncoder;
import it.geosolutions.geoserver.rest.encoder.identifier.GSIdentifierInfoEncoder;

import java.io.File;
import java.util.List;

import nl.pdok.catalogus.Catalogus;
import nl.pdok.workspacebuilder.GeoserverRestProxy;
import nl.pdok.workspacebuilder.model.Service;
import nl.pdok.workspacebuilder.model.Style;
import nl.pdok.workspacebuilder.model.datastore.Datastore;
import org.apache.commons.lang.StringUtils;

public abstract class SingleLayer extends Layer {
	
	public SingleLayer (Type typeArg, Catalogus catalogus){
        super(typeArg, catalogus);
    }

    protected abstract boolean publish(String workspaceName, GSResourceEncoder resourceEncoder, GSLayerEncoder layerEncoder, GeoserverRestProxy geoserverRestProxy);
    protected abstract GSResourceEncoder createResourceEncoder();
    
    @Override
    public void addLayerToGeoserver(Service service, GeoserverRestProxy geoserverRestProxy) {
    	if (datastoreExistsForLayer(service.getDatastores())) {
	    	addStyleToGeoserver(getDefaultStyle(), service.getWorkspaceName(), geoserverRestProxy);
    	    addStylesToGeoserver(getSelectableStyles(), service.getWorkspaceName(), geoserverRestProxy);
		
		    GSResourceEncoder resourceEncoder = createResourceEncoder();
		    for (String keyword : getKeywords()){
		        resourceEncoder.addKeyword(keyword);
		    }
		
	        GSLayerEncoder layerEncoder = new GSLayerEncoder();
	        layerEncoder.setEnabled(true);
	        layerEncoder.setQueryable(true);
	        addDefaultStyleToLayer(layerEncoder);
	        addSelectableStylesToLayer(layerEncoder);
		        
	        String wmsPath = getWmspath();
	        if (wmsPath != null) {
	        	layerEncoder.setWmsPath(wmsPath);
	        }
	
	        // Authority Info
	        addAuthority(service, layerEncoder);
            
            boolean created = publish(service.getWorkspaceName(), resourceEncoder, layerEncoder, geoserverRestProxy);

            if(!created) {
            	throw new RuntimeException("Error creating layer " +  this.getName());
            }
        }
	}
    
	private void addSelectableStylesToLayer( GSLayerEncoder layerEncoder) {
		for ( Style style : getSelectableStyles()){
		    if ( null == style.getWorkspace()){
		        layerEncoder.addStyle(style.getName());
		    } else {
		        layerEncoder.addStyle(style.getName(), style.getWorkspace());
		    }
		}
	}

	private void addDefaultStyleToLayer(GSLayerEncoder layerEncoder) {
		if (defaultStyle != null ){
		    if ( null == defaultStyle.getWorkspace()) {
		        layerEncoder.setDefaultStyle(defaultStyle.getName());
		    } else {
		        layerEncoder.setDefaultStyle(defaultStyle.getWorkspace(), defaultStyle.getName());
		    }
		}
	}

    private void addStylesToGeoserver(List