1 /* 2 * Copyright (c) 2002-2012, Mairie de Paris 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 9 * 1. Redistributions of source code must retain the above copyright notice 10 * and the following disclaimer. 11 * 12 * 2. Redistributions in binary form must reproduce the above copyright notice 13 * and the following disclaimer in the documentation and/or other materials 14 * provided with the distribution. 15 * 16 * 3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its 17 * contributors may be used to endorse or promote products derived from 18 * this software without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE 24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 30 * POSSIBILITY OF SUCH DAMAGE. 31 * 32 * License 1.0 33 */ 34 package fr.paris.lutece.plugins.dataviz.web.rs; 35 36 import fr.paris.lutece.plugins.dataviz.business.Stat; 37 import fr.paris.lutece.plugins.dataviz.business.StatHome; 38 import fr.paris.lutece.plugins.rest.service.RestConstants; 39 import fr.paris.lutece.plugins.rest.util.json.JSONUtil; 40 import fr.paris.lutece.plugins.rest.util.xml.XMLUtil; 41 import fr.paris.lutece.util.xml.XmlUtil; 42 import fr.paris.lutece.portal.service.util.AppLogService; 43 import java.io.IOException; 44 45 import net.sf.json.JSONObject; 46 47 import java.util.Collection; 48 import javax.ws.rs.DELETE; 49 import javax.ws.rs.FormParam; 50 import javax.ws.rs.GET; 51 import javax.ws.rs.HeaderParam; 52 import javax.ws.rs.POST; 53 import javax.ws.rs.Path; 54 import javax.ws.rs.PathParam; 55 import javax.ws.rs.QueryParam; 56 import javax.ws.rs.core.HttpHeaders; 57 import javax.ws.rs.core.MediaType; 58 import javax.ws.rs.core.Response; 59 60 /** 61 * Page resource 62 */ 63 64 @Path( RestConstants.BASE_PATH + Constants.PLUGIN_PATH + Constants.STAT_PATH ) 65 public class StatRest 66 { 67 // private static final String KEY_STATS = "stats"; 68 // private static final String KEY_STAT = "stat"; 69 // 70 // private static final String KEY_ID="id"; 71 // private static final String KEY_DESCRIPTION="description"; 72 // private static final String KEY_REQUETE_SQL="requete_sql"; 73 // 74 // @GET 75 // @Path( Constants.ALL_PATH ) 76 // public Response getStats( @HeaderParam(HttpHeaders.ACCEPT) String accept , @QueryParam( Constants.FORMAT_QUERY ) String format ) throws IOException 77 // { 78 // String entity; 79 // String mediaType; 80 // 81 // if ( (accept != null && accept.contains(MediaType.APPLICATION_JSON)) || (format != null && format.equals(Constants.MEDIA_TYPE_JSON)) ) 82 // { 83 // entity = getStatsJson(); 84 // mediaType = MediaType.APPLICATION_JSON; 85 // } 86 // else 87 // { 88 // entity = getStatsXml(); 89 // mediaType = MediaType.APPLICATION_XML; 90 // } 91 // return Response 92 // .ok(entity, mediaType) 93 // .build(); 94 // } 95 // 96 // /** 97 // * Gets all resources list in XML format 98 // * @return The list 99 // */ 100 // public String getStatsXml( ) 101 // { 102 // StringBuffer sbXML = new StringBuffer( XmlUtil.getXmlHeader() ); 103 // Collection<Stat> list = StatHome.getStatsList(); 104 // 105 // XmlUtil.beginElement( sbXML , KEY_STATS ); 106 // 107 // for ( Stat stat : list ) 108 // { 109 // addStatXml( sbXML, stat ); 110 // } 111 // 112 // XmlUtil.endElement( sbXML , KEY_STATS ); 113 // 114 // return sbXML.toString( ); 115 // } 116 // 117 // /** 118 // * Gets all resources list in JSON format 119 // * @return The list 120 // */ 121 // public String getStatsJson( ) 122 // { 123 // JSONObject jsonStat = new JSONObject( ); 124 // JSONObject json = new JSONObject( ); 125 // 126 // Collection<Stat> list = StatHome.getStatsList(); 127 // 128 // for ( Stat stat : list ) 129 // { 130 // addStatJson( jsonStat, stat ); 131 // } 132 // 133 // json.accumulate( KEY_STATS, jsonStat ); 134 // 135 // return json.toString( ); 136 // } 137 // 138 // @GET 139 // @Path( "{" + Constants.ID_PATH + "}" ) 140 // public Response getStat( @PathParam( Constants.ID_PATH ) String strId, @HeaderParam(HttpHeaders.ACCEPT) String accept , @QueryParam( Constants.FORMAT_QUERY ) String format ) throws IOException 141 // { 142 // String entity; 143 // String mediaType; 144 // 145 // if ( (accept != null && accept.contains(MediaType.APPLICATION_JSON)) || (format != null && format.equals(Constants.MEDIA_TYPE_JSON)) ) 146 // { 147 // entity = getStatJson( strId ); 148 // mediaType = MediaType.APPLICATION_JSON; 149 // } 150 // else 151 // { 152 // entity = getStatXml( strId ); 153 // mediaType = MediaType.APPLICATION_XML; 154 // } 155 // return Response 156 // .ok(entity, mediaType) 157 // .build(); 158 // } 159 // 160 // /** 161 // * Gets a resource in XML format 162 // * @param strId The resource ID 163 // * @return The XML output 164 // */ 165 // public String getStatXml( String strId ) 166 // { 167 // StringBuffer sbXML = new StringBuffer( ); 168 // 169 // try 170 // { 171 // int nId = Integer.parseInt( strId ); 172 // Stat stat = StatHome.findByPrimaryKey( nId ); 173 // 174 // if ( stat != null ) 175 // { 176 // sbXML.append( "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n" ); 177 // addStatXml( sbXML, stat ); 178 // } 179 // } 180 // catch ( NumberFormatException e ) 181 // { 182 // sbXML.append( XMLUtil.formatError( "Invalid stat number", 3 ) ); 183 // } 184 // catch ( Exception e ) 185 // { 186 // sbXML.append( XMLUtil.formatError( "Stat not found", 1 ) ); 187 // } 188 // 189 // return sbXML.toString( ); 190 // } 191 // 192 // /** 193 // * Gets a resource in JSON format 194 // * @param strId The resource ID 195 // * @return The JSON output 196 // */ 197 // public String getStatJson( String strId ) 198 // { 199 // JSONObject json = new JSONObject( ); 200 // String strJson = ""; 201 // 202 // try 203 // { 204 // int nId = Integer.parseInt( strId ); 205 // Stat stat = StatHome.findByPrimaryKey( nId ); 206 // 207 // if ( stat != null ) 208 // { 209 // addStatJson( json, stat ); 210 // strJson = json.toString( ); 211 // } 212 // } 213 // catch ( NumberFormatException e ) 214 // { 215 // strJson = JSONUtil.formatError( "Invalid stat number", 3 ); 216 // } 217 // catch ( Exception e ) 218 // { 219 // strJson = JSONUtil.formatError( "Stat not found", 1 ); 220 // } 221 // 222 // return strJson; 223 // } 224 // 225 // @DELETE 226 // @Path( "{" + Constants.ID_PATH + "}" ) 227 // public Response deleteStat( @PathParam( Constants.ID_PATH ) String strId, @HeaderParam(HttpHeaders.ACCEPT) String accept, @QueryParam( Constants.FORMAT_QUERY ) String format ) throws IOException 228 // { 229 // try 230 // { 231 // int nId = Integer.parseInt( strId ); 232 // 233 // if ( StatHome.findByPrimaryKey( nId ) != null ) 234 // { 235 // StatHome.remove( nId ); 236 // } 237 // } 238 // catch ( NumberFormatException e ) 239 // { 240 // AppLogService.error( "Invalid stat number" ); 241 // } 242 // return getStats(accept, format); 243 // } 244 // 245 // @POST 246 // public Response createStat( 247 // @FormParam( "id" ) String id, 248 // @FormParam( "description" ) String description, 249 // @FormParam( "requete_sql" ) String requete_sql, 250 // @HeaderParam(HttpHeaders.ACCEPT) String accept, @QueryParam( Constants.FORMAT_QUERY ) String format) throws IOException 251 // { 252 // if( id != null ) 253 // { 254 // int nId = Integer.parseInt( id); 255 // 256 // Stat stat = StatHome.findByPrimaryKey( nId ); 257 // 258 // if ( stat != null ) 259 // { 260 // stat.setDescription( description ); 261 // stat.setRequeteSql( requete_sql ); 262 // StatHome.update( stat ); 263 // } 264 // } 265 // else 266 // { 267 // Stat stat = new Stat( ); 268 // 269 // stat.setDescription( description ); 270 // stat.setRequeteSql( requete_sql ); 271 // StatHome.create( stat ); 272 // } 273 // return getStats(accept, format); 274 // } 275 // 276 // /** 277 // * Write a stat into a buffer 278 // * @param sbXML The buffer 279 // * @param stat The stat 280 // */ 281 // private void addStatXml( StringBuffer sbXML, Stat stat ) 282 // { 283 // XmlUtil.beginElement( sbXML, KEY_STAT ); 284 // XmlUtil.addElement( sbXML, KEY_ID , stat.getId ( ) ); 285 // XmlUtil.addElement( sbXML, KEY_DESCRIPTION , stat.getDescription ( ) ); 286 // XmlUtil.addElement( sbXML, KEY_REQUETE_SQL , stat.getRequeteSql ( ) ); 287 // XmlUtil.endElement( sbXML, KEY_STAT ); 288 // } 289 // 290 // /** 291 // * Write a stat into a JSON Object 292 // * @param json The JSON Object 293 // * @param stat The stat 294 // */ 295 // private void addStatJson( JSONObject json, Stat stat ) 296 // { 297 // JSONObject jsonStat = new JSONObject( ); 298 // jsonStat.accumulate( KEY_ID, stat.getId( ) ); 299 // jsonStat.accumulate( KEY_DESCRIPTION, stat.getDescription( ) ); 300 // jsonStat.accumulate( KEY_REQUETE_SQL, stat.getRequeteSql( ) ); 301 // json.accumulate( KEY_STAT, jsonStat ); 302 // } 303 }