Skip to content

Commit 2370d68

Browse files
authored
Feat/nomenclatures (#220)
* Refactor: clean props * Fix properties name * ref: props more efficient * bumb Pogues-Model to 1.1.0 * ref: fix props name * feat: add nomenclature to visu url * feat: retrieve EnoError to throw it * bumb: version to 4.3.7-SNAPSHOT * fix: reading props for v2 * fix: error for lunaticV2 * Bumb: version * ref: add comment on properties * ref: exception for eno * ref: exception handler * chore: fix import * bumb version to 4.3.8
1 parent 40cbadf commit 2370d68

20 files changed

+367
-316
lines changed

pom.xml

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
<groupId>fr.insee</groupId>
1414
<artifactId>Pogues-BO</artifactId>
1515
<packaging>jar</packaging>
16-
<version>4.3.6</version>
16+
<version>4.3.8</version>
1717
<name>Pogues-BO</name>
1818

1919
<properties>
@@ -22,7 +22,7 @@
2222
<final.asset.name>pogues-bo</final.asset.name>
2323

2424
<json-simple.version>1.1.1</json-simple.version>
25-
<pogues-model.version>1.0.5</pogues-model.version>
25+
<pogues-model.version>1.1.0</pogues-model.version>
2626
<fop.version>2.9</fop.version>
2727
<springdoc-openapi-ui.version>1.7.0</springdoc-openapi-ui.version>
2828
<jacoco.version>0.8.11</jacoco.version>

src/main/java/fr/insee/pogues/api/remote/eno/transforms/EnoClient.java

+5-4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import java.net.URISyntaxException;
66
import java.util.Map;
77

8+
import fr.insee.pogues.exception.EnoException;
89
import org.apache.http.client.ClientProtocolException;
910

1011
public interface EnoClient {
@@ -24,13 +25,13 @@ public interface EnoClient {
2425

2526
String getDDIToPDF (File fileInput) throws URISyntaxException, ClientProtocolException, IOException;
2627

27-
String getDDIToFO(File fileInput) throws URISyntaxException, ClientProtocolException, IOException;
28+
String getDDIToFO(File fileInput) throws URISyntaxException, ClientProtocolException, IOException, EnoException;
2829

29-
String getDDITOLunaticXML(File fileInput) throws URISyntaxException, ClientProtocolException, IOException;
30+
String getDDITOLunaticXML(File fileInput) throws URISyntaxException, ClientProtocolException, IOException, EnoException;
3031

31-
String getDDITOLunaticJSON(File fileInput, Map<String, Object> params) throws URISyntaxException, ClientProtocolException, IOException;
32+
String getDDITOLunaticJSON(File fileInput, Map<String, Object> params) throws URISyntaxException, ClientProtocolException, IOException, EnoException;
3233

33-
String getDDITOXForms(File fileInput) throws URISyntaxException, ClientProtocolException, IOException;
34+
String getDDITOXForms(File fileInput) throws URISyntaxException, ClientProtocolException, IOException, EnoException;
3435

3536
void getParameters () throws Exception;
3637

src/main/java/fr/insee/pogues/api/remote/eno/transforms/EnoClientImpl.java

+26-23
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import java.net.URISyntaxException;
99
import java.util.Map;
1010

11+
import fr.insee.pogues.exception.EnoException;
1112
import org.apache.commons.io.FilenameUtils;
1213
import org.apache.commons.io.IOUtils;
1314
import org.apache.http.HttpEntity;
@@ -42,22 +43,29 @@ public class EnoClientImpl implements EnoClient{
4243
private static final String FORMAT = "UTF-8";
4344
private static final String BASE_PATH = "/questionnaire/DEFAULT";
4445
private static final String MODE = "CAWI";
46+
47+
private static String getFinalResponseFromResponse(HttpResponse httpResponse) throws EnoException, IOException {
48+
HttpEntity entityResponse = httpResponse.getEntity();
49+
String responseContent = EntityUtils.toString(entityResponse, FORMAT);
50+
if(httpResponse.getStatusLine().getStatusCode() != 200){
51+
throw new EnoException(responseContent, null);
52+
}
53+
return responseContent;
54+
}
4555

4656
@Override
4757
public String getDDI32ToDDI33 (File fileInput) throws Exception{
48-
HttpEntity entityResponse = callEnoApi(fileInput, "/questionnaire/ddi32-2-ddi33");
49-
return EntityUtils.toString(entityResponse, FORMAT);
58+
return getFinalResponseFromResponse(callEnoApi(fileInput, "/questionnaire/ddi32-2-ddi33"));
5059
};
5160

5261

5362
@Override
5463
public String getXMLPoguesToDDI (File fileInput) throws Exception{
55-
HttpEntity entityResponse = callEnoApi(fileInput, "/questionnaire/poguesxml-2-ddi");
56-
return EntityUtils.toString(entityResponse, FORMAT);
64+
return getFinalResponseFromResponse(callEnoApi(fileInput, "/questionnaire/poguesxml-2-ddi"));
5765
};
5866

5967
@Override
60-
public String getDDIToPDF (File fileInput) throws URISyntaxException, ClientProtocolException, IOException{
68+
public String getDDIToPDF (File fileInput) throws URISyntaxException, IOException {
6169
URIBuilder uriBuilder = new URIBuilder();
6270
uriBuilder.setScheme(enoScheme).setHost(enoHost).setPath(BASE_PATH+"/pdf");
6371

@@ -82,19 +90,17 @@ public String getDDIToPDF (File fileInput) throws URISyntaxException, ClientProt
8290
};
8391

8492
@Override
85-
public String getDDIToFO(File fileInput) throws URISyntaxException, ClientProtocolException, IOException {
86-
HttpEntity entityResponse = callEnoApi(fileInput, BASE_PATH+"/fo");
87-
return EntityUtils.toString(entityResponse, FORMAT);
93+
public String getDDIToFO(File fileInput) throws URISyntaxException, IOException, EnoException {
94+
return getFinalResponseFromResponse(callEnoApi(fileInput, BASE_PATH+"/fo"));
8895
}
8996

9097
@Override
91-
public String getDDITOLunaticXML(File fileInput) throws URISyntaxException, ClientProtocolException, IOException {
92-
HttpEntity entityResponse = callEnoApi(fileInput, BASE_PATH+"/lunatic-xml");
93-
return EntityUtils.toString(entityResponse, FORMAT);
98+
public String getDDITOLunaticXML(File fileInput) throws URISyntaxException, IOException, EnoException {
99+
return getFinalResponseFromResponse(callEnoApi(fileInput, BASE_PATH+"/lunatic-xml"));
94100
}
95101

96102
@Override
97-
public String getDDITOLunaticJSON(File fileInput, Map<String, Object> params) throws URISyntaxException, ClientProtocolException, IOException {
103+
public String getDDITOLunaticJSON(File fileInput, Map<String, Object> params) throws URISyntaxException, IOException, EnoException {
98104
String WSPath;
99105

100106
if (params.get("mode") != null) {
@@ -113,20 +119,18 @@ public String getDDITOLunaticJSON(File fileInput, Map<String, Object> params) th
113119
HttpEntity entity = builder.build();
114120
post.setEntity(entity);
115121
HttpResponse response = httpclient.execute(post);
116-
HttpEntity entityResponse = response.getEntity();
117-
return EntityUtils.toString(entityResponse, FORMAT);
122+
123+
return getFinalResponseFromResponse(response);
118124
}
119125

120126
@Override
121-
public String getDDITOXForms(File fileInput) throws URISyntaxException, ClientProtocolException, IOException {
122-
HttpEntity entityResponse = callEnoApi(fileInput, BASE_PATH+"/xforms");
123-
return EntityUtils.toString(entityResponse, FORMAT);
127+
public String getDDITOXForms(File fileInput) throws URISyntaxException, IOException, EnoException {
128+
return getFinalResponseFromResponse(callEnoApi(fileInput, BASE_PATH+"/xforms"));
124129
}
125130

126131
@Override
127-
public String getDDIToODT (File fileInput) throws Exception{
128-
HttpEntity entityResponse = callEnoApi(fileInput, BASE_PATH+"/fodt");
129-
return EntityUtils.toString(entityResponse, FORMAT);
132+
public String getDDIToODT (File fileInput) throws Exception {
133+
return getFinalResponseFromResponse(callEnoApi(fileInput, BASE_PATH+"/fodt"));
130134
};
131135

132136

@@ -140,7 +144,7 @@ public void getParameters () throws Exception{
140144
ResponseEntity<String> result = restTemplate.exchange(uriBuilder.build(), HttpMethod.GET, null, String.class);
141145
};
142146

143-
private HttpEntity callEnoApi(File fileInput, String WSPath) throws URISyntaxException, ClientProtocolException, IOException {
147+
private HttpResponse callEnoApi(File fileInput, String WSPath) throws URISyntaxException, ClientProtocolException, IOException {
144148
URIBuilder uriBuilder = new URIBuilder();
145149
uriBuilder.setScheme(enoScheme).setHost(enoHost).setPath(WSPath);
146150

@@ -150,8 +154,7 @@ private HttpEntity callEnoApi(File fileInput, String WSPath) throws URISyntaxExc
150154
builder.addBinaryBody("in", fileInput, ContentType.DEFAULT_BINARY, fileInput.getName());
151155
HttpEntity entity = builder.build();
152156
post.setEntity(entity);
153-
HttpResponse response = httpclient.execute(post);
154-
return response.getEntity();
157+
return httpclient.execute(post);
155158
}
156159

157160

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package fr.insee.pogues.exception;
2+
3+
/**
4+
* Exception thrown if an error occurs during questionnaire de-referencing (composition feature).
5+
*/
6+
public class EnoException extends Exception {
7+
8+
public EnoException(String message, Exception e) {
9+
super(message, e);
10+
}
11+
12+
}

src/main/java/fr/insee/pogues/transforms/visualize/DDIToLunaticJSONImpl.java

+1-5
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,7 @@ public String transform(String input, Map<String, Object> params, String surveyN
5454
}
5555

5656
private String transform(File file, Map<String, Object> params, String surveyName) throws Exception {
57-
try {
58-
return enoClient.getDDITOLunaticJSON(file, params);
59-
} catch (Exception e) {
60-
throw new Exception(String.format("%s:%s", getClass().getName(), e.getMessage()));
61-
}
57+
return enoClient.getDDITOLunaticJSON(file, params);
6258
}
6359

6460
}

src/main/java/fr/insee/pogues/transforms/visualize/LunaticJSONToUriQueenImpl.java

+35-15
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,26 @@
11
package fr.insee.pogues.transforms.visualize;
22

3-
import java.io.InputStream;
4-
import java.io.OutputStream;
5-
import java.net.URLEncoder;
6-
import java.util.Map;
7-
3+
import fr.insee.pogues.persistence.service.QuestionnairesService;
4+
import fr.insee.pogues.utils.suggester.SuggesterVisuTreatment;
5+
import fr.insee.pogues.webservice.rest.PoguesException;
86
import org.json.simple.JSONObject;
97
import org.json.simple.parser.JSONParser;
108
import org.springframework.beans.factory.annotation.Autowired;
119
import org.springframework.beans.factory.annotation.Value;
1210
import org.springframework.stereotype.Service;
1311

14-
import fr.insee.pogues.persistence.service.QuestionnairesService;
15-
import fr.insee.pogues.webservice.rest.PoguesException;
12+
import java.io.InputStream;
13+
import java.io.OutputStream;
14+
import java.net.URLEncoder;
15+
import java.util.List;
16+
import java.util.Map;
1617

1718
@Service
1819
public class LunaticJSONToUriQueenImpl implements LunaticJSONToUriQueen{
1920

2021
@Autowired
2122
private QuestionnairesService questionnaireService;
22-
23+
2324
@Value("${fr.insee.pogues.api.host}")
2425
private String apiHost;
2526

@@ -28,12 +29,19 @@ public class LunaticJSONToUriQueenImpl implements LunaticJSONToUriQueen{
2829

2930
@Value("${fr.insee.pogues.api.scheme}")
3031
private String apiScheme;
31-
32-
@Value("${fr.insee.pogues.api.remote.queen.host}")
33-
private String queenHost;
34-
32+
@Value("${fr.insee.pogues.api.remote.queen.vis.host}")
33+
private String orchestratorHost;
34+
3535
@Value("${fr.insee.pogues.api.remote.queen.vis.path}")
36-
private String queenVisualizationPath;
36+
private String visualizePath;
37+
@Value("${fr.insee.pogues.api.remote.queen.vis.queryparams.questionnaire}")
38+
private String queryParamsQuestionnaire;
39+
40+
@Value("${fr.insee.pogues.api.remote.queen.vis.queryparams.nomenclatures}")
41+
private String queryParamsNomenclatures;
42+
43+
@Value("${fr.insee.pogues.api.remote.api.nomenclatures}")
44+
private String apiNomenclatures;
3745

3846
@Override
3947
public void transform(InputStream input, OutputStream output, Map<String, Object> params, String surveyName)
@@ -59,8 +67,20 @@ public String transform(String input, Map<String, Object> params, String surveyN
5967
} catch (Exception e) {
6068
throw new Exception(String.format("%s:%s", getClass().getName(), e.getMessage()));
6169
}
62-
String urlGetJsonLunatic = String.format("%s://%s%s/api/persistence/questionnaire/json-lunatic/%s",apiScheme,apiHost,apiName,id);
63-
return String.format("%s/%s%s", queenHost, queenVisualizationPath, URLEncoder.encode(urlGetJsonLunatic, "UTF-8"));
70+
71+
List<String> nomenclatureIds = (List<String>) params.get("nomenclatureIds");
72+
String jsonStringNomenclaturesForVisu = SuggesterVisuTreatment.createJsonNomenclaturesForVisu(nomenclatureIds, apiNomenclatures).toJSONString();
73+
String urlGetJsonLunatic = String.format("%s://%s%s/api/persistence/questionnaire/json-lunatic/%s", apiScheme, apiHost, apiName, id);
74+
75+
return String.format(
76+
"%s%s?%s=%s&%s=%s",
77+
orchestratorHost,
78+
visualizePath,
79+
queryParamsQuestionnaire,
80+
URLEncoder.encode(urlGetJsonLunatic, "UTF-8"),
81+
queryParamsNomenclatures,
82+
URLEncoder.encode(jsonStringNomenclaturesForVisu, "UTF-8")
83+
);
6484
}
6585

6686
}

src/main/java/fr/insee/pogues/transforms/visualize/LunaticJSONToUriStromaeV2Impl.java

+40-15
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
package fr.insee.pogues.transforms.visualize;
22

3-
import java.io.InputStream;
4-
import java.io.OutputStream;
5-
import java.net.URLEncoder;
6-
import java.util.Map;
7-
3+
import fr.insee.pogues.persistence.service.QuestionnairesService;
4+
import fr.insee.pogues.utils.suggester.SuggesterVisuTreatment;
5+
import fr.insee.pogues.webservice.rest.PoguesException;
86
import org.json.simple.JSONObject;
97
import org.json.simple.parser.JSONParser;
108
import org.springframework.beans.factory.annotation.Autowired;
119
import org.springframework.beans.factory.annotation.Value;
1210
import org.springframework.stereotype.Service;
1311

14-
import fr.insee.pogues.persistence.service.QuestionnairesService;
15-
import fr.insee.pogues.webservice.rest.PoguesException;
12+
import java.io.InputStream;
13+
import java.io.OutputStream;
14+
import java.net.URLEncoder;
15+
import java.util.List;
16+
import java.util.Map;
1617

1718
@Service
1819
public class LunaticJSONToUriStromaeV2Impl implements LunaticJSONToUriStromaeV2{
@@ -29,8 +30,20 @@ public class LunaticJSONToUriStromaeV2Impl implements LunaticJSONToUriStromaeV2{
2930
@Value("${fr.insee.pogues.api.scheme}")
3031
private String apiScheme;
3132

32-
@Value("${fr.insee.pogues.api.remote.stromaev2.vis.url}")
33-
private String uriStromaeV2;
33+
@Value("${fr.insee.pogues.api.remote.stromaev2.vis.host}")
34+
private String orchestratorHost;
35+
36+
@Value("${fr.insee.pogues.api.remote.stromaev2.vis.path}")
37+
private String visualizePath;
38+
39+
@Value("${fr.insee.pogues.api.remote.stromaev2.vis.queryparams.questionnaire}")
40+
private String queryParamsQuestionnaire;
41+
42+
@Value("${fr.insee.pogues.api.remote.stromaev2.vis.queryparams.nomenclatures}")
43+
private String queryParamsNomenclatures;
44+
45+
@Value("${fr.insee.pogues.api.remote.api.nomenclatures}")
46+
private String apiNomenclatures;
3447

3548
@Override
3649
public void transform(InputStream input, OutputStream output, Map<String, Object> params, String surveyName)
@@ -48,16 +61,28 @@ public String transform(InputStream input, Map<String, Object> params, String su
4861
public String transform(String input, Map<String, Object> params, String surveyName) throws Exception {
4962
JSONParser parser = new JSONParser();
5063
JSONObject jsonContent = (JSONObject) parser.parse(input);
51-
String id = (String) jsonContent.get("id");
64+
String id = (String) jsonContent.get("id");
5265
try {
5366
questionnaireService.createJsonLunatic(jsonContent);
5467
} catch (PoguesException e) {
5568
questionnaireService.updateJsonLunatic(id, jsonContent);
56-
} catch (Exception e) {
57-
throw new Exception(String.format("%s:%s", getClass().getName(), e.getMessage()));
58-
}
59-
String urlGetJsonLunatic = String.format("%s://%s%s/api/persistence/questionnaire/json-lunatic/%s",apiScheme,apiHost,apiName,id);
60-
return String.format("%s%s", uriStromaeV2, URLEncoder.encode(urlGetJsonLunatic, "UTF-8"));
69+
} catch (Exception e) {
70+
throw new Exception(String.format("%s:%s", getClass().getName(), e.getMessage()));
71+
}
72+
73+
List<String> nomenclatureIds = (List<String>) params.get("nomenclatureIds");
74+
String jsonStringNomenclaturesForVisu = SuggesterVisuTreatment.createJsonNomenclaturesForVisu(nomenclatureIds, apiNomenclatures).toJSONString();
75+
String urlGetJsonLunatic = String.format("%s://%s%s/api/persistence/questionnaire/json-lunatic/%s", apiScheme, apiHost, apiName, id);
76+
77+
return String.format(
78+
"%s%s?%s=%s&%s=%s",
79+
orchestratorHost,
80+
visualizePath,
81+
queryParamsQuestionnaire,
82+
URLEncoder.encode(urlGetJsonLunatic, "UTF-8"),
83+
queryParamsNomenclatures,
84+
URLEncoder.encode(jsonStringNomenclaturesForVisu, "UTF-8")
85+
);
6186
}
6287

6388
}

0 commit comments

Comments
 (0)