PageRenderTime 35ms CodeModel.GetById 20ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 1ms

/layers-service/src/main/java/org/ala/layers/web/LayersService.java

http://alageospatialportal.googlecode.com/
Java | 197 lines | 127 code | 16 blank | 54 comment | 6 complexity | 32978c5aaf8f8476f343b68f461c94d4 MD5 | raw file
  1/**************************************************************************
  2 *  Copyright (C) 2010 Atlas of Living Australia
  3 *  All Rights Reserved.
  4 *
  5 *  The contents of this file are subject to the Mozilla Public
  6 *  License Version 1.1 (the "License"); you may not use this file
  7 *  except in compliance with the License. You may obtain a copy of
  8 *  the License at http://www.mozilla.org/MPL/
  9 *
 10 *  Software distributed under the License is distributed on an "AS
 11 *  IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
 12 *  implied. See the License for the specific language governing
 13 *  rights and limitations under the License.
 14 ***************************************************************************/
 15
 16package org.ala.layers.web;
 17
 18import java.io.BufferedWriter;
 19import java.io.OutputStreamWriter;
 20import java.net.URI;
 21import java.util.List;
 22import javax.annotation.Resource;
 23import javax.servlet.http.HttpServletRequest;
 24import javax.servlet.http.HttpServletResponse;
 25
 26import org.ala.layers.dao.LayerDAO;
 27import org.ala.layers.dto.Layer;
 28import org.apache.commons.lang.StringEscapeUtils;
 29import org.apache.commons.lang.StringUtils;
 30import org.apache.log4j.Logger;
 31import org.springframework.stereotype.Controller;
 32import org.springframework.web.bind.annotation.PathVariable;
 33import org.springframework.web.bind.annotation.RequestMapping;
 34import org.springframework.web.bind.annotation.RequestMethod;
 35import org.springframework.web.bind.annotation.RequestParam;
 36import org.springframework.web.bind.annotation.ResponseBody;
 37import org.springframework.web.servlet.ModelAndView;
 38
 39/**
 40 * 
 41 * @author jac24n
 42 */
 43@Controller
 44public class LayersService {
 45    /**
 46     * Log4j instance
 47     */
 48    protected Logger logger = Logger.getLogger(this.getClass());
 49
 50    @Resource(name = "layerDao")
 51    private LayerDAO layerDao;
 52
 53    /**
 54     * This method returns all layers
 55     * 
 56     * @param req
 57     * @return
 58     */
 59    @RequestMapping(value = "/layers", method = RequestMethod.GET)
 60    public @ResponseBody
 61    List<Layer> layerObjects(HttpServletRequest req) {
 62        logger.info("Retriving enabled layers");
 63        return layerDao.getLayers();
 64    }
 65
 66    /**
 67     * This method returns a single layer, provided an id
 68     * 
 69     * @param req
 70     * @return
 71     */
 72    @RequestMapping(value = "/layer/{id}", method = RequestMethod.GET)
 73    public @ResponseBody
 74    Layer layerObject(@PathVariable("id") String id, HttpServletRequest req) {
 75        Layer l = null;
 76        try {
 77            l = layerDao.getLayerById(Integer.parseInt(id));
 78        } catch (Exception e) {
 79        }
 80
 81        if (l == null) {
 82            l = layerDao.getLayerByName(id);
 83        }
 84        return l;
 85    }
 86
 87    /**
 88     * This method returns all layers
 89     * 
 90     * @param req
 91     * @return
 92     */
 93    @RequestMapping(value = "/layers/search", method = RequestMethod.GET)
 94    public @ResponseBody
 95    List<Layer> layerObjects(@RequestParam("q") String q, HttpServletRequest req) {
 96        logger.info("search enabled layers for " + q);
 97        return layerDao.getLayersByCriteria(q);
 98    }
 99
100    @RequestMapping(value = "/layers/grids", method = RequestMethod.GET)
101    public @ResponseBody
102    List<Layer> gridsLayerObjects(HttpServletRequest req) {
103        // String query =
104        // "SELECT * FROM layers WHERE enabled='TRUE' and type='Environmental';";
105        // ResultSet r = DBConnection.query(query);
106        // return Utils.resultSetToJSON(r);
107        return layerDao.getLayersByEnvironment();
108    }
109
110    @RequestMapping(value = "/layers/shapes", method = RequestMethod.GET)
111    public @ResponseBody
112    List<Layer> shapesLayerObjects(HttpServletRequest req) {
113        // String query =
114        // "SELECT * FROM layers WHERE enabled='TRUE' and type='Contextual';";
115        // ResultSet r = DBConnection.query(query);
116        // return Utils.resultSetToJSON(r);
117        return layerDao.getLayersByContextual();
118
119    }
120
121    /**
122     * Return layers list if RIF-CS XML format
123     * @param req
124     * @param res
125     * @throws Exception
126     */
127    @RequestMapping(value = "/layers/rif-cs", method = RequestMethod.GET)
128    public void layersRifCs(HttpServletRequest req, HttpServletResponse res) throws Exception {
129        res.setContentType("text/xml");
130
131        // Build XML by hand here because JSP processing seems to omit CDATA sections from the output
132        StringBuilder sb = new StringBuilder();
133        sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>");
134        sb.append("<registryObjects xmlns=\"http://ands.org.au/standards/rif-cs/registryObjects\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://ands.org.au/standards/rif-cs/registryObjects http://services.ands.org.au/documentation/rifcs/schema/registryObjects.xsd\">");
135        for (Layer layer: layerDao.getLayers()) {
136            sb.append("<registryObject group=\"Atlas of Living Australia\">");
137            sb.append("<key>ala.org.au/uid_" + layer.getUid() + "</key>");
138            sb.append("<originatingSource><![CDATA[" + layer.getMetadatapath() + "]]></originatingSource>");
139            sb.append("<collection type=\"dataset\">");
140            sb.append("<identifier type=\"local\">ala.org.au/uid_" + layer.getUid() + "</identifier>");
141            sb.append("<name type=\"abbreviated\">");
142            sb.append("<namePart>" + layer.getName() + "</namePart>");
143            sb.append("</name>");
144            sb.append("<name type=\"alternative\">");
145            sb.append("<namePart><![CDATA[" + layer.getDescription() + "]]></namePart>");
146            sb.append("</name>");
147            sb.append("<name type=\"primary\">");
148            sb.append("<namePart><![CDATA[" + layer.getDescription() + "]]></namePart>");
149            sb.append("</name>");
150            sb.append("<location>");
151            sb.append("<address>");
152            sb.append("<electronic type=\"url\">");
153            sb.append("<value>http://spatial.ala.org.au/layers</value>");
154            sb.append("</electronic>");
155            sb.append("</address>");
156            sb.append("</location>");
157            sb.append("<relatedObject>");
158            sb.append("<key>Contributor:Atlas of Living Australia</key>");
159            sb.append("<relation type=\"hasCollector\" />");
160            sb.append("</relatedObject>");
161            sb.append("<subject type=\"anzsrc-for\">0502</subject>");
162            sb.append("<subject type=\"local\">" + layer.getClassification1() + "</subject>");
163            if (!StringUtils.isEmpty(layer.getClassification2())) {
164                sb.append("<subject type=\"local\">" + layer.getClassification2() + "</subject>");
165            }
166            sb.append("<description type=\"full\"><![CDATA[" + layer.getNotes() + "]]></description>");
167            sb.append("<relatedInfo type=\"website\">");
168            sb.append("<identifier type=\"uri\"><![CDATA[" + layer.getMetadatapath() + "]]></identifier>");
169            sb.append("<title>Further metadata</title>");
170            sb.append("</relatedInfo>");
171            sb.append("<relatedInfo type=\"website\">");
172            sb.append("<identifier type=\"uri\"><![CDATA[" + layer.getSource_link() + "]]></identifier>");
173            sb.append("<title>Original source of this data</title>");
174            sb.append("</relatedInfo>");
175            sb.append("<rights>");
176            sb.append("<licence ");
177            if (!StringUtils.isEmpty(layer.getLicence_link())) {
178                sb.append("rightsUri=\"" + StringEscapeUtils.escapeXml(layer.getLicence_link()) + "\">");
179            } else {
180                sb.append(">");
181            }
182            sb.append("<![CDATA[" + layer.getLicence_notes() + "]]></licence>");
183            sb.append("</rights>");
184            sb.append("<coverage>");
185            sb.append("<spatial type=\"iso19139dcmiBox\">northlimit=" + layer.getMaxlatitude() + "; southlimit=" + layer.getMinlatitude() + "; westlimit=" + layer.getMinlongitude() + "; eastLimit=" + layer.getMaxlongitude()+ "; projection=WGS84</spatial>");
186            sb.append("</coverage>");
187            sb.append("</collection>");
188            sb.append("</registryObject>");
189        }
190        sb.append("</registryObjects>");
191        
192        res.getWriter().append(sb.toString());
193        res.getWriter().flush();
194        res.getWriter().close();
195    }
196
197}