package com.ejie.ad74a.ws.client.platea.service;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.exception.ExceptionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ejie.ad74a.dao.MaestroTiposDocDao;
import com.ejie.ad74a.exceptions.ManagerErrors;
import com.ejie.ad74a.model.BandejaPapel0;
import com.ejie.ad74a.model.DocumentoPapel0;
import com.ejie.ad74a.model.DocumentosTarea;
import com.ejie.ad74a.model.Expedientes;
import com.ejie.ad74a.model.MaestroTiposDoc;
import com.ejie.ad74a.model.Solicitantes;
import com.ejie.ad74a.service.interoperabilidad.InteroperabilidadService;
import com.ejie.ad74a.util.comun.UtilSeguridad;
import com.ejie.ad74a.util.comun.UtilidadesFecha;
import com.ejie.ad74a.util.constantes.Constantes;
import com.ejie.ad74a.util.constantes.ProcedimientosEnum;
import com.ejie.ad74a.ws.client.platea.expedienteElectronico.TFolderList;
import com.ejie.ad74a.ws.client.platea.expedienteElectronico.TFolderSearchType;
import com.ejie.ad74a.ws.client.platea.expedienteElectronico.TFolderStoredFilter;
import com.ejie.ad74a.ws.client.platea.expedienteElectronico.TFolderStoredStatus;
import com.ejie.ad74a.ws.client.platea.expedienteElectronico.TFolderStoredStatusList;
import com.ejie.ad74a.ws.client.platea.expedienteElectronico.TFolderStoredStatusResultList;
import com.ejie.ad74a.ws.client.platea.expedienteElectronico.TParticipantRRI;
import com.ejie.ad74a.ws.client.platea.expedienteElectronico.TStoreDocumentList;
import com.ejie.ad74a.ws.client.platea.expedienteElectronico.X43REEPortType;
import com.ejie.ad74a.ws.client.platea.notificaciones.TAttachmentSend;
import com.ejie.ad74a.ws.client.platea.notificaciones.X43NOTPortType;
import com.ejie.ad74a.ws.client.platea.procedimientos.TProcedureList;
import com.ejie.ad74a.ws.client.platea.procedimientos.X43FCPPortType;
import com.ejie.ad74a.ws.client.platea.regRepresentantes.X43AWebService;
import com.ejie.ad74a.ws.client.platea.solAportacion.X43PPSPortType;
import com.ejie.ad74a.ws.client.platea.tareasTramitacion.TKeyValue;
import com.ejie.ad74a.ws.client.platea.tareasTramitacion.TMessage;
import com.ejie.ad74a.ws.client.platea.tareasTramitacion.TMessageFilter;
import com.ejie.ad74a.ws.client.platea.tareasTramitacion.TWaitFilter;
import com.ejie.ad74a.ws.client.platea.tareasTramitacion.X43RTAPortType;
import com.ejie.ad74a.ws.client.platea.toolkit.X43KWebService;

@Service(value = "PlateaService")
public class PlateaServiceImpl implements PlateaService {

	/**
	 * Final static logger.
	 */
	private static final Logger logger = LoggerFactory.getLogger(PlateaServiceImpl.class);

	@Autowired
	private X43KWebService x43KWebService;

	@Autowired
	private X43AWebService x43aWebServices;

	@Autowired
	private X43RTAPortType x43RTAPortType;

	@Autowired
	private X43REEPortType x43REEPortType;

	@Autowired
	private X43FCPPortType x43FCPPortType;

	@Autowired
	private X43PPSPortType x43Solicitud;

	@Autowired
	private X43NOTPortType x43notPortType;

	@Autowired
	private MaestroTiposDocDao maestroTiposDocDao;

	@Autowired()
	private InteroperabilidadService interoperabilidadService;

	/**
	 * Clase que adaptara los servicios de Dokusi a nuestro sistema
	 */
	private PlateaServiceImpl() {
		super();
	}

	/**
	 * INICIO TOOLKIT X43KWebService com.ejie.ad74a.ws.client.platea.toolkit
	 **/

	@Override
	public String getRequestInformation(String idSolicitud, String folderId) {
		PlateaServiceImpl.logger.info("[PlateaServiceImpl] - getRequestInformation Inicio");
		String result = "";
		try {
			result = this.x43KWebService.getRequestInformation(idSolicitud, folderId, null, null);
		} catch (Exception e) {
			ManagerErrors.manageException(e, "Error al recuperar el ejgv document: " + ExceptionUtils.getStackTrace(e));
		}
		PlateaServiceImpl.logger.info("[PlateaServiceImpl] - getRequestInformation Fin: " + result);
		return result;
	}

	/**
	 * FIN TOOLKIT X43KWebService com.ejie.ad74a.ws.client.platea.toolkit
	 * 
	 **/

