public ActionResult Edit(ViewModels.Assets.FileViewModel viewModel)
        {
            Common.Models.Account.Users currentUser;
            Common.Models.Assets.File   file;

            using (Data.Transaction trans = Data.Transaction.Create())
            {
                try
                {
                    currentUser = Data.Account.Users.Get(User.Identity.Name);
                    file        = Data.Assets.File.Get(trans, viewModel.Id.Value);
                    Common.Models.Assets.Version version = Data.Assets.Version.Get(trans, file.Version.Id.Value);


                    file = Mapper.Map <Common.Models.Assets.File>(viewModel);

                    file = Data.Assets.File.Edit(trans, file, currentUser);

                    trans.Commit();

                    return(RedirectToAction("Details", "Assets", new { Id = version.Asset.Id.Value }));
                }
                catch (Exception ex)
                {
                    trans.Rollback();

                    throw ex;
                }
            }
        }
Beispiel #2
0
        public ActionResult Create(Guid id, ViewModels.Assets.CreateAssetViewModel viewModel)
        {
            Common.Models.Account.Users currentUser;

            // FS layout
            // Asset/Version/File
            Common.Models.Assets.Asset       asset;
            Common.Models.Assets.Version     version;
            List <Common.Models.Assets.File> files = new List <Common.Models.Assets.File>();

            Common.FileSystem.Asset       fsAsset;
            Common.FileSystem.Version     fsVersion;
            List <Common.FileSystem.File> fsFiles = new List <Common.FileSystem.File>();

            asset = Mapper.Map <Common.Models.Assets.Asset>(viewModel.Asset);
            if (!asset.Id.HasValue)
            {
                asset.Id = Guid.NewGuid();
            }
            fsAsset = new Common.FileSystem.Asset(asset);

            if (!System.IO.Directory.Exists(fsAsset.Path))
            {
                System.IO.Directory.CreateDirectory(fsAsset.Path);
            }

            version = new Common.Models.Assets.Version()
            {
                Asset          = asset,
                Id             = Guid.NewGuid(),
                SequenceNumber = 1,
                ChangeDetails  = "Initial version."
            };
            fsVersion = new Common.FileSystem.Version(fsAsset, version);

            if (!System.IO.Directory.Exists(fsVersion.Path))
            {
                System.IO.Directory.CreateDirectory(fsVersion.Path);
            }

            viewModel.Files.ForEach(vmFile =>
            {
                bool isSource = false;
                if (viewModel.SourceFiles != null &&
                    viewModel.SourceFiles.Select(x => x == vmFile.FileName).Count() > 0)
                {
                    isSource = true;
                }
                Common.Models.Assets.File file = new Common.Models.Assets.File()
                {
                    Version       = version,
                    Id            = Guid.NewGuid(),
                    ContentLength = vmFile.ContentLength,
                    ContentType   = vmFile.ContentType.ToContentType(),
                    //ContentType = (Common.Models.Assets.ContentType)Enum.Parse(typeof(Common.Models.Assets.ContentType), vmFile.ContentType.Substring(vmFile.ContentType.LastIndexOf('/') + 1), true),
                    IsSource  = isSource,
                    Extension = System.IO.Path.GetExtension(vmFile.FileName)
                };
                Common.FileSystem.File fsFile = new Common.FileSystem.File(fsAsset, fsVersion, file);

                files.Add(file);
                fsFiles.Add(fsFile);

                vmFile.SaveAs(fsFile.Path);
            });

            using (Data.Transaction trans = Data.Transaction.Create())
            {
                try
                {
                    currentUser = Data.Account.Users.Get(User.Identity.Name);
                    Common.Models.Matters.Matter matter = Data.Matters.Matter.Get(trans, id);

                    asset = Data.Assets.Asset.Create(trans, asset, currentUser);

                    Data.Assets.AssetMatter.Create(trans, new Common.Models.Assets.AssetMatter()
                    {
                        Asset  = asset,
                        Matter = matter
                    }, currentUser);

                    version = Data.Assets.Version.Create(trans, version, currentUser);

                    files.ForEach(file =>
                    {
                        Data.Assets.File.Create(trans, file, currentUser);
                    });

                    while (string.Join("", viewModel.Tags).Contains(","))
                    {
                        List <string> bldr = new List <string>();
                        foreach (string str in viewModel.Tags)
                        {
                            bldr.AddRange(str.Split(','));
                        }
                        bldr.ForEach(x => x = x.Trim());
                        viewModel.Tags      = bldr.ToArray();
                    }

                    foreach (string tag in viewModel.Tags)
                    {
                        if (!string.IsNullOrEmpty(tag))
                        {
                            Common.Models.Assets.Tag mTag = new Common.Models.Assets.Tag()
                            {
                                Name = tag
                            };
                            Data.Assets.Tag.Create(trans, mTag, currentUser);

                            Data.Assets.AssetTag.Create(trans, new Common.Models.Assets.AssetTag()
                            {
                                Asset = asset, Tag = mTag
                            }, currentUser);
                        }
                    }

                    trans.Commit();
                }
                catch (Exception ex)
                {
                    trans.Rollback();

                    System.IO.Directory.Delete(fsAsset.Path, true);
                }
            }

            return(RedirectToAction("Details", new { Id = asset.Id }));
        }
        public ActionResult Create(Guid id, ViewModels.Assets.CreateAssetViewModel viewModel)
        {
            Common.Models.Account.Users currentUser;

            // FS layout
            // Asset/Version/File
            Common.Models.Assets.Asset asset;
            Common.Models.Assets.Version version;
            List<Common.Models.Assets.File> files = new List<Common.Models.Assets.File>();

            Common.FileSystem.Asset fsAsset;
            Common.FileSystem.Version fsVersion;
            List<Common.FileSystem.File> fsFiles = new List<Common.FileSystem.File>();

            asset = Mapper.Map<Common.Models.Assets.Asset>(viewModel.Asset);
            if (!asset.Id.HasValue) asset.Id = Guid.NewGuid();
            fsAsset = new Common.FileSystem.Asset(asset);

            if(!System.IO.Directory.Exists(fsAsset.Path))
                System.IO.Directory.CreateDirectory(fsAsset.Path);

            version = new Common.Models.Assets.Version()
            {
                Asset = asset,
                Id = Guid.NewGuid(),
                SequenceNumber = 1,
                ChangeDetails = "Initial version."
            };
            fsVersion = new Common.FileSystem.Version(fsAsset, version);

            if (!System.IO.Directory.Exists(fsVersion.Path))
                System.IO.Directory.CreateDirectory(fsVersion.Path);

            viewModel.Files.ForEach(vmFile =>
            {
                bool isSource = false;
                if (viewModel.SourceFiles != null && 
                    viewModel.SourceFiles.Select(x => x == vmFile.FileName).Count() > 0)
                    isSource = true;
                Common.Models.Assets.File file = new Common.Models.Assets.File()
                {
                    Version = version,
                    Id = Guid.NewGuid(),
                    ContentLength = vmFile.ContentLength,
                    ContentType = vmFile.ContentType.ToContentType(),
                    //ContentType = (Common.Models.Assets.ContentType)Enum.Parse(typeof(Common.Models.Assets.ContentType), vmFile.ContentType.Substring(vmFile.ContentType.LastIndexOf('/') + 1), true),
                    IsSource = isSource,
                    Extension = System.IO.Path.GetExtension(vmFile.FileName)
                };
                Common.FileSystem.File fsFile = new Common.FileSystem.File(fsAsset, fsVersion, file);

                files.Add(file);
                fsFiles.Add(fsFile);

                vmFile.SaveAs(fsFile.Path);
            });

            using (Data.Transaction trans = Data.Transaction.Create())
            {
                try
                {
                    currentUser = Data.Account.Users.Get(User.Identity.Name);
                    Common.Models.Matters.Matter matter = Data.Matters.Matter.Get(trans, id);

                    asset = Data.Assets.Asset.Create(trans, asset, currentUser);

                    Data.Assets.AssetMatter.Create(trans, new Common.Models.Assets.AssetMatter()
                    {
                        Asset = asset,
                        Matter = matter
                    }, currentUser);

                    version = Data.Assets.Version.Create(trans, version, currentUser);

                    files.ForEach(file =>
                    {
                        Data.Assets.File.Create(trans, file, currentUser);
                    });

                    while (string.Join("", viewModel.Tags).Contains(","))
                    {
                        List<string> bldr = new List<string>();
                        foreach (string str in viewModel.Tags)
                        {
                            bldr.AddRange(str.Split(','));
                        }
                        bldr.ForEach(x => x = x.Trim());
                        viewModel.Tags = bldr.ToArray();
                    }

                    foreach (string tag in viewModel.Tags)
                    {
                        if (!string.IsNullOrEmpty(tag))
                        {
                            Common.Models.Assets.Tag mTag = new Common.Models.Assets.Tag() { Name = tag };
                            Data.Assets.Tag.Create(trans, mTag, currentUser);

                            Data.Assets.AssetTag.Create(trans, new Common.Models.Assets.AssetTag()
                            { Asset = asset, Tag = mTag }, currentUser);
                        }
                    }

                    trans.Commit();
                }
                catch (Exception ex)
                {
                    trans.Rollback();

                    System.IO.Directory.Delete(fsAsset.Path, true);
                }
            }

            return RedirectToAction("Details", new { Id = asset.Id });
        }