public OperationResult <TemplateDefinition> Handle(GetTemplateDefinitionById query) { if (query == null) { throw new ArgumentNullException(nameof(query)); } using (var context = new TemplatingContext(_persistenceConfiguration)) { try { return(OperationResult <TemplateDefinition> .Success(context.Templates .Include(t => t.DefinedPages) .ThenInclude(dp => dp.ReferenceCanvas) .Include(t => t.DefinedPages) .ThenInclude(dp => dp.DefinedAreas) .ThenInclude(da => da.InterestPoints) .SingleOrDefault(t => t.Id == query.TemplateId)?.AsDomainModel())); } catch (Exception ex) { return(OperationResult <TemplateDefinition> .Failure(new UncaughtException(ex))); } } }
public OperationResult <TemplatePageDefinition> Handle(AddTemplatePageAreas command) { ThrowOnInvalidInput(command); using (var context = new TemplatingContext(_persistenceConfiguration)) { var pageFromDb = context.Pages .Include(p => p.ReferenceCanvas) .Include(p => p.DefinedAreas) .SingleOrDefault(p => p.Id == command.TemplatePageId); var page = pageFromDb?.AsDomainModel(); if (page == null) { return(OperationResult <TemplatePageDefinition> .Failure( new ObjectNotFoundById(typeof(TemplatePageDefinition), command.TemplatePageId))); } foreach (var area in command.Areas) { var newArea = new TemplatePageArea( new Rectangle(area.DimensionX, area.DimensionY, area.DimensionWidth, area.DimensionHeight), area.AreaName, area.ExpectedData); foreach (var areaPart in area.AreaParts) { var addAreaPartResult = newArea.DefinePointOfInterest( new Rectangle(areaPart.DimensionX, areaPart.DimensionY, areaPart.DimensionWidth, areaPart.DimensionHeight), (uint)areaPart.OrderInArea); if (!addAreaPartResult.Successful) { return(OperationResult <TemplatePageDefinition> .Failure(addAreaPartResult.Details)); } } var addPageResult = page.DefineArea(newArea); if (!addPageResult.Successful) { return(OperationResult <TemplatePageDefinition> .Failure(addPageResult.Details)); } pageFromDb.DefinedAreas.Add(newArea.AsPersistenceModel()); } context.Update(pageFromDb); context.SaveChanges(); return(OperationResult <TemplatePageDefinition> .Success(page)); } }
public OperationResult <TemplateDefinition> Handle(CreateTemplateDefinition command) { if (command == null) { throw new ArgumentNullException(nameof(command)); } using (var context = new TemplatingContext(_persistenceConfiguration)) { try { var definition = new TemplateDefinition(command.TemplateName, command.TemplateCreatorId); context.Templates.Add(definition.AsPersistenceModel()); context.SaveChanges(); return(OperationResult <TemplateDefinition> .Success(definition)); } catch (Exception ex) { return(OperationResult <TemplateDefinition> .Failure(new UncaughtException(ex))); } } }
/// <summary> /// Provides a hosting shim between a 'tool' host and the underlying TextMetal run-time. /// </summary> /// <param name="argc"> The raw argument count passed into the host. </param> /// <param name="argv"> The raw arguments passed into the host. </param> /// <param name="args"> The parsed arguments passed into the host. </param> /// <param name="templateFilePath"> The file path of the input TextMetal template file to execute. </param> /// <param name="sourceFilePath"> The file path (or source specific URI) of the input data source to leverage. </param> /// <param name="baseDirectoryPath"> The root output directory path to place output arifacts (since this implementation uses file output mechanics). </param> /// <param name="sourceStrategyAqtn"> The assembly qualified type name for the ISourceStrategy to instantiate and execute. </param> /// <param name="strictMatching"> A value indicating whether to use strict matching semantics for tokens. </param> /// <param name="properties"> Arbitrary dictionary of string lists used to further customize the text templating process. The individual components or template files can use the properties as they see fit. </param> public void Host(int argc, string[] argv, IDictionary <string, object> args, string templateFilePath, string sourceFilePath, string baseDirectoryPath, string sourceStrategyAqtn, bool strictMatching, IDictionary <string, IList <string> > properties) { DateTime startUtc = DateTime.UtcNow, endUtc; IXmlPersistEngine xpe; TemplateConstruct template; object source; Dictionary <string, object> globalVariableTable; string toolVersion; Type sourceStrategyType; ISourceStrategy sourceStrategy; if ((object)templateFilePath == null) { throw new ArgumentNullException(nameof(templateFilePath)); } if ((object)sourceFilePath == null) { throw new ArgumentNullException(nameof(sourceFilePath)); } if ((object)baseDirectoryPath == null) { throw new ArgumentNullException(nameof(baseDirectoryPath)); } if ((object)sourceStrategyAqtn == null) { throw new ArgumentNullException(nameof(sourceStrategyAqtn)); } if ((object)properties == null) { throw new ArgumentNullException(nameof(properties)); } if (SolderFascadeAccessor.DataTypeFascade.IsWhiteSpace(templateFilePath)) { throw new ArgumentOutOfRangeException(nameof(templateFilePath)); } if (SolderFascadeAccessor.DataTypeFascade.IsWhiteSpace(sourceFilePath)) { throw new ArgumentOutOfRangeException(nameof(sourceFilePath)); } if (SolderFascadeAccessor.DataTypeFascade.IsWhiteSpace(baseDirectoryPath)) { throw new ArgumentOutOfRangeException(nameof(baseDirectoryPath)); } if (SolderFascadeAccessor.DataTypeFascade.IsWhiteSpace(sourceStrategyAqtn)) { throw new ArgumentOutOfRangeException(nameof(sourceStrategyAqtn)); } toolVersion = new AssemblyInformationFascade(this.ReflectionFascade, typeof(TextMetalToolHost).GetTypeInfo().Assembly).AssemblyVersion; templateFilePath = Path.GetFullPath(templateFilePath); baseDirectoryPath = Path.GetFullPath(baseDirectoryPath); sourceStrategyType = Type.GetType(sourceStrategyAqtn, false); if ((object)sourceStrategyType == null) { throw new InvalidOperationException(string.Format("Failed to load source strategy from assembly qualified type name '{0}'.", sourceStrategyAqtn)); } if (!typeof(ISourceStrategy).IsAssignableFrom(sourceStrategyType)) { throw new InvalidOperationException(string.Format("Source strategy type '{0}' is not assignable to '{1}'.", sourceStrategyType, typeof(ISourceStrategy))); } sourceStrategy = (ISourceStrategy)Activator.CreateInstance(sourceStrategyType); xpe = new XmlPersistEngine(); xpe.RegisterWellKnownConstructs(); // TODO: this was a bad idea and need to be fixed in a next version. using (IInputMechanism inputMechanism = new FileInputMechanism(templateFilePath, xpe, sourceStrategy)) // relative to template directory { source = inputMechanism.LoadSource(sourceFilePath, properties); if ((object)source == null) { return; } template = (TemplateConstruct)inputMechanism.LoadTemplate(templateFilePath); using (IOutputMechanism outputMechanism = new FileOutputMechanism(baseDirectoryPath, "#textmetal.log", Encoding.UTF8, xpe)) // relative to base directory { outputMechanism.LogTextWriter.WriteLine("[DIAGNOSTIC INFOMRATION]", startUtc); outputMechanism.LogTextWriter.WriteLine("argv: '{0}'", string.Join(" ", argv)); outputMechanism.LogTextWriter.WriteLine("toolVersion: '{0}'", toolVersion); outputMechanism.LogTextWriter.WriteLine("baseDirectoryPath: \"{0}\"", baseDirectoryPath); outputMechanism.LogTextWriter.WriteLine("sourceFilePath: \"{0}\"", sourceFilePath); outputMechanism.LogTextWriter.WriteLine("templateFilePath: \"{0}\"", templateFilePath); outputMechanism.LogTextWriter.WriteLine("sourceStrategyType: '{0}'", sourceStrategyType.FullName); outputMechanism.WriteObject(template, "#template.xml"); outputMechanism.WriteObject(source, "#source.xml"); outputMechanism.LogTextWriter.WriteLine("['{0:O}' (UTC)]\tText templating started.", startUtc); using (ITemplatingContext templatingContext = new TemplatingContext(xpe, new Tokenizer(strictMatching), inputMechanism, outputMechanism, properties)) { templatingContext.Tokenizer.RegisterWellKnownTokenReplacementStrategies(templatingContext); // globals globalVariableTable = new Dictionary <string, object>(); var environment = new { ARGC = argc, ARGV = argv, ARGS = args, CurrentManagedThreadId = Environment.CurrentManagedThreadId, HasShutdownStarted = Environment.HasShutdownStarted, NewLine = Environment.NewLine, ProcessorCount = Environment.ProcessorCount, StackTrace = Environment.StackTrace, TickCount = Environment.TickCount, Variables = Environment.GetEnvironmentVariables() }; globalVariableTable.Add("ToolVersion", toolVersion); globalVariableTable.Add("Environment", environment); globalVariableTable.Add("StartUtc", startUtc); // add properties to GVT foreach (KeyValuePair <string, IList <string> > property in properties) { if (property.Value.Count == 0) { continue; } if (property.Value.Count == 1) { globalVariableTable.Add(property.Key, property.Value[0]); } else { globalVariableTable.Add(property.Key, property.Value); } } templatingContext.VariableTables.Push(globalVariableTable); templatingContext.IteratorModels.Push(source); template.ExpandTemplate(templatingContext); templatingContext.IteratorModels.Pop(); templatingContext.VariableTables.Pop(); } endUtc = DateTime.UtcNow; outputMechanism.LogTextWriter.WriteLine("['{0:O}' (UTC)]\tText templating completed with duration: '{1}'.", endUtc, (endUtc - startUtc)); } } }
public OperationResult <TemplateDefinition> Handle(AddTemplateDefinitionPage command) { if (command == null) { throw new ArgumentNullException(nameof(command)); } using (var context = new TemplatingContext(_persistenceConfiguration)) { var templateFromDb = context.Templates .Include(t => t.DefinedPages) .SingleOrDefault(t => t.Id == command.TemplateId); var template = templateFromDb?.AsDomainModel(); if (template == null) { return(OperationResult <TemplateDefinition> .Failure( new ObjectNotFoundById(typeof(TemplateDefinition), command.TemplateId))); } var templatePage = new TemplatePageDefinition((int)command.PageNumber); using (var bitmap = command.ReferenceCanvas.FileData.AsBitmap()) { var modificationResult = templatePage .ModifyReferenceCanvas(new Rectangle(0, 0, bitmap.Width, bitmap.Height)); if (!modificationResult.Successful) { return(OperationResult <TemplateDefinition> .Failure(modificationResult.Details)); } } var addPageResult = template.AddPageDefinition(templatePage); if (!addPageResult.Successful) { return(OperationResult <TemplateDefinition> .Failure(addPageResult.Details)); } templateFromDb.DefinedPages.Add(templatePage.AsPersistenceModel()); context.Update(templateFromDb); context.SaveChanges(); var saveBitmapCommand = new SaveBitmapForTemplatePageCanvasDefinition { FileData = command.ReferenceCanvas.FileData, FileLabel = command.ReferenceCanvas.FileLabel, FileType = command.ReferenceCanvas.FileType, TemplatePageId = templatePage.Id, TemplateId = template.Id }; var savedCanvas = _saveCanvasHandler.Handle(saveBitmapCommand); if (!savedCanvas.Success) { return(OperationResult <TemplateDefinition> .Failure(new FileStorageSaveFailed(saveBitmapCommand))); } return(OperationResult <TemplateDefinition> .Success(template)); } }
public EmailMessage Host(bool strictMatching, EmailTemplate emailTemplate, object modelObject) { EmailMessage emailMessage; XmlPersistEngine xpe; TemplateConstruct template; ITemplatingContext templatingContext; XmlTextReader templateXmlTextReader; if ((object)emailTemplate == null) { throw new ArgumentNullException(nameof(emailTemplate)); } emailMessage = new EmailMessage(); xpe = new XmlPersistEngine(); xpe.RegisterWellKnownConstructs(); using (IInputMechanism inputMechanism = new NullInputMechanism()) { using (StringWriter stringWriter = new StringWriter()) { using (IOutputMechanism outputMechanism = new TextWriterOutputMechanism(stringWriter, xpe)) { using (templatingContext = new TemplatingContext(xpe, new Tokenizer(strictMatching), inputMechanism, outputMechanism, new Dictionary <string, IList <string> >())) { // FROM using (templateXmlTextReader = new XmlTextReader(new StringReader(emailTemplate.FromXml.OuterXml))) template = (TemplateConstruct)xpe.DeserializeFromXml(templateXmlTextReader); templatingContext.IteratorModels.Push(modelObject); template.ExpandTemplate(templatingContext); templatingContext.IteratorModels.Pop(); emailMessage.From = stringWriter.ToString(); stringWriter.GetStringBuilder().Clear(); // SENDER using (templateXmlTextReader = new XmlTextReader(new StringReader(emailTemplate.SenderXml.OuterXml))) template = (TemplateConstruct)xpe.DeserializeFromXml(templateXmlTextReader); templatingContext.IteratorModels.Push(modelObject); template.ExpandTemplate(templatingContext); templatingContext.IteratorModels.Pop(); emailMessage.Sender = stringWriter.ToString(); stringWriter.GetStringBuilder().Clear(); // REPLYTO using (templateXmlTextReader = new XmlTextReader(new StringReader(emailTemplate.ReplyToXml.OuterXml))) template = (TemplateConstruct)xpe.DeserializeFromXml(templateXmlTextReader); templatingContext.IteratorModels.Push(modelObject); template.ExpandTemplate(templatingContext); templatingContext.IteratorModels.Pop(); emailMessage.ReplyTo = stringWriter.ToString(); stringWriter.GetStringBuilder().Clear(); // TO using (templateXmlTextReader = new XmlTextReader(new StringReader(emailTemplate.ToXml.OuterXml))) template = (TemplateConstruct)xpe.DeserializeFromXml(templateXmlTextReader); templatingContext.IteratorModels.Push(modelObject); template.ExpandTemplate(templatingContext); templatingContext.IteratorModels.Pop(); emailMessage.To = stringWriter.ToString(); stringWriter.GetStringBuilder().Clear(); // CC using (templateXmlTextReader = new XmlTextReader(new StringReader(emailTemplate.CarbonCopyXml.OuterXml))) template = (TemplateConstruct)xpe.DeserializeFromXml(templateXmlTextReader); templatingContext.IteratorModels.Push(modelObject); template.ExpandTemplate(templatingContext); templatingContext.IteratorModels.Pop(); emailMessage.CarbonCopy = stringWriter.ToString(); stringWriter.GetStringBuilder().Clear(); // BCC using (templateXmlTextReader = new XmlTextReader(new StringReader(emailTemplate.BlindCarbonCopyXml.OuterXml))) template = (TemplateConstruct)xpe.DeserializeFromXml(templateXmlTextReader); templatingContext.IteratorModels.Push(modelObject); template.ExpandTemplate(templatingContext); templatingContext.IteratorModels.Pop(); emailMessage.BlindCarbonCopy = stringWriter.ToString(); stringWriter.GetStringBuilder().Clear(); // SUBJECT using (templateXmlTextReader = new XmlTextReader(new StringReader(emailTemplate.SubjectXml.OuterXml))) template = (TemplateConstruct)xpe.DeserializeFromXml(templateXmlTextReader); templatingContext.IteratorModels.Push(modelObject); template.ExpandTemplate(templatingContext); templatingContext.IteratorModels.Pop(); emailMessage.Subject = stringWriter.ToString(); stringWriter.GetStringBuilder().Clear(); // ISBODYHTML emailMessage.IsBodyHtml = emailTemplate.IsBodyHtml; // BODY using (templateXmlTextReader = new XmlTextReader(new StringReader(emailTemplate.BodyXml.OuterXml))) template = (TemplateConstruct)xpe.DeserializeFromXml(templateXmlTextReader); templatingContext.IteratorModels.Push(modelObject); template.ExpandTemplate(templatingContext); templatingContext.IteratorModels.Pop(); emailMessage.Body = stringWriter.ToString(); stringWriter.GetStringBuilder().Clear(); } } } } return(emailMessage); }