Saltar al contenido principal
Este ejemplo muestra cómo puede crear una etapa adicional de procesamiento manual de datos. Por ejemplo, esta etapa puede utilizarla el departamento de seguridad de su empresa. El departamento de seguridad puede verificar y confirmar el envío de los documentos que cumplan determinadas condiciones para su posterior procesamiento, y rechazar los documentos que no cumplan esas condiciones. Todos los documentos confirmados van acompañados de un conjunto de bytes que simulan una firma digital. Puede descargar el proyecto y los materiales aquí: UserStage.zip

El código de ejemplo realiza los siguientes pasos:

  • Configura la conexión
  • Empieza a trabajar con el proyecto
  • Obtiene y procesa la lista de tareas
  • Procesa cada tarea en la etapa de servicio
  • Carga el archivo en el servidor
  • Cierra la sesión

Para continuar con este ejemplo:

Abra UserStageExample.sln en Visual Studio 2013 o versiones posteriores:
  1. Introduzca la URL del servidor de aplicaciones.
  2. Seleccione un tipo de autenticación.
  3. Haga clic en Login.

Configurar la conexión

private const string FlexiHandlerPath = "/FlexiCapture12/Server/{0}API/v1/Soap"; // Ruta a la API de FlexiCapture
private const string FlexiCaptureAuthPart = "FCAuth/"; // Ruta para autenticar FlexiCapture
private const string UserStageProjectName = "UserStageExample"; // El nombre del proyecto utilizado en este ejemplo
private const string UserStageName = "Service"; // El nombre de la etapa de servicio
// Creación de un nuevo enlace
var binding = new BasicHttpBinding();
// El tamaño del mensaje es demasiado pequeño para FlexiCapture y es necesario aumentarlo
binding.MaxReceivedMessageSize = MessageSize;
// Selección del modo de seguridad de conexión según el protocolo
binding.Security.Mode = serverUri.Scheme == Uri.UriSchemeHttps
   ? BasicHttpSecurityMode.Transport
   : BasicHttpSecurityMode.TransportCredentialOnly;
// Selección del método de autenticación
binding.Security.Transport.ClientCredentialType = windowsAuth
   ? HttpClientCredentialType.Windows
   : HttpClientCredentialType.Basic;
// Selección de la ruta a la API según el método de autenticación
var externalSuffix = windowsAuth ? string.Empty : FlexiCaptureAuthPart;
var remoteAddress = new EndpointAddress(new Uri(serverUri, string.Format(FlexiHandlerPath, externalSuffix)));
// Creación de un cliente SOAP
_client = new FlexiCapture.FlexiCaptureWebServiceSoapClient(binding, remoteAddress);
// Para autenticar FlexiCapture, especifique un nombre de usuario y una contraseña
if (_client.ClientCredentials != null && !windowsAuth)
{
   _client.ClientCredentials.UserName.UserName = login;
   _client.ClientCredentials.UserName.Password = password;
}
Starting working with the project
// Abrir la sesión
_sessionId = _client.OpenSession(RoleType, WorkstationType);
var projectGuid = string.Empty;
// Buscar el proyecto necesario
foreach (var project in _client.GetProjects())
{
   if (project.Name == UserStageProjectName)
   {
      projectGuid = project.Guid;
   }
}
if (string.IsNullOrEmpty(projectGuid))
{
   MessageBox.Show("The server does not contain the UserStageExample project which is needed for this example");
}
// Abrir el proyecto
_projectId = _client.OpenProject(_sessionId, projectGuid);
// Buscar y guardar el identificador de la etapa de servicio
var stages = _client.GetProcessingStages(_projectId, 0, 0, UserStageName);
if (stages.Count != 1) throw new Exception("Invalid project");
   _serviceStageId = stages[0].Id;
// Buscar y guardar el ID externo de la etapa de excepciones
stages = _client.GetProcessingStages(_projectId, 0, ExceptionStageType, string.Empty);
if (stages.Count != 1) throw new Exception("Invalid project");
   _exceptionsStageId = stages[0].ExternalId;

Obtención y procesamiento de la lista de tareas pendientes en la etapa de servicio

// Obtención de la lista de tareas
var tasks = _client.GetAvailableTasksByStageId(_sessionId, _projectId,
new FlexiCapture.stageIds() { _serviceStageId }, false);
var result = new Dictionary<int, Task>();
if (tasks == null || tasks.Count == 0) return result;
foreach (var task in tasks)
{
   // Para cada tarea se crea un objeto del tipo Task
   var item = new Task
   {
      Id = task.Id,
      BatchId = task.BatchId
   };
   // Obtención de la lista de documentos de la tarea
   var documents = _client.GetTaskDocuments(task.Id);
   if (documents == null || documents.Count == 0) continue;
   foreach (var document in documents)
   {
      // Carga del XML de la tarea
      var file = _client.LoadDocumentResult(_sessionId, document.BatchId, document.Id, "DocumentBody"); // "DocumentBody" - el nombre del archivo que contiene los datos reconocidos
      if (file == null || file.Bytes == null) continue;
      // Descomposición del XML
      var doc = new Document(document.Id, XDocument.Load(new MemoryStream(file.Bytes)));
      // Guardado de la lista de documentos descompuesta
      item.Documents.Add(document.Id, doc);
   }
   result.Add(item.Id, item);
}

Enviar la tarea para su procesamiento posterior

var ids = new FlexiCapture.docIds();
foreach (var rejectedDocument in rejectedDocuments)
{
   ids.Add(rejectedDocument.Id);
}
if (ids.Count > 0)
{
   _client.CreateTask(_sessionId, batchId, _exceptionsStageId, 0, "Rejection reason", 0, ids, false);
}
_client.OpenBatch(_sessionId, batchId);
var hasDocuments = false;
try
{
   var uri = _client.Endpoint.Address.Uri.OriginalString.Replace("/API/v1/Soap", "/FileService/v1");
   foreach (var document in acceptedDocuments)
   {
      hasDocuments = true;
      var buffer = new byte[SignatureSize]; // El tamaño de la firma digital añadida a los documentos: 1024
      _rng.GetBytes(buffer);
      //DocumentAttachment: El tipo de adjunto (9 - adjunto de documento)
      _client.SaveAttachment(_sessionId, DocumentAttachment, document.Id, batchId, _projectId, new FlexiCapture.File
      {
         Bytes = buffer,
         Name = "Signature.dat"
      });
      document.CreateStamp();
      UploadTextFileToServer(new Uri(uri),
         _client.ClientCredentials.UserName.UserName,
         _client.ClientCredentials.UserName.Password,
         document.GetXml(), 0, _sessionId, _projectId,
         batchId, 0, document.Id, 0, "DocumentBody"
      ).Wait();
   }
};
private static async Task<string> UploadTextFileToServer(Uri server, string login, string password,
        string stream, int objectType, int sessionId, int projectId, int batchId, int parentId,
        int objectId, int version, string streamName)
{
        var creds = CredentialCache.DefaultNetworkCredentials;
        if (!string.IsNullOrEmpty(login)) creds = new NetworkCredential(login, password);
        using (var handler = new HttpClientHandler { Credentials = creds })
        using (var client = new HttpClient(handler))
        {
                var encodedStreamName = Convert.ToBase64String(Encoding.Unicode.GetBytes(streamName));
                var content = new MultipartFormDataContent
                {
                        {new StringContent("Save"), "Action"},
                        {new StringContent(objectType.ToString()), "objectType"},
                        {new StringContent(sessionId.ToString()), "sessionId"},
                        {new StringContent(projectId.ToString()), "projectId"},
                        {new StringContent(batchId.ToString()), "batchId"},
                        {new StringContent(parentId.ToString()), "parentId"},
                        {new StringContent(objectId.ToString()), "objectId"},
                        {new StringContent(version.ToString()), "version"},
                        {new StringContent(encodedStreamName), "streamName"},
                        {new ByteArrayContent(Encoding.Unicode.GetBytes(stream)), "blob", "data.txt"}
                };
                var msg = client.PostAsync(server, content).Result;
                return await msg.Content.ReadAsStringAsync();
        }
}

Cerrar la sesión

_client.CloseProject(_sessionId, _projectId);
   _client.CloseSession(_sessionId);