Exemplo n.º 1
0
        public void LoadTemplate(string template, TemplateModel parameters)
        {
            PassbookGeneratorSection section = 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;
                }
            }
        }