	@Override
	public boolean validateRepresentative(String idTitular, String idRepresentante) {
		PlateaServiceImpl.logger.info("[PlateaServiceImpl] - validateRepresentative Inicio idTitular: " + idTitular
				+ " - idRepresentante: " + idRepresentante);
		boolean result = false;

		try {
			result = this.x43aWebServices.isValidRequestRepresentation(idTitular, null, idRepresentante, null,
					Constantes.RegRepresentantes.TITULARIDAD_REPRESENTANTE,
					Constantes.RegRepresentantes.TIPO_ACT_SOLICITUDES, ProcedimientosEnum.ALTA.getId());
		} catch (Exception e) {
			ManagerErrors.manageException(e, "Error en validateRepresentative: " + ExceptionUtils.getStackTrace(e));
		}
		PlateaServiceImpl.logger.info("[PlateaServiceImpl] - validateRepresentative Fin: " + result);
		return result;
	}

	@Override
	public com.ejie.ad74a.ws.client.platea.tareasTramitacion.TTask prepareTask(String taskGUID,
			com.ejie.ad74a.ws.client.platea.tareasTramitacion.TTaskType taskType) {
		PlateaServiceImpl.logger.info("[PlateaServiceImpl] - prepareTask Inicio - taskGUID: " + taskGUID);
		com.ejie.ad74a.ws.client.platea.tareasTramitacion.AExtensionPoint ep = new com.ejie.ad74a.ws.client.platea.tareasTramitacion.AExtensionPoint();
		return this.x43RTAPortType.prepareTask(taskType, taskGUID, ep);

	}

	@Override
	public com.ejie.ad74a.ws.client.platea.tareasTramitacion.TMessageList getGATTask(String folderID,
			com.ejie.ad74a.ws.client.platea.tareasTramitacion.TMessageStatus estado,
			com.ejie.ad74a.ws.client.platea.tareasTramitacion.TMessageTipology tipologia,
			com.ejie.ad74a.ws.client.platea.tareasTramitacion.TMessageFilterValue... filters) {

		PlateaServiceImpl.logger.info("[PlateaServiceImpl] - getGATTask Inicio - folderID: {}", folderID);
		com.ejie.ad74a.ws.client.platea.tareasTramitacion.AExtensionPoint ep = new com.ejie.ad74a.ws.client.platea.tareasTramitacion.AExtensionPoint();
		com.ejie.ad74a.ws.client.platea.tareasTramitacion.TStringList groupIDList = new com.ejie.ad74a.ws.client.platea.tareasTramitacion.TStringList();
		com.ejie.ad74a.ws.client.platea.tareasTramitacion.TMessageTipologyList messageTipologyList = new com.ejie.ad74a.ws.client.platea.tareasTramitacion.TMessageTipologyList();
		com.ejie.ad74a.ws.client.platea.tareasTramitacion.TMessageStatusList statusList = new com.ejie.ad74a.ws.client.platea.tareasTramitacion.TMessageStatusList();
		com.ejie.ad74a.ws.client.platea.tareasTramitacion.TMessageFilterValueList filterList = new com.ejie.ad74a.ws.client.platea.tareasTramitacion.TMessageFilterValueList();
		com.ejie.ad74a.ws.client.platea.tareasTramitacion.TMessageList resultList = new com.ejie.ad74a.ws.client.platea.tareasTramitacion.TMessageList();

		try {
			// Se agrega tipología de las tareas
			messageTipologyList.getMessageTipology().add(tipologia);
			// Siempre se agrega filtro por folderId
			com.ejie.ad74a.ws.client.platea.tareasTramitacion.TMessageFilterValue valueFilter = new com.ejie.ad74a.ws.client.platea.tareasTramitacion.TMessageFilterValue();
			valueFilter.setValue(folderID);
			valueFilter.setKey(TMessageFilter.FOLDERID);
			filterList.getItem().add(valueFilter);
			if (!ArrayUtils.isEmpty(filters)) {
				// Se agrega resto de filtros opcionales que se hayan indicado
				for (com.ejie.ad74a.ws.client.platea.tareasTramitacion.TMessageFilterValue filterValue : filters) {
					filterList.getItem().add(filterValue);
				}
			}
			// Se agrega estado de las tareas
			statusList.getMessageStatus().add(estado);
			resultList = this.x43RTAPortType.getGATTasks(groupIDList, messageTipologyList, statusList, filterList, null,
					null, null, ep);
		} catch (Exception e) {
			ManagerErrors.manageException(e);
		}

		return resultList;
	}

	@Override
	public String getMessageParameter(TMessage tMessage, String key) {
		String value = null;
		if (tMessage != null && CollectionUtils.isNotEmpty(tMessage.getMessage().getParameter())) {
			for (TKeyValue item : tMessage.getMessage().getParameter()) {
				if (item.getKey().equalsIgnoreCase(key)) {
					value = item.getValue();
					break;
				}
			}
		}
		return value;
	}

