+++ /dev/null
-/**
- *
- */
-package fr.mbdx.service;
-
-import java.io.ByteArrayOutputStream;
-import java.io.IOException;
-import java.io.InputStream;
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-import java.util.zip.ZipOutputStream;
-
-import javax.xml.bind.JAXBException;
-
-import org.activiti.engine.delegate.DelegateExecution;
-import org.activiti.engine.impl.util.json.JSONException;
-import org.activiti.engine.impl.util.json.JSONObject;
-import org.alfresco.cmis.client.AlfrescoDocument;
-import org.apache.chemistry.opencmis.client.api.CmisObject;
-import org.apache.chemistry.opencmis.client.api.Folder;
-import org.apache.chemistry.opencmis.client.api.Property;
-import org.apache.chemistry.opencmis.commons.data.ContentStream;
-import org.apache.chemistry.opencmis.commons.impl.Base64;
-import org.apache.log4j.Logger;
-
-import fr.mbdx.ixbus.client.Attachment;
-import fr.mbdx.ixbus.client.ClientException;
-import fr.mbdx.ixbus.client.impl.AttachmentCfxImpl;
-import fr.mbdx.ixbus.client.impl.Utils;
-
-/**
- * Intégration AirsDelib
- */
-public class ServiceDelib {
-
- protected Logger log = Logger.getLogger(getClass());
-
- private ServiceRestAlfresco serviceRestAlfresco;
- private ServiceCMIS serviceCMIS;
- private Utils utils;
- private String activitiHost;
- private String activitiPort;
- private String activitiProtocol;
-
- // Envoi de l'original au TDT ou non
- private boolean sendOriginalToTDT;
-
- /**
- * Crée une pièce jointe iXbus à partir du contenu d'un répertoire de
- * délibérations
- *
- * @param id
- * @return
- * @throws ClientException
- * @throws IOException
- * @throws JAXBException
- */
- public Attachment createPjFromFolderId(String id) throws ClientException,
- IOException, JAXBException {
- Attachment attach = new AttachmentCfxImpl();
- byte[] bytes = null;
- ByteArrayOutputStream baos = null;
- ZipOutputStream zos = null;
- List<String> annexes = new ArrayList<String>();
- serviceCMIS.createSession();
-
- try {
- baos = new ByteArrayOutputStream();
- zos = new ZipOutputStream(baos);
-
- Folder folder = serviceCMIS.getFolder(id);
- String foldername = folder.getName();
- List<CmisObject> list = serviceCMIS
- .getChildren(folder, true, false);
- for (CmisObject obj : list) {
- AlfrescoDocument doc = (AlfrescoDocument) obj;
- Property<?> envoiTdTProp = doc.getProperty("mdbx:envoiTdT");
- if (envoiTdTProp != null) {
- boolean envoiTdT = (Boolean) envoiTdTProp.getValue();
- if (envoiTdT) {
- if (sendOriginalToTDT) {
- String refArchiveRm = (String) doc.getProperty(
- "mbdx:refArchiveRmString").getValue();
- doc = serviceCMIS.getDocument(refArchiveRm);
- }
- ContentStream contentStream = doc.getContentStream();
- InputStream is = contentStream.getStream();
- String filename = contentStream.getFileName();
- String uniqueFileName = utils
- .getUniqueFileName(filename);
- log.debug("Création pièce jointe " + uniqueFileName);
- utils.putEntryIntoZipOutputStream(zos, is,
- uniqueFileName);
- annexes.add(uniqueFileName);
- }
- }
- }
-
- if (annexes.size() > 0) {
- String mainDocument = annexes.get(0);
- annexes.remove(0);
- String attachmentDescription = utils
- .createAttachmentDescription(mainDocument, annexes);
- zos.setComment(attachmentDescription);
- }
-
- zos.finish();
-
- bytes = baos.toByteArray();
- attach.setContent(bytes);
- attach.setSize(bytes.length);
- attach.setName("DELIB_" + foldername + ".pj");
- } finally {
- if (baos != null) {
- baos.close();
- }
- if (zos != null) {
- zos.close();
- }
- }
-
- return attach;
- }
-
- /**
- * Met à jour les métadonnées sur l'ensemble des documents d'un dossier de
- * délibérations suite à la réception de l'A/R TDT
- *
- * @param id
- * Identifiant Alfresco du dossier
- * @param prop
- */
- public void updateTdTProperties(String id, Map<String, Object> prop) {
- Folder folder = serviceCMIS.getFolder(id);
- List<CmisObject> list = serviceCMIS.getChildren(folder, true, false);
- for (CmisObject obj : list) {
- AlfrescoDocument doc = (AlfrescoDocument) obj;
- Property<?> envoiTdTProp = doc.getProperty("mdbx:envoiTdT");
- if (envoiTdTProp != null) {
- boolean envoiTdT = (Boolean) envoiTdTProp.getValue();
- if (envoiTdT) {
- doc.addAspect("P:mbdx:accuseReceptionTdT", prop);
- }
- }
- }
- }
-
- /**
- * Initialise dans le fil d'exécution certaines variables spécifiques aux
- * délibérations -> actID, actNature, matiere et subject
- *
- * @param execution
- */
- public void setMandatoryVariables(DelegateExecution execution,
- AlfrescoDocument doc) {
- execution.setVariable("actID", doc.getId());
- execution.setVariable("actNature", "1");
- execution.setVariable("matiere", getMatiere(doc));
- execution
- .setVariable("subject", doc.getProperty("cm:title").getValue());
- }
-
- /**
- * Retourne le code matière correspondant à la nomenclature document
- * attendue par la préfecture / TdT
- *
- * @param doc
- * @return
- */
- public String getMatiere(AlfrescoDocument doc) {
- String matiere = null;
- String req = "/wcs/api/metadata";
-
- Map<String, String> param = new HashMap<String, String>();
- param.put("nodeRef", doc.getId());
-
- JSONObject json = serviceRestAlfresco.get(req, param);
- try {
- // Récupération de la valeur (noderef) de la métadonnée "matière" du
- // document délib courant
- JSONObject properties = json.getJSONObject("properties");
- String refMatiere = properties
- .getString("{http://wwww.bordeaux.fr/model/mbdx}matiere");
-
- // Récupération du libellé de la matière à partir du noderef
- // précédent
- param.put("nodeRef", refMatiere);
- json = serviceRestAlfresco.get(req, param);
- properties = json.getJSONObject("properties");
- matiere = properties
- .getString("{http://www.alfresco.org/model/content/1.0}name");
-
- // Extraction du code matière à partir du libellé complet. Exemple :
- // libelle = 1.1 xxx -> codeMatière = 1.1
- int endIndex = matiere.indexOf(" ");
- if (endIndex > 0) {
- matiere = matiere.substring(0, endIndex);
- if (matiere.endsWith(".")) {
- matiere = matiere.substring(0, endIndex - 1);
- }
- }
- } catch (JSONException e) {
- log.info("Interrogation de la matière du document " + doc.getName()
- + " -> Echec !!!!");
- log.error(e.getMessage(), e);
- throw e;
- }
-
- return matiere;
- }
-
- /**
- * Retourne un URL pointant directement vers la tâche affectée à
- * l'utilisateur en cours au sein d'Activiti Explorer Il a la forme suivante
- * : http://localhost:8180/activiti-explorer/ui/#tasks/115034?category=inbox
- *
- * @param execution
- * @param taskId
- * @return
- */
- public String getUserTaskUrl(DelegateExecution execution) {
- String port = activitiPort.equals("80") ? "" : ":" + activitiPort;
- String url = activitiProtocol + "://" + activitiHost + port
- + "/activiti-explorer/ui/#tasks/"
- + execution.getProcessInstanceId() + "?category=involved";
- return url;
- }
-
- public String encodeBytes(byte[] source) {
- return Base64.encodeBytes(source);
- }
-
- public byte[] decodeBytes(String source) throws IOException {
- return Base64.decode(source);
- }
-
- /**
- * @param serviceCMIS
- * the serviceCMIS to set
- */
- public void setServiceCMIS(ServiceCMIS serviceCMIS) {
- this.serviceCMIS = serviceCMIS;
- }
-
- /**
- * @return the sendOriginalToTDT
- */
- public boolean isSendOriginalToTDT() {
- return sendOriginalToTDT;
- }
-
- /**
- * @param sendOriginalToTDT
- * the sendOriginalToTDT to set
- */
- public void setSendOriginalToTDT(boolean sendOriginalToTDT) {
- this.sendOriginalToTDT = sendOriginalToTDT;
- }
-
- /**
- * @param utils
- * the utils to set
- */
- public void setUtils(Utils utils) {
- this.utils = utils;
- }
-
- /**
- * @param serviceRestAlfresco
- * the serviceRestAlfresco to set
- */
- public void setServiceRestAlfresco(ServiceRestAlfresco serviceRestAlfresco) {
- this.serviceRestAlfresco = serviceRestAlfresco;
- }
-
- /**
- * @param activitiHost
- * the activitiHost to set
- */
- public void setActivitiHost(String activitiHost) {
- this.activitiHost = activitiHost;
- }
-
- /**
- * @param activitiPort
- * the activitiPort to set
- */
- public void setActivitiPort(String activitiPort) {
- this.activitiPort = activitiPort;
- }
-
- /**
- * @param activitiProtocol
- * the activitiProtocol to set
- */
- public void setActivitiProtocol(String activitiProtocol) {
- this.activitiProtocol = activitiProtocol;
- }
-}