Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace VisSolutions.Commands
- {
- using System;
- using System.ComponentModel.Composition;
- using System.Linq;
- using System.Threading.Tasks;
- using Caliburn.Micro;
- using Combyte.Practices;
- using Combyte.Practices.ExtensionMethods;
- using Combyte.Practices.Logging;
- using Combyte.VisSolutions.Core.Extensions;
- using Combyte.VisSolutions.Core.Services;
- using VisSolutions.Core;
- using VisSolutions.Core.UI;
- using VisSolutions.Model;
- [Export(typeof(RemoveCurrentVersion))]
- [Export(typeof(IVisCommand))]
- [PartCreationPolicy(CreationPolicy.NonShared)]
- public class RemoveCurrentVersion : CommandBase, IVisCommand
- {
- private static readonly ILogger log = VisLogManager.GetLog(typeof(ShowDetails));
- private readonly IWindowManager windowManager;
- private readonly IVisMessageBoxFactory messageBoxFactory;
- private enum Trans
- {
- NurEineVersionVorhanden,
- DokumentIstGeöffnet,
- Bestätigung,
- AktuelleVersionWirdGelöscht
- }
- protected override object[] Translations => Vis.App.Resolve<IEnumService>().GetValues(typeof(Trans));
- [ImportingConstructor]
- public RemoveCurrentVersion(IVisContext visContext, IWindowManager windowManager, IVisMessageBoxFactory messageBoxFactory)
- : base(visContext)
- {
- this.windowManager = windowManager;
- this.messageBoxFactory = messageBoxFactory;
- this.ActionKey = "RemoveCurrentVersion";
- }
- public override async Task<VisResult> Execute(VisTaskState vts)
- {
- var dataContext = this.Dialog.DialogProperties.DataContext;
- if (dataContext.VisitenKarten.Count <= 1)
- {
- this.VisResult.AddError(this.TL(Trans.NurEineVersionVorhanden));
- return this.VisResult.AsCanceled();
- }
- var docVersionMe = dataContext.VisitenKarten.First().MasterEntity;
- var docMe = (IMasterEntityDocument)await docVersionMe.GetFieldValueAsync(Entities.DocVersion.DokMasterEntity);
- await docMe.LoadAsync();
- var geöffnetVon = docMe.GetFieldValue(Entities.DokumentBase.IstGeöffnetVon);
- if (!geöffnetVon.IsNullOrWhiteSpace())
- {
- this.VisResult.AddError(this.TL(Trans.DokumentIstGeöffnet));
- return this.VisResult.AsCanceled();
- }
- var response =
- await
- this.messageBoxFactory.ShowMessageBoxAsync(
- this.TL(Trans.Bestätigung),
- this.TL(Trans.AktuelleVersionWirdGelöscht),
- MessageType.Warning,
- this.Dialog,
- VisMessageBoxCommands.YesNo);
- if (response.VisCommand.ActionKey != AK.Ok)
- {
- return this.VisResult.AsCanceled();
- }
- var visDocAktuell = docMe.GetDocument();
- var oldDocDb = visDocAktuell.DokumentDb;
- oldDocDb.Delete();
- visDocAktuell.Version = -visDocAktuell.Version;
- visDocAktuell.Persisted = false;
- var df = (IVisDatenFeldDokument) docMe.Felder[Entities.DokumentBase.BLOB.EntityFieldId];
- var eList = df.Save();
- eList.AddUnique(oldDocDb);
- await this.VisContext.VisRepository.SaveAsync(eList);
- var neueAktuelleVersion = await df.GetValueAsync(true);
- docMe.SetFieldValue(Entities.DokumentBase.Dateigrösse, neueAktuelleVersion.Size);
- docMe.SetFieldValue(Entities.DokumentBase.LastModified, neueAktuelleVersion.MutiertAm);
- await docMe.UpdateCacheAsync();
- eList = await docMe.PrepareSave(true, Guid.NewGuid());
- await this.VisContext.VisRepository.SaveAsync(eList);
- var vk = dataContext.VisitenKarten.First();
- dataContext.VisitenKarten.Remove(vk);
- dataContext.VisitenKarten.Add(vk);
- return this.VisResult;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement