Exemplo n.º 1
0
        public static LocationTelemetryDTOA Convert(SpecificTelemetryEN en, NHibernate.ISession session = null)
        {
            LocationTelemetryDTOA    dto = null;
            LocationTelemetryRESTCAD locationTelemetryRESTCAD = null;
            LocationTelemetryCEN     locationTelemetryCEN     = null;
            LocationTelemetryCP      locationTelemetryCP      = null;

            if (en != null)
            {
                dto = new LocationTelemetryDTOA();
                locationTelemetryRESTCAD = new LocationTelemetryRESTCAD(session);
                locationTelemetryCEN     = new LocationTelemetryCEN(locationTelemetryRESTCAD);
                locationTelemetryCP      = new LocationTelemetryCP(session);


                LocationTelemetryEN enHijo = locationTelemetryRESTCAD.ReadOIDDefault(en.Id);



                //
                // Attributes

                dto.Id = en.Id;

                if (enHijo != null)
                {
                    dto.Latitude = enHijo.Latitude;
                }


                if (enHijo != null)
                {
                    dto.Longitude = enHijo.Longitude;
                }


                if (enHijo != null)
                {
                    dto.Altitude = enHijo.Altitude;
                }


                //
                // TravesalLink


                //
                // Service
            }

            return(dto);
        }
        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);
        }
        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 ReadOID(int idLocationTelemetry)
        {
            // CAD, CEN, EN, returnValue
            LocationTelemetryRESTCAD locationTelemetryRESTCAD = null;
            LocationTelemetryCEN     locationTelemetryCEN     = null;
            LocationTelemetryEN      locationTelemetryEN      = null;
            LocationTelemetryDTOA    returnValue = null;

            try
            {
                SessionInitializeWithoutTransaction();


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

                // Data
                locationTelemetryEN = locationTelemetryCEN.ReadOID(idLocationTelemetry);

                // Convert return
                if (locationTelemetryEN != null)
                {
                    returnValue = LocationTelemetryAssembler.Convert(locationTelemetryEN, session);
                }
            }

            catch (Exception e)
            {
                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
            {
                return(this.Request.CreateResponse(HttpStatusCode.OK, returnValue));
            }
        }