diff --git a/pom.xml b/pom.xml
index ddfd5c798..4a6c82b1f 100644
--- a/pom.xml
+++ b/pom.xml
@@ -4,7 +4,7 @@
fr.insee.rmes
Bauhaus-BO
war
- 3.0.11
+ 3.0.12
Bauhaus-Back-Office
Back-office services for Bauhaus
https://github.com/InseeFr/Bauhaus-Back-Office
diff --git a/src/main/java/fr/insee/rmes/bauhaus_services/CodeListService.java b/src/main/java/fr/insee/rmes/bauhaus_services/CodeListService.java
index 9243558b4..a96f52a10 100644
--- a/src/main/java/fr/insee/rmes/bauhaus_services/CodeListService.java
+++ b/src/main/java/fr/insee/rmes/bauhaus_services/CodeListService.java
@@ -11,15 +11,15 @@ public interface CodeListService {
String getCodeUri(String notationCodeList, String notationCode) throws RmesException;
- String getAllCodesLists() throws RmesException;
+ String getAllCodesLists(boolean partial) throws RmesException;
String geCodesListByIRI(String iri) throws RmesException;
- String getDetailedCodesList(String notation) throws RmesException;
+ String getDetailedCodesList(String notation, boolean partial) throws RmesException;
- String getDetailedCodesListForSearch() throws RmesException;
+ String getDetailedCodesListForSearch(boolean partial) throws RmesException;
- String setCodesList(String body) throws RmesException;
+ String setCodesList(String body, boolean partial) throws RmesException;
- String setCodesList(String id, String body) throws RmesException;
+ String setCodesList(String id, String body, boolean partial) throws RmesException;
}
diff --git a/src/main/java/fr/insee/rmes/bauhaus_services/GeographyService.java b/src/main/java/fr/insee/rmes/bauhaus_services/GeographyService.java
index 84ab6083a..71af46f78 100644
--- a/src/main/java/fr/insee/rmes/bauhaus_services/GeographyService.java
+++ b/src/main/java/fr/insee/rmes/bauhaus_services/GeographyService.java
@@ -20,5 +20,5 @@ public interface GeographyService {
JSONObject getGeoFeature(IRI uri) throws RmesException;
-
+ void updateFeature(String id, String body) throws RmesException;
}
diff --git a/src/main/java/fr/insee/rmes/bauhaus_services/OperationsService.java b/src/main/java/fr/insee/rmes/bauhaus_services/OperationsService.java
index db9554681..ead78a011 100644
--- a/src/main/java/fr/insee/rmes/bauhaus_services/OperationsService.java
+++ b/src/main/java/fr/insee/rmes/bauhaus_services/OperationsService.java
@@ -87,6 +87,8 @@ public interface OperationsService {
String getIndicators() throws RmesException;
+ String getIndicatorsWithSims() throws RmesException;
+
String getIndicatorsForSearch() throws RmesException;
String getIndicatorJsonByID(String id) throws RmesException;
@@ -158,4 +160,6 @@ public interface OperationsService {
Response exportMetadataReportTempFiles(String id, Boolean includeEmptyMas, Boolean lg1, Boolean lg2) throws RmesException;
+ String getOperationsWithReport(String id) throws RmesException;
+
}
diff --git a/src/main/java/fr/insee/rmes/bauhaus_services/code_list/CodeListServiceImpl.java b/src/main/java/fr/insee/rmes/bauhaus_services/code_list/CodeListServiceImpl.java
index d31cbc3a5..e0ca749c2 100644
--- a/src/main/java/fr/insee/rmes/bauhaus_services/code_list/CodeListServiceImpl.java
+++ b/src/main/java/fr/insee/rmes/bauhaus_services/code_list/CodeListServiceImpl.java
@@ -9,13 +9,7 @@
import org.eclipse.rdf4j.model.Model;
import org.eclipse.rdf4j.model.Resource;
import org.eclipse.rdf4j.model.impl.LinkedHashModel;
-import org.eclipse.rdf4j.model.impl.SimpleIRI;
-import org.eclipse.rdf4j.model.vocabulary.DC;
-import org.eclipse.rdf4j.model.vocabulary.DCTERMS;
-import org.eclipse.rdf4j.model.vocabulary.OWL;
-import org.eclipse.rdf4j.model.vocabulary.RDF;
-import org.eclipse.rdf4j.model.vocabulary.RDFS;
-import org.eclipse.rdf4j.model.vocabulary.SKOS;
+import org.eclipse.rdf4j.model.vocabulary.*;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
@@ -38,6 +32,10 @@
import fr.insee.rmes.persistance.sparql_queries.code_list.CodeListQueries;
import fr.insee.rmes.utils.DateUtils;
+import java.util.*;
+import java.util.concurrent.atomic.AtomicInteger;
+import java.util.stream.Collectors;
+
@Service
public class CodeListServiceImpl extends RdfService implements CodeListService {
@@ -52,12 +50,12 @@ public class CodeListServiceImpl extends RdfService implements CodeListService
@Autowired
FamOpeSerIndUtils famOpeSerIndUtils;
-
-
+
+
@Override
public String getCodeListJson(String notation) throws RmesException{
JSONObject codeList = repoGestion.getResponseAsObject(CodeListQueries.getCodeListLabelByNotation(notation));
- codeList.put("notation",notation);
+ codeList.put(Constants.NOTATION,notation);
JSONArray items = repoGestion.getResponseAsArray(CodeListQueries.getCodeListItemsByNotation(notation));
if (items.length() != 0){
codeList.put(CODES, items);
@@ -81,48 +79,162 @@ public CodeList getCodeList(String notation) throws RmesException {
}
@Override
- public String getDetailedCodesList(String notation) throws RmesException {
+ public String getDetailedCodesList(String notation, boolean partial) throws RmesException {
JSONObject codeList = repoGestion.getResponseAsObject(CodeListQueries.getDetailedCodeListByNotation(notation));
- JSONArray codes = repoGestion.getResponseAsArray(CodeListQueries.getDetailedCodes(notation));
+ JSONArray codes = repoGestion.getResponseAsArray(CodeListQueries.getDetailedCodes(notation, partial));
+
+ if(!partial){
+ formatCodesForFullList(notation, codeList, codes);
+ }
+ else {
+ formatCodesForPartialList(codeList, codes);
+ }
+
+ return codeList.toString();
+ }
+
+ private void formatCodesForFullList(String notation, JSONObject codeList, JSONArray codes) throws RmesException {
+ Map> parents = new HashMap<>();
if(codes.length() > 0){
JSONObject formattedCodes = new JSONObject();
codes.forEach(c -> {
JSONObject tempCode = (JSONObject) c;
String code = tempCode.getString("code");
-
- if(!formattedCodes.has(code)){
- if(tempCode.has(Constants.PARENTS)){
- JSONArray parents = new JSONArray();
- parents.put(tempCode.getString(Constants.PARENTS));
- tempCode.put(Constants.PARENTS, parents);
+ if(tempCode.has(Constants.PARENTS)){
+ String parentCode = tempCode.getString(Constants.PARENTS);
+ if(!parents.containsKey(parentCode)){
+ parents.put(parentCode, Arrays.asList(code));
+ formattedCodes.put(code, tempCode);
+ } else {
+ parents.get(parentCode).add(code);
}
+ tempCode.remove(Constants.PARENTS);
+ }
+ formattedCodes.put(code, tempCode);
+ });
+
+
+ JSONArray seq = repoGestion.getResponseAsArray(CodeListQueries.getCodesSeq(notation));
+
+ int rootPosition = 1;
+ if(seq.length() > 0){
+ int startPosition = 0;
+ for(int i = 0; i < seq.length(); i++){
+ JSONObject codeAndPosition = seq.getJSONObject(i);
+ String code = codeAndPosition.getString("code");
- formattedCodes.put(code, tempCode);
- } else {
- JSONObject previousCode = formattedCodes.getJSONObject(code);
- JSONArray parents = new JSONArray();
- if(previousCode.has(Constants.PARENTS)){
- parents = previousCode.getJSONArray(Constants.PARENTS);
+ if(parents.containsKey(code)){
+ for(int j = startPosition; j < i; j++){
+ String childCode = seq.getJSONObject(j).getString("code");
+
+ JSONObject child = formattedCodes.getJSONObject(childCode);
+
+ if(!child.has(Constants.PARENTS)){
+ child.put(Constants.PARENTS, new JSONArray());
+ }
+ child.getJSONArray(Constants.PARENTS).put(new JSONObject().put("code", code).put("position", j - startPosition + 1));
+ formattedCodes.put(childCode, child);
+ }
+ startPosition = i + 1;
+
+ rootPosition = setPositonForRootNode(formattedCodes, startPosition, code);
+
+ } else if(isRootNodeWithoutChildren(code, parents)){
+ rootPosition = setPositonForRootNode(formattedCodes, rootPosition, code);
+ startPosition = i + 1;
}
- parents.put(tempCode.getString(Constants.PARENTS));
- previousCode.put(Constants.PARENTS, parents);
- formattedCodes.put(code, previousCode);
+
+
}
- });
+ } else {
+ // If we do not have a Seq, we have to sort alphabetically.
+ parents.keySet().forEach(key -> {
+ List children = parents.get(key);
+ children.sort((o1, o2) -> o1.compareTo(o2));
+
+ for(int i = 0; i < children.size(); i++){
+ String child = children.get(i);
+ JSONObject codeObject = formattedCodes.getJSONObject(child);
+ if(!codeObject.has(Constants.PARENTS)){
+ codeObject.put(Constants.PARENTS, new JSONArray());
+ }
+ codeObject.getJSONArray(Constants.PARENTS).put(new JSONObject().put("code", key).put("position", i + 1));
+ formattedCodes.put(child, codeObject);
+ }
+ });
+ // Here will order all root codes. Codes without parents
+ orderRootCodes(formattedCodes);
+ }
+
codeList.put(CODES, formattedCodes);
}
+ }
- return codeList.toString();
+ private int setPositonForRootNode(JSONObject formattedCodes, int rootPosition, String code) {
+ JSONObject child = formattedCodes.getJSONObject(code);
+
+ if(!child.has(Constants.PARENTS)){
+ child.put(Constants.PARENTS, new JSONArray());
+ }
+ child.getJSONArray(Constants.PARENTS).put(new JSONObject().put("code", "").put("position", rootPosition));
+ formattedCodes.put(code, child);
+ rootPosition = rootPosition + 1;
+ return rootPosition;
+ }
+
+ /**
+ *
+ * @param code the identifier of a code
+ * @param parents a data structure with all Parent -> Children relationship { parent1: [child1, child2]}
+ * @return true if the code is a rootNode without any children
+ */
+ private boolean isRootNodeWithoutChildren(String code, Map> parents) {
+ return parents.keySet().stream().filter((parentCode) -> {
+ List codes = parents.get(parentCode);
+ return codes.stream().filter((c) -> c.equalsIgnoreCase(code)).collect(Collectors.toList()).size() > 0;
+ }).collect(Collectors.toList()).size() == 0;
+ }
+
+ /**
+ * Format the codes list for a partial code list.
+ * We just need to remove the parents property.
+ *
+ * @param codeList
+ * @param codes
+ */
+ private void formatCodesForPartialList(JSONObject codeList, JSONArray codes) {
+ JSONObject formattedCodes = new JSONObject();
+ codes.forEach(c -> {
+ JSONObject tempCode = (JSONObject) c;
+ String code = tempCode.getString("code");
+ if (tempCode.has(Constants.PARENTS)) {
+ tempCode.remove(Constants.PARENTS);
+ }
+ formattedCodes.put(code, tempCode);
+ });
+ codeList.put(CODES, formattedCodes);
+ }
+
+ private void orderRootCodes(JSONObject formattedCodes) {
+ List rootCodes = formattedCodes.keySet().stream().filter(key -> {
+ return !formattedCodes.getJSONObject(key).has(Constants.PARENTS);
+ }).sorted(Comparator.comparing(code -> code)).collect(Collectors.toList());
+ if(rootCodes.size() > 0) {
+ for(int i = 0; i < rootCodes.size(); i++) {
+ JSONObject parent = new JSONObject().put("code", "").put("position", i + 1);
+ formattedCodes.getJSONObject(rootCodes.get(i)).put(Constants.PARENTS, new JSONArray().put(parent));
+ }
+ }
}
@Override
- public String getDetailedCodesListForSearch() throws RmesException {
- JSONArray lists = repoGestion.getResponseAsArray(CodeListQueries.getCodesListsForSearch());
- JSONArray codes = repoGestion.getResponseAsArray(CodeListQueries.getCodesForSearch());
+ public String getDetailedCodesListForSearch(boolean partial) throws RmesException {
+ JSONArray lists = repoGestion.getResponseAsArray(CodeListQueries.getCodesListsForSearch(partial));
+ JSONArray codes = repoGestion.getResponseAsArray(CodeListQueries.getCodesForSearch(partial));
for (int i = 0 ; i < lists.length(); i++) {
JSONObject list = lists.getJSONObject(i);
@@ -132,7 +244,7 @@ public String getDetailedCodesListForSearch() throws RmesException {
return lists.toString();
}
- public void validateCodeList(JSONObject codeList){
+ public void validateCodeList(JSONObject codeList, boolean partial){
if (!codeList.has(Constants.ID)) {
throw new BadRequestException("The id of the list should be defined");
}
@@ -142,7 +254,7 @@ public void validateCodeList(JSONObject codeList){
if (!codeList.has(Constants.LABEL_LG2)) {
throw new BadRequestException("The labelLg2 of the list should be defined");
}
- if (!codeList.has("lastClassUriSegment")) {
+ if (!partial && !codeList.has("lastClassUriSegment")) {
throw new BadRequestException("The lastClassUriSegment of the list should be defined");
}
if (!codeList.has(LAST_LIST_URI_SEGMENT)) {
@@ -150,13 +262,13 @@ public void validateCodeList(JSONObject codeList){
}
}
@Override
- public String setCodesList(String body) throws RmesException {
+ public String setCodesList(String body, boolean partial) throws RmesException {
ObjectMapper mapper = new ObjectMapper();
mapper.configure(
DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
JSONObject codesList = new JSONObject(body);
- this.validateCodeList(codesList);
+ this.validateCodeList(codesList, partial);
IRI codeListIri = RdfUtils.codeListIRI(codesList.getString(LAST_LIST_URI_SEGMENT));
repoGestion.clearStructureNodeAndComponents(codeListIri);
@@ -164,17 +276,17 @@ public String setCodesList(String body) throws RmesException {
Resource graph = RdfUtils.codesListGraph();
RdfUtils.addTripleDateTime(codeListIri, DCTERMS.CREATED, DateUtils.getCurrentDate(), model, graph);
RdfUtils.addTripleDateTime(codeListIri, DCTERMS.MODIFIED, DateUtils.getCurrentDate(), model, graph);
- return this.createOrUpdateCodeList(model, graph, codesList, codeListIri);
+ return this.createOrUpdateCodeList(model, graph, codesList, codeListIri, partial);
}
@Override
- public String setCodesList(String id, String body) throws RmesException {
+ public String setCodesList(String id, String body, boolean partial) throws RmesException {
ObjectMapper mapper = new ObjectMapper();
mapper.configure(
DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
JSONObject codesList = new JSONObject(body);
- this.validateCodeList(codesList);
+ this.validateCodeList(codesList, partial);
IRI codeListIri = RdfUtils.codeListIRI(codesList.getString(LAST_LIST_URI_SEGMENT));
repoGestion.clearStructureNodeAndComponents(codeListIri);
@@ -184,22 +296,29 @@ public String setCodesList(String id, String body) throws RmesException {
RdfUtils.addTripleDateTime(codeListIri, DCTERMS.CREATED, codesList.getString("created"), model, graph);
RdfUtils.addTripleDateTime(codeListIri, DCTERMS.MODIFIED, DateUtils.getCurrentDate(), model, graph);
- return this.createOrUpdateCodeList(model, graph, codesList, codeListIri);
+ return this.createOrUpdateCodeList(model, graph, codesList, codeListIri, partial);
}
- private String createOrUpdateCodeList(Model model, Resource graph, JSONObject codesList, IRI codeListIri) throws RmesException {
+ private String createOrUpdateCodeList(Model model, Resource graph, JSONObject codesList, IRI codeListIri, boolean partial) throws RmesException {
String codeListId = codesList.getString(Constants.ID);
model.add(codeListIri, INSEE.VALIDATION_STATE, RdfUtils.setLiteralString(ValidationStatus.UNPUBLISHED), graph);
- RdfUtils.addTripleUri(codeListIri, RDF.TYPE, SKOS.CONCEPT_SCHEME, model, graph);
+ if(partial){
+ RdfUtils.addTripleUri(codeListIri, RDF.TYPE, SKOS.COLLECTION, model, graph);
+ } else {
+ RdfUtils.addTripleUri(codeListIri, RDF.TYPE, SKOS.CONCEPT_SCHEME, model, graph);
+ }
model.add(codeListIri, SKOS.NOTATION, RdfUtils.setLiteralString(codeListId), graph);
- IRI owlClassUri = RdfUtils.codeListIRI("concept/" + codesList.getString("lastClassUriSegment"));
- RdfUtils.addTripleUri(codeListIri, RDFS.SEEALSO, owlClassUri, model, graph);
- RdfUtils.addTripleUri(owlClassUri, RDF.TYPE, OWL.CLASS, model, graph);
- RdfUtils.addTripleUri(owlClassUri, RDFS.SEEALSO, codeListIri, model, graph);
+ if(!partial){
+ IRI owlClassUri = RdfUtils.codeListIRI("concept/" + codesList.getString("lastClassUriSegment"));
+ RdfUtils.addTripleUri(codeListIri, RDFS.SEEALSO, owlClassUri, model, graph);
+ RdfUtils.addTripleUri(owlClassUri, RDF.TYPE, OWL.CLASS, model, graph);
+ RdfUtils.addTripleUri(owlClassUri, RDFS.SEEALSO, codeListIri, model, graph);
+ }
+
if(codesList.has("disseminationStatus")){
RdfUtils.addTripleUri(codeListIri, INSEE.DISSEMINATIONSTATUS, codesList.getString("disseminationStatus"), model, graph);
@@ -222,69 +341,153 @@ private String createOrUpdateCodeList(Model model, Resource graph, JSONObject co
RdfUtils.addTripleString(codeListIri, DC.CONTRIBUTOR, codesList.getString(Constants.CONTRIBUTOR), model, graph);
}
- CodeList original = getCodeList(codeListId);
- if(original.getCodes() != null) {
- original.getCodes().forEach(code -> {
- IRI codeIri = RdfUtils.codeListIRI(codesList.getString(LAST_LIST_URI_SEGMENT) + "/" + code.getCode());
- try {
- repoGestion.deleteObject(codeIri, null);
- } catch (RmesException e) {
- logger.error(e.getMessage());
+ if(partial){
+ if(codesList.has(CODES)) {
+ JSONObject codes = codesList.getJSONObject(CODES);
+ for (String key : codes.keySet()) {
+ JSONObject code = codes.getJSONObject(key);
+ RdfUtils.addTripleUri(codeListIri, SKOS.MEMBER, RdfUtils.createIRI(code.getString("codeUri")), model, graph);
}
- });
+ }
+ if(codesList.has("iriParent")){
+ RdfUtils.addTripleUri(codeListIri, PROV.WAS_DERIVED_FROM, codesList.getString("iriParent"), model, graph);
+ }
+ } else {
+ CodeList original = getCodeList(codeListId);
+ if(original.getCodes() != null) {
+ original.getCodes().forEach(code -> {
+ IRI codeIri = RdfUtils.codeListIRI(codesList.getString(LAST_LIST_URI_SEGMENT) + "/" + code.getCode());
+ try {
+ repoGestion.deleteObject(codeIri, null);
+ } catch (RmesException e) {
+ logger.error(e.getMessage());
+ }
+ });
+ }
+ createCodeTriplet(graph, codesList, codeListIri, model);
}
- createCodeTriplet(graph, codesList, codeListIri);
+
repoGestion.loadSimpleObject(codeListIri, model, null);
- return ((SimpleIRI)codeListIri).toString();
+ return codeListId;
}
- private void createCodeTriplet(Resource graph, JSONObject codesList, IRI codeListIri) {
+ private void createCodeTriplet(Resource graph, JSONObject codesList, IRI codeListIri, Model codeListModel) {
if(codesList.has(CODES)){
JSONObject parentsModel = new JSONObject();
JSONObject codes = codesList.getJSONObject(CODES);
- codes.keySet().forEach(key -> {
+ for (String key : codes.keySet()) {
try {
JSONObject code = codes.getJSONObject(key);
- Model codeListModel = new LinkedHashModel();
+ Model codeModel = new LinkedHashModel();
IRI codeIri = RdfUtils.codeListIRI(codesList.getString(LAST_LIST_URI_SEGMENT) + "/" + code.get("code"));
- createMainCodeTriplet(graph, codeListIri, code, codeListModel, codeIri);
-
- if(code.has("parents")){
- JSONArray parents = code.getJSONArray("parents");
- parents.forEach( parent -> {
- IRI parentIRI = RdfUtils.codeListIRI(codesList.getString(LAST_LIST_URI_SEGMENT) + "/" + parent);
- RdfUtils.addTripleUri(codeIri, SKOS.BROADER, parentIRI, codeListModel, graph);
-
- if(parentsModel.has((String) parent)){
- parentsModel.getJSONArray((String) parent).put(((SimpleIRI) codeIri).toString());
- } else {
- parentsModel.put((String) parent, new JSONArray().put(((SimpleIRI) codeListIri).toString()));
+ createMainCodeTriplet(graph, codeListIri, code, codeModel, codeIri);
+
+ if (code.has("parents")) {
+ JSONArray parentsWithPosition = code.getJSONArray(Constants.PARENTS);
+ parentsWithPosition.forEach(parentWithPosition -> {
+ String parentCode = ((JSONObject) parentWithPosition).getString("code");
+ if(!parentCode.equalsIgnoreCase("")){
+ IRI parentIRI = RdfUtils.codeListIRI(codesList.getString(LAST_LIST_URI_SEGMENT) + "/" + parentCode);
+ RdfUtils.addTripleUri(codeIri, SKOS.BROADER, parentIRI, codeModel, graph);
+
+ if (parentsModel.has(parentCode)) {
+ parentsModel.getJSONArray(parentCode).put(code.get("code"));
+ } else {
+ parentsModel.put(parentCode, new JSONArray().put(code.get("code")));
+ }
+ }
+ else {
+ if (parentsModel.has("")) {
+ parentsModel.getJSONArray("").put(code.get("code"));
+ } else {
+ parentsModel.put("", new JSONArray().put(code.get("code")));
+ }
}
});
}
- repoGestion.loadSimpleObject(codeIri, codeListModel, null);
+ repoGestion.loadSimpleObject(codeIri, codeModel, null);
} catch (Exception e) {
logger.debug(e.getMessage());
}
- });
+ }
+ createCodesSeq(graph, codeListIri, parentsModel, codeListModel, codesList);
createParentChildRelationForCodes(graph, codesList, parentsModel);
}
}
+ /**
+ * We will create triplets to define the Sequence of codes
+ * @param graph
+ * @param codeListIri
+ * @param parentsModel
+ * @param codeListModel
+ * @param codesList
+ */
+ private void createCodesSeq(Resource graph, IRI codeListIri, JSONObject parentsModel, Model codeListModel, JSONObject codesList) {
+ //IRI codeIri = RdfUtils.codeListIRI(codesList.getString(LAST_LIST_URI_SEGMENT) + "/" + code.get("code"));
+
+ RdfUtils.addTripleUri(codeListIri, RDF.TYPE, RDF.SEQ, codeListModel, graph);
+ JSONObject codes = codesList.getJSONObject("codes");
+
+ AtomicInteger i = new AtomicInteger();
+
+ List