christian@4654: package de.intevation.flys.utils; christian@4654: christian@4654: import de.intevation.artifacts.CallContext; christian@4654: import de.intevation.flys.artifacts.FLYSArtifact; christian@4654: import de.intevation.flys.artifacts.model.LayerInfo; christian@4654: import de.intevation.flys.artifacts.model.map.WMSDBLayerFacet; christian@4654: import de.intevation.flys.artifacts.model.map.WMSLayerFacet; christian@4654: import de.intevation.flys.artifacts.model.map.WSPLGENLayerFacet; christian@4654: import de.intevation.flys.artifacts.resources.Resources; christian@4654: christian@4654: import java.io.File; christian@4654: import java.io.FileNotFoundException; christian@4654: import java.io.IOException; christian@4654: import java.util.List; christian@4654: christian@4654: import org.apache.log4j.Logger; christian@4654: import org.apache.velocity.Template; christian@4654: import org.geotools.data.shapefile.ShpFiles; christian@4654: import org.geotools.data.shapefile.shp.ShapefileHeader; christian@4654: import org.geotools.data.shapefile.shp.ShapefileReader; christian@4654: christian@4654: public class ArtefactMapfileGenerator extends MapfileGenerator { christian@4654: christian@4654: private static Logger logger = Logger.getLogger(ArtefactMapfileGenerator.class); christian@4654: christian@4654: /** christian@4654: * Method which starts searching for meta information file and mapfile christian@4654: * generation. christian@4654: */ christian@4654: @Override christian@4654: public void generate() throws IOException christian@4654: { christian@4654: File[] userDirs = getUserDirs(); christian@4654: List layers = parseLayers(userDirs); christian@4654: logger.info("Found " + layers.size() + " layers for user mapfile."); christian@4654: christian@4654: writeMapfile(layers); christian@4654: } christian@4654: christian@4654: /** christian@4654: * Creates a layer file used for Mapserver's mapfile which represents the christian@4654: * floodmap. christian@4654: * christian@4654: * @param flys The FLYSArtifact that owns wms. christian@4654: * @param wms The WMSLayerFacet that contains information for the layer. christian@4654: */ christian@4654: public void createUeskLayer( christian@4654: FLYSArtifact flys, christian@4654: WSPLGENLayerFacet wms, christian@4654: String style, christian@4654: CallContext context christian@4654: ) throws FileNotFoundException, IOException christian@4654: { christian@4654: logger.debug("createUeskLayer"); christian@4654: christian@4654: LayerInfo layerinfo = new LayerInfo(); christian@4654: layerinfo.setName(MS_WSPLGEN_PREFIX + flys.identifier()); christian@4654: layerinfo.setType("POLYGON"); christian@4654: layerinfo.setDirectory(flys.identifier()); christian@4654: layerinfo.setData(WSPLGEN_RESULT_SHAPE); christian@4654: layerinfo.setTitle(Resources.getMsg(Resources.getLocale(context.getMeta()), christian@4654: "floodmap.uesk", christian@4654: "Floodmap")); christian@4654: layerinfo.setStyle(style); christian@4654: layerinfo.setSrid(wms.getSrid()); christian@4654: christian@4654: String name = MS_LAYER_PREFIX + wms.getName(); christian@4654: christian@4654: Template template = getTemplateByName(WSPLGEN_LAYER_TEMPLATE); christian@4654: if (template == null) { christian@4654: logger.warn("Template '" + WSPLGEN_LAYER_TEMPLATE + "' found."); christian@4654: return; christian@4654: } christian@4654: christian@4654: try { christian@4654: File dir = new File(getShapefileBaseDir(), flys.identifier()); christian@4654: writeLayer(layerinfo, dir, name, template); christian@4654: } christian@4654: catch (FileNotFoundException fnfe) { christian@4654: logger.error(fnfe, fnfe); christian@4654: logger.warn("Unable to write layer: " + name); christian@4654: } christian@4654: } christian@4654: christian@4654: christian@4654: /** christian@4654: * Creates a layer file used for Mapserver's mapfile which represents the christian@4654: * user defined barriers. christian@4654: * christian@4654: * @param flys The FLYSArtifact that owns wms. christian@4654: * @param wms The WMSLayerFacet that contains information for the layer. christian@4654: */ christian@4654: public void createBarriersLayer(FLYSArtifact flys, WMSLayerFacet wms) christian@4654: throws FileNotFoundException, IOException christian@4654: { christian@4654: logger.debug("createBarriersLayer"); christian@4654: christian@4654: //String uuid = flys.identifier(); christian@4654: //File dir = new File(getShapefileBaseDir(), uuid); christian@4654: christian@4654: createBarriersLineLayer(flys, wms); christian@4654: createBarriersPolygonLayer(flys, wms); christian@4654: } christian@4654: christian@4654: christian@4654: protected void createBarriersLineLayer( christian@4654: FLYSArtifact flys, christian@4654: WMSLayerFacet wms christian@4654: ) christian@4654: throws FileNotFoundException, IOException christian@4654: { christian@4654: String uuid = flys.identifier(); christian@4654: String group = MS_BARRIERS_PREFIX + uuid; christian@4654: String groupTitle = "I18N_BARRIERS_TITLE"; christian@4654: christian@4654: File dir = new File(getShapefileBaseDir(), uuid); christian@4654: File test = new File(dir, WSPLGEN_LINES_SHAPE); christian@4654: christian@4654: if (!test.exists() || !test.canRead()) { christian@4654: logger.debug("No barrier line layer existing."); christian@4654: return; christian@4654: } christian@4654: christian@4654: LayerInfo lineInfo = new LayerInfo(); christian@4654: lineInfo.setName(MS_LINE_PREFIX + uuid); christian@4654: lineInfo.setType("LINE"); christian@4654: lineInfo.setDirectory(uuid); christian@4654: lineInfo.setData(WSPLGEN_LINES_SHAPE); christian@4654: lineInfo.setTitle("I18N_LINE_SHAPE"); christian@4654: lineInfo.setGroup(group); christian@4654: lineInfo.setGroupTitle(groupTitle); christian@4654: lineInfo.setSrid(wms.getSrid()); christian@4654: christian@4654: String nameLines = MS_LAYER_PREFIX + wms.getName() + "-lines"; christian@4654: christian@4654: Template tpl = getTemplateByName(SHP_LAYER_TEMPLATE); christian@4654: if (tpl == null) { christian@4654: logger.warn("Template '" + SHP_LAYER_TEMPLATE + "' found."); christian@4654: return; christian@4654: } christian@4654: christian@4654: try { christian@4654: writeLayer(lineInfo, dir, nameLines, tpl); christian@4654: } christian@4654: catch (FileNotFoundException fnfe) { christian@4654: logger.error(fnfe, fnfe); christian@4654: logger.warn("Unable to write layer: " + nameLines); christian@4654: } christian@4654: } christian@4654: christian@4654: protected void createBarriersPolygonLayer( christian@4654: FLYSArtifact flys, christian@4654: WMSLayerFacet wms christian@4654: ) christian@4654: throws FileNotFoundException, IOException christian@4654: { christian@4654: String uuid = flys.identifier(); christian@4654: String group = uuid + MS_BARRIERS_PREFIX; christian@4654: String groupTitle = "I18N_BARRIERS_TITLE"; christian@4654: christian@4654: File dir = new File(getShapefileBaseDir(), uuid); christian@4654: File test = new File(dir, WSPLGEN_POLYGONS_SHAPE); christian@4654: christian@4654: if (!test.exists() || !test.canRead()) { christian@4654: logger.debug("No barrier line layer existing."); christian@4654: return; christian@4654: } christian@4654: christian@4654: LayerInfo polygonInfo = new LayerInfo(); christian@4654: polygonInfo.setName(MS_POLYGONS_PREFIX + uuid); christian@4654: polygonInfo.setType("POLYGON"); christian@4654: polygonInfo.setDirectory(uuid); christian@4654: polygonInfo.setData(WSPLGEN_POLYGONS_SHAPE); christian@4654: polygonInfo.setTitle("I18N_POLYGON_SHAPE"); christian@4654: polygonInfo.setGroup(group); christian@4654: polygonInfo.setGroupTitle(groupTitle); christian@4654: polygonInfo.setSrid(wms.getSrid()); christian@4654: christian@4654: String namePolygons = MS_LAYER_PREFIX + wms.getName() + "-polygons"; christian@4654: christian@4654: Template tpl = getTemplateByName(SHP_LAYER_TEMPLATE); christian@4654: if (tpl == null) { christian@4654: logger.warn("Template '" + SHP_LAYER_TEMPLATE + "' found."); christian@4654: return; christian@4654: } christian@4654: christian@4654: try { christian@4654: writeLayer(polygonInfo, dir, namePolygons, tpl); christian@4654: } christian@4654: catch (FileNotFoundException fnfe) { christian@4654: logger.error(fnfe, fnfe); christian@4654: logger.warn("Unable to write layer: " + namePolygons); christian@4654: } christian@4654: } christian@4654: christian@4654: christian@4654: /** christian@4654: * Creates a layer file used for Mapserver's mapfile which represents the christian@4654: * shape files uploaded by the user. christian@4654: * christian@4654: * @param flys The FLYSArtifact that owns wms. christian@4654: * @param wms The WMSLayerFacet that contains information for the layer. christian@4654: */ christian@4654: public void createUserShapeLayer(FLYSArtifact flys, WMSLayerFacet wms) christian@4654: throws FileNotFoundException, IOException christian@4654: { christian@4654: logger.debug("createUserShapeLayer"); christian@4654: christian@4654: String uuid = flys.identifier(); christian@4654: File dir = new File(getShapefileBaseDir(), uuid); christian@4654: File test = new File(dir, WSPLGEN_USER_SHAPE); christian@4654: christian@4654: if (!test.exists() || !test.canRead()) { christian@4654: logger.debug("No user layer existing."); christian@4654: return; christian@4654: } christian@4654: christian@4654: File userShape = new File(dir, WSPLGEN_USER_SHAPE); christian@4654: ShpFiles sf = new ShpFiles(userShape); christian@4654: ShapefileReader sfr = new ShapefileReader(sf, true, false, null); christian@4654: ShapefileHeader sfh = sfr.getHeader(); christian@4654: christian@4654: String group = uuid + MS_USERSHAPE_PREFIX; christian@4654: String groupTitle = "I18N_USER_SHAPE_TITLE"; christian@4654: christian@4654: LayerInfo info = new LayerInfo(); christian@4654: info.setName(MS_USERSHAPE_PREFIX + uuid); christian@4654: if (sfh.getShapeType().isLineType()) { christian@4654: info.setType("LINE"); christian@4654: } christian@4654: else if (sfh.getShapeType().isPolygonType()) { christian@4654: info.setType("POLYGON"); christian@4654: } christian@4654: else { christian@4654: return; christian@4654: } christian@4654: info.setDirectory(uuid); christian@4654: info.setData(WSPLGEN_USER_SHAPE); christian@4654: info.setTitle("I18N_USER_SHAPE"); christian@4654: info.setGroup(group); christian@4654: info.setGroupTitle(groupTitle); christian@4654: info.setSrid(wms.getSrid()); christian@4654: christian@4654: String nameUser = MS_LAYER_PREFIX + wms.getName(); christian@4654: christian@4654: Template tpl = getTemplateByName(SHP_LAYER_TEMPLATE); christian@4654: if (tpl == null) { christian@4654: logger.warn("Template '" + SHP_LAYER_TEMPLATE + "' found."); christian@4654: return; christian@4654: } christian@4654: christian@4654: try { christian@4654: writeLayer(info, dir, nameUser, tpl); christian@4654: } christian@4654: catch (FileNotFoundException fnfe) { christian@4654: logger.error(fnfe, fnfe); christian@4654: logger.warn("Unable to write layer: " + nameUser); christian@4654: } christian@4654: christian@4654: } christian@4654: christian@4654: christian@4654: /** christian@4654: * Creates a layer file used for Mapserver's mapfile which represents christian@4654: * geometries from database. christian@4654: * christian@4654: * @param flys The FLYSArtifact that owns wms. christian@4654: * @param wms The WMSLayerFacet that contains information for the layer. christian@4654: */ christian@4654: public void createDatabaseLayer( christian@4654: FLYSArtifact flys, christian@4654: WMSDBLayerFacet wms, christian@4654: String style christian@4654: ) christian@4654: throws FileNotFoundException, IOException christian@4654: { christian@4654: logger.debug("createDatabaseLayer"); christian@4654: christian@4654: LayerInfo layerinfo = new LayerInfo(); christian@4654: layerinfo.setName(wms.getName() + "-" + flys.identifier()); christian@4654: layerinfo.setType(wms.getGeometryType()); christian@4654: layerinfo.setFilter(wms.getFilter()); christian@4654: layerinfo.setData(wms.getData()); christian@4654: layerinfo.setTitle(wms.getDescription()); christian@4654: layerinfo.setStyle(style); christian@4654: if(wms.getExtent() != null) { christian@4654: layerinfo.setExtent(GeometryUtils.jtsBoundsToOLBounds(wms.getExtent())); christian@4654: } christian@4654: layerinfo.setConnection(wms.getConnection()); christian@4654: layerinfo.setConnectionType(wms.getConnectionType()); christian@4654: layerinfo.setLabelItem(wms.getLabelItem()); christian@4654: layerinfo.setSrid(wms.getSrid()); christian@4654: christian@4654: String name = MS_LAYER_PREFIX + wms.getName(); christian@4654: christian@4654: Template template = getTemplateByName(DB_LAYER_TEMPLATE); christian@4654: if (template == null) { christian@4654: logger.warn("Template '" + DB_LAYER_TEMPLATE + "' found."); christian@4654: return; christian@4654: } christian@4654: christian@4654: try { christian@4654: File dir = new File(getShapefileBaseDir(), flys.identifier()); christian@4654: writeLayer(layerinfo, dir, name, template); christian@4654: } christian@4654: catch (FileNotFoundException fnfe) { christian@4654: logger.error(fnfe, fnfe); christian@4654: logger.warn("Unable to write layer: " + name); christian@4654: } christian@4654: } christian@4654: christian@4654: }