Exemple #1
0
        public ActionResult Checkin(Guid id, ViewModels.Assets.CheckinAssetViewModel 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 = null;
            List <Common.FileSystem.File> fsFiles   = new List <Common.FileSystem.File>();

            using (Data.Transaction trans = Data.Transaction.Create())
            {
                try
                {
                    currentUser = Data.Account.Users.Get(User.Identity.Name);

                    // current asset
                    asset = Data.Assets.Asset.Get(trans, id);

                    if (asset.CheckedOutBy == null ||
                        asset.CheckedOutBy.PId.Value == currentUser.PId.Value)
                    {
                        // If files submitted -> new version
                        if (viewModel.Files.Count > 0)
                        {
                            // For some reason, the files input is posting with 1 even when there is none
                            // it is always 1 and always null
                            if (viewModel.Files.Count > 1 ||
                                viewModel.Files[0] != null)
                            {
                                fsAsset = new Common.FileSystem.Asset(asset);

                                version                = Mapper.Map <Common.Models.Assets.Version>(viewModel.Version);
                                version.Asset          = asset;
                                version.Id             = Guid.NewGuid();
                                version.SequenceNumber = Data.Assets.Asset.GetNextVersionSequenceNumber(trans, version.Asset.Id.Value);
                                fsVersion              = new Common.FileSystem.Version(fsAsset, version);

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

                                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(),
                                        IsSource      = isSource,
                                        Extension     = System.IO.Path.GetExtension(vmFile.FileName)
                                    };
                                    Common.FileSystem.File fsFile = new Common.FileSystem.File(fsAsset, fsVersion, file);

                                    Data.Assets.File.Create(trans, file, currentUser);

                                    fsFiles.Add(fsFile);

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

                        // Update asset if needed
                        if (viewModel.Asset.IsCourtFiled != asset.IsCourtFiled ||
                            viewModel.Asset.IsFinal != asset.IsFinal)
                        {
                            asset.IsFinal      = viewModel.Asset.IsFinal;
                            asset.IsCourtFiled = viewModel.Asset.IsCourtFiled;
                            asset.CheckedOutAt = null;
                            asset.CheckedOutBy = null;
                            asset = Data.Assets.Asset.Edit(trans, asset, currentUser);
                        }
                        else
                        {
                            // No asset update done -> Checkin
                            Data.Assets.Asset.Checkin(trans, id, currentUser);
                        }
                    }
                    else
                    {   // Wrong user trying to checkin
                        return(RedirectToAction("Checkin", new { Id = id }));
                    }

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

                    if (fsVersion != null)
                    {
                        System.IO.Directory.Delete(fsVersion.Path, true);
                    }

                    throw ex;
                }
            }

            return(RedirectToAction("Details", new { Id = id }));
        }
Exemple #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.CreateVersionViewModel viewModel)
        {
            Common.Models.Account.Users currentUser;

            // FS layout
            // Asset/Version/File
            Guid assetId = Guid.Empty;
            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 = null;
            List<Common.FileSystem.File> fsFiles = new List<Common.FileSystem.File>();
            
            using (Data.Transaction trans = Data.Transaction.Create())
            {
                try
                {
                    currentUser = Data.Account.Users.Get(User.Identity.Name);
                    asset = Data.Assets.Asset.Get(trans, id);
                    assetId = asset.Id.Value;
                    if (asset.CheckedOutBy != null && asset.CheckedOutBy.PId.HasValue)
                    {
                        if (asset.CheckedOutBy.PId.Value != currentUser.PId.Value)
                            throw new Exception("Asset is checked out by another user.");
                    }

                    fsAsset = new Common.FileSystem.Asset(asset);

                    version = Mapper.Map<Common.Models.Assets.Version>(viewModel.Version);
                    version.Asset = asset;
                    version.Id = Guid.NewGuid();
                    version.SequenceNumber = Data.Assets.Asset.GetNextVersionSequenceNumber(trans, version.Asset.Id.Value);
                    fsVersion = new Common.FileSystem.Version(fsAsset, version);

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

                    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);

                        Data.Assets.File.Create(trans, file, currentUser);

                        fsFiles.Add(fsFile);

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

                    if (viewModel.IsCourtFiled != asset.IsCourtFiled ||
                        viewModel.IsFinal != asset.IsFinal)
                    {
                        asset.IsFinal = viewModel.IsFinal;
                        asset.IsCourtFiled = viewModel.IsCourtFiled;
                        asset = Data.Assets.Asset.Edit(trans, asset, currentUser);
                    }

                    trans.Commit();

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

                    if (fsVersion != null)
                        System.IO.Directory.Delete(fsVersion.Path, true);

                    throw ex;
                }
            }

        }
        public ActionResult Checkin(Guid id, ViewModels.Assets.CheckinAssetViewModel 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 = null;
            List<Common.FileSystem.File> fsFiles = new List<Common.FileSystem.File>();

            using (Data.Transaction trans = Data.Transaction.Create())
            {
                try
                {
                    currentUser = Data.Account.Users.Get(User.Identity.Name);

                    // current asset
                    asset = Data.Assets.Asset.Get(trans, id);

                    if (asset.CheckedOutBy == null ||
                        asset.CheckedOutBy.PId.Value == currentUser.PId.Value)
                    {
                        // If files submitted -> new version
                        if (viewModel.Files.Count > 0)
                        {
                            // For some reason, the files input is posting with 1 even when there is none
                            // it is always 1 and always null
                            if (viewModel.Files.Count > 1 ||
                                viewModel.Files[0] != null)
                            {
                                fsAsset = new Common.FileSystem.Asset(asset);

                                version = Mapper.Map<Common.Models.Assets.Version>(viewModel.Version);
                                version.Asset = asset;
                                version.Id = Guid.NewGuid();
                                version.SequenceNumber = Data.Assets.Asset.GetNextVersionSequenceNumber(trans, version.Asset.Id.Value);
                                fsVersion = new Common.FileSystem.Version(fsAsset, version);

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

                                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(),
                                        IsSource = isSource,
                                        Extension = System.IO.Path.GetExtension(vmFile.FileName)
                                    };
                                    Common.FileSystem.File fsFile = new Common.FileSystem.File(fsAsset, fsVersion, file);

                                    Data.Assets.File.Create(trans, file, currentUser);

                                    fsFiles.Add(fsFile);

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

                        // Update asset if needed
                        if (viewModel.Asset.IsCourtFiled != asset.IsCourtFiled ||
                            viewModel.Asset.IsFinal != asset.IsFinal)
                        {
                            asset.IsFinal = viewModel.Asset.IsFinal;
                            asset.IsCourtFiled = viewModel.Asset.IsCourtFiled;
                            asset.CheckedOutAt = null;
                            asset.CheckedOutBy = null;
                            asset = Data.Assets.Asset.Edit(trans, asset, currentUser);
                        }
                        else
                        {
                            // No asset update done -> Checkin
                            Data.Assets.Asset.Checkin(trans, id, currentUser);
                        }
                    }
                    else
                    {   // Wrong user trying to checkin
                        return RedirectToAction("Checkin", new { Id = id });
                    }

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

                    if (fsVersion != null)
                        System.IO.Directory.Delete(fsVersion.Path, true);

                    throw ex;
                }
            }

            return RedirectToAction("Details", new { Id = 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 });
        }
        public ActionResult Create(Guid id, ViewModels.Assets.CreateVersionViewModel viewModel)
        {
            Common.Models.Account.Users currentUser;

            // FS layout
            // Asset/Version/File
            Guid assetId = Guid.Empty;

            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 = null;
            List <Common.FileSystem.File> fsFiles   = new List <Common.FileSystem.File>();

            using (Data.Transaction trans = Data.Transaction.Create())
            {
                try
                {
                    currentUser = Data.Account.Users.Get(User.Identity.Name);
                    asset       = Data.Assets.Asset.Get(trans, id);
                    assetId     = asset.Id.Value;
                    if (asset.CheckedOutBy != null && asset.CheckedOutBy.PId.HasValue)
                    {
                        if (asset.CheckedOutBy.PId.Value != currentUser.PId.Value)
                        {
                            throw new Exception("Asset is checked out by another user.");
                        }
                    }

                    fsAsset = new Common.FileSystem.Asset(asset);

                    version                = Mapper.Map <Common.Models.Assets.Version>(viewModel.Version);
                    version.Asset          = asset;
                    version.Id             = Guid.NewGuid();
                    version.SequenceNumber = Data.Assets.Asset.GetNextVersionSequenceNumber(trans, version.Asset.Id.Value);
                    fsVersion              = new Common.FileSystem.Version(fsAsset, version);

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

                    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);

                        Data.Assets.File.Create(trans, file, currentUser);

                        fsFiles.Add(fsFile);

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

                    if (viewModel.IsCourtFiled != asset.IsCourtFiled ||
                        viewModel.IsFinal != asset.IsFinal)
                    {
                        asset.IsFinal      = viewModel.IsFinal;
                        asset.IsCourtFiled = viewModel.IsCourtFiled;
                        asset = Data.Assets.Asset.Edit(trans, asset, currentUser);
                    }

                    trans.Commit();

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

                    if (fsVersion != null)
                    {
                        System.IO.Directory.Delete(fsVersion.Path, true);
                    }

                    throw ex;
                }
            }
        }