2.6 to 3.5 API Breaking Changes - Sequences and Field Renaming

Posts   
 
    
segilbert
User
Posts: 10
Joined: 02-Jul-2012
# Posted on: 02-Jul-2012 20:14:01   

I'm working with some inherited helper class referencing LLBLGen 2.6 API that I'm trying to upgrade to 3.5 API. I'm running into some conversion issues.

**1. Automate setting of sequences ** The helper class automates the mapping of sequences from a file. The 1st thing it does is clear out the sequence based on IsIdentity, then sets the sequences. During that process in 2.6 the following methods are available to set the Identity and ReadOnly members. In 3.5 I can't find similar methods and the properties have no public setter. How can I set the IsIdentity and ReadOnly members?

field.SetIdentityFlagManually(seq.bSet); field.SetReadOnlyFlagManually(seq.bSet);

Legacy Code


// make sure that there are no sequences set in this table
foreach (EntityFieldDefinition field in entity.Fields)
{
    if (field.IsIdentity)
    {
        Program.Write("Identity Not Listed in csv: " +entity.TargetName+"."+ field.TargetName + " Seq: " + field.IdentityValueSequenceName);
        
        Program.Write("Clearing Sequence: " + field.IdentityValueSequenceName + " on " + entity.TargetName + "." + field.TargetName);
        field.IdentityValueSequenceName = "";
        field.SetIdentityFlagManually(false);
        field.SetReadOnlyFlagManually(false);
    }
}


foreach(EntityFieldDefinition field in entity.Fields)
{
    foreach (SeqInfo seq in seqs)
    {
        if (string.Compare(field.TargetName, seq.sCol) == 0)
        {
            if (field.IsIdentity == seq.bSet &&
                field.IsReadOnly == seq.bSet &&
                ((!seq.bSet && string.IsNullOrEmpty(field.IdentityValueSequenceName)) ||
                (seq.bSet && string.Compare(field.IdentityValueSequenceName, schemaName + "." + seq.sSeq) == 0)))
            {
                // is the same settings
                Program.Write("SAME SETTINGS: " + field.IdentityValueSequenceName);
                Program.Write("Current Settings: Table: " + entity.TargetName + " Column: " + field.TargetName + " Seq: " + field.IdentityValueSequenceName + " IsReadOnly: " + field.IsReadOnly + " IsIdentity: " + field.IsIdentity);
            }
            else
            {
                Program.Write("DIFFERENT SETTINGS: " + field.IdentityValueSequenceName);
                Program.Write("Current Settings: Table: " + entity.TargetName + " Column: " + field.TargetName + " Seq: " + field.IdentityValueSequenceName +" IsReadOnly: " + field.IsReadOnly + " IsIdentity: " + field.IsIdentity);
                Program.Write("New Settings: Table: " + entity.TargetName + " Column: " + field.TargetName + " Seq: " + seq.sSeq + " IsReadOnly: " + seq.bSet + " IsIdentity: " + seq.bSet);

                if (seq.bSet)
                    Program.Write("Setting Sequence: " + seq.sSeq + " on " + entity.TargetName + "." + seq.sCol);
                else
                    Program.Write("Clearing Sequence: " + seq.sSeq + " on " + entity.TargetName + "." + seq.sCol);
                if (seq.bSet)
                {
                    field.IdentityValueSequenceName = schemaName + "." + seq.sSeq;
                }
                else
                {
                    field.IdentityValueSequenceName = "";
                }
                field.SetIdentityFlagManually(seq.bSet);
                field.SetReadOnlyFlagManually(seq.bSet);
            }
        }
    }
}

Partially Converted Code


foreach (FieldElement field in item.Key.Fields)
{
    FieldMapping map = item.Value.GetFieldMappingOfField(field);

    if (map.MappedTarget.IsIdentity)
    {
        Program.Logger.LogInfo("Identity Not Listed in csv: {0}. {1} Seq: {2}" + map.MappedTarget.Parent.Name, map.MappedTarget.FieldName, GetSequenceName(map.SequenceToUse));
        
        Program.Logger.LogInfo("Clearing Sequence: " + GetSequenceName(map.SequenceToUse) + " on " + map.MappedTarget.Parent.Name + "." + map.MappedTarget.FieldName);

        if (map.SequenceToUse != null)
        {
            map.SequenceToUse.Name = string.Empty;
        }
        //field.SetIdentityFlagManually(false);
        //field.SetReadOnlyFlagManually(false);
    }
}

 foreach (FieldElement field in item.Key.Fields)
{
    FieldMapping map = item.Value.GetFieldMappingOfField(field);
    
    foreach (SeqInfo seq in seqs)
    {
        if (string.Compare(map.MappedTarget.FieldName, seq.sCol) == 0)
        {
            if (map.MappedTarget.IsIdentity == seq.bSet &&
                field.IsReadOnly == seq.bSet &&
                ((!seq.bSet && string.IsNullOrEmpty(GetSequenceName(map.SequenceToUse, true))) ||
                (seq.bSet && string.Compare(GetSequenceName(map.SequenceToUse, true), string.Format("{0}.{1}", schema.SchemaOwner, seq.sSeq)) == 0)))
            {
                // is the same settings
                Program.Logger.LogInfo("SAME SETTINGS: {0}", GetSequenceName(map.SequenceToUse, true));
                Program.Logger.LogInfo("Entity Mapping Element: {0} to Mapped Field Element: {1}", field.Name, map.MappedFieldName);
                Program.Logger.LogInfo("Current Settings: Table: {0} Column: {1} Seq: {2} IsReadOnly: {3} IsIdentity: {4}", map.MappedTarget.Parent.Name, map.MappedTarget.FieldName, GetSequenceName(map.SequenceToUse, true), field.IsReadOnly, map.MappedTarget.IsIdentity);
            }
            else
            {
                Program.Logger.LogInfo("DIFFERENT SETTINGS: {0}", GetSequenceName(map.SequenceToUse, true));
                Program.Logger.LogInfo("Entity Mapping Element: {0} to Mapped Field Element: {1}", field.Name, map.MappedFieldName);
                Program.Logger.LogInfo("Current Settings: Table: {0} Column: {1} Seq: {2} IsReadOnly: {3} IsIdentity: {4}", map.MappedTarget.Parent.Name, map.MappedTarget.FieldName, GetSequenceName(map.SequenceToUse, true), field.IsReadOnly, map.MappedTarget.IsIdentity);
                Program.Logger.LogInfo("New Settings: Table: {0} Column: {1} Seq: {2} IsReadOnly: {3} IsIdentity: {4}", map.MappedTarget.Parent.Name + " Column: " + map.MappedTarget.FieldName + " Seq: " + seq.sSeq + " IsReadOnly: " + seq.bSet + " IsIdentity: " + seq.bSet);

                if (seq.bSet)
                {
                    Program.Logger.LogInfo(
                        "Setting Sequence: {0} on {1}. {2}", seq.sSeq, map.MappedTarget.Parent.Name, seq.sCol);
                    map.SequenceToUse.Name = seq.sSeq;
                }
                else
                {
                    Program.Logger.LogInfo("Clearing Sequence: on {1}. {2}", seq.sSeq, map.MappedTarget.Parent.Name, seq.sCol);
                    map.SequenceToUse.Name = string.Empty;
                }
                
                //field.SetIdentityFlagManually(seq.bSet);
                //field.SetReadOnlyFlagManually(seq.bSet);
            }
        }
    }
}   // End Loop

