Esempio n. 1
0
        public HttpResponseMessage Modify(int idProperty, [FromBody] PropertyDTO dto)
        {
            // CAD, CEN, returnValue
            PropertyRESTCAD propertyRESTCAD = null;
            PropertyCEN     propertyCEN     = null;
            PropertyDTOA    returnValue     = null;

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

            try
            {
                SessionInitializeTransaction();


                propertyRESTCAD = new PropertyRESTCAD(session);
                propertyCEN     = new PropertyCEN(propertyRESTCAD);

                // Modify
                propertyCEN.Modify(idProperty,
                                   dto.Name
                                   ,
                                   dto.IsWritable
                                   ,
                                   dto.IsCloudable
                                   );

                // Return modified object
                returnValue = PropertyAssembler.Convert(propertyRESTCAD.ReadOIDDefault(idProperty), 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);
            }
        }
Esempio n. 2
0
        public HttpResponseMessage New_([FromBody] PropertyDTO dto)
        {
            // CAD, CEN, returnValue, returnOID
            PropertyRESTCAD propertyRESTCAD = null;
            PropertyCEN     propertyCEN     = null;
            PropertyDTOA    returnValue     = null;
            int             returnOID       = -1;

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

            try
            {
                SessionInitializeTransaction();


                propertyRESTCAD = new PropertyRESTCAD(session);
                propertyCEN     = new PropertyCEN(propertyRESTCAD);

                // Create
                returnOID = propertyCEN.New_(

                    //Atributo OID: p_deviceTemplate
                    // attr.estaRelacionado: true
                    dto.DeviceTemplate_oid                     // association role

                    , dto.Name                                 //Atributo Primitivo: p_name
                    , dto.IsWritable                           //Atributo Primitivo: p_isWritable
                    , dto.IsCloudable                          //Atributo Primitivo: p_isCloudable
                    );
                SessionCommit();

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

            return(response);
        }