	@Override
	public com.ejie.ad74a.ws.client.platea.tareasTramitacion.TBoolean prepareNewTask(String procedureID,
			String folderID, String proceedingID, String taskID,
			com.ejie.ad74a.ws.client.platea.tareasTramitacion.TPersonIDName senderID,
			com.ejie.ad74a.ws.client.platea.tareasTramitacion.TStringList mailboxes) {

		PlateaServiceImpl.logger
				.info("[PlateaServiceImpl] - prepareNewTask Inicio - folderID: " + folderID + " - taskID: " + taskID);
		com.ejie.ad74a.ws.client.platea.tareasTramitacion.AExtensionPoint ep = new com.ejie.ad74a.ws.client.platea.tareasTramitacion.AExtensionPoint();
		return this.x43RTAPortType.prepareNewTask(procedureID, folderID, proceedingID, taskID, senderID, mailboxes, ep);
	}

	@Override
	public com.ejie.ad74a.ws.client.platea.tareasTramitacion.TTask doTask(
			com.ejie.ad74a.ws.client.platea.tareasTramitacion.TTaskType taskType,
			com.ejie.ad74a.ws.client.platea.tareasTramitacion.TTask task) {

		PlateaServiceImpl.logger.info("[PlateaServiceImpl] - getGATTask Inicio - doTask ");
		com.ejie.ad74a.ws.client.platea.tareasTramitacion.AExtensionPoint ep = new com.ejie.ad74a.ws.client.platea.tareasTramitacion.AExtensionPoint();

		com.ejie.ad74a.ws.client.platea.tareasTramitacion.TTask result = this.x43RTAPortType.doTask(taskType, task, ep);

		return result;
	}

	@Override
	public boolean cancelarTarea(String taskGUID) {
		com.ejie.ad74a.ws.client.platea.tareasTramitacion.AExtensionPoint ep = new com.ejie.ad74a.ws.client.platea.tareasTramitacion.AExtensionPoint();
		com.ejie.ad74a.ws.client.platea.tareasTramitacion.TBoolean result = this.x43RTAPortType.cancelTask(taskGUID,
				ep);
		return Boolean.getBoolean(result.toString());
	}

	@Override
	public boolean actualizarInstructorExpediente(String folderID, String buzonInstructor) {

		com.ejie.ad74a.ws.client.platea.expedienteElectronico.AExtensionPoint ep = new com.ejie.ad74a.ws.client.platea.expedienteElectronico.AExtensionPoint();

		com.ejie.ad74a.ws.client.platea.expedienteElectronico.TStringList listaExptes = new com.ejie.ad74a.ws.client.platea.expedienteElectronico.TStringList();
		listaExptes.getItem().add(folderID);

		com.ejie.ad74a.ws.client.platea.expedienteElectronico.TBoolean resultado = this.x43REEPortType
				.updateFoldersInstructor(listaExptes, buzonInstructor, ep);

		return Boolean.getBoolean(resultado.toString());

	}

	@Override
	public com.ejie.ad74a.ws.client.platea.tareasTramitacion.TBoolean assignTasks(String taskGuid, String mailBoxId) {

		com.ejie.ad74a.ws.client.platea.tareasTramitacion.AExtensionPoint ep = new com.ejie.ad74a.ws.client.platea.tareasTramitacion.AExtensionPoint();
		com.ejie.ad74a.ws.client.platea.tareasTramitacion.TStringList taskListId = new com.ejie.ad74a.ws.client.platea.tareasTramitacion.TStringList();
		taskListId.getItem().add(0, taskGuid);
		return this.x43RTAPortType.assignTasks(taskListId, mailBoxId, ep);
	}

	@Override
	public com.ejie.ad74a.ws.client.platea.expedienteElectronico.TGetTelActionDocumentList getDocumentsTelAction(
			String folderID, String procedureID) {
		PlateaServiceImpl.logger.info("[PlateaServiceImpl] - getDocumentsTelAction Inicio");
		try {
			com.ejie.ad74a.ws.client.platea.expedienteElectronico.AExtensionPoint ep = new com.ejie.ad74a.ws.client.platea.expedienteElectronico.AExtensionPoint();

			com.ejie.ad74a.ws.client.platea.expedienteElectronico.TGetTelActionDocumentList listaDocument = this.x43REEPortType
					.getTelActionDocuments(folderID, procedureID, UtilSeguridad.getAuditUser(), ep);

			PlateaServiceImpl.logger.info("[PlateaServiceImpl] - getDocumentsTelAction Fin");
			return listaDocument;
		} catch (Throwable e) {
			ManagerErrors.manageException(e, "Error al buscar documentos por accion telematica");
		}
		return null;
	}

