277 lines
11 KiB
C#
277 lines
11 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Net;
|
|
using System.Net.Http;
|
|
using System.Web.Http;
|
|
using ULA.Cathay.ModuloMantenimiento.Json;
|
|
using ULA.Cathay.CreditoModel.Credito;
|
|
//using ULA.Cathay.CreditoModel.DAL.Credito;
|
|
using Ultimus.Interfaces;
|
|
using Ultimus.Utilitarios;
|
|
using System.Data.Entity;
|
|
using System.Data.Entity.Validation;
|
|
using Ultimus.Interfaces.UltimusForm;
|
|
using Newtonsoft.Json;
|
|
using System.Web;
|
|
using System.ServiceModel;
|
|
using System.Text;
|
|
|
|
namespace ULA.Cathay.ModuloMantenimiento.ControllersWebApi
|
|
{
|
|
public class DocumentosAPIController : ApiController
|
|
{
|
|
private UltimusLogs logs = new UltimusLogs("DocumentosAPIController");
|
|
|
|
|
|
[HttpPost]
|
|
public DocumentosAdjuntosCategoriaJSON CargarDocumentosAdjuntos(JsonDetalleTipoDocumento model)
|
|
{
|
|
|
|
DocumentosAdjuntosCategoriaJSON resp = new DocumentosAdjuntosCategoriaJSON() {
|
|
ListaDocumentos = new List<DocumentosAdjuntosCategoria>()
|
|
};
|
|
|
|
try
|
|
{
|
|
UltimusFormAPIController Adjuntos = new UltimusFormAPIController();
|
|
|
|
Ultimus.Interfaces.UltimusForm.DocumentosParametros parametros = new Ultimus.Interfaces.UltimusForm.DocumentosParametros();
|
|
|
|
parametros.CodSolicitud = model.Solicitud.IdSolicitud;
|
|
parametros.NombreProceso = model.UltimusProcess;
|
|
|
|
List<Ultimus.Interfaces.UltimusForm.DocumentosAdjuntos> ListaAdjuntos = Adjuntos.ObtenerDocumentosAdjuntos(parametros);
|
|
|
|
using (CreditoContex db = new CreditoContex())
|
|
{
|
|
if (model.DetalleDocumento.CodDocumento != null)
|
|
{
|
|
if (ListaAdjuntos != null && ListaAdjuntos.Count > 0)
|
|
{
|
|
resp.ListaDocumentos = (
|
|
from doc in db.TipoDocumento.Include(c => c.CategoriaTipoDocumento).ToList()
|
|
where doc.CodDocumento == model.DetalleDocumento.CodDocumento
|
|
join ad in ListaAdjuntos on doc.CodDocumento equals ad.CodDocumento
|
|
select new DocumentosAdjuntosCategoria()
|
|
{
|
|
CodDocumento = ad.CodDocumento,
|
|
RutaDescarga = ad.RutaDescarga,
|
|
RutaFisica = ad.RutaFisica,
|
|
NombreDocumento = ad.NombreDocumento + ad.TipoArchivo
|
|
}).ToList();
|
|
|
|
if (resp.ListaDocumentos == null || resp.ListaDocumentos.Count == 0)
|
|
{
|
|
resp.ListaDocumentos = ListaAdjuntos
|
|
.Where(x => x.CodDocumento == model.DetalleDocumento.CodDocumento)
|
|
.Select(x => new DocumentosAdjuntosCategoria() {
|
|
CodDocumento = x.CodDocumento,
|
|
RutaDescarga = x.RutaDescarga,
|
|
RutaFisica = x.RutaFisica,
|
|
NombreDocumento = x.NombreDocumento + x.TipoArchivo
|
|
}).ToList();
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
resp.CodigoRespuesta = TipoCodigoRespuesta.EXITOSO.ToString();
|
|
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
logs.Error(ex);
|
|
|
|
resp.CodigoRespuesta = TipoCodigoRespuesta.ERROR.ToString();
|
|
}
|
|
|
|
return resp;
|
|
}
|
|
|
|
|
|
[HttpPost]
|
|
[ActionName("AdjuntarDocumento")]
|
|
public object AdjuntarDocumentos()
|
|
{
|
|
DocumentosParametros Parametros = null;
|
|
HttpRequest Request = HttpContext.Current.Request;
|
|
DocumentoAdjuntoJSON respuesta = new DocumentoAdjuntoJSON();
|
|
System.Web.Http.Results.RedirectResult redirect = null;
|
|
|
|
try
|
|
{
|
|
HttpFileCollection files = Request.Files.Count > 0 ? Request.Files : null;
|
|
|
|
string jsonParam = Request.Form["AttacmentJSONParameters"];
|
|
|
|
Parametros = JsonConvert.DeserializeObject<DocumentosParametros>(jsonParam);
|
|
|
|
List<FileJSON> Documentos = new List<FileJSON>();
|
|
|
|
foreach (string name in files)
|
|
{
|
|
HttpPostedFile item = files[name];
|
|
Documentos.Add(
|
|
new FileJSON()
|
|
{
|
|
FileName = item.FileName,
|
|
FileData = ReadFullyBytes(item.InputStream),
|
|
ContentType = item.ContentType,
|
|
ContentLength = item.ContentLength
|
|
}
|
|
);
|
|
}
|
|
|
|
|
|
System.ServiceModel.WSHttpBinding tmp = new System.ServiceModel.WSHttpBinding();
|
|
tmp.Security.Mode = System.ServiceModel.SecurityMode.None;
|
|
tmp.MaxReceivedMessageSize = int.MaxValue;
|
|
tmp.MaxBufferPoolSize = int.MaxValue;
|
|
System.ServiceModel.HttpTransportSecurity security = new System.ServiceModel.HttpTransportSecurity();
|
|
security.ClientCredentialType = System.ServiceModel.HttpClientCredentialType.None;
|
|
tmp.Security.Transport = security;
|
|
|
|
using (UltimusFormClient client = new UltimusFormClient(tmp, new System.ServiceModel.EndpointAddress(new ObtieneParametros().GetValueKeyRegedit("UltimusFormUrl", false) + "/soap")))
|
|
{
|
|
respuesta = client.AdjuntarDocumentos(new DocumentosAdjuntosJSON()
|
|
{
|
|
Files = Documentos,
|
|
Parametros = Parametros
|
|
});
|
|
}
|
|
|
|
//********************************************
|
|
//Siempre realiza el redirect
|
|
//********************************************
|
|
if (Parametros != null && Parametros.IEVersion != -1 && Parametros.IEVersion < 10)
|
|
{
|
|
string _Descripcion = Request.QueryString["Descripcion"];
|
|
string _IdTipoExpediente = Request.QueryString["TipoExpediente"];
|
|
string _IdTipoInformacion = Request.QueryString["TipoInformacion"];
|
|
string _IdUbicacion = Request.QueryString["Ubicacion"];
|
|
string _IsRequisitoBasico = Request.QueryString["RequisitoBasico"];
|
|
string _IsAutoGenerable = Request.QueryString["AutoGenerable"];
|
|
string _IdTipoDocumento = Request.QueryString["IdTipoDocumento"];
|
|
string _MinCantPeriodoReq = Request.QueryString["MinCantPeriodoReq"];
|
|
string _IdTipoRequisito = Request.QueryString["IdTipoRequisito"];
|
|
string _IdTipoDocumentoEpower = Request.QueryString["IdTipoDocumentoEpower"];
|
|
string _Prefijo = Request.QueryString["Prefijo"];
|
|
|
|
|
|
|
|
StringBuilder sb = new StringBuilder();
|
|
sb.Append("&Desc=" + _Descripcion);
|
|
sb.Append("&IdTExp=" + _IdTipoExpediente);
|
|
sb.Append("&IdTInf=" + _IdTipoInformacion);
|
|
sb.Append("&IdUbic=" + _IdUbicacion);
|
|
sb.Append("&IsReqBasico=" + _IsRequisitoBasico);
|
|
sb.Append("&IdAutoGen=" + _IsAutoGenerable);
|
|
sb.Append("&CodDoc=" + respuesta.CodDocumento.ToString());
|
|
sb.Append("&IdTipoDoc=" + _IdTipoDocumento);
|
|
sb.Append("&MinCantPeriodoReq=" + _MinCantPeriodoReq);
|
|
sb.Append("&IdTipoRequisito=" + _IdTipoRequisito);
|
|
sb.Append("&IdTipoDocumentoEpower=" + _IdTipoDocumentoEpower);
|
|
sb.Append("&Prefijo=" + _Prefijo);
|
|
sb.Append("&RutaPlantillaCargada=" + respuesta.RutaFisica);
|
|
|
|
|
|
int nQueryIni = HttpContext.Current.Request.UrlReferrer.ToString().IndexOf("&Desc=");
|
|
string urlRefer = HttpContext.Current.Request.UrlReferrer.ToString();
|
|
|
|
if (nQueryIni > -1)
|
|
{
|
|
string queryAttachToReplace = urlRefer.Substring(nQueryIni, urlRefer.Length - nQueryIni);
|
|
urlRefer = urlRefer.Replace(queryAttachToReplace, sb.ToString());
|
|
}
|
|
else
|
|
{
|
|
urlRefer = urlRefer + sb.ToString();
|
|
}
|
|
|
|
|
|
var url = new Uri(urlRefer);
|
|
|
|
|
|
redirect = new System.Web.Http.Results.RedirectResult(url, new HttpRequestMessage());
|
|
|
|
return redirect;
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
respuesta.CodigoRespuesta = TipoCodigoRespuesta.ERROR.ToString();
|
|
respuesta.DescripcionRespuesta = ex.Message;
|
|
}
|
|
|
|
return respuesta;
|
|
|
|
}
|
|
|
|
|
|
[HttpPost]
|
|
public BaseJson EliminarDocumentoAdjunto(DocumentosAdjuntosCategoria model)
|
|
{
|
|
BaseJson resp = new BaseJson();
|
|
|
|
try
|
|
{
|
|
using (CreditoContex db = new CreditoContex())
|
|
{
|
|
var doc = db.TipoDocumento.Where(c => c.CodDocumento == model.CodDocumento).FirstOrDefault();
|
|
|
|
|
|
if (doc != null)
|
|
{
|
|
var actualizar = doc;
|
|
|
|
actualizar.CodDocumento = null;
|
|
actualizar.RutaMachote = null;
|
|
|
|
db.Entry(doc).State = System.Data.Entity.EntityState.Detached;
|
|
db.TipoDocumento.Attach(actualizar);
|
|
db.Entry(actualizar).State = System.Data.Entity.EntityState.Modified;
|
|
db.SaveChanges();
|
|
}
|
|
|
|
new UltimusFormAPIController().EliminarDocumentoAdjunto((int)model.CodDocumento);
|
|
}
|
|
|
|
resp.CodigoRespuesta = TipoCodigoRespuesta.EXITOSO.ToString();
|
|
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
logs.Error(ex);
|
|
|
|
resp.CodigoRespuesta = TipoCodigoRespuesta.ERROR.ToString();
|
|
}
|
|
|
|
return resp;
|
|
}
|
|
|
|
|
|
|
|
static byte[] ReadFullyBytes(System.IO.Stream input)
|
|
{
|
|
byte[] buffer = new byte[16 * 1024];
|
|
using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
|
|
{
|
|
int read;
|
|
while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
|
|
{
|
|
ms.Write(buffer, 0, read);
|
|
}
|
|
return ms.ToArray();
|
|
}
|
|
}
|
|
|
|
|
|
}
|
|
}
|