PageRenderTime 23ms CodeModel.GetById 1ms app.highlight 18ms RepoModel.GetById 0ms app.codeStats 0ms

/gazetteer/src/main/java/org/ala/rest/FeatureList.java

http://alageospatialportal.googlecode.com/
Java | 157 lines | 123 code | 22 blank | 12 comment | 21 complexity | 6c69b5f2146baf7ee6f3ac0ea6ffb953 MD5 | raw file
  1/*
  2 * To change this template, choose Tools | Templates
  3 * and open the template in the editor.
  4 */
  5package org.ala.rest;
  6
  7import java.io.IOException;
  8import java.util.ArrayList;
  9import java.util.HashMap;
 10import java.util.List;
 11import java.util.Map;
 12import java.util.logging.Logger;
 13import javax.servlet.ServletContext;
 14
 15import org.geoserver.catalog.Catalog;
 16import org.geoserver.catalog.LayerInfo;
 17import org.geoserver.config.GeoServer;
 18import org.geoserver.platform.GeoServerExtensions;
 19import org.geotools.data.DataStore;
 20import org.geotools.data.FeatureSource;
 21import org.geotools.data.Query;
 22import org.geotools.feature.FeatureIterator;
 23import org.geotools.util.logging.Logging;
 24import org.opengis.feature.Feature;
 25import org.vfny.geoserver.util.DataStoreUtils;
 26
 27/**
 28 * Represents a list of features (as hyperlinks) for a given layer
 29 * @author angus
 30 */
 31public class FeatureList {
 32
 33    private static final Logger logger = Logging.getLogger("org.ala.rest.FeatureList");
 34    List<String> links = new ArrayList();
 35    private static int PAGE_SIZE = 100;
 36    private int total_features;
 37    private int page;
 38    private String layerName;
 39    private String layerAlias;
 40
 41    private GazetteerConfig gc = new GazetteerConfig();
 42    private String format = "";
 43
 44    public FeatureList(String layerName) throws IOException, Exception {
 45        this(layerName, 1);
 46    }
 47
 48    public FeatureList(String layerName, int page) throws IOException, Exception {
 49        logger.finer("param: layerName: " + layerName);
 50        this.page = page;
 51        this.layerName = layerName;
 52
 53
 54        GeoServer gs = GeoServerExtensions.bean(GeoServer.class);
 55        ServletContext sc = GeoServerExtensions.bean(ServletContext.class);
 56        Catalog catalog = gs.getCatalog();
 57
 58        //check to see if layer exists, if not check to see if alias exists ...
 59        if (!gc.layerNameExists(layerName)) {
 60            logger.finer("layer " + layerName + " does not exist - trying aliases.");
 61            layerName = gc.getNameFromAlias(layerName);
 62            if (layerName.compareTo("") == 0) {
 63                logger.finer("no aliases found for layer, giving up");
 64                return;
 65            }
 66        }
 67        if (gc.getLayerAlias(layerName).compareTo("") != 0){
 68            layerAlias = gc.getLayerAlias(layerName);
 69        }
 70        else{
 71            layerAlias = layerName;
 72        }
 73        LayerInfo layerInfo = catalog.getLayerByName(layerName);
 74        Map params = layerInfo.getResource().getStore().getConnectionParameters();
 75
 76        DataStore dataStore = DataStoreUtils.acquireDataStore(params, sc);
 77
 78        try {
 79            if (dataStore == null) {
 80                throw new Exception("Could not find datastore for this layer");
 81            } else {
 82                FeatureSource layer = dataStore.getFeatureSource(layerName);
 83                FeatureIterator features = layer.getFeatures().features(); //FIXME: this could be a lot faster if we didn't request the_geom?.
 84                //Feature[] features = (Feature[])layer.getFeatures().t;
 85
 86                String idAttribute1Name = gc.getIdAttribute1Name(layerName);
 87                String idAttribute2Name = gc.getIdAttribute2Name(layerName);
 88                total_features = layer.getCount(Query.ALL);
 89                if (features.hasNext()) {
 90                    int featureNumber = 0;
 91                    logger.finer("PAGE: " + page);
 92                    while (features.hasNext()) {
 93                        Feature feature = features.next(); //TODO: Really dumb way to do paging - but FeatureSource doesn't seem to allow anything else
 94                        if (featureNumber == PAGE_SIZE * (page)) {
 95                            System.out.println("Finished page");
 96                            break;
 97                        }
 98                        if (featureNumber >= PAGE_SIZE * (page - 1)) {
 99                            String link = gc.getBaseURL() + "/" + layerAlias + "/" + feature.getProperty(idAttribute1Name).getValue().toString();
100                            if (idAttribute2Name.compareTo("") != 0) {
101                                link += "/" + feature.getProperty(idAttribute2Name).getValue().toString();
102                            }
103                            link += ".json";
104                            links.add(link.replace(" ", "_"));
105                        }
106                        featureNumber++;
107                    }
108                } else {
109                    throw new Exception("Could not find feature");
110                }
111
112                features.close();
113            }
114        } finally {
115
116            dataStore.dispose();
117
118        }
119    }
120
121    public void setFormat(String format) {
122        this.format = format;
123    }
124
125    public Map getMap() {
126        Map map = new HashMap();
127        map.put("features", this.links);
128        if (total_features > page * PAGE_SIZE) {
129            int nextPage = page + 1;
130            String next = String.valueOf(nextPage);
131            if (!format.isEmpty()) {
132                next = String.valueOf(nextPage) + "." + format;
133            }
134
135            map.put("next", gc.getBaseURL() + "/" + layerAlias + "/features/" + next);
136
137        }
138        //Previous button (if applicable)
139        if (page > 1) {
140            int prevPage = page-1;
141            String prev = String.valueOf(prevPage);
142            if (!format.isEmpty()) {
143                prev = String.valueOf(prev) + "." + format;
144            }
145            map.put("prev", gc.getBaseURL() + "/" + layerAlias + "/features/" + prev);
146        }
147
148        //List total number of pages
149        int pages = total_features / PAGE_SIZE;
150        if (total_features % PAGE_SIZE > 0) {
151            pages++;
152        }
153        map.put("total_pages", pages);
154
155        return map;
156    }
157}