Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: communicationRequest to edition File #71

Merged
merged 11 commits into from
Oct 25, 2024
15 changes: 14 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,26 @@
<version>2.5.0</version>
</dependency>

<dependency>
<groupId>com.sun.istack</groupId>
<artifactId>istack-commons-runtime</artifactId>
<version>3.0.12</version>
</dependency>

<!--Dependency for jars version logging -->
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-model</artifactId>
<version>3.9.8</version>
</dependency>
</dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.34</version>
<scope>provided</scope>
</dependency>

</dependencies>

<build>
<pluginManagement>
Expand Down
299 changes: 156 additions & 143 deletions src/main/java/fr/insee/pearljam/batch/Launcher.java
Original file line number Diff line number Diff line change
@@ -1,168 +1,181 @@
package fr.insee.pearljam.batch;

import java.io.IOException;
import java.sql.SQLException;

import javax.xml.stream.XMLStreamException;

import fr.insee.pearljam.batch.exception.*;
import fr.insee.pearljam.batch.service.CommunicationService;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import fr.insee.pearljam.batch.config.ApplicationConfig;
import fr.insee.pearljam.batch.config.ApplicationContext;
import fr.insee.pearljam.batch.enums.BatchOption;
import fr.insee.pearljam.batch.exception.ArgumentException;
import fr.insee.pearljam.batch.exception.BatchException;
import fr.insee.pearljam.batch.exception.DataBaseException;
import fr.insee.pearljam.batch.exception.FolderException;
import fr.insee.pearljam.batch.exception.ValidateException;
import fr.insee.pearljam.batch.service.PilotageDBService;
import fr.insee.pearljam.batch.service.PilotageFolderService;
import fr.insee.pearljam.batch.service.PilotageLauncherService;
import fr.insee.pearljam.batch.service.TriggerService;
import fr.insee.pearljam.batch.utils.BatchErrorCode;
import fr.insee.pearljam.batch.utils.PathUtils;
import org.springframework.stereotype.Component;

