Пример #1
0
        public override OM2MResponsePrimitive DoRetrieve(OM2MRequestPrimitive request)
        {
            using (var db = CreateDbContext())
            {
                // Check existence of the resource
                var cseBaseEntity = db.Resources.Find(request.TargetId) as OM2MCseBaseEntity;
                if (cseBaseEntity == null)
                {
                    throw new OM2MNotFoundException($"Resouce {request.TargetId} not found.");
                }

                // Check authorization
                var acpList = new List <OM2MAccessControlPolicyEntity>();
                foreach (var i in cseBaseEntity.AccessControlPolicyIds)
                {
                    var queryAcp = db.Resources.Where(x => x.ResourceId == i)
                                   .OfType <OM2MAccessControlPolicyEntity>()
                                   .Include(x => x.PrivilegesCore);

                    if (queryAcp.Count() == 0)
                    {
                        // Damaged
                        continue;
                    }

                    acpList.Add(queryAcp.First());
                }

                CheckACP(acpList, request.From, request.Operation.Value);

                OM2MResultContent resultContent = request.ResultContent ?? OM2MResultContent.Attributes;

                if (resultContent.HasChildResourceReferences())
                {
                    db.Entry(cseBaseEntity).Collection(x => x.Resources)
                    .Load();
                }

                OM2MCSEBase cseBaseResource = cseBaseEntity.ToResource(resultContent);

                var response = new OM2MResponsePrimitive(CseConfig, request);
                response.ResponseStatusCode = OM2MResponseStatusCode.Ok;
                response.Content            = cseBaseResource;

                return(response);
            }
        }
        // ToDo:
        public static void MapEntityToResourceCommonProcudure(OM2MResourceEntity entity,
                                                              OM2MResource resource,
                                                              OM2MResultContent resultContent)
        {
            if (resultContent.HasAttributes())
            {
                CopyAttributesResource(entity, resource);
            }

            if (resultContent.HasChildResourceReferences())
            {
                var container = resource as IOM2MChildResourceContainer;
                if (container != null)
                {
                    MapChildResourceRef(entity, container);
                }
            }
        }