--- /dev/null
+/*******************************************************************************
+ * Copyright
+ * 2012 axYus - www.axyus.com
+ * 2012 P.Renier - paul.renier@axyus.com
+ *
+ * This file is part of XEMELIOS.
+ *
+ * XEMELIOS is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * XEMELIOS is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with XEMELIOS; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ *******************************************************************************/
+package fr.gouv.finances.dgfip.xemelios.controls.connector.adapters;
+
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.util.Hashtable;
+import java.util.Map;
+import java.util.zip.ZipEntry;
+import java.util.zip.ZipInputStream;
+
+import javax.xml.namespace.NamespaceContext;
+
+import org.apache.log4j.Logger;
+
+import fr.gouv.finances.dgfip.utils.Pair;
+import fr.gouv.finances.dgfip.xemelios.auth.UnauthorizedException;
+import fr.gouv.finances.dgfip.xemelios.common.config.DocumentModel;
+import fr.gouv.finances.dgfip.xemelios.common.config.DocumentsModel;
+import fr.gouv.finances.dgfip.xemelios.common.config.Loader;
+import fr.gouv.finances.dgfip.xemelios.controls.connector.ControlConnector;
+import fr.gouv.finances.dgfip.xemelios.controls.connector.InitializationException;
+import fr.gouv.finances.dgfip.xemelios.controls.connector.ParameterNeededException;
+import fr.gouv.finances.dgfip.xemelios.controls.core.AbstractControlContentHandler;
+import fr.gouv.finances.dgfip.xemelios.controls.core.ControlParser;
+import fr.gouv.finances.dgfip.xemelios.controls.core.ControlProgressListener;
+import fr.gouv.finances.dgfip.xemelios.controls.core.MainControl;
+import fr.gouv.finances.dgfip.xemelios.controls.core.models.ControlModelTech;
+import fr.gouv.finances.dgfip.xemelios.controls.core.models.ControlsModel;
+import fr.gouv.finances.dgfip.xemelios.controls.core.models.DocumentControlModel;
+import fr.gouv.finances.dgfip.xemelios.controls.core.models.ParamModel;
+
+/**
+ * @author paul.renier
+ *
+ */
+public class PesControlConnector implements ControlConnector {
+
+ private static Logger logger = Logger.getLogger(PesControlConnector.class);
+
+ private static final String PES_DOC_ID = "pes-aller";
+ private static final String PES_CONFIG_PATH = "/pes-config.zip";
+
+ private MainControl xemeliosControlEngine;
+ private ControlProgressListener ctrlPrgListener;
+
+ public PesControlConnector() {
+
+ }
+
+ protected void extractPesConfiguration(File confFolder) throws Exception {
+ // unzip de la conf PES
+ ZipInputStream confZipIn = new ZipInputStream(getClass().getResourceAsStream(PES_CONFIG_PATH));
+ try {
+ confFolder.mkdirs();
+ ZipEntry entry;
+ while ((entry = confZipIn.getNextEntry()) != null) {
+ File f = new File(confFolder, entry.getName());
+
+ if (entry.isDirectory()) {
+ f.mkdirs();
+ } else {
+ f.getParentFile().mkdirs();
+
+ OutputStream fOut = new FileOutputStream(f);
+ try {
+ byte[] buffer = new byte[1024];
+ int len = 0;
+ while ((len = confZipIn.read(buffer)) > 0) {
+ fOut.write(buffer, 0, len);
+ }
+ } finally {
+ fOut.close();
+ }
+ }
+
+ confZipIn.closeEntry();
+ }
+ } finally {
+ confZipIn.close();
+ }
+ }
+
+ protected DocumentModel loadDocumentModel(File confFolder) throws Exception {
+ DocumentsModel docModels = Loader.getDocumentsInfos(confFolder.getAbsolutePath());
+ return docModels.getDocumentById(PES_DOC_ID);
+ }
+
+ protected ControlsModel loadControlsModel(File confFolder, DocumentModel documentModel) throws Exception {
+ ControlParser cp = new ControlParser();
+ cp.parse(new File(documentModel.getBaseDirectory(), documentModel.getControlConfigFile()));
+ ControlsModel c = (ControlsModel) cp.getMarshallable();
+ c.validate();
+ return c;
+ }
+
+ @Override
+ public void initialize(File tempFolder) throws InitializationException {
+ final DocumentModel documentModel;
+ final DocumentControlModel documentCtrlModel;
+ File schemaLocation = null;
+
+ logger.debug("Initializing PesosControlConnector");
+
+ // -------- Récupération du documentModel ----------
+ try {
+ extractPesConfiguration(tempFolder);
+ logger.debug("PES configuration extracted");
+
+ documentModel = loadDocumentModel(tempFolder);
+ if (documentModel == null) {
+ throw new InitializationException("Unable to load configuration from : " + tempFolder.getAbsolutePath());
+ }
+ logger.debug("PES configuration loaded");
+
+ ControlsModel controlsModel = loadControlsModel(tempFolder, documentModel);
+ documentCtrlModel = controlsModel.getDocumentById(documentModel.getId());
+ if (documentCtrlModel == null) {
+ throw new InitializationException("Unable to resolve control model");
+ }
+ logger.debug("PES controls loaded");
+
+ // ------ Schéma complémentaire --------
+ ControlModelTech tech03 = documentCtrlModel.getControlTechById("TECH03");
+ if (tech03 != null) {
+ schemaLocation = new File(documentModel.getBaseDirectory(), tech03.getParamTechById("schemaLocation").getValue());
+ }
+ } catch (InitializationException e) {
+ logger.error(e, e);
+ throw e;
+ } catch (Exception e) {
+ logger.error(e, e);
+ throw new InitializationException(e);
+ }
+
+ // --------- Instantiation du moteur de control --------
+ xemeliosControlEngine = new MainControl(documentModel, documentModel.getNamespaces(), documentCtrlModel, schemaLocation) {
+ @Override
+ public AbstractControlContentHandler getControlContentHandler(String documentModelId, DocumentControlModel docCtrlMod, Hashtable<String, Object> parameters, String file, ControlProgressListener cpl) {
+ return new ControlContentHandler(documentModelId, documentModel.getNamespaces(), docCtrlMod, parameters, file, cpl);
+ }
+ };
+
+ logger.debug("PesosControlConnector initialization complete");
+ }
+
+ @Override
+ public ControlResult processControl(File xmlIn, File xmlOut, File reportOut, Map<String, Object> parameters) throws IOException, ParameterNeededException {
+ // vérification des paramètres obligatoire
+ DocumentControlModel dc = xemeliosControlEngine.getDocumentControl();
+ for (ParamModel pm : dc.getParams()) {
+ if (!pm.isOptional() && !parameters.containsKey(pm.getId())) {
+ throw new ParameterNeededException(pm.getId(), pm.getName());
+ }
+ }
+
+ xemeliosControlEngine.setControlProgressListener(ctrlPrgListener);
+ xemeliosControlEngine.setParameters(new Hashtable<String, Object>(parameters));
+ xemeliosControlEngine.setFileToControl(xmlIn);
+ xemeliosControlEngine.setFichierEnrichiLocation(xmlOut.getAbsolutePath());
+ xemeliosControlEngine.setRapportLocation(reportOut.getAbsolutePath());
+
+ // lancement du control
+ xemeliosControlEngine.run();
+
+ // --------- Résultat ---------
+ int ctrlStatus = xemeliosControlEngine.getControlStatus();
+ switch (ctrlStatus) {
+ case MainControl.STATUS_CONTROL_NO_ERROR:
+ return ControlResult.VALID;
+ case MainControl.STATUS_HAS_UNIT_CONTROL_ERRORS:
+ return ControlResult.CONTROL_ERROR;
+ case MainControl.STATUS_INVALID_ENCODING:
+ return ControlResult.INVALID_ENCODING;
+ case MainControl.STATUS_NOT_CONTROLLED:
+ return ControlResult.NOT_CONTROLLED;
+ case MainControl.STATUS_NOT_SCHEMA_VALID:
+ return ControlResult.INVALID_SCHEMA;
+ case MainControl.STATUS_NOT_XML:
+ return ControlResult.NOT_XML;
+ default:
+ // normalement ce cas ne devrait jamais arriver ;-)
+ throw new RuntimeException("Control status unknow : " + ctrlStatus);
+ }
+ }
+
+ @Override
+ public void setControlProgressListener(ControlProgressListener ctrlPrgListener) {
+ this.ctrlPrgListener = ctrlPrgListener;
+ }
+
+ @Override
+ public ControlProgressListener getControlProgressListener() {
+ return this.ctrlPrgListener;
+ }
+
+ private static class ControlContentHandler extends AbstractControlContentHandler {
+ public ControlContentHandler(String documentId, NamespaceContext nsCtx, DocumentControlModel docCtrlMod, Hashtable<String, Object> parameters, String file, ControlProgressListener cpl) {
+ super(documentId, nsCtx, docCtrlMod, parameters, file, cpl);
+ }
+
+ @Override
+ public Pair getCollectivite(String documentModelId, String fileName) throws Exception {
+ return new Pair("00000000000000", "dummy");
+ }
+
+ @Override
+ public Pair getBudget(String documentModelId, Pair collectivite, String fileName) throws Exception, UnauthorizedException {
+ return new Pair("00", "dummy");
+
+ }
+ }
+
+}