diff --git a/kraftwerk-core/src/main/java/fr/insee/kraftwerk/core/extradata/reportingdata/XMLReportingDataParser.java b/kraftwerk-core/src/main/java/fr/insee/kraftwerk/core/extradata/reportingdata/XMLReportingDataParser.java index 2d037dd4..de3c64a5 100644 --- a/kraftwerk-core/src/main/java/fr/insee/kraftwerk/core/extradata/reportingdata/XMLReportingDataParser.java +++ b/kraftwerk-core/src/main/java/fr/insee/kraftwerk/core/extradata/reportingdata/XMLReportingDataParser.java @@ -16,201 +16,201 @@ @Log4j2 public class XMLReportingDataParser extends ReportingDataParser { - - private Document document; + + private Document document; public XMLReportingDataParser(FileUtilsInterface fileUtilsInterface) { super(fileUtilsInterface); } public void parseReportingData(ReportingData reportingData, SurveyRawData data, boolean withAllReportingData) throws NullException { - Path filePath = reportingData.getFilepath(); - readFile(filePath); - Element root; - try{ - root = this.document.getRootElement(); - } catch (NullPointerException e) { - throw new NullException(); - } - // Read information on each survey unit - List surveyUnitsNodeList = getSurveyUnitList(root); - - log.info("Read {} surveyUnit in file {}", surveyUnitsNodeList.size(),filePath); - - for (Element surveyUnitElement : surveyUnitsNodeList) { - ReportingDataUE reportingDataUE = new ReportingDataUE(); - Element identifierElement = surveyUnitElement.getFirstChildElement("Id"); - String identifier = identifierElement.getValue(); - reportingDataUE.setIdentifier(identifier); - - Element interviewerIdentifierElement = surveyUnitElement.getFirstChildElement("InterviewerId"); - String interviewerIdentifier = Constants.REPORTING_DATA_INTERVIEWER_ID_NULL_PLACEHOLDER + identifier; - if (interviewerIdentifierElement != null) { - interviewerIdentifier = interviewerIdentifierElement.getValue(); - } - reportingDataUE.setInterviewerId(interviewerIdentifier); - - Element organizationalUnitIdentifierElement = surveyUnitElement - .getFirstChildElement("OrganizationalUnitId"); - if (organizationalUnitIdentifierElement != null) { - String organizationalUnitIdentifier = organizationalUnitIdentifierElement.getValue(); - reportingDataUE.setOrganizationUnitId(organizationalUnitIdentifier); - } - - // Get address values - getAddress(surveyUnitElement, reportingDataUE); - - Elements stateNodeList = surveyUnitElement.getFirstChildElement("States").getChildElements("State"); - for (int j = 0; j < stateNodeList.size(); j++) { - Element stateElement = stateNodeList.get(j); - String type = stateElement.getFirstChildElement("type").getValue().toUpperCase(); - String timestamp = stateElement.getFirstChildElement("date").getValue(); - reportingDataUE.addState(new State(type, Long.parseLong(timestamp))); - } - reportingDataUE.sortStates(); - - // Get outcome values - Element contactOutcomeElement = surveyUnitElement.getFirstChildElement("ContactOutcome"); - reportingDataUE.setContactOutcome(new ContactOutcome()); - if (contactOutcomeElement != null) { - reportingDataUE.getContactOutcome() - .setOutcomeType(contactOutcomeElement.getFirstChildElement("outcomeType").getValue()); - reportingDataUE.getContactOutcome().setDateEndContact( - Long.parseLong(contactOutcomeElement.getFirstChildElement("date").getValue())); - reportingDataUE.getContactOutcome().setTotalNumberOfContactAttempts( - Integer.parseInt(contactOutcomeElement.getFirstChildElement("totalNumberOfContactAttempts").getValue())); - } - - Element contactAttemptsNode = surveyUnitElement.getFirstChildElement("ContactAttempts"); - if (contactAttemptsNode != null) { - Elements contactAttemptsElements = contactAttemptsNode.getChildElements("ContactAttempt"); - for (int k = 0; k < contactAttemptsElements.size(); k++) { - Element contactAttemptsElement = contactAttemptsElements.get(k); - String status = contactAttemptsElement.getFirstChildElement("status").getValue().toUpperCase(); - String timestamp = contactAttemptsElement.getFirstChildElement("date").getValue(); - reportingDataUE.addContactAttempt(new ContactAttempt(status, Long.parseLong(timestamp))); - } - } - - //Get identification - getIdentification(surveyUnitElement, reportingDataUE); - - //Get comments - getComments(surveyUnitElement, reportingDataUE); - - //Get survey validation date from states - setSurveyValidationDate(reportingDataUE); - - reportingData.addReportingDataUE(reportingDataUE); - } - integrateReportingDataIntoUE(data, reportingData, withAllReportingData, fileUtilsInterface); - this.document = null; - } - - - private void readFile(Path filePath) { - XmlFileReader xmlFileReader = new XmlFileReader(fileUtilsInterface); - this.document = xmlFileReader.readXmlFile(filePath); - if (this.document != null) { - log.info("Successfully parsed Coleman/Moog answers file: " + filePath); - } else { - log.warn("Failed to parse Coleman/Moog answers file: " + filePath); - } - } - - private List getSurveyUnitList(Element root){ - List surveyUnitList = new ArrayList<>(); - - Element partitioningsNode = root.getFirstChildElement("Partitionings"); - if(partitioningsNode == null){ - // Survey unit list directly in root - getSurveyUnitsFromElement(root,surveyUnitList); - }else{ - // Survey unit list divided into partitionings - for(Element partitioningElement : partitioningsNode.getChildElements("Partitioning")){ - getSurveyUnitsFromElement(partitioningElement, surveyUnitList); - } - } - - return surveyUnitList; - } - - private void getSurveyUnitsFromElement(Element element, List surveyUnitList){ - Element surveyUnitsNode = element.getFirstChildElement("SurveyUnits"); - for(Element surveyUnitElement : surveyUnitsNode.getChildElements("SurveyUnit")) { - surveyUnitList.add(surveyUnitElement); - } - } - - private void getAddress(Element surveyUnitElement, ReportingDataUE reportingDataUE) { - Element addressElement = surveyUnitElement.getFirstChildElement("InseeSampleIdentiers"); - - if(addressElement != null) { - reportingDataUE.setInseeSampleIdentifier(new InseeSampleIdentifier()); - reportingDataUE.getInseeSampleIdentifier().setRges(addressElement.getFirstChildElement("Rges").getValue()); - reportingDataUE.getInseeSampleIdentifier().setNumfa(addressElement.getFirstChildElement("Numfa").getValue()); - reportingDataUE.getInseeSampleIdentifier().setSsech(addressElement.getFirstChildElement("Ssech").getValue()); - reportingDataUE.getInseeSampleIdentifier().setLe(addressElement.getFirstChildElement("Le").getValue()); - reportingDataUE.getInseeSampleIdentifier().setEc(addressElement.getFirstChildElement("Ec").getValue()); - reportingDataUE.getInseeSampleIdentifier().setBs(addressElement.getFirstChildElement("Bs").getValue()); - reportingDataUE.getInseeSampleIdentifier().setNoi(addressElement.getFirstChildElement("Noi").getValue()); + Path filePath = reportingData.getFilepath(); + readFile(filePath); + Element root; + try { + root = this.document.getRootElement(); + } catch (NullPointerException e) { + throw new NullException(); + } + // Read information on each survey unit + List surveyUnitsNodeList = getSurveyUnitList(root); + + log.info("Read {} surveyUnit in file {}", surveyUnitsNodeList.size(), filePath); + + for (Element surveyUnitElement : surveyUnitsNodeList) { + ReportingDataUE reportingDataUE = new ReportingDataUE(); + Element identifierElement = surveyUnitElement.getFirstChildElement("Id"); + String identifier = identifierElement.getValue(); + reportingDataUE.setIdentifier(identifier); + + Element interviewerIdentifierElement = surveyUnitElement.getFirstChildElement("InterviewerId"); + String interviewerIdentifier = Constants.REPORTING_DATA_INTERVIEWER_ID_NULL_PLACEHOLDER + identifier; + if (interviewerIdentifierElement != null) { + interviewerIdentifier = interviewerIdentifierElement.getValue(); + } + reportingDataUE.setInterviewerId(interviewerIdentifier); + + Element organizationalUnitIdentifierElement = surveyUnitElement + .getFirstChildElement("OrganizationalUnitId"); + if (organizationalUnitIdentifierElement != null) { + String organizationalUnitIdentifier = organizationalUnitIdentifierElement.getValue(); + reportingDataUE.setOrganizationUnitId(organizationalUnitIdentifier); + } + + // Get address values + getAddress(surveyUnitElement, reportingDataUE); + + Elements stateNodeList = surveyUnitElement.getFirstChildElement("States").getChildElements("State"); + for (int j = 0; j < stateNodeList.size(); j++) { + Element stateElement = stateNodeList.get(j); + String type = stateElement.getFirstChildElement("type").getValue().toUpperCase(); + String timestamp = stateElement.getFirstChildElement("date").getValue(); + reportingDataUE.addState(new State(type, Long.parseLong(timestamp))); + } + reportingDataUE.sortStates(); + + // Get outcome values + Element contactOutcomeElement = surveyUnitElement.getFirstChildElement("ContactOutcome"); + reportingDataUE.setContactOutcome(new ContactOutcome()); + if (contactOutcomeElement != null) { + reportingDataUE.getContactOutcome() + .setOutcomeType(contactOutcomeElement.getFirstChildElement("outcomeType").getValue()); + reportingDataUE.getContactOutcome().setDateEndContact( + Long.parseLong(contactOutcomeElement.getFirstChildElement("date").getValue())); + reportingDataUE.getContactOutcome().setTotalNumberOfContactAttempts( + Integer.parseInt(contactOutcomeElement.getFirstChildElement("totalNumberOfContactAttempts").getValue())); + } + + Element contactAttemptsNode = surveyUnitElement.getFirstChildElement("ContactAttempts"); + if (contactAttemptsNode != null) { + Elements contactAttemptsElements = contactAttemptsNode.getChildElements("ContactAttempt"); + for (int k = 0; k < contactAttemptsElements.size(); k++) { + Element contactAttemptsElement = contactAttemptsElements.get(k); + String status = contactAttemptsElement.getFirstChildElement("status").getValue().toUpperCase(); + String timestamp = contactAttemptsElement.getFirstChildElement("date").getValue(); + reportingDataUE.addContactAttempt(new ContactAttempt(status, Long.parseLong(timestamp))); + } + } + + //Get identification + getIdentification(surveyUnitElement, reportingDataUE); + + //Get comments + getComments(surveyUnitElement, reportingDataUE); + + //Get survey validation date from states + setSurveyValidationDate(reportingDataUE); + + reportingData.addReportingDataUE(reportingDataUE); + } + integrateReportingDataIntoUE(data, reportingData, withAllReportingData, fileUtilsInterface); + this.document = null; } - } - private void getIdentification(Element surveyUnitElement, ReportingDataUE reportingDataUE) { - Element identificationElement = surveyUnitElement.getFirstChildElement("Identification"); - reportingDataUE.setIdentification(new ReportingIdentification()); - if (identificationElement == null) { - return; - } - - if (identificationElement.getFirstChildElement(Constants.IDENTIFICATION_NAME) != null) { - reportingDataUE.getIdentification().setIdentification(identificationElement.getFirstChildElement(Constants.IDENTIFICATION_NAME).getValue()); - } - if (identificationElement.getFirstChildElement(Constants.ACCESS_NAME) != null) { - reportingDataUE.getIdentification().setAccess(identificationElement.getFirstChildElement(Constants.ACCESS_NAME).getValue()); - } - if (identificationElement.getFirstChildElement(Constants.SITUATION_NAME) != null) { - reportingDataUE.getIdentification().setSituation(identificationElement.getFirstChildElement(Constants.SITUATION_NAME).getValue()); + private void readFile(Path filePath) { + XmlFileReader xmlFileReader = new XmlFileReader(fileUtilsInterface); + this.document = xmlFileReader.readXmlFile(filePath); + if (this.document != null) { + log.info("Successfully parsed Coleman/Moog answers file: " + filePath); + } else { + log.warn("Failed to parse Coleman/Moog answers file: " + filePath); + } } - if (identificationElement.getFirstChildElement(Constants.CATEGORY_NAME) != null) { - reportingDataUE.getIdentification().setCategory(identificationElement.getFirstChildElement(Constants.CATEGORY_NAME).getValue()); + private List getSurveyUnitList(Element root) { + List surveyUnitList = new ArrayList<>(); + + Element partitioningsNode = root.getFirstChildElement("Partitionings"); + if (partitioningsNode == null) { + // Survey unit list directly in root + getSurveyUnitsFromElement(root, surveyUnitList); + } else { + // Survey unit list divided into partitionings + for (Element partitioningElement : partitioningsNode.getChildElements("Partitioning")) { + getSurveyUnitsFromElement(partitioningElement, surveyUnitList); + } + } + + return surveyUnitList; } - if (identificationElement.getFirstChildElement(Constants.OCCUPANT_NAME) != null) { - reportingDataUE.getIdentification().setOccupant(identificationElement.getFirstChildElement(Constants.OCCUPANT_NAME).getValue()); + private void getSurveyUnitsFromElement(Element element, List surveyUnitList) { + Element surveyUnitsNode = element.getFirstChildElement("SurveyUnits"); + for (Element surveyUnitElement : surveyUnitsNode.getChildElements("SurveyUnit")) { + surveyUnitList.add(surveyUnitElement); + } } - } - private void getComments(Element surveyUnitElement, ReportingDataUE reportingDataUE) { - Element commentsNode = surveyUnitElement.getFirstChildElement("Comments"); - if(commentsNode == null) { - return; + private void getAddress(Element surveyUnitElement, ReportingDataUE reportingDataUE) { + Element addressElement = surveyUnitElement.getFirstChildElement("InseeSampleIdentiers"); + + if (addressElement != null) { + reportingDataUE.setInseeSampleIdentifier(new InseeSampleIdentifier()); + reportingDataUE.getInseeSampleIdentifier().setRges(addressElement.getFirstChildElement("Rges").getValue()); + reportingDataUE.getInseeSampleIdentifier().setNumfa(addressElement.getFirstChildElement("Numfa").getValue()); + reportingDataUE.getInseeSampleIdentifier().setSsech(addressElement.getFirstChildElement("Ssech").getValue()); + reportingDataUE.getInseeSampleIdentifier().setLe(addressElement.getFirstChildElement("Le").getValue()); + reportingDataUE.getInseeSampleIdentifier().setEc(addressElement.getFirstChildElement("Ec").getValue()); + reportingDataUE.getInseeSampleIdentifier().setBs(addressElement.getFirstChildElement("Bs").getValue()); + reportingDataUE.getInseeSampleIdentifier().setNoi(addressElement.getFirstChildElement("Noi").getValue()); + } } - Elements commentsElements = commentsNode.getChildElements("Comment"); - for (int k = 0; k < commentsElements.size(); k++) { - Element commentsElement = commentsElements.get(k); - String status = commentsElement.getFirstChildElement("type").getValue(); - String value = commentsElement.getFirstChildElement("value").getValue(); - reportingDataUE.addComment(new Comment(status, value)); + private void getIdentification(Element surveyUnitElement, ReportingDataUE reportingDataUE) { + Element identificationElement = surveyUnitElement.getFirstChildElement("Identification"); + reportingDataUE.setIdentification(new ReportingIdentification()); + if (identificationElement == null) { + return; + } + + if (identificationElement.getFirstChildElement(Constants.IDENTIFICATION_NAME) != null) { + reportingDataUE.getIdentification().setIdentification(identificationElement.getFirstChildElement(Constants.IDENTIFICATION_NAME).getValue()); + } + if (identificationElement.getFirstChildElement(Constants.ACCESS_NAME) != null) { + reportingDataUE.getIdentification().setAccess(identificationElement.getFirstChildElement(Constants.ACCESS_NAME).getValue()); + } + + if (identificationElement.getFirstChildElement(Constants.SITUATION_NAME) != null) { + reportingDataUE.getIdentification().setSituation(identificationElement.getFirstChildElement(Constants.SITUATION_NAME).getValue()); + } + + if (identificationElement.getFirstChildElement(Constants.CATEGORY_NAME) != null) { + reportingDataUE.getIdentification().setCategory(identificationElement.getFirstChildElement(Constants.CATEGORY_NAME).getValue()); + } + + if (identificationElement.getFirstChildElement(Constants.OCCUPANT_NAME) != null) { + reportingDataUE.getIdentification().setOccupant(identificationElement.getFirstChildElement(Constants.OCCUPANT_NAME).getValue()); + } } - } - private void setSurveyValidationDate(ReportingDataUE reportingDataUE) { - if(reportingDataUE.getStates().isEmpty()) { - return; + private void getComments(Element surveyUnitElement, ReportingDataUE reportingDataUE) { + Element commentsNode = surveyUnitElement.getFirstChildElement("Comments"); + if (commentsNode == null) { + return; + } + + Elements commentsElements = commentsNode.getChildElements("Comment"); + for (int k = 0; k < commentsElements.size(); k++) { + Element commentsElement = commentsElements.get(k); + String status = commentsElement.getFirstChildElement("type").getValue(); + String value = commentsElement.getFirstChildElement("value").getValue(); + reportingDataUE.addComment(new Comment(status, value)); + } } - State validationState = null; - for (State contactState : reportingDataUE.getStates()) { - if(contactState.isValidationState() && contactState.isPriorTo(validationState)){ - validationState = contactState; - } + private void setSurveyValidationDate(ReportingDataUE reportingDataUE) { + if (reportingDataUE.getStates().isEmpty()) { + return; + } + + State validationState = null; + for (State contactState : reportingDataUE.getStates()) { + if (contactState.isValidationState() && contactState.isPriorTo(validationState)) { + validationState = contactState; + } + } + if (validationState != null) + reportingDataUE.setSurveyValidationDateTimeStamp(validationState.getTimestamp()); } - if(validationState != null) - reportingDataUE.setSurveyValidationDateTimeStamp(validationState.getTimestamp()); - } }