	@Override
	public com.ejie.ad74a.ws.client.platea.expedienteElectronico.TBoolean authorizationCall(String authorizationID,
			String proceedingID, com.ejie.ad74a.ws.client.platea.expedienteElectronico.TAuthSvt svt,
			com.ejie.ad74a.ws.client.platea.expedienteElectronico.TAuthSvt svtTer) {
		PlateaServiceImpl.logger.info("[PlateaServiceImpl] - authorizationCall");
		com.ejie.ad74a.ws.client.platea.expedienteElectronico.TBoolean result = null;
		try {
			com.ejie.ad74a.ws.client.platea.expedienteElectronico.AExtensionPoint ep = new com.ejie.ad74a.ws.client.platea.expedienteElectronico.AExtensionPoint();
			result = this.x43REEPortType.authorizationCall(authorizationID, proceedingID, svt, svtTer,
					com.ejie.ad74a.ws.client.platea.expedienteElectronico.TBoolean.TRUE, ep);

		} catch (Throwable e) {
			ManagerErrors.manageException(e, "Error al ejecutar la autorización sustitutiva de un documento");
		}

		return result;
	}

	@Override
	public com.ejie.ad74a.ws.client.platea.expedienteElectronico.TBoolean documentChangeRoleID(String oidDocument,
			String documentRoleID, com.ejie.ad74a.ws.client.platea.expedienteElectronico.TPersonIDName senderID,
			String procedureID) {
		PlateaServiceImpl.logger.info("[PlateaServiceImpl] - documentChangeRoleID - oidDocument: " + oidDocument
				+ " - documentRoleID: " + documentRoleID);
		com.ejie.ad74a.ws.client.platea.expedienteElectronico.TBoolean result = null;
		try {

			com.ejie.ad74a.ws.client.platea.expedienteElectronico.AExtensionPoint ep = new com.ejie.ad74a.ws.client.platea.expedienteElectronico.AExtensionPoint();
			// dokusiID, documentRoleID, senderID, metadataList, auditUser, procedureID, ep)
			result = this.x43REEPortType.documentChangeRoleID(oidDocument, documentRoleID, senderID, null,
					UtilSeguridad.getAuditUser(), procedureID, ep);

			return result;
		} catch (Throwable e) {
			ManagerErrors.manageException(e, "Error al cambiar el roleID de un documento");
		}

		return result;
	}

	@Override
	public com.ejie.ad74a.ws.client.platea.procedimientos.TDocumentRoleList getDocumentalRoles(String procedureID,
			String idActionTelem) {
		PlateaServiceImpl.logger.info("[PlateaServiceImpl] - getProcedure Inicio getDocumentalRoles: " + procedureID);
		com.ejie.ad74a.ws.client.platea.procedimientos.AExtensionPoint ep = new com.ejie.ad74a.ws.client.platea.procedimientos.AExtensionPoint();
		com.ejie.ad74a.ws.client.platea.procedimientos.TDocumentRoleList listRole = null;
		try {
//			listRole = this.x43FCPPortType.getDocumentalRoles(procedureID,
//					com.ejie.ad74a.ws.client.platea.procedimientos.TBoolean.FALSE, idActionTelem, null,
//					com.ejie.ad74a.ws.client.platea.procedimientos.TDocTypesFilter.FOR_PROCEDURE, ep);
			listRole = this.x43FCPPortType.getDocumentalRoles(procedureID,
					com.ejie.ad74a.ws.client.platea.procedimientos.TBoolean.FALSE, idActionTelem, null,
					com.ejie.ad74a.ws.client.platea.procedimientos.TDocTypesFilter.FOR_TELACTION, ep);
		} catch (Exception e) {
			ManagerErrors.manageException(e, "Error al recuperar la lista de roles por action telematic Platea");
		}
		PlateaServiceImpl.logger.info("[PlateaServiceImpl] - getDocumentalRoles Fin: " + listRole.toString());
		return listRole;
	}

	@Override
	public com.ejie.ad74a.ws.client.platea.solAportacion.TSubmissionResponse iniciarExpOficio(
			com.ejie.ad74a.ws.client.platea.solAportacion.TAdminInitRequest request) {

		PlateaServiceImpl.logger.info("[PlateaServiceImpl] - iniciarExpOficio Inicio");
		com.ejie.ad74a.ws.client.platea.solAportacion.TSubmissionResponse result = null;

		try {
			com.ejie.ad74a.ws.client.platea.solAportacion.AExtensionPoint ep = new com.ejie.ad74a.ws.client.platea.solAportacion.AExtensionPoint();
			result = this.x43Solicitud.addAdminApplication(request, ep);
		} catch (Exception e) {
			ManagerErrors.manageException(e,
					"Error al iniciar el expediente de oficio: " + ExceptionUtils.getStackTrace(e));
		}
		PlateaServiceImpl.logger.info("[PlateaServiceImpl] - iniciarExpOficio Fin: " + result.toString());
		return result;
	}