/**
* Launcher : Pearl Jam Batch main class
*
*
* @author Claudel Benjamin
*
*/
public abstract class Launcher {
/**
* The folder in use to insert datas
*/
public static String FOLDER_IN;
/**
* The folder out use to store logs and file treated
*/
public static String FOLDER_OUT;

/**
* The Application context
*/
static AnnotationConfigApplicationContext context;

static PilotageDBService pilotageDBService;
static PilotageFolderService pilotageFolderService;
static PilotageLauncherService pilotageLauncherService;
static TriggerService triggerService;

/**
* The class logger
*/
private static final Logger logger = LogManager.getLogger(Launcher.class);

public static void main(String[] args) throws IOException, ValidateException, SQLException, XMLStreamException {
context = new AnnotationConfigApplicationContext(ApplicationContext.class);
pilotageDBService = context.getBean(PilotageDBService.class);
pilotageFolderService = context.getBean(PilotageFolderService.class);
pilotageLauncherService = context.getBean(PilotageLauncherService.class);
BatchErrorCode batchErrorCode = BatchErrorCode.OK;
try {
initBatch();
checkFolderTree();
batchErrorCode = runBatch(args);
} catch (ArgumentException | FolderException | IOException | SQLException | DataBaseException te) {
logger.log(Level.ERROR, te.getMessage(), te);
batchErrorCode = BatchErrorCode.KO_TECHNICAL_ERROR;
} catch (BatchException | XMLStreamException | ValidateException fe) {
logger.log(Level.ERROR, fe.getMessage(), fe);
batchErrorCode = BatchErrorCode.KO_FONCTIONAL_ERROR;
}
finally {
logger.log(Level.INFO, Constants.MSG_RETURN_CODE, batchErrorCode);
pilotageDBService.closeConnection();
context.close();
System.exit(batchErrorCode.getCode());
}
}

/**
* Init Batch check all prerequisities before run batch : folder properties and
* database structure
*
* @throws FolderException
* @throws SQLException
* @throws DataBaseException
*/
public static void initBatch() throws FolderException, DataBaseException, SQLException {
// Check folder properties
FOLDER_IN = ApplicationConfig.FOLDER_IN;
FOLDER_OUT = ApplicationConfig.FOLDER_OUT;
if (StringUtils.isBlank(FOLDER_IN) || "${fr.insee.pearljam.folder.in}".equals(FOLDER_IN)) {
throw new FolderException("property fr.insee.pearljam.batch.folder.in is not defined in properties");
}
if (StringUtils.isBlank(FOLDER_OUT) || "${fr.insee.pearljam.folder.out}".equals(FOLDER_OUT)) {
throw new FolderException("property fr.insee.pearljam.batch.folder.out is not defined in properties");
}
logger.log(Level.INFO, "Folder properties are OK");

// Check database

pilotageDBService.checkDatabaseAccess();
logger.log(Level.INFO, "Database is OK");
}

/**
* Check folder tree : folders defined in properties exist or not. If folder not
* exist, folder is created
*
* @throws FolderException
*/
public static void checkFolderTree() throws FolderException {
PathUtils.createMissingFolder(FOLDER_IN);
PathUtils.createMissingFolder(FOLDER_IN + "/processing");
PathUtils.createMissingFolder(FOLDER_IN + "/sample");
PathUtils.createMissingFolder(FOLDER_IN + "/campaign");
PathUtils.createMissingFolder(FOLDER_OUT);
PathUtils.createMissingFolder(FOLDER_OUT + "/sample");
PathUtils.createMissingFolder(FOLDER_OUT + "/campaign");
PathUtils.createMissingFolder(FOLDER_OUT + "/synchro");
}


/**
* run batch : Check if argument is well fielded ant start to run the batch
* @param options arguments define on cmd execution
* @return BatchErrorCode of batch execution
* @throws ArgumentException
* @throws ValidateException
* @throws BatchException
* @throws IOException
* @throws SQLException
* @throws XMLStreamException
* @throws FolderException
*/
public static BatchErrorCode runBatch(String[] options)
throws ArgumentException, ValidateException, BatchException, IOException, SQLException, XMLStreamException, FolderException {
if (options.length == 0) {
throw new ArgumentException(
"No batch type found in parameter, you must choose between [DELETECAMPAIGN] || [EXTRACT] || [LOADCONTEXT] || [DAILYUPDATE] || [SYNCHRONIZE] || [SAMPLEPROCESSING]");
}
BatchOption batchOption = null;
try {
batchOption = BatchOption.valueOf(options[0].trim());
} catch (Exception e) {
throw new ArgumentException("Batch type [" + options[0].trim()
+ "] does not exist, you must choose between [DELETECAMPAIGN] || [EXTRACT] || [LOADCONTEXT] || [DAILYUPDATE] || [SYNCHRONIZE] || [SAMPLEPROCESSING]");
}
logger.log(Level.INFO, "Batch is running with option {}", batchOption.getLabel());
switch(batchOption) {
case DAILYUPDATE:
triggerService = context.getBean(TriggerService.class);
return triggerService.updateStates();
case SYNCHRONIZE:
logger.log(Level.INFO, "Running synchronization with context referential");
triggerService = context.getBean(TriggerService.class);
return triggerService.synchronizeWithOpale(FOLDER_OUT);
default:
return pilotageLauncherService.validateLoadClean(batchOption, FOLDER_IN, FOLDER_OUT);
}


}
@Component
public class Launcher {
/**
* The in folder used to input batch file
*/
@Value("${fr.insee.pearljam.folder.in}")
private String FOLDER_IN;
/**
* The out folder used to store logs, treated file and output files
*/
@Value("${fr.insee.pearljam.folder.out}")
private String FOLDER_OUT;


@Autowired
private PilotageDBService pilotageDBService;

@Autowired
private PilotageLauncherService pilotageLauncherService;

@Autowired
private TriggerService triggerService;

@Autowired
private CommunicationService communicationService;


/**
* The class logger
*/
private static final Logger logger = LogManager.getLogger(Launcher.class);

public static void main(String[] args) throws IOException, ValidateException, SQLException, XMLStreamException {
// Spring context initialization
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ApplicationContext.class);
Launcher launcher = context.getBean(Launcher.class);
launcher.run(args);
}

public void run(String[] args) throws SQLException {
BatchErrorCode batchErrorCode = BatchErrorCode.OK;
try {
initBatch();
checkFolderTree();
batchErrorCode = runBatch(args);
} catch (ArgumentException | FolderException | IOException | SQLException | DataBaseException te) {
logger.error(te.getMessage(), te);
batchErrorCode = BatchErrorCode.KO_TECHNICAL_ERROR;
} catch (BatchException | XMLStreamException | ValidateException fe) {
logger.error(fe.getMessage(), fe);
batchErrorCode = BatchErrorCode.KO_FONCTIONAL_ERROR;
} finally {
logger.info(Constants.MSG_RETURN_CODE, batchErrorCode);
pilotageDBService.closeConnection();
}
System.exit(batchErrorCode.getCode());
}

/**
* Init Batch check all prerequisities before run batch : folder properties and
* database structure
*
* @throws FolderException e
* @throws SQLException e
* @throws DataBaseException e
*/
public void initBatch() throws FolderException, DataBaseException, SQLException {
// Check folder properties
if (StringUtils.isBlank(FOLDER_IN) || "${fr.insee.pearljam.folder.in}".equals(FOLDER_IN)) {
throw new FolderException("property fr.insee.pearljam.batch.folder.in is not defined in properties");
}
if (StringUtils.isBlank(FOLDER_OUT) || "${fr.insee.pearljam.folder.out}".equals(FOLDER_OUT)) {
throw new FolderException("property fr.insee.pearljam.batch.folder.out is not defined in properties");
}
logger.log(Level.INFO, "Folder properties are OK");

// Check database

pilotageDBService.checkDatabaseAccess();
logger.log(Level.INFO, "Database is OK");
}

/**
* Check folder tree : folders defined in properties exist or not. If folder not
* exist, folder is created
*
* @throws FolderException when I/O exception is thrown
*/
public void checkFolderTree() throws FolderException {
PathUtils.createMissingFolder(FOLDER_IN);
PathUtils.createMissingFolder(FOLDER_IN + "/processing");
PathUtils.createMissingFolder(FOLDER_IN + "/sample");
PathUtils.createMissingFolder(FOLDER_IN + "/campaign");
PathUtils.createMissingFolder(FOLDER_OUT);
PathUtils.createMissingFolder(FOLDER_OUT + "/sample");
PathUtils.createMissingFolder(FOLDER_OUT + "/campaign");
PathUtils.createMissingFolder(FOLDER_OUT + "/synchro");
PathUtils.createMissingFolder(FOLDER_OUT + "/communication");
PathUtils.createMissingFolder(FOLDER_OUT + "/communication/success");
PathUtils.createMissingFolder(FOLDER_OUT + "/communication/fail");
}


/**
* run batch : Check if argument is well fielded ant start to run the batch
*
* @param options arguments define on cmd execution
* @return BatchErrorCode of batch execution
* @throws ArgumentException e
* @throws ValidateException e
* @throws BatchException e
* @throws IOException e
* @throws SQLException e
* @throws XMLStreamException e
* @throws FolderException e
*/
public BatchErrorCode runBatch(String[] options)
throws ArgumentException, ValidateException, BatchException, IOException, SQLException,
XMLStreamException, FolderException {
if (options.length == 0) {
throw new ArgumentException(
"No batch type found in parameter, you must choose between [DELETECAMPAIGN] || [EXTRACT] || " +
"[LOADCONTEXT] || [DAILYUPDATE] || [SYNCHRONIZE] || [SAMPLEPROCESSING] || [COMMUNICATION]");
}
BatchOption batchOption;
try {
batchOption = BatchOption.valueOf(options[0].trim());
} catch (Exception e) {
throw new ArgumentException("Batch type [" + options[0].trim()
+ "] does not exist, you must choose between [DELETECAMPAIGN] || [EXTRACT] || [LOADCONTEXT] || " +
"[DAILYUPDATE] || [SYNCHRONIZE] || [SAMPLEPROCESSING] || [COMMUNICATION]");
}
logger.log(Level.INFO, "Batch is running with option {}", batchOption.getLabel());

return switch (batchOption) {
case DAILYUPDATE -> triggerService.updateStates();
case SYNCHRONIZE -> triggerService.synchronizeWithOpale(FOLDER_OUT);
case COMMUNICATION -> {
try {
yield communicationService.handleCommunications();
} catch (SynchronizationException | MissingCommunicationException e) {
yield BatchErrorCode.KO_TECHNICAL_ERROR;
}
}
default -> pilotageLauncherService.validateLoadClean(batchOption, FOLDER_IN, FOLDER_OUT);
};


}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package fr.insee.pearljam.batch.communication;

import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.util.List;

@Getter
@Setter
@NoArgsConstructor
public class CommunicationData {

private long communicationTemplateId;

private String bddL1;
private String bddL2;
private String bddL3;
private String bddL4;
private String bddL5;
private String bddL6;
private String bddL7;

/// vu dans xsd_ds_ouvertureFAFCOLEM.xsd
private String editionDate; //Ue_DateEdition
private String mailAssistance; //Ue_MailAssistance
private String telAssistance; //Ue_TelAssistance

private String interviewerTitle; //Ue_CiviliteEnqueteur
private String interviewerLastName; //Ue_NomEnqueteur
private String interviewerFirstName; //Ue_PrenomEnqueteur
private String interviewerEmail; //Ue_MailEnqueteur
private String interviewerTel; //Ue_TelEnqueteur

private String reminderReason; // Ue_TypeRelance

private String recipientPostCode; //CodePostalDestinataire
private String communicationRequestId; //BddIdentifiantUniteEnquetee /!\ max 14 chars

private List<Metadata> templateMetadata;

}
Loading
Loading