Exemplo n.º 1
0
        public HttpResponseMessage Modify(int idIMCommunication, [FromBody] IMCommunicationDTO dto)
        {
            // CAD, CEN, returnValue
            IMCommunicationRESTCAD iMCommunicationRESTCAD = null;
            IMCommunicationCEN     iMCommunicationCEN     = null;
            IMCommunicationDTOA    returnValue            = null;

            // HTTP response
            HttpResponseMessage response = null;
            string uri = null;

            try
            {
                SessionInitializeTransaction();


                iMCommunicationRESTCAD = new IMCommunicationRESTCAD(session);
                iMCommunicationCEN     = new IMCommunicationCEN(iMCommunicationRESTCAD);

                // Modify
                iMCommunicationCEN.Modify(idIMCommunication,
                                          dto.Name
                                          ,
                                          dto.Description
                                          );

                // Return modified object
                returnValue = IMCommunicationAssembler.Convert(iMCommunicationRESTCAD.ReadOIDDefault(idIMCommunication), session);

                SessionCommit();
            }

            catch (Exception e)
            {
                SessionRollBack();

                if (e.GetType() == typeof(HttpResponseException))
                {
                    throw e;
                }
                else if (e.GetType() == typeof(MoSIoTGenNHibernate.Exceptions.ModelException) && e.Message.Equals("El token es incorrecto"))
                {
                    throw new HttpResponseException(HttpStatusCode.Forbidden);
                }
                else if (e.GetType() == typeof(MoSIoTGenNHibernate.Exceptions.ModelException) || e.GetType() == typeof(MoSIoTGenNHibernate.Exceptions.DataLayerException))
                {
                    throw new HttpResponseException(HttpStatusCode.BadRequest);
                }
                else
                {
                    throw new HttpResponseException(HttpStatusCode.InternalServerError);
                }
            }
            finally
            {
                SessionClose();
            }

            // Return 404 - Not found
            if (returnValue == null)
            {
                return(this.Request.CreateResponse(HttpStatusCode.NotFound));
            }
            // Return 200 - OK
            else
            {
                response = this.Request.CreateResponse(HttpStatusCode.OK, returnValue);

                return(response);
            }
        }
