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

nl.pdok.workspacebuilder.PdokBuilder Maven / Gradle / Ivy

The newest version!
package nl.pdok.workspacebuilder;

import it.geosolutions.geoserver.rest.decoder.RESTCoverageList;
import it.geosolutions.geoserver.rest.decoder.RESTFeatureTypeList;
import it.geosolutions.geoserver.rest.decoder.RESTWorkspaceList;
import it.geosolutions.geoserver.rest.encoder.service.GSServiceSettingsEncoder;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

import nl.pdok.catalogus.Catalogus;
import nl.pdok.workspacebuilder.exceptions.WorkspaceBuilderException;
import nl.pdok.workspacebuilder.model.Service;
import nl.pdok.workspacebuilder.model.ServiceInfo;
import nl.pdok.workspacebuilder.model.datastore.Datastore;
import nl.pdok.workspacebuilder.model.layer.Coverage;
import nl.pdok.workspacebuilder.model.layer.GroupLayer;
import nl.pdok.workspacebuilder.model.layer.Layer;
import nl.pdok.workspacebuilder.model.layer.SingleLayer;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/**
 * Created with IntelliJ IDEA.
 * User: nijhur
 * Date: 24-2-14
 * Time: 10:31
 *
 * Class to generate a geoserver configuration.
 * The configuration is generated via REST calls on a live geoserver
 */
public class PdokBuilder {

    final static Logger logger = LoggerFactory.getLogger(PdokBuilder.class);

    public static final String BASERASTERDIR = "//usr/data/rasters/";
    public static final String METADATATYPE = "TC211";
    public static final String METADATACODETYPE = "text/plain";
    public static final String METADATABASEURL = "http://www.nationaalgeoregister.nl/geonetwork/srv/dut/xml.metadata.get?uuid=";
    
    @SuppressWarnings("unused")
    private Catalogus catalogus;
    
    public PdokBuilder(Catalogus catalogus) {
    	this.catalogus = catalogus;
    }

    /**
     * Check if a workspace exists in geoserver with the given name
     * @param workspaceName
     * @return true is the workspace exists
     */
    public boolean workSpaceExist(Service service, GeoserverRestProxy geoserverRestProxy){
    	String workspaceName = service.getWorkspaceName();
        logger.info( workspaceName + ":Check workspace exists");
        for (RESTWorkspaceList.RESTShortWorkspace workspace:geoserverRestProxy.getReader().getWorkspaces()){
            if ( workspace.getName().equals(workspaceName)){
                return true;
            }
        }
        return false;
    }

    /**
     * Deletes a workspace and all it's content from the geoserver.
     * @param workspaceName
     */
    public void removeWorkspace(Service service, GeoserverRestProxy geoserverRestProxy){
    	String workspaceName = service.getWorkspaceName();
        if ( workSpaceExist(service, geoserverRestProxy)){
            logger.info( workspaceName + ":Remove workspace");
            boolean removed = geoserverRestProxy.getPublisher().removeWorkspace(workspaceName, true);
            if(!removed) {
            	throw new RuntimeException("Error removing workspace " + workspaceName);
            }
        }
    }

    /**
     * Creates an empty workspace
     * @param workspaceName
     */
    public void createWorkspace(Service service, GeoserverRestProxy geoserverRestProxy) throws WorkspaceBuilderException {
    	String workspaceName = service.getWorkspaceName();
    	logger.info( workspaceName + ":Create workspace");
        try {
            URI uri = new URI("http://" + workspaceName + ".geonovum.nl");
            boolean created = geoserverRestProxy.getPublisher().createWorkspace(workspaceName, uri );
            if(!created) {
            	throw new RuntimeException("Error creating workspace " + workspaceName);
            }
        } catch (URISyntaxException e) {
            logger.error("Error creating URI for workspace: " + e.getMessage());
            throw new WorkspaceBuilderException("Error creating URI for workspace.");
        }
    }
    
    public void createStores(Service service, GeoserverRestProxy geoserverRestProxy) {
        for (Datastore datastore:service.getDatastores()) {
        	datastore.createDatastoreIfNotExists(service.getWorkspaceName(), geoserverRestProxy);
        }
    }

    public void createLayers(Service service, GeoserverRestProxy geoserverRestProxy){
        logger.info( service.getWorkspaceName() + ":Create Layers for service " + service.getTitle());
        RESTFeatureTypeList availableFeatureTypes = geoserverRestProxy.getReader().getFeatureTypes(service.getWorkspaceName(), "available");
        for (SingleLayer layer:service.getLayers()) {
        	if (layer.featureExistsForLayer(service, availableFeatureTypes, geoserverRestProxy)) {
        		layer.addLayerToGeoserver(service, geoserverRestProxy);
            	logger.info("Adding layer " + layer.getName() + " --> succeeded." );
        	} else {
        		logger.info("Adding layer " + layer.getName() + " --> feature doesn't exist." );
        	}
        }
    }

    public void createGroupLayer(Service service, GeoserverRestProxy geoserverRestProxy){
        logger.info( service.getWorkspaceName() + ":Create GroupLayers");
        for (GroupLayer groupLayer : service.getGroupLayers()){
        	groupLayer.addLayerToGeoserver(service, geoserverRestProxy);
        }
    }
    
