|
18 | 18 | */ |
19 | 19 | package controllers; |
20 | 20 |
|
| 21 | +import com.fasterxml.jackson.core.JsonProcessingException; |
| 22 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 23 | +import com.google.common.collect.Lists; |
21 | 24 | import com.google.inject.Inject; |
22 | 25 | import lib.APIException; |
23 | 26 | import lib.ApiClient; |
|
27 | 30 | import play.mvc.Result; |
28 | 31 |
|
29 | 32 | import java.io.IOException; |
| 33 | +import java.util.List; |
30 | 34 | import java.util.Map; |
31 | 35 |
|
32 | 36 | /** |
@@ -162,6 +166,44 @@ public Result delete(String nodeId, String inputId, String extractorId) { |
162 | 166 | } |
163 | 167 | } |
164 | 168 |
|
| 169 | + public Result export(String nodeId, String inputId) { |
| 170 | + try { |
| 171 | + Node node = nodeService.loadNode(nodeId); |
| 172 | + Input input = node.getInput(inputId); |
| 173 | + |
| 174 | + BreadcrumbList bc = standardBreadcrumbs(node, input); |
| 175 | + bc.addCrumb("Export", routes.ExtractorsController.export(nodeId, inputId)); |
| 176 | + |
| 177 | + List<Map<String, Object>> exports = Lists.newArrayList(); |
| 178 | + for (Extractor extractor : extractorService.all(node, input)) { |
| 179 | + exports.add(extractor.export()); |
| 180 | + } |
| 181 | + |
| 182 | + String extractorExport = "[]"; |
| 183 | + try { |
| 184 | + ObjectMapper om = new ObjectMapper(); |
| 185 | + extractorExport = om.writeValueAsString(exports); |
| 186 | + } catch(JsonProcessingException e) { |
| 187 | + Logger.error("Could not generate extractor export.", e); |
| 188 | + } |
| 189 | + |
| 190 | + return ok(views.html.system.inputs.extractors.export.render( |
| 191 | + currentUser(), |
| 192 | + bc, |
| 193 | + node, |
| 194 | + input, |
| 195 | + extractorExport) |
| 196 | + ); |
| 197 | + } catch (IOException e) { |
| 198 | + return status(500, views.html.errors.error.render(ApiClient.ERROR_MSG_IO, e, request())); |
| 199 | + } catch (APIException e) { |
| 200 | + String message = "Could not fetch system information. We expected HTTP 200, but got a HTTP " + e.getHttpCode() + "."; |
| 201 | + return status(500, views.html.errors.error.render(message, e, request())); |
| 202 | + } catch (NodeService.NodeNotFoundException e) { |
| 203 | + return status(404, views.html.errors.error.render(ApiClient.ERROR_MSG_NODE_NOT_FOUND, e, request())); |
| 204 | + } |
| 205 | + } |
| 206 | + |
165 | 207 | private static BreadcrumbList standardBreadcrumbs(Node node, Input input) { |
166 | 208 | BreadcrumbList bc = new BreadcrumbList(); |
167 | 209 | bc.addCrumb("System", routes.SystemController.index(0)); |
|
0 commit comments