Exemplo n.º 2
0
        public static IMCommunicationEN Convert(IMCommunicationDTO dto)
        {
            IMCommunicationEN newinstance = null;

            try
            {
                if (dto != null)
                {
                    newinstance = new IMCommunicationEN();



                    if (dto.Comunication_oid != -1)
                    {
                        MoSIoTGenNHibernate.CAD.MosIoT.IComunicationCAD comunicationCAD = new MoSIoTGenNHibernate.CAD.MosIoT.ComunicationCAD();

                        newinstance.Comunication = comunicationCAD.ReadOIDDefault(dto.Comunication_oid);
                    }
                    newinstance.Id   = dto.Id;
                    newinstance.Name = dto.Name;
                    if (dto.OriginAssociation_oid != null)
                    {
                        MoSIoTGenNHibernate.CAD.MosIoT.IAssociationCAD associationCAD = new MoSIoTGenNHibernate.CAD.MosIoT.AssociationCAD();

                        newinstance.OriginAssociation = new System.Collections.Generic.List <MoSIoTGenNHibernate.EN.MosIoT.AssociationEN>();
                        foreach (int entry in dto.OriginAssociation_oid)
                        {
                            newinstance.OriginAssociation.Add(associationCAD.ReadOIDDefault(entry));
                        }
                    }
                    if (dto.TargetAssociation_oid != null)
                    {
                        MoSIoTGenNHibernate.CAD.MosIoT.IAssociationCAD associationCAD = new MoSIoTGenNHibernate.CAD.MosIoT.AssociationCAD();

                        newinstance.TargetAssociation = new System.Collections.Generic.List <MoSIoTGenNHibernate.EN.MosIoT.AssociationEN>();
                        foreach (int entry in dto.TargetAssociation_oid)
                        {
                            newinstance.TargetAssociation.Add(associationCAD.ReadOIDDefault(entry));
                        }
                    }
                    if (dto.Scenario_oid != -1)
                    {
                        MoSIoTGenNHibernate.CAD.MosIoT.IIoTScenarioCAD ioTScenarioCAD = new MoSIoTGenNHibernate.CAD.MosIoT.IoTScenarioCAD();

                        newinstance.Scenario = ioTScenarioCAD.ReadOIDDefault(dto.Scenario_oid);
                    }
                    newinstance.Description = dto.Description;

                    if (dto.Operations != null)
                    {
                        MoSIoTGenNHibernate.CAD.MosIoT.IEntityOperationCAD entityOperationCAD = new MoSIoTGenNHibernate.CAD.MosIoT.EntityOperationCAD();

                        newinstance.Operations = new System.Collections.Generic.List <MoSIoTGenNHibernate.EN.MosIoT.EntityOperationEN>();
                        foreach (EntityOperationDTO entry in dto.Operations)
                        {
                            newinstance.Operations.Add(EntityOperationAssemblerDTO.Convert(entry));
                        }
                    }

                    if (dto.Attributes != null)
                    {
                        MoSIoTGenNHibernate.CAD.MosIoT.IEntityAttributesCAD entityAttributesCAD = new MoSIoTGenNHibernate.CAD.MosIoT.EntityAttributesCAD();

                        newinstance.Attributes = new System.Collections.Generic.List <MoSIoTGenNHibernate.EN.MosIoT.EntityAttributesEN>();
                        foreach (EntityAttributesDTO entry in dto.Attributes)
                        {
                            newinstance.Attributes.Add(EntityAttributesAssemblerDTO.Convert(entry));
                        }
                    }

                    if (dto.States != null)
                    {
                        MoSIoTGenNHibernate.CAD.MosIoT.IEntityStateCAD entityStateCAD = new MoSIoTGenNHibernate.CAD.MosIoT.EntityStateCAD();

                        newinstance.States = new System.Collections.Generic.List <MoSIoTGenNHibernate.EN.MosIoT.EntityStateEN>();
                        foreach (EntityStateDTO entry in dto.States)
                        {
                            newinstance.States.Add(EntityStateAssemblerDTO.Convert(entry));
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return(newinstance);
        }
Exemplo n.º 3
0
        public HttpResponseMessage New_([FromBody] IMCommunicationDTO dto)
        {
            // CAD, CEN, returnValue, returnOID
            IMCommunicationRESTCAD iMCommunicationRESTCAD = null;
            IMCommunicationCEN     iMCommunicationCEN     = null;
            IMCommunicationDTOA    returnValue            = null;
            int returnOID = -1;

            // HTTP response
            HttpResponseMessage response = null;
            string uri = null;

            try
            {
                SessionInitializeTransaction();


                iMCommunicationRESTCAD = new IMCommunicationRESTCAD(session);
                iMCommunicationCEN     = new IMCommunicationCEN(iMCommunicationRESTCAD);

                // Create
                returnOID = iMCommunicationCEN.New_(
                    dto.Name                                                                                     //Atributo Primitivo: p_name
                    ,
                    //Atributo OID: p_scenario
                    // attr.estaRelacionado: true
                    dto.Scenario_oid                     // association role

                    , dto.Description                    //Atributo Primitivo: p_description
                    ,
                    //Atributo OID: p_comunication
                    // attr.estaRelacionado: true
                    dto.Comunication_oid                     // association role

                    );
                SessionCommit();

                // Convert return
                returnValue = IMCommunicationAssembler.Convert(iMCommunicationRESTCAD.ReadOIDDefault(returnOID), session);
            }

            catch (Exception e)
            {
                SessionRollBack();

                if (e.GetType() == typeof(HttpResponseException))
                {
                    throw e;
                }
                else if (e.GetType() == typeof(MoSIoTGenNHibernate.Exceptions.ModelException) && e.Message.Equals("El token es incorrecto"))
                {
                    throw new HttpResponseException(HttpStatusCode.Forbidden);
                }
                else if (e.GetType() == typeof(MoSIoTGenNHibernate.Exceptions.ModelException) || e.GetType() == typeof(MoSIoTGenNHibernate.Exceptions.DataLayerException))
                {
                    throw new HttpResponseException(HttpStatusCode.BadRequest);
                }
                else
                {
                    throw new HttpResponseException(HttpStatusCode.InternalServerError);
                }
            }
            finally
            {
                SessionClose();
            }

            // Return 201 - Created
            response = this.Request.CreateResponse(HttpStatusCode.Created, returnValue);

            // Location Header

            /*
             * Dictionary<string, object> routeValues = new Dictionary<string, object>();
             *
             * // TODO: y rolPaths
             * routeValues.Add("id", returnOID);
             *
             * uri = Url.Link("GetOIDIMCommunication", routeValues);
             * response.Headers.Location = new Uri(uri);
             */

            return(response);
        }