	@Override
	public com.ejie.ad74a.ws.client.platea.procedimientos.TProcedureList getProcedure(String procedureID) {
		PlateaServiceImpl.logger.info("[PlateaServiceImpl] - getProcedure Inicio procedureID: " + procedureID);
		com.ejie.ad74a.ws.client.platea.procedimientos.AExtensionPoint ep = new com.ejie.ad74a.ws.client.platea.procedimientos.AExtensionPoint();
		com.ejie.ad74a.ws.client.platea.procedimientos.TProcedureList proceduresResult = null;
		try {
			proceduresResult = this.x43FCPPortType.getProcedures(
					com.ejie.ad74a.ws.client.platea.procedimientos.TProcedureFilter.FOR_PROCEDURE, procedureID, null,
					null, null, ep);
		} catch (Exception e) {
			ManagerErrors.manageException(e, "Error al recuperar el procedimiento de Platea");
		}
		PlateaServiceImpl.logger.info("[PlateaServiceImpl] - getProcedure Fin: " + proceduresResult.toString());
		return proceduresResult;
	}

	@Override
	public com.ejie.ad74a.ws.client.platea.expedienteElectronico.TFolderList searchFolders(ArrayList<String> foldersID,
			com.ejie.ad74a.ws.client.platea.expedienteElectronico.TFolderSearchType tipoBusqueda) {
		PlateaServiceImpl.logger.info("[PlateaServiceImpl] - searchFolders");
		try {

			com.ejie.ad74a.ws.client.platea.expedienteElectronico.AExtensionPoint ep = new com.ejie.ad74a.ws.client.platea.expedienteElectronico.AExtensionPoint();

			// buscamos datos del expediente
			com.ejie.ad74a.ws.client.platea.expedienteElectronico.TFolderQuery folderQuery = new com.ejie.ad74a.ws.client.platea.expedienteElectronico.TFolderQuery();
			com.ejie.ad74a.ws.client.platea.expedienteElectronico.TFolderCompleteQuery complete = new com.ejie.ad74a.ws.client.platea.expedienteElectronico.TFolderCompleteQuery();
			com.ejie.ad74a.ws.client.platea.expedienteElectronico.TStringList folderIDList = new com.ejie.ad74a.ws.client.platea.expedienteElectronico.TStringList();
			com.ejie.ad74a.ws.client.platea.expedienteElectronico.TFolderSearchTypeList paramList = new com.ejie.ad74a.ws.client.platea.expedienteElectronico.TFolderSearchTypeList();

			folderIDList.getItem().addAll(foldersID);
			complete.setFolderIDList(folderIDList);
			paramList.getParam().add(tipoBusqueda);
			complete.setParamList(paramList);
			complete.setAuditUser(UtilSeguridad.getAuditUser());
			complete.setInterestedMode(com.ejie.ad74a.ws.client.platea.expedienteElectronico.TBoolean.TRUE);
			folderQuery.setComplete(complete);
			com.ejie.ad74a.ws.client.platea.expedienteElectronico.TFolderList listFolders = this.x43REEPortType
					.searchFolders(folderQuery, ep);

			return listFolders;
		} catch (Throwable e) {
			ManagerErrors.manageException(e, "Error al buscar expedientes");
		}

		return new com.ejie.ad74a.ws.client.platea.expedienteElectronico.TFolderList();
	}

	@Override
	public String aperturaSolicitud(Expedientes expediente, List<DocumentosTarea> listaDocumentos) {

		PlateaServiceImpl.logger.info("[PlateaServiceImpl] - aperturaSolicitud ");
		com.ejie.ad74a.ws.client.platea.solAportacion.TSubmissionRequest solicitud = null;

		// recuperamos los documentos de interoperabilidad
		MaestroTiposDoc tiposDoc = new MaestroTiposDoc();
		tiposDoc.setOrigen36(Constantes.OrigenTiposDocumento.ORIGEN_TIPO_DOC_OTROS_ORGANOS);
		List<MaestroTiposDoc> listDocInterop = this.maestroTiposDocDao.findAll(tiposDoc, null);
		try {
			solicitud = Papel0Utils.createSolicitudPapel0(expediente, listaDocumentos, listDocInterop,
					interoperabilidadService.getIRPFYear());
		} catch (Exception e) {
			ManagerErrors.manageException(e, "Error al crear la solicitud presencial a enviar a Platea");
		}

		com.ejie.ad74a.ws.client.platea.solAportacion.AExtensionPoint ep = new com.ejie.ad74a.ws.client.platea.solAportacion.AExtensionPoint();

		com.ejie.ad74a.ws.client.platea.solAportacion.TSubmissionResponse result = this.x43Solicitud.addApplication(
				com.ejie.ad74a.ws.client.platea.solAportacion.TSubmissionType.PRESENCIAL, solicitud, null, ep);

		return result.getPropertyList().getProperty().get(1).getXmlValue().get(0).getStoredApplicationInfo()
				.getFolderID();
	}

