samedi 8 novembre 2014

Templating from Object

The idea here is to provide a really lightweight template engine.
The point here is to demonstrate how light such a code can be. In his current state the code suffers some defaults discussed at the end.
A template is a String comprising tokens of the form [ProperyName] or [ProperyName:format]. PropertyName can be any name of any property of an object or navigable property of the object. However IEnumerable are not handled.
public static String MergeObjectFromTemplate(String st, Object o) {
    if (String.IsNullOrEmpty(st)) throw new ArgumentNullException("st");
    if (o == null) return st;

    Int32 i = st.IndexOf("[");
    Int32 j = 0;
    String pName;
    Object lo;
    String[] a;
    while (i >= 0) {
        j = st.IndexOf("]");
        if (j == -1)
            return st;

        pName = st.Substring(i + 1, j - i - 1);
        a = pName.Split(':');
        lo = GetObjectByReflection(o, a[0]);
        if (lo == null) { 
            st = st.Replace("[" + pName + "]", "_" + pName + "_");
        } else {
            if (a.Count() == 1) {
                st = st.Replace("[" + pName + "]", lo.ToString().Trim());
            } else {
                st = st.Replace("[" + pName + "]", String.Format("{0:" + a[1] + "}", lo));
            }
        }

        i = st.IndexOf("[");
    }

    return st;
}
The key function here is: GetObjectByReflection.
public static Object GetObjectByReflection(Object src, String propertyName) {
    Type type;
    Object o = src;
    foreach (String fragment in propertyName.Split('.')) {
        type = o.GetType();
        PropertyInfo pi = type.GetProperty(fragment);
        if (pi == null) {
            o = null;
            break;
        }
        o = pi.GetValue(o, null);
        if (o == null) return String.Empty;
    }

    return o;
}
That is it!
Well, not totally. It remains some defaults, but for 50 lines, that's may be not so bad. Known defaults are:

  • Bad format string are not handled.
  • This is blocking, if throwing an exception is blocking. Correcting it is easy once expected behavior is known. At the time the code was updated I was just needing to format DateTime in a relatively safe environment: templates are edited by me. It is another story if you want to allow user to produce templates.
  • Excepted for a bad format String, the code always returns a String, even if a bad object is provided, or bad property name is used.

Windows 8 new account

I recently try to create a new account on a relatively old windows 8 computer (one year, several updates).

Account creation was just fine but... impossible to connect due to the inability of the system to load the profile of the newly created user.

After some googling and hours, I finally found that the problem was the inability of the system to create, by copying from c:\Users\Default, the new profile.

I found which files were involved with the windows events observer:


Once the rights fixed, I was able to use the new account.


mercredi 29 octobre 2014

Setup Apache 2.2 for FastCgi handling of PHP on windows 7

Why ?
Because I used to setup PHP as an Apache Module. But, this morning, after starting my good old 2012 apache2.2 service, it says "mysql driver not installed"! I'm quite sure they were in 2012. In addition, IIS, on the same php.ini, says they are installed. So I try Apache with FastCGI as IIS does. Then, my drivers come back.
We here assume that
  • Apache 2.2 is yet installed.
  • php5 is in a directory named c:\php-5
From this assumptions:
Edit httpd.conf as follow:
    #LoadModule php5_module "c:/php-5/php5apache2_2.dll"
    #AddHandler application/x-httpd-php .php
    #PHPIniDir "C:/php-5"

    LoadModule fcgid_module modules/mod_fcgid.so
    FcgidInitialEnv PHPRC "c:/php-5"
    AddHandler fcgid-script .php
    FcgidWrapper "c:/php-5/php-cgi.exe" .php
And, to avoid HTTP 403 errors, don't forget ExecCGI in your directory configuration.
    
        Options Indexes FollowSymLinks ExecCGI
        #...
    
