public static DeviceTemplateDTOA Convert(DeviceTemplateEN en, NHibernate.ISession session = null)
        {
            DeviceTemplateDTOA    dto = null;
            DeviceTemplateRESTCAD deviceTemplateRESTCAD = null;
            DeviceTemplateCEN     deviceTemplateCEN     = null;
            DeviceTemplateCP      deviceTemplateCP      = null;

            if (en != null)
            {
                dto = new DeviceTemplateDTOA();
                deviceTemplateRESTCAD = new DeviceTemplateRESTCAD(session);
                deviceTemplateCEN     = new DeviceTemplateCEN(deviceTemplateRESTCAD);
                deviceTemplateCP      = new DeviceTemplateCP(session);



                //
                // Attributes

                dto.Id = en.Id;

                dto.Name = en.Name;


                dto.Type = en.Type;


                dto.IsEdge = en.IsEdge;


                //
                // TravesalLink

                /* Rol: DeviceTemplate o--> Property */
                dto.Properties = null;
                List <PropertyEN> Properties = deviceTemplateRESTCAD.Properties(en.Id).ToList();
                if (Properties != null)
                {
                    dto.Properties = new List <PropertyDTOA>();
                    foreach (PropertyEN entry in Properties)
                    {
                        dto.Properties.Add(PropertyAssembler.Convert(entry, session));
                    }
                }

                /* Rol: DeviceTemplate o--> Command */
                dto.Commands = null;
                List <CommandEN> Commands = deviceTemplateRESTCAD.Commands(en.Id).ToList();
                if (Commands != null)
                {
                    dto.Commands = new List <CommandDTOA>();
                    foreach (CommandEN entry in Commands)
                    {
                        dto.Commands.Add(CommandAssembler.Convert(entry, session));
                    }
                }

                /* Rol: DeviceTemplate o--> Telemetry */
                dto.Telemetries = null;
                List <TelemetryEN> Telemetries = deviceTemplateRESTCAD.Telemetries(en.Id).ToList();
                if (Telemetries != null)
                {
                    dto.Telemetries = new List <TelemetryDTOA>();
                    foreach (TelemetryEN entry in Telemetries)
                    {
                        dto.Telemetries.Add(TelemetryAssembler.Convert(entry, session));
                    }
                }


                //
                // Service
            }

            return(dto);
        }
예제 #2
0
        public HttpResponseMessage New_([FromBody] DeviceTemplateDTO dto)
        {
            // CAD, CEN, returnValue, returnOID
            DeviceTemplateRESTCAD deviceTemplateRESTCAD = null;
            DeviceTemplateCEN     deviceTemplateCEN     = null;
            DeviceTemplateDTOA    returnValue           = null;
            int returnOID = -1;

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

            try
            {
                SessionInitializeTransaction();


                deviceTemplateRESTCAD = new DeviceTemplateRESTCAD(session);
                deviceTemplateCEN     = new DeviceTemplateCEN(deviceTemplateRESTCAD);

                // Create
                returnOID = deviceTemplateCEN.New_(
                    dto.Name                                                                                     //Atributo Primitivo: p_name
                    , dto.Type                                                                                   //Atributo Primitivo: p_type
                    , dto.IsEdge                                                                                 //Atributo Primitivo: p_isEdge
                    );
                SessionCommit();

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

            return(response);
        }
예제 #3
0
        public HttpResponseMessage Modify(int idDeviceTemplate, [FromBody] DeviceTemplateDTO dto)
        {
            // CAD, CEN, returnValue
            DeviceTemplateRESTCAD deviceTemplateRESTCAD = null;
            DeviceTemplateCEN     deviceTemplateCEN     = null;
            DeviceTemplateDTOA    returnValue           = null;

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

            try
            {
                SessionInitializeTransaction();


                deviceTemplateRESTCAD = new DeviceTemplateRESTCAD(session);
                deviceTemplateCEN     = new DeviceTemplateCEN(deviceTemplateRESTCAD);

                // Modify
                deviceTemplateCEN.Modify(idDeviceTemplate,
                                         dto.Name
                                         ,
                                         dto.Type
                                         ,
                                         dto.IsEdge
                                         );

                // Return modified object
                returnValue = DeviceTemplateAssembler.Convert(deviceTemplateRESTCAD.ReadOIDDefault(idDeviceTemplate), 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);
            }
        }
예제 #4
0
        public HttpResponseMessage ReadOID(int idDeviceTemplate)
        {
            // CAD, CEN, EN, returnValue
            DeviceTemplateRESTCAD deviceTemplateRESTCAD = null;
            DeviceTemplateCEN     deviceTemplateCEN     = null;
            DeviceTemplateEN      deviceTemplateEN      = null;
            DeviceTemplateDTOA    returnValue           = null;

            try
            {
                SessionInitializeWithoutTransaction();


                deviceTemplateRESTCAD = new DeviceTemplateRESTCAD(session);
                deviceTemplateCEN     = new DeviceTemplateCEN(deviceTemplateRESTCAD);

                // Data
                deviceTemplateEN = deviceTemplateCEN.ReadOID(idDeviceTemplate);

                // Convert return
                if (deviceTemplateEN != null)
                {
                    returnValue = DeviceTemplateAssembler.Convert(deviceTemplateEN, 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));
            }
        }