	@Override
	public String aportacionDocuPapel0(Expedientes expediente, List<DocumentoPapel0> listDocPapel0,
			BandejaPapel0 bandejaPapel0) {
		PlateaServiceImpl.logger.info("[PlateaServiceImpl] - aportacion ");
		com.ejie.ad74a.ws.client.platea.solAportacion.TSubmissionRequest aportacion = null;
		try {
			aportacion = Papel0Utils.createAportacionPapel0(expediente, null, listDocPapel0, bandejaPapel0);
		} catch (Exception e) {
			ManagerErrors.manageException(e, "Error al crear la solicitud presencial a enviar a Platea");
		}

		com.ejie.ad74a.ws.client.platea.solAportacion.AExtensionPoint ep = new com.ejie.ad74a.ws.client.platea.solAportacion.AExtensionPoint();

		com.ejie.ad74a.ws.client.platea.solAportacion.TRegistryID result = this.x43Solicitud.addAttachment(
				com.ejie.ad74a.ws.client.platea.solAportacion.TSubmissionType.PRESENCIAL, aportacion, null, ep);

		return result.getRegistryNumber();
	}

	@Override
	public String aportacionInteroperabilidad(Expedientes expediente) {
		PlateaServiceImpl.logger.info("[PlateaServiceImpl] - aportacionInteroperabilidad ");

		TProcedureList tProcedureList = getProcedure(expediente.getIdProcedimiento00());

		MaestroTiposDoc tiposDoc = new MaestroTiposDoc();

		tiposDoc.setOrigen36(Constantes.OrigenTiposDocumento.ORIGEN_TIPO_DOC_OTROS_ORGANOS);

		List<MaestroTiposDoc> listDocInterop = this.maestroTiposDocDao.findAll(tiposDoc, null);

		com.ejie.ad74a.ws.client.platea.solAportacion.TSubmissionRequest aportacion = null;
		try {
			aportacion = PlateaUtils.createAportacionInteroperabilidad(expediente, listDocInterop,
					tProcedureList.getProcedure().get(0), this.getParticipantID(expediente.getFolderId00()));
		} catch (Exception e) {
			ManagerErrors.manageException(e, "Error al crear la solicitud presencial a enviar a Platea");
		}

		com.ejie.ad74a.ws.client.platea.solAportacion.AExtensionPoint ep = new com.ejie.ad74a.ws.client.platea.solAportacion.AExtensionPoint();

		com.ejie.ad74a.ws.client.platea.solAportacion.TRegistryID result = this.x43Solicitud.addAttachment(
				com.ejie.ad74a.ws.client.platea.solAportacion.TSubmissionType.PRESENCIAL, aportacion, null, ep);

		return result.getRegistryNumber();
	}

	@Override
	public com.ejie.ad74a.ws.client.platea.tareasTramitacion.TBoolean endWait(String waitID, String idResultWait) {
		com.ejie.ad74a.ws.client.platea.tareasTramitacion.AExtensionPoint ep = new com.ejie.ad74a.ws.client.platea.tareasTramitacion.AExtensionPoint();
		return this.x43RTAPortType.endWait(waitID, idResultWait, ep);
	}

	@Override
	public com.ejie.ad74a.ws.client.platea.tareasTramitacion.TWaitList getWait(String folderID) {
		com.ejie.ad74a.ws.client.platea.tareasTramitacion.AExtensionPoint ep = new com.ejie.ad74a.ws.client.platea.tareasTramitacion.AExtensionPoint();
		return this.x43RTAPortType.getWaits(folderID, ep);
	}

	@Override
	public com.ejie.ad74a.ws.client.platea.tareasTramitacion.TTaskAndWaitList searchWaits(
			com.ejie.ad74a.ws.client.platea.tareasTramitacion.TStringList folderIDList,
			com.ejie.ad74a.ws.client.platea.tareasTramitacion.TStringList procedureIDList, TWaitFilter filter) {
		return this.x43RTAPortType.searchWaits(folderIDList, procedureIDList, filter,
				new com.ejie.ad74a.ws.client.platea.tareasTramitacion.AExtensionPoint());
	}

