Exemplo n.º 1
0
        public static LocationTelemetryEN Convert(LocationTelemetryDTO dto)
        {
            LocationTelemetryEN newinstance = null;

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



                    newinstance.Latitude  = dto.Latitude;
                    newinstance.Longitude = dto.Longitude;
                    newinstance.Altitude  = dto.Altitude;
                    if (dto.Telemetry_oid != -1)
                    {
                        MoSIoTGenNHibernate.CAD.MosIoT.ITelemetryCAD telemetryCAD = new MoSIoTGenNHibernate.CAD.MosIoT.TelemetryCAD();

                        newinstance.Telemetry = telemetryCAD.ReadOIDDefault(dto.Telemetry_oid);
                    }
                    newinstance.Id   = dto.Id;
                    newinstance.Name = dto.Name;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return(newinstance);
        }
        public HttpResponseMessage Modify(int idLocationTelemetry, [FromBody] LocationTelemetryDTO dto)
        {
            // CAD, CEN, returnValue
            LocationTelemetryRESTCAD locationTelemetryRESTCAD = null;
            LocationTelemetryCEN     locationTelemetryCEN     = null;
            LocationTelemetryDTOA    returnValue = null;

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

            try
            {
                SessionInitializeTransaction();


                locationTelemetryRESTCAD = new LocationTelemetryRESTCAD(session);
                locationTelemetryCEN     = new LocationTelemetryCEN(locationTelemetryRESTCAD);

                // Modify
                locationTelemetryCEN.Modify(idLocationTelemetry,
                                            dto.Name
                                            ,
                                            dto.Latitude
                                            ,
                                            dto.Longitude
                                            ,
                                            dto.Altitude
                                            );

                // Return modified object
                returnValue = LocationTelemetryAssembler.Convert(locationTelemetryRESTCAD.ReadOIDDefault(idLocationTelemetry), 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);
            }
        }
        public HttpResponseMessage New_([FromBody] LocationTelemetryDTO dto)
        {
            // CAD, CEN, returnValue, returnOID
            LocationTelemetryRESTCAD locationTelemetryRESTCAD = null;
            LocationTelemetryCEN     locationTelemetryCEN     = null;
            LocationTelemetryDTOA    returnValue = null;
            int returnOID = -1;

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

            try
            {
                SessionInitializeTransaction();


                locationTelemetryRESTCAD = new LocationTelemetryRESTCAD(session);
                locationTelemetryCEN     = new LocationTelemetryCEN(locationTelemetryRESTCAD);

                // Create
                returnOID = locationTelemetryCEN.New_(

                    //Atributo OID: p_telemetry
                    // attr.estaRelacionado: true
                    dto.Telemetry_oid                     // association role

                    , dto.Name                            //Atributo Primitivo: p_name
                    , dto.Latitude                        //Atributo Primitivo: p_latitude
                    , dto.Longitude                       //Atributo Primitivo: p_longitude
                    , dto.Altitude                        //Atributo Primitivo: p_altitude
                    );
                SessionCommit();

                // Convert return
                returnValue = LocationTelemetryAssembler.Convert(locationTelemetryRESTCAD.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("GetOIDLocationTelemetry", routeValues);
             * response.Headers.Location = new Uri(uri);
             */

            return(response);
        }