Passbook.Generator.PassGeneratorRequest.LoadTemplate C# (CSharp) Method

LoadTemplate() public method

public LoadTemplate ( string template, TemplateModel parameters ) : void
template string
parameters TemplateModel
return void
        public void LoadTemplate(string template, TemplateModel parameters)
        {
            PassbookGeneratorSection section =
                System.Configuration.ConfigurationManager.GetSection("passbookGenerator") as PassbookGeneratorSection;

            if (section == null)
                throw new System.Configuration.ConfigurationErrorsException("\"passbookGenerator\" section could not be loaded.");

            String path = TemplateModel.MapPath(section.AppleWWDRCACertificate);
            if (File.Exists(path))
                this.AppleWWDRCACertificate = File.ReadAllBytes(path);

            TemplateElement templateConfig = section
                .Templates
                .OfType<TemplateElement>()
                .FirstOrDefault(t => String.Equals(t.Name, template, StringComparison.OrdinalIgnoreCase));

            if (templateConfig == null)
                throw new System.Configuration.ConfigurationErrorsException(String.Format("Configuration for template \"{0}\" could not be loaded.", template));

            this.Style = templateConfig.PassStyle;

            if (this.Style == PassStyle.BoardingPass)
                this.TransitType = templateConfig.TransitType;

            // Certificates
            this.CertificatePassword = templateConfig.CertificatePassword;
            this.CertThumbprint = templateConfig.CertificateThumbprint;

            path = TemplateModel.MapPath(templateConfig.Certificate);
            if (File.Exists(path))
                this.Certificate = File.ReadAllBytes(path);

            if (String.IsNullOrEmpty(this.CertThumbprint) && this.Certificate == null)
                throw new System.Configuration.ConfigurationErrorsException("Either Certificate or CertificateThumbprint is not configured correctly.");

            // Standard Keys
            this.Description = templateConfig.Description.Value;
            this.OrganizationName = templateConfig.OrganizationName.Value;
            this.PassTypeIdentifier = templateConfig.PassTypeIdentifier.Value;
            this.TeamIdentifier = templateConfig.TeamIdentifier.Value;

            // Associated App Keys
            if (templateConfig.AppLaunchURL != null && !String.IsNullOrEmpty(templateConfig.AppLaunchURL.Value))
                this.AppLaunchURL = templateConfig.AppLaunchURL.Value;

            this.AssociatedStoreIdentifiers.AddRange(templateConfig.AssociatedStoreIdentifiers.OfType<ConfigurationProperty<int>>().Select(s => s.Value));

            // Visual Appearance Keys
            this.BackgroundColor = templateConfig.BackgroundColor.Value;
            this.ForegroundColor = templateConfig.ForegroundColor.Value;
            this.GroupingIdentifier = templateConfig.GroupingIdentifier.Value;
            this.LabelColor = templateConfig.LabelColor.Value;
            this.LogoText = templateConfig.LogoText.Value;
            this.SuppressStripShine = templateConfig.SuppressStripShine.Value;

            // Web Service Keys
            this.AuthenticationToken = templateConfig.AuthenticationToken.Value;
            this.WebServiceUrl = templateConfig.WebServiceURL.Value;

            // Fields
            this.AuxiliaryFields.AddRange(TemplateFields(templateConfig.AuxiliaryFields, parameters));
            this.BackFields.AddRange(TemplateFields(templateConfig.BackFields, parameters));
            this.HeaderFields.AddRange(TemplateFields(templateConfig.HeaderFields, parameters));
            this.PrimaryFields.AddRange(TemplateFields(templateConfig.PrimaryFields, parameters));
            this.SecondaryFields.AddRange(TemplateFields(templateConfig.SecondaryFields, parameters));

            // Template Images
            foreach (ImageElement image in templateConfig.Images)
            {
                String imagePath = TemplateModel.MapPath(image.FileName);
                if (File.Exists(imagePath))
                    this.Images[image.Type] = File.ReadAllBytes(imagePath);
            }

            // Model Images (Overwriting template images)
            foreach (KeyValuePair<PassbookImage, byte[]> image in parameters.GetImages())
            {
                this.Images[image.Key] = image.Value;
            }

            // Localization
            foreach (LanguageElement localization in templateConfig.Localizations)
            {
                Dictionary<string, string> values;

                if (!Localizations.TryGetValue(localization.Code, out values))
                {
                    values = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
                    Localizations.Add(localization.Code, values);
                }

                foreach (LocalizedEntry entry in localization.Localizations)
                {
                    values[entry.Key] = entry.Value;
                }
            }
        }

Usage Example

        public override Passbook.Generator.PassGeneratorRequest GetPass(string serialNumber)
        {
            PassGeneratorRequest request = new PassGeneratorRequest();

            request.PassTypeIdentifier = PassTypeIdentifier;
            request.SerialNumber = Guid.NewGuid().ToString("D");

            TemplateModel parameters = new TemplateModel();

            parameters.AddField("origin", FieldAttribute.Label, "San Francisco");
            parameters.AddField("origin", FieldAttribute.Value, "SFO");

            parameters.AddField("destination", FieldAttribute.Label, "London Heathrow");
            parameters.AddField("destination", FieldAttribute.Value, "LHR");

            parameters.AddField("seat", FieldAttribute.Value, "7A");
            parameters.AddField("boarding-gate", FieldAttribute.Value, "F12");
            parameters.AddField("passenger-name", FieldAttribute.Value, "John Appleseed");

            request.AddBarCode("M1APPLESEED/JMR EZQ7O92 GVALHRBA 00723319C002F00009100", BarcodeType.PKBarcodeFormatPDF417, "iso-8859-1");

            request.LoadTemplate("BoardingPass", parameters);

            return request;
        }
All Usage Examples Of Passbook.Generator.PassGeneratorRequest::LoadTemplate