By the way, mod_fcgid.so can be found here (http://mir2.ovh.net/ftp.apache.org/dist//httpd/binaries/win32/)
I'm on a windows 7 64bits professional edition.

lundi 22 septembre 2014

Managing default values for entities in EF II: the just on time way

You probably still know how to create an entity with default values. This is the 'from birth' way.
Well, there is another way to set default values: a 'just on time way'.
The principle is to override the SaveChanges method to explore entities in the tracker to find the one having unset values, just like in the 'from birth way'. Here it is how it can be done.
        //the default value for datetime properties
        public DateTime DefaultDate { get; set; }

        public override int SaveChanges() {
            Boolean defaultSet = false;

            //seeking only pertinent entities
            foreach (DbEntityEntry dbee in ChangeTracker.Entries().
                     Where(x => x.State != EntityState.Deleted && x.State != EntityState.Detached ) ) {
                defaultSet = false;
                Object o = dbee.Entity;
                Type t = o.GetType();
                foreach ( MemberInfo m in t.GetProperties() ) {
                    PropertyInfo p = t.GetProperty(m.Name);
                    switch (Type.GetTypeCode(p.PropertyType)) {
                        case TypeCode.String:
                            if (p.GetValue(o, null) == null) {
                                p.SetValue(o, String.Empty, null);
                                defaultSet = true;
                            }
                            break;
                        case TypeCode.DateTime:
                            if ((DateTime)p.GetValue(o, null) == DateTime.MinValue) {
                                p.SetValue(o, DefaultDate, null);
                                defaultSet = true;
                            }
                            break;
                    }
                    
                    //update the state if needed
                    if ( defaultSet ) {
                        switch ( dbee.State ) {
                            case EntityState.Unchanged :
                                dbee.State = EntityState.Modified;
                                break;
                        }
                    }
                }
            }

            return base.SaveChanges();
        }
This could be used to implement business rules, such as LastModificationDate.
Before EF6, you may have to look for the tracker in the underlying ObjectContext
A full piece of code:
using System;
using System.Linq;
using System.Data.Entity;
using System.Collections.Generic;
using System.Data.Entity.ModelConfiguration;
using System.Reflection;
using System.Data.Entity.Infrastructure;

namespace testef {
    public class Order {
        public Int32 Id { get; set; }
        public String Title { get; set; }

        public virtual ICollection Details { get; set; }

        public DateTime OrderDate { get; set; }

        public virtual OrderType Type { get; set; }
    }

    public class OrderType {
        public String Code { get; set; }
        public String Description { get; set; }
    }

    public class OrderDetail {
        public virtual Order Order { get; set; }
        public Int32 Id { get; set; }
        public String D { get; set; }
        public Boolean IsActive { get; set; }
    }

    public class OrderDetailConfiguration : EntityTypeConfiguration {
        public OrderDetailConfiguration()
            : base() {
            HasRequired(d => d.Order).WithMany(o => o.Details);
        }
    }

    public class OrderConfiguration : EntityTypeConfiguration {
        public OrderConfiguration()
            : base() {
            HasRequired(d => d.Type).WithMany();
        }
    }

    public class OrderTypeConfiguration : EntityTypeConfiguration {
        public OrderTypeConfiguration()
            : base() {
            Property(x => x.Code).HasColumnType("varchar").HasMaxLength(10);
            HasKey(x => x.Code);
        }
    }

    public class TestEFContext : DbContext {
        public DbSet Orders { get; set; }
        public DbSet Details { get; set; }

        public TestEFContext(String cs)
            : base(cs) {
            Database.SetInitializer(new DropCreateDatabaseAlways());
            //Database.SetInitializer(null);
            //Database.SetInitializer(new CreateDatabaseIfNotExists());
            //Database.SetInitializer(new CustomDataBaseInitializer());

        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder) {
        }

        public DateTime DefaultDate { get; set; }

        public override int SaveChanges() {

            Boolean defaultSet = false;
            foreach (DbEntityEntry dbee in ChangeTracker.Entries().
                     Where(x => x.State != EntityState.Deleted && x.State != EntityState.Detached ) ) {
                defaultSet = false;
                Object o = dbee.Entity;
                Type t = o.GetType();
                foreach ( MemberInfo m in t.GetProperties() ) {
                    PropertyInfo p = t.GetProperty(m.Name);
                    switch (Type.GetTypeCode(p.PropertyType)) {
                        case TypeCode.String:
                            if (p.GetValue(o, null) == null) {
                                p.SetValue(o, String.Empty, null);
                                defaultSet = true;
                            }
                            break;
                        case TypeCode.DateTime:
                            if ((DateTime)p.GetValue(o, null) == DateTime.MinValue) {
                                p.SetValue(o, DefaultDate, null);
                                defaultSet = true;
                            }
                            break;
                    }
                    if ( defaultSet ) {
                        switch ( dbee.State ) {
                            case EntityState.Unchanged :
                                dbee.State = EntityState.Modified;
                                break;
                        }
                    }
                }
            }

            return base.SaveChanges();
        }
    }

    public class CustomDataBaseInitializer : CreateDatabaseIfNotExists {
        public CustomDataBaseInitializer()
            : base() {
        }
    }
        
    class Program {
        static void Main(string[] args) {
            String cs = @"Data Source=ALIASTVALK;Initial Catalog=TestEF;Integrated Security=True; MultipleActiveResultSets=True";
            using (TestEFContext ctx = new TestEFContext(cs)) {
                ctx.DefaultDate = new DateTime(2000, 1, 1);

                OrderType t = new OrderType { 
                    Code = "T1",
                    Description = "type One"
                };
                ctx.Orders.Add(new Order { 
                    Title = "first order",
                    Type = t
                });

                ctx.SaveChanges();
            }

            using (TestEFContext ctx = new TestEFContext(cs)) {
                Order o = ctx.Orders.First();
                Console.WriteLine(o.OrderDate);
            }
        }
    }
}

mardi 9 septembre 2014

Bulleting in MsWord openXml document, using OpenXml SDK: the basics.

As often the most important is the understanding of the model, here of the XML model.
This article is a simplifcation of Working with Numbered Lists in Open XML WordprocessingML.
Bulleting is a part of Numbering.
Numbering is a NumberingProperties (numPr) property in a ParagraphProperties property of a Paragraph from a Document object.
That is a hierarchy as follow:
<w:document>
    <w:body>
        <w:p>
            <w:pPr>
                <w:numPr>
                    <w:ilvl>X</w:ilvl>
                    <w:numId val="Y" />
numId leads to an NumberingInstance (num) in NumberingDefinitionsPart of MainDocumentPart of WordProcessingDocument.
//word/numbering.xml
    <w:numbering>
        <w:num w:numId="Y">
            <w:abstractNumId w:val="Z" />
abstractNumId leads to a AbstractNum (abstractNum)
//word/numbering.xml
    <w:numbering>
        <w:abstractNum w:abstractNumId="Z" >
            <w:lvl w:ilvl="X">
                ...
            </w:lvl>
        <w:num w:numId="Y">
            <w:abstractNumId w:val="Z" />

It is now clear that to get the bullet properties we have to traverse from the NumberingProperties of the Paragraph comprising the text of the bullet. Then we go to the NumberingDefinitionsPart searching the corresponding numId, That give us an abstractNumId which, with the ilvl of the ParagraphProperties, give us the Level we seek.
Remark
If you have a ParagraphProperties at the Paragraph level, the Paragraph stays a bullet (because of Paragraph.NumberingProperties), but the values from Paragraph.ParagraphProperties prevail over those of NumberingProperties.

mardi 1 juillet 2014

Filter DataGridView out of the DbContext

Imagine you load data into a DataGridView.DataSource from a database.
Then you want to allow end user to filter the results, but without hitting database again.
So you use a TextBox to prompt a filter an, OnTextChanged
private void tbFilter_TextChanged(object sender, EventArgs e) {
    CurrencyManager cm = (CurrencyManager)BindingContext[dgvList.DataSource];
    cm.SuspendBinding();
    foreach (DataGridViewRow r in dgvList.Rows) {
        r.Visible = String.IsNullOrEmpty(tbFilter.Text) || r.Cells[1].Value.ToString().Contains(tbFilter.Text);
    }
    cm.ResumeBinding();
}
Here the difficulty is the use of a CurrencyManager.

samedi 10 mai 2014

using UserManager/IdentityFramework with an Int32 as TKey for IUser

This article is about how to use Int32 as a key for users in asp.net identity.

First the model. As I seed it, you can also see how to instanciate the UserManage.

For this thank you to SymbolSource and to Microsoft of course!

namespace cclw4c.Models
{
    // You can add profile data for the user by adding more properties to your ApplicationUser class, please visit http://go.microsoft.com/fwlink/?LinkID=317594 to learn more.
    public class ApplicationUser : IdentityUser <Int32, CustomUserLogin, CustomUserRole, CustomUserClaim&gt: {
    }

    public class CustomRole : IdentityRole<Int32, CustomUserRole> {
        public CustomRole() { }
        public CustomRole(String name) { Name = name; }
    }

    public class CustomUserRole : IdentityUserRole<Int32> { }
    public class CustomUserClaim : IdentityUserClaim<Int32> { }
    public class CustomUserLogin : IdentityUserLogin<Int32> { }

    public class ApplicationUserDbContext : IdentityDbContext<ApplicationUser, CustomRole, Int32, CustomUserLogin, CustomUserRole, CustomUserClaim>
    {
        public ApplicationUserDbContext()
            : base("ApplicationUser")
        {
            Database.SetInitializer<ApplicationUserDbContext>(new CreateDatabaseIfNotExistsWithSeedData());
        }

        
    }

    public class CreateDatabaseIfNotExistsWithSeedData : CreateDatabaseIfNotExists<ApplicationUserDbContext> {
        protected override void Seed(ApplicationUserDbContext context) {
            base.Seed(context);

            var user = new ApplicationUser() { UserName = "root" };
            UserManager<ApplicationUser, Int32> am = 
                new UserManager<ApplicationUser, Int32>(new UserStore<ApplicationUser, CustomRole, Int32, CustomUserLogin, CustomUserRole, CustomUserClaim>(context));
            IdentityResult ir = am.Create(user, "******");
            if (ir.Succeeded) {
                RoleManager<CustomRole, Int32> rm = new RoleManager<CustomRole, Int32>(new RoleStore<CustomRole, Int32, CustomUserRole>(context));
                rm.Create(new CustomRole("Root"));
                am.AddToRole(user.Id, "Root");
            }
        }
    }
}   
Second use
Int32.Parse(User.Identity.GetUserId()
every where it is needed.
Hightlight on UserManager instanciation
new UserManager<ApplicationUser, Int32>(new UserStore<
    ApplicationUser, 
    CustomRole, 
    Int32, 
    CustomUserLogin, 
    CustomUserRole, 
    CustomUserClaim
>(context));