|
5 | 5 | import java.io.OutputStream; |
6 | 6 | import java.io.OutputStreamWriter; |
7 | 7 | import java.io.Writer; |
| 8 | +import java.text.SimpleDateFormat; |
8 | 9 | import java.util.ArrayList; |
| 10 | +import java.util.Calendar; |
| 11 | +import java.util.GregorianCalendar; |
9 | 12 | import java.util.Iterator; |
10 | 13 | import java.util.List; |
11 | 14 | import java.util.Map.Entry; |
|
46 | 49 |
|
47 | 50 | import com.fasterxml.jackson.databind.JsonNode; |
48 | 51 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 52 | +import com.google.gson.Gson; |
49 | 53 | import com.google.gson.JsonObject; |
50 | 54 | import com.ontos.ldiw.vocabulary.LDIWO; |
51 | 55 |
|
52 | 56 | import eu.geoknow.generator.configuration.FrameworkConfiguration; |
53 | 57 | import eu.geoknow.generator.exceptions.InformationMissingException; |
| 58 | +import eu.geoknow.generator.graphs.GraphsManager; |
| 59 | +import eu.geoknow.generator.graphs.beans.Contribution; |
| 60 | +import eu.geoknow.generator.graphs.beans.NamedGraph; |
54 | 61 | import eu.geoknow.generator.rdf.SecureRdfStoreManagerImpl; |
55 | 62 | import eu.geoknow.generator.users.FrameworkUserManager; |
56 | 63 | import eu.geoknow.generator.users.UserProfile; |
@@ -148,31 +155,14 @@ public Response post(@PathParam("sessionToken") String sessionToken, @Context Ur |
148 | 155 | MultivaluedMap<String, String> formParams) throws Exception { |
149 | 156 |
|
150 | 157 | String username = ""; |
151 | | - /* |
152 | | - * retrieves form user that created that session and the rdfUser and paswword for that user |
153 | | - */ |
154 | 158 | try { |
155 | | - String query = |
156 | | - "PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>" + "\n" + "SELECT ?user FROM <" |
157 | | - + sessionsGraph + "> WHERE { ?user " + " <" + LDIWO.sessionToken + "> \"" |
158 | | - + sessionToken + "\"^^xsd:string .}"; |
159 | | - |
160 | | - String result = frameworkRdfStoreManager.execute(query, jsonResponseFormat); |
161 | | - |
162 | | - ObjectMapper mapper = new ObjectMapper(); |
163 | | - JsonNode rootNode = mapper.readTree(result); |
164 | | - Iterator<JsonNode> bindingsIter = rootNode.path("results").path("bindings").elements(); |
165 | | - |
166 | | - if (bindingsIter.hasNext()) { |
167 | | - JsonNode bindingNode = bindingsIter.next(); |
168 | | - username = bindingNode.get("user").path("value").textValue(); |
169 | | - } |
170 | | - |
| 159 | + username = getUser(sessionToken); |
171 | 160 | } catch (Exception e) { |
172 | 161 | log.error(e); |
173 | 162 | e.printStackTrace(); |
174 | 163 | return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build(); |
175 | 164 | } |
| 165 | + |
176 | 166 | log.debug("user:" + username + "-"); |
177 | 167 | if (username.equals("")) |
178 | 168 | return Response.status(Response.Status.NOT_FOUND).build(); |
@@ -223,6 +213,30 @@ public void write(OutputStream os) throws IOException, WebApplicationException { |
223 | 213 |
|
224 | 214 | } |
225 | 215 |
|
| 216 | + private String getUser(String sessionToken) throws Exception { |
| 217 | + /* |
| 218 | + * retrieves form user that created that session and the rdfUser and paswword for that user |
| 219 | + */ |
| 220 | + String username = ""; |
| 221 | + String query = |
| 222 | + "PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>" + "\n" + "SELECT ?user FROM <" |
| 223 | + + sessionsGraph + "> WHERE { ?user " + " <" + LDIWO.sessionToken + "> \"" |
| 224 | + + sessionToken + "\"^^xsd:string .}"; |
| 225 | + |
| 226 | + String result = frameworkRdfStoreManager.execute(query, jsonResponseFormat); |
| 227 | + |
| 228 | + ObjectMapper mapper = new ObjectMapper(); |
| 229 | + JsonNode rootNode = mapper.readTree(result); |
| 230 | + Iterator<JsonNode> bindingsIter = rootNode.path("results").path("bindings").elements(); |
| 231 | + |
| 232 | + if (bindingsIter.hasNext()) { |
| 233 | + JsonNode bindingNode = bindingsIter.next(); |
| 234 | + username = bindingNode.get("user").path("value").textValue(); |
| 235 | + } |
| 236 | + return username; |
| 237 | + |
| 238 | + } |
| 239 | + |
226 | 240 | @DELETE |
227 | 241 | @Path("{sessionToken}") |
228 | 242 | public Response delete(@PathParam("sessionToken") String sessionToken, @CookieParam( |
@@ -257,4 +271,55 @@ public Response delete(@PathParam("sessionToken") String sessionToken, @CookiePa |
257 | 271 |
|
258 | 272 | return Response.status(Response.Status.NO_CONTENT).build(); |
259 | 273 | } |
| 274 | + |
| 275 | + /** |
| 276 | + * Update the metadata of a contributiuon |
| 277 | + * |
| 278 | + * @param sessionToken |
| 279 | + * @param userc |
| 280 | + * @param token |
| 281 | + * @return |
| 282 | + */ |
| 283 | + @PUT |
| 284 | + @Path("{sessionToken}") |
| 285 | + public Response addContribution(@PathParam("sessionToken") String sessionToken, |
| 286 | + Contribution contribution) { |
| 287 | + |
| 288 | + /* |
| 289 | + * Check that the session exists |
| 290 | + */ |
| 291 | + String username = ""; |
| 292 | + try { |
| 293 | + username = getUser(sessionToken); |
| 294 | + } catch (Exception e) { |
| 295 | + log.error(e); |
| 296 | + e.printStackTrace(); |
| 297 | + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build(); |
| 298 | + } |
| 299 | + |
| 300 | + log.debug("user:" + username + "-"); |
| 301 | + if (username.equals("")) |
| 302 | + return Response.status(Response.Status.UNAUTHORIZED).build(); |
| 303 | + |
| 304 | + try { |
| 305 | + if (contribution.getDate() == null) { |
| 306 | + // 2015-06-12T14:35:00 |
| 307 | + Calendar cal = GregorianCalendar.getInstance(); |
| 308 | + SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"); |
| 309 | + format.setCalendar(cal); |
| 310 | + contribution.setDate(format.format(cal.getTime())); |
| 311 | + } |
| 312 | + GraphsManager manager = new GraphsManager(); |
| 313 | + NamedGraph graph = manager.addContribution(contribution); |
| 314 | + |
| 315 | + Gson gson = new Gson(); |
| 316 | + String json = "{ \"namedgraph\" : " + gson.toJson(graph) + "}"; |
| 317 | + return Response.status(Response.Status.OK).entity(json).type(MediaType.APPLICATION_JSON) |
| 318 | + .build(); |
| 319 | + |
| 320 | + } catch (Exception e) { |
| 321 | + log.error(e); |
| 322 | + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build(); |
| 323 | + } |
| 324 | + } |
260 | 325 | } |
0 commit comments