git_sitools_idoc / sitools-idoc / hesiod / javaExt / src / fr / ias / sitools / vo / representation / VOTableRepresentation.java @ 779bac69
1 |
/*******************************************************************************
|
---|---|
2 |
* Copyright 2010-2013 CNES - CENTRE NATIONAL d'ETUDES SPATIALES
|
3 |
*
|
4 |
* This file is part of SITools2.
|
5 |
*
|
6 |
* SITools2 is free software: you can redistribute it and/or modify
|
7 |
* it under the terms of the GNU General Public License as published by
|
8 |
* the Free Software Foundation, either version 3 of the License, or
|
9 |
* (at your option) any later version.
|
10 |
*
|
11 |
* SITools2 is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* GNU General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License
|
17 |
* along with SITools2. If not, see <http://www.gnu.org/licenses/>.
|
18 |
******************************************************************************/
|
19 |
package fr.ias.sitools.vo.representation; |
20 |
|
21 |
import java.io.IOException; |
22 |
import java.io.OutputStream; |
23 |
import java.util.Map; |
24 |
import java.util.logging.Level; |
25 |
import java.util.logging.Logger; |
26 |
import org.restlet.data.LocalReference; |
27 |
import org.restlet.data.MediaType; |
28 |
import org.restlet.ext.freemarker.TemplateRepresentation; |
29 |
import org.restlet.representation.OutputRepresentation; |
30 |
import org.restlet.representation.Representation; |
31 |
import org.restlet.resource.ClientResource; |
32 |
|
33 |
/**
|
34 |
* Creates a VOTable representation with a template and a data model. The data model is the following :
|
35 |
* <pre>
|
36 |
* root
|
37 |
* |__ description
|
38 |
* |__ infos
|
39 |
* | |__ id
|
40 |
* | |__ name (required)
|
41 |
* | |__ valueAttribute (required)
|
42 |
* | |__ xtype
|
43 |
* | |__ ref
|
44 |
* | |__ unit
|
45 |
* | |__ ucd
|
46 |
* | |__ utype
|
47 |
* | |__ value
|
48 |
* |__ params (List)
|
49 |
* | |__ param
|
50 |
* | |__ id
|
51 |
* | |__ unit
|
52 |
* | |__ datatype (required)
|
53 |
* | |__ precision
|
54 |
* | |__ width
|
55 |
* | |__ xtype
|
56 |
* | |__ ref
|
57 |
* | |__ name (required)
|
58 |
* | |__ ucd
|
59 |
* | |__ utype
|
60 |
* | |__ arraysize
|
61 |
* | |__ value (required)
|
62 |
* | |__ DESCRIPTION
|
63 |
* | |__ VALUES
|
64 |
* | |__ id
|
65 |
* | |__ type
|
66 |
* | |__ null
|
67 |
* | |__ ref
|
68 |
* | |__ OPTION (List)
|
69 |
* | |__ option
|
70 |
* | |__ name
|
71 |
* | |__ value (required)
|
72 |
* |__ fields (List)
|
73 |
* | |__ field
|
74 |
* | |__ DESCRIPTION
|
75 |
* | |__ id
|
76 |
* | |__ name (required)
|
77 |
* | |__ ucd
|
78 |
* | |__ utype
|
79 |
* | |__ ref
|
80 |
* | |__ datatype (required)
|
81 |
* | |__ width
|
82 |
* | |__ precision
|
83 |
* | |__ unit
|
84 |
* | |__ type
|
85 |
* | |__ xtype
|
86 |
* | |__ arraysize
|
87 |
* |__ rows (List) (required)
|
88 |
* | |__ row (required)
|
89 |
* |
|
90 |
* |__ sqlColAlias (List) (required)
|
91 |
* | |__ sqlcol (required)
|
92 |
* |__ mappingColAliasConceptSql (Hash) (optional)
|
93 |
* |__ siaCut (boolean, optional)
|
94 |
* |__ nrows (optional)
|
95 |
* |__ mapPartFileCutUrl (Hash) (optional)
|
96 |
* |__ primaryKey (optional)
|
97 |
* |__ queryParams (POS,SIZE,...)
|
98 |
* |__ queryInfos (Query status)
|
99 |
*
|
100 |
* </pre> Provide a VOTable representation by streaming based on Freemarker To have a dataModel by streaming, dataModel for rows element
|
101 |
* must use the DatabaseRequestModel adapter
|
102 |
*
|
103 |
* @author Jean-Christophe Malapert <jean-christophe.malapert@cnes.fr>
|
104 |
*/
|
105 |
public class VOTableRepresentation extends OutputRepresentation { |
106 |
|
107 |
/**
|
108 |
* Logger.
|
109 |
*/
|
110 |
private static final Logger LOG = Logger.getLogger(VOTableRepresentation.class.getName()); |
111 |
/**
|
112 |
* Default template file = votable.ftl.
|
113 |
*/
|
114 |
public static final String DEFAULT_TEMPLATE = "votable.ftl"; |
115 |
/**
|
116 |
* Data model that contains the information to represent.
|
117 |
*/
|
118 |
private Map dataModel; |
119 |
/**
|
120 |
* Template file.
|
121 |
*/
|
122 |
private String ftl; |
123 |
|
124 |
/**
|
125 |
* Creates a VOTableRepresentation based on a dataModel and a templateFile.
|
126 |
*
|
127 |
* @param dataModelVal DataModel
|
128 |
* @param ftlVal template File
|
129 |
*/
|
130 |
public VOTableRepresentation(final Map dataModelVal, final String ftlVal) { |
131 |
super(MediaType.TEXT_XML);
|
132 |
setDataModel(dataModelVal); |
133 |
setFtl(ftlVal); |
134 |
} |
135 |
/**
|
136 |
* Creates a GeoJson representation with the default template (<code>DEFAULT_TEMPLATE</code>).
|
137 |
*
|
138 |
* @param dataModelVal the data model
|
139 |
*/
|
140 |
public VOTableRepresentation(final Map dataModelVal) { |
141 |
this(dataModelVal, DEFAULT_TEMPLATE);
|
142 |
} |
143 |
/**
|
144 |
* Writes the representation.
|
145 |
*
|
146 |
* @param outputStream output stream
|
147 |
* @throws IOException Exception
|
148 |
*/
|
149 |
@Override
|
150 |
public final void write(final OutputStream outputStream) throws IOException { |
151 |
final Representation metadataFtl = new ClientResource(LocalReference.createClapReference(getClass().getPackage()) + "/" |
152 |
+ getFtl()).get(); |
153 |
final TemplateRepresentation tpl = new TemplateRepresentation(metadataFtl, getDataModel(), getMediaType()); |
154 |
LOG.log(Level.FINEST, getFtl(), tpl);
|
155 |
outputStream.write(tpl.getText().getBytes()); |
156 |
outputStream.flush(); |
157 |
} |
158 |
|
159 |
|
160 |
/**
|
161 |
* Returns the data model.
|
162 |
* @return the dataModel
|
163 |
*/
|
164 |
protected final Map getDataModel() { |
165 |
return dataModel;
|
166 |
} |
167 |
|
168 |
/**
|
169 |
* Sets the data model.
|
170 |
* @param dataModelVal the dataModel to set
|
171 |
*/
|
172 |
protected final void setDataModel(final Map dataModelVal) { |
173 |
this.dataModel = dataModelVal;
|
174 |
} |
175 |
|
176 |
/**
|
177 |
* Returns the template filename.
|
178 |
* @return the ftl
|
179 |
*/
|
180 |
protected final String getFtl() { |
181 |
return ftl;
|
182 |
} |
183 |
|
184 |
/**
|
185 |
* Sets the template filename.
|
186 |
* @param ftlVal the ftl to set
|
187 |
*/
|
188 |
protected final void setFtl(final String ftlVal) { |
189 |
this.ftl = ftlVal;
|
190 |
} |
191 |
} |