2. Field Renaming In 3.5 EntityDefinition no longer supports ValidateFieldName as it does in 2.6. How can I validate the field rename in 3.5?

entity.ValidateFieldName(field.MappedField.FieldName, ref errMsg, ref chkName);

Legacy Code:


 foreach (EntityDefinition entity in project.Entities)
                {
                    foreach (EntityFieldDefinition field in entity.Fields)
                    {
                        if (!field.OutputSettingValues.CustomProperties.ContainsValue("FieldRenameIgnore"))
                        {
                            string errMsg = null;
                            string chkName = null;
                            entity.ValidateFieldName(field.MappedField.FieldName, ref errMsg, ref chkName);
                            if (chkName != field.FieldName)
                            {
                                if (entity.ValidateFieldName(chkName, ref errMsg, ref chkName))
                                {
                                    Program.Write("   Renaming: {0}.{1} >> {2}", entity.Name, field.FieldName, chkName);
                                    field.FieldName = chkName;
                                    Program.IsLlblProjectModified = true;
                                }
                            }
                        }
                    }
                }

Partially Converted Code


foreach (KeyValuePair<EntityDefinition, GroupableModelElementMapping> item in project.GetAllEntitiesWithTheirMappingsForDatabase(DriverId))
{
    foreach (FieldElement field in item.Key.Fields)
    {
        if (!field.OutputSettingValues.CustomProperties.ContainsValue("FieldRenameIgnore"))
        {
            FieldMapping map = item.Value.GetFieldMappingOfField(field);

            string errMsg = null;
            string chkName = null;
            //project.MetaData.GetMetaDataForDriverID(project.MappingData.MappingDataPerDriverID[0].DriverID).ValidateTableFieldName()
            
            // How can I vaidate this field rename?
            //item.Key.ValidateFieldName(map.MappedTarget.FieldName, ref errMsg, ref chkName);
            
            
            if (chkName != field.Name)
            {
                // How can I vaidate this field rename?
                //if (entity.ValidateFieldName(chkName, ref errMsg, ref chkName))
                //{
                //  Program.Logger.LogInfo("   Renaming: {0}.{1} >> {2}", item.Key.FullName, field.Name, chkName);
                //
                //  Rename field
                //  field.Name = chkName;
                //  Program.IsLlblProjectModified = true;
                //}
            }
        }
    }
}

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 03-Jul-2012 07:18:13   

Hi Sean,

About (1), you are seeing that the interface has no setter. In this case: IProjectElementFieldMapTargetElement.IsIdentity ... and IMappableField.IsReadOnly

... however you can cast to the actual type which has a setter implementation. Example:

// obtain entity and its mappings
var entity = p.EntityModel.Vertices.FirstOrDefault(e=>e.Name=="Category");
var mappings = p.MappingData.MappingDataPerDriverID[0].FindEntityMappingByEntity(entity);

// obtain the fields mappings of those target fields who are auto-identity.
var autoGenFieldMappings = from fm in mappings.FieldMappings
    where fm.MappedTarget.IsIdentity
    select fm;

// set identity off
foreach(var fmmt in autoGenFieldMappings)
{
    ((DBField)fmmt.MappedTarget).IsIdentity = false;    
        ((FieldElement)fmmt.MappedFieldInstance).IsReadOnly = false;    
}

About (2), now you have specific validator classes that inherit from SD.LLBLGen.Pro.ApplicationCore.EntityModel.Validation.EntityModelNameValidator. One of them is GroupableElementNameValidator. Example:

var renameValidator = new GroupableElementNameValidator(entity, p);
string clsCompliantVersion = string.Empty;
bool renameSuccess = renameValidator.Validate("MyNewName", true, out clsCompliantVersion);

For more info about this, download the CoreAssembliesReferenceManual from customer area.

David Elizondo | LLBLGen Support Team
segilbert
User
Posts: 10
Joined: 02-Jul-2012
# Posted on: 03-Jul-2012 14:22:05   

Thanks! I will have a look CoreAssembliesReferenceManual.