	@Override
	public void updateTitularFolders(ArrayList<String> foldersID, Solicitantes solicitante,
			Solicitantes representante) {
		PlateaServiceImpl.logger.info("[PlateaServiceImpl] - updateTitularFolders");
		try {

			com.ejie.ad74a.ws.client.platea.expedienteElectronico.TFolderList folderList = this.searchFolders(foldersID,
					TFolderSearchType.PARAM_INTERESTED);

			com.ejie.ad74a.ws.client.platea.expedienteElectronico.AExtensionPoint ep = new com.ejie.ad74a.ws.client.platea.expedienteElectronico.AExtensionPoint();
			com.ejie.ad74a.ws.client.platea.expedienteElectronico.TApplicantRRI applicantRRI = null;
			com.ejie.ad74a.ws.client.platea.expedienteElectronico.TInterestedList listaInteresadosRRI = null;
			com.ejie.ad74a.ws.client.platea.expedienteElectronico.TInterested interesadoRRI = null;
			com.ejie.ad74a.ws.client.platea.expedienteElectronico.TAccount cuentaInteresadoRRI = null;
			com.ejie.ad74a.ws.client.platea.expedienteElectronico.TRepresentative representanteRRI = null;

			for (com.ejie.ad74a.ws.client.platea.expedienteElectronico.TFolder folder : folderList.getFolder()) {
				listaInteresadosRRI = new com.ejie.ad74a.ws.client.platea.expedienteElectronico.TInterestedList();
				interesadoRRI = null;
				cuentaInteresadoRRI = null;
				representanteRRI = null;
				if (folder.getParticipantRRIList() != null) {
					for (TParticipantRRI participante : folder.getParticipantRRIList().getParticipantList()) {
						applicantRRI = new com.ejie.ad74a.ws.client.platea.expedienteElectronico.TApplicantRRI();
						for (com.ejie.ad74a.ws.client.platea.expedienteElectronico.TInterested person : participante
								.getInterestedList().getInterested()) {
							if (person
									.getInterestedInLaw() == com.ejie.ad74a.ws.client.platea.expedienteElectronico.TBoolean.TRUE) {
								interesadoRRI = new com.ejie.ad74a.ws.client.platea.expedienteElectronico.TInterested();
								cuentaInteresadoRRI = new com.ejie.ad74a.ws.client.platea.expedienteElectronico.TAccount();
								applicantRRI.setRole(participante.getRole());
								applicantRRI.setParticipantID(participante.getParticipantID());
								interesadoRRI.setParticipantPersonID(person.getParticipantPersonID());
								interesadoRRI.setPersonID(PlateaUtils.getNewPerson(solicitante));
								cuentaInteresadoRRI.setContactList(person.getAccount().getContactList());
								cuentaInteresadoRRI.setNotify(person.getAccount().getNotify());
								interesadoRRI.setAccount(cuentaInteresadoRRI);
								break;
							}
						}
						if (representante != null) {
							representanteRRI = new com.ejie.ad74a.ws.client.platea.expedienteElectronico.TRepresentative();
							representanteRRI.setPersonID(PlateaUtils.getNewPerson(representante));
							representanteRRI.setAccount(cuentaInteresadoRRI);
						}
					}
				}

				if (interesadoRRI != null) {
					listaInteresadosRRI.getInterested().add(interesadoRRI);
					applicantRRI.setInterestedList(listaInteresadosRRI);
					if (representanteRRI != null) {
						applicantRRI.setRepresentative(representanteRRI);
					}
					this.x43REEPortType.updateFolderApplicantParticipant(folder.getFolderID(), null, applicantRRI, ep);
					PlateaServiceImpl.logger.info("[PlateaServiceImpl] - updateTitularFolders Fin");
				}
			}
		} catch (Throwable e) {// NOSONAR Se quiere capturar todo
			ManagerErrors.manageException(e, "Error al actualizar el titular");
		}
	}

	@Override
	public List<com.ejie.ad74a.ws.client.platea.notificaciones.TNotification> searchNotifications(
			com.ejie.ad74a.ws.client.platea.notificaciones.TNotificationFilter filter) {
		com.ejie.ad74a.ws.client.platea.notificaciones.AExtensionPoint ep = new com.ejie.ad74a.ws.client.platea.notificaciones.AExtensionPoint();
		com.ejie.ad74a.ws.client.platea.notificaciones.TNotificationList tNotificationList = this.x43notPortType
				.searchNotification(filter, null);
		return tNotificationList == null ? new ArrayList<>() : tNotificationList.getNotification();
	}

	/**
	 * 
	 * @param folderID
	 * @return
	 */
	@Override
	public String getParticipantID(String folderID) {
		String participantID = "";
		try {
			ArrayList<String> folderIDList = new ArrayList<String>();
			folderIDList.add(folderID);
			TFolderList listFolders = this.searchFolders(folderIDList, TFolderSearchType.PARAM_INTERESTED);
			if (null != listFolders && !listFolders.getFolder().isEmpty()
					&& null != listFolders.getFolder().get(0).getParticipantRRIList()
					&& null != listFolders.getFolder().get(0).getParticipantRRIList().getParticipantList()) {
				participantID = listFolders.getFolder().get(0).getParticipantRRIList().getParticipantList().get(0)
						.getParticipantID();
			}
		} catch (Exception e) {
			PlateaServiceImpl.logger
					.error("ExpedientesController getParticipantID error al recuperar el participantID");
		}
		return participantID;
	}