    public void createCoverages(Service service, GeoserverRestProxy geoserverRestProxy){
        logger.info( service.getWorkspaceName() + ":Create Coverages for service " + service.getTitle());
                       
        for (Coverage coverage:service.getCoverages()) {        	
        	RESTCoverageList availableCoverages = geoserverRestProxy.getReader().getCoverageNames(service.getWorkspaceName(), coverage.getDatastoreName(),  "all");
        	if (coverage.featureExistsForCoverage(service, availableCoverages, geoserverRestProxy)) {
        		coverage.addLayerToGeoserver(service, geoserverRestProxy);
            	logger.info("Adding coverage " + coverage.getName() + " --> succeeded." );
        	} else {
        		logger.info("Adding coverage " + coverage.getName() + " --> coverage doesn't exist." );
        	}
        }        
    }    


    public void createKaartConfig(Service service){
        logger.info("Creating kaartconfig: " + service.getWorkspaceName());

        File configFile = new File("generated_files/kaart_config/" + service.getWorkspaceName() + ".xml");

        try {
            FileWriter fw = new FileWriter(configFile.getAbsoluteFile());
            BufferedWriter bw = new BufferedWriter(fw);

            for ( ServiceInfo info : service.getServiceSettings()){
                if (ServiceInfo.ServiceType.WMS == info.getType()){
                    for ( Layer layer : service.getLayers()){
                        String id = service.getWorkspaceName() + "_" + layer.getName();
                        bw.write("  " + id.toUpperCase() + ": {\n");
                        bw.write("    " +"layertype: 'WMS',\n");
                        bw.write("    " +"name: '" + info.getTitle() + "',\n");
                        bw.write("    " +"url: 'http://geodata.nationaalgeoregister.nl/" + service.getWorkspaceName() + "/wms',\n");
                        bw.write("    " +"layers: '" + layer.getName() + "',\n");
                        bw.write("    " +"transparent: 'true',\n");
                        bw.write("    " +"format: 'image/png',\n");
                        bw.write("    " +"visibility: true,\n");
                        bw.write("    " +"isBaseLayer: false,\n");
                        bw.write("    " + "singleTile: true\n");
                        bw.write("  " + "},\n");
                    }
                }
            }
            bw.close();
        } catch (IOException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
    }

    public void createPdokViewerConfig(Service service, GeoserverRestProxy geoserverConfig){
        logger.info( service.getWorkspaceName() + ":Create viewer config");
        File configFile = new File("generated_files/pdokviewer_config/" + service.getWorkspaceName() + ".xml");

        try {
            FileWriter fw = new FileWriter(configFile.getAbsoluteFile());
            BufferedWriter bw = new BufferedWriter(fw);
            bw.write("\n");
            bw.write("\t\n");
            bw.write("\t\t" + service.getTitle() + "\n");

            if ( service.getGroupLayers() != null && service.getGroupLayers().size() > 0 ){
                for(GroupLayer grouplayer : service.getGroupLayers()){
                    bw.write("\t\n");
                    bw.write("\t\t" + grouplayer.getTitle() + "\n");
                    for(Layer layer : grouplayer.getLayers()){
                        PrintViewerLayer(layer, service.getWorkspaceName(), bw);
                    }
                    bw.write("\t\n");
                }
            } else {
                for(Layer layer : service.getLayers()){
                    PrintViewerLayer(layer, service.getWorkspaceName(), bw);
                }
            }


            bw.write("\t\n");
            bw.write("");
            bw.close();
        } catch (IOException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
    }

    public void createServiceSettings(Service service, GeoserverRestProxy geoserverConfig){
        for ( ServiceInfo info : service.getServiceSettings()){

            logger.info( service.getWorkspaceName() + ":Create " + info.getType().toString().toUpperCase() + " service");

            GSServiceSettingsEncoder encoder = new GSServiceSettingsEncoder(info.getType().toString().toLowerCase(), info.getMetadata_id());
            encoder.setTitle(info.getTitle());
            encoder.setWorkspace(service.getWorkspaceName());
            encoder.setAccessConstraints(info.getAccessConstraints());
            
            encoder.setAbstract(info.getAbstract());
            encoder.setFees(info.getFees());

            encoder.setSrs(info.getSrs());

            for ( String keyword : info.getKeywords()){
                encoder.addKeyword(keyword);
            }

            boolean published = geoserverConfig.getPublisher().publishServiceLayer(service.getWorkspaceName(), info.getType().toString().toLowerCase(), encoder);
            if(!published) {
            	throw new RuntimeException("Error publishing WcsServiceLayer of type '" +  info.getType().toString() + "' for " +  info.getTitle());
            }
        }

    }

    private void PrintViewerLayer(Layer layer, String service, BufferedWriter bw) throws IOException{
        bw.write("\t\t\n");
        bw.write("		    " + layer.getTitle() + " [wms]" + "\n");
        bw.write("		    {baseUrl}" + service + "/wms?\n");
        bw.write("		    false\n");
        bw.write("		    1.0\n");
        bw.write("		    true\n");
        bw.write("		    false\n");
        //bw.write("		    53.76\n");
        bw.write("		    " + layer.getName() + "\n");
        bw.write("		    image/png\n");
        bw.write("		    application/vnd.ogc.gml\n");
        bw.write("		    true\n");
        bw.write("		    true\n");
        bw.write("\t\t\n");
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy