private IndiagramForDevice ToIndiagramForDevice(Device device, Indiagram indiagram, IndiagramInfo info)
        {
            IndiagramState state = info.States.FirstOrDefault(s => s.DeviceId == device.Id);

            return ToIndiagramForDevice(indiagram, info, state);
        }
 private IndiagramForDevice ToIndiagramForDevice(Indiagram indiagram, IndiagramInfo info, IndiagramState state)
 {
     return new IndiagramForDevice
     {
         Id = indiagram.Id,
         Version = info.Version,
         ImageHash = info.ImageHash,
         IsCategory = info.IsCategory,
         ParentId = info.ParentId,
         SoundHash = info.SoundHash,
         Text = info.Text,
         Position = info.Position,
         IsEnabled = state == null || state.IsEnabled,
         ImageFile = info.ImagePath,
         SoundFile = info.SoundPath
     };
 }
        private IndiagramInfo CreateIndiagramInfo(Indiagram indiagram, IndiagramInfo old, long version)
        {
            IndiagramInfo info = _context.Set<IndiagramInfo>().Add(new IndiagramInfo
            {
                IndiagramId = indiagram.Id,
                Version = version,
                ParentId = old.ParentId,
                Position = old.Position,
                Text = old.Text,
                SoundPath = old.SoundPath,
                SoundHash = old.SoundHash,
                ImagePath = old.ImagePath,
                ImageHash = old.ImageHash,
                IsCategory = old.IsCategory,
            });
            DbSet<IndiagramState> stateSet = _context.Set<IndiagramState>();

            info.States = old.States.Select(x => stateSet.Add(
                new IndiagramState
                {
                    DeviceId = x.DeviceId,
                    IndiagramInfoId = info.Id,
                    IsEnabled = x.IsEnabled
                })).ToList();

            if (indiagram.LastIndiagramInfoId == null || indiagram.LastIndiagramInfo.Version < version)
            {
                indiagram.LastIndiagramInfoId = info.Id;
            }

            _context.SaveChanges();
            return info;
        }
 public void SetIndiagramSound(IndiagramInfo indiagramInfo, string filename, byte[] fileContent)
 {
     indiagramInfo.SoundPath = filename;
     indiagramInfo.SoundHash = ComputeFileHash(fileContent);
     _context.SaveChanges();
 }
        private bool UploadFile(IndiagramInfo indiagram, byte[] fileBuffer, string containerName)
        {
            if (fileBuffer == null || string.IsNullOrEmpty(containerName) || indiagram == null)
            {
                return false;
            }

            try
            {
                CloudBlobContainer container = _blobClient.GetContainerReference(containerName);
                container.CreateIfNotExists(BlobContainerPublicAccessType.Container);

                string filename = string.Format("{0}_{1}", indiagram.IndiagramId, indiagram.Version);

                if (container.ListBlobs(filename).Any())
                {
                    return false;
                }
                CloudBlockBlob blob = container.GetBlockBlobReference(filename);
                blob.UploadFromByteArray(fileBuffer, 0, fileBuffer.Length);
            }
            catch (Exception e)
            {
                Trace.TraceError("Exception while uploading file to container {0} : {1}\n{2}", containerName, e.Message, e.StackTrace);
                return false;
            }
            return true;
        }
 public bool UploadSound(IndiagramInfo indiagram, byte[] fileBuffer)
 {
     return UploadFile(indiagram, fileBuffer, SOUNDS_CONTAINER);
 }
 public bool UploadImage(IndiagramInfo indiagram, byte[] fileBuffer)
 {
     return UploadFile(indiagram, fileBuffer, IMAGES_CONTAINER);
 }