	/**
	 * Recupera la información de expedientes y documentos a archivar
	 * 
	 * @param listaFolderIDs ArrayList<String> Lista de folderIds
	 * @return TStoreDocumentList con la lista de documentos
	 */
	public TStoreDocumentList getStoredFolderDocuments(ArrayList<String> listaFolderIDs) {
		PlateaServiceImpl.logger.info("[PlateaServiceImpl] - getStoredFolderDocuments Inicio");
		TStoreDocumentList listaDocumentos = null;
		try {

			com.ejie.ad74a.ws.client.platea.expedienteElectronico.TStringList folderIDList = new com.ejie.ad74a.ws.client.platea.expedienteElectronico.TStringList();

			for (String folderID : listaFolderIDs) {
				folderIDList.getItem().add("" + folderID);
			}

			listaDocumentos = this.x43REEPortType.getStoredFolderDocuments(folderIDList,
					new com.ejie.ad74a.ws.client.platea.expedienteElectronico.AExtensionPoint());

		} catch (Throwable e) {
			PlateaServiceImpl.logger
					.error("[PlateaServiceImpl] - Error al recuperar los datos de los documentos para Archivo", e);
			ManagerErrors.manageException(e, "Error al recuperar los datos de los documentos para Archivo");
		}
		PlateaServiceImpl.logger.info("[PlateaServiceImpl] - getStoredFolderDocuments Fin");

		return listaDocumentos;

	}

	/**
	 * Informa a Platea de los expedientes Archivados o NO archivados
	 * 
	 * @param listaExpedientes Lista de expedients
	 * @param archivados       boolean 1=archivados / 0=No archivados
	 * @return
	 */
	public TFolderStoredStatusResultList setStoredFolderStatus(List<Expedientes> listaExpedientes, boolean archivados) {
		PlateaServiceImpl.logger.info("[PlateaServiceImpl] - setStoredFolderStatus Inicio");

		// Archivados o no
		TFolderStoredFilter filter = null;
		TFolderStoredStatusList storedList = null;
		com.ejie.ad74a.ws.client.platea.expedienteElectronico.TStringList folderList = null;

		if (archivados) {
			// ********************** EXPTES ARCHIVADOS ********************* //
			PlateaServiceImpl.logger
					.info("[PlateaServiceImpl] - setStoredFolderStatus informar expedientes archivados");
			filter = TFolderStoredFilter.STORED_EXPEDIENTS_STATUS;
			storedList = new TFolderStoredStatusList();

			for (Expedientes expte : listaExpedientes) {
				TFolderStoredStatus folderStoredStatus = new TFolderStoredStatus();
				folderStoredStatus.setFolderNumber(expte.getFolderId00());
				folderStoredStatus.setSgaID(expte.getCodigoSga00().toString());
				try {
					folderStoredStatus.setStoredDate(UtilidadesFecha.formatearDateToStringMasc(expte.getfArchivo00(),
							// com.ejie.ad74a.constantes.Constantes.PLATEA_XML_EVENTS_DATE_FORMAT));
							"yyyy/MM/dd hh:mm:ss"));// yyyy/MM/dd hh24:mm:ss
				} catch (Exception e) {
					PlateaServiceImpl.logger.error(
							"[PlateaServiceImpl] - setStoredFolderStatus informar expedientes archivados.formatearDateToStringMasc {}",
							expte.getfArchivo00());
				}

				storedList.getFolderStoredStatus().add(folderStoredStatus);
			}

		} else {
			// ********************** EXPTES ARCHIVADOS ********************* //
			PlateaServiceImpl.logger
					.info("[PlateaServiceImpl] - setStoredFolderStatus informar expedientes NO archivados");
			filter = TFolderStoredFilter.NON_STORED_EXPEDIENTS_STATUS;
			folderList = new com.ejie.ad74a.ws.client.platea.expedienteElectronico.TStringList();

			for (Expedientes expte : listaExpedientes) {
				folderList.getItem().add(expte.getFolderId00());
			}

		}
		PlateaServiceImpl.logger.info("[PlateaServiceImpl] - setStoredFolderStatus invocando a Platea...");

		TFolderStoredStatusResultList listaResultados = this.x43REEPortType.setStoredFolderStatus(folderList,
				storedList, filter, new com.ejie.ad74a.ws.client.platea.expedienteElectronico.AExtensionPoint());

		PlateaServiceImpl.logger.info("[PlateaServiceImpl] - setStoredFolderStatus Fin");

		return listaResultados;
	}

	@Override
	public String getAttachmentSend(TAttachmentSend attachmentSend) {
		return this.x43notPortType.getAttachmentSend(attachmentSend, null);

	}
}