Tuesday, March 6, 2012

Notes: Order of posts to complete – 6/3/2012

As I said before this is not my real blog just a place holder for the drafts for my real blog. Subscribers should note that the blog posts are being updated on the fly, so use a feed reader that can handle this (Google Reader will not mark the post differently for updated posts).

I am sure some of you are wondering why I don’t post some of the more complete posts and for those posts I simply would like to post them in a certain order. It’s like writing a story I can’t really post on FluentMigrator before I write about the SDE or ArcObjects (since extending FluentMigrator is all about using ArcObjects to deploy the SDE).

Since at this point in time I have 55 draft posts I decided to add categories to the posts:

  • ????? – haven’t reviewed the post
  • Title Draft – only has a title and lots of TODOs
  • Draft – has some text but the text is rough
  • Beta – almost ready: spelling, ordering, checking the grammar…
  • Post In Wait – waiting for another post before posting

I have also decided to post the exceptions/error solving posts without waiting for background posts (if they are ready). Since they are mostly posts I am not going to edit.

I have decided to write here the order of the Notes’ drafts I am going to work on:

  1. Unit Tests: What to Test?  - Draft: code, go over all the cases, add my Resharper template, TODOs 
  2. Unit Test Code Coverage – Title Draft: even the TODOs are rough…
  3. To Rhino or not to Rhino? – Draft: code, font, the explanations are too short
  4. Tracking down a simple bug - Draft: code, font, draw the examples
  5. TODO: ArcObjects: WorkspaceUtils
  6. File GeoDatabase: Getting the Workspace – Draft: might need to split to 2 posts
  7. Multithreading with ArcObjects – Draft: font, code, TODOs
  8. Refactoring ESRI: Making the Examples Readable, Part 1 – Draft: continue the example, maybe remove the first refactor result ( just keep the number of lines), to many text strings in the end code – move to consts
  9. Mocking ESRI (testing wise) – Title Draft: even the TODOs are rough… Though the title is just right!
  10. TODO: SQL
  11. TODO: SQL Server
  12. ArcSDE–Connection to the Geodatabase – Draft: write how to get the service number, images in old style, remove del tags
  13. TODO: ArcSDE: Different DB types
  14. SDE – A look at the layer table structure – Draft: images, font, write about deleting features and triggers
  15. Reading SDE queries – Draft: images, font, weird characters, SQL, del tags, grammer
  16. Deleting SDE left over tables – Draft: images, font, SQL, rewrite query
  17. TODO: WorkspaceProvider
  18. TODO: WorkspaceUtils
  19. Working with CAD files in ArcObjects – Draft: code, links, font
  20. Upgrading the code base from ArcGIS 9.3.1 to 10 – Beta: images, code, del tags
  21. Rename Polyline layer – Beta: code, font
  22. ArcSDE–Adding domains from code – Beta: refactor the code
  23. Simplify and the ArcEngine license – Draft: code, font, images, name should be different
  24. ESRI License error – Draft: code, font, images, name should be different
  25. TODO: ArcGIS Server Introduction
  26. TODO: ArcGIS Server Publish mxd
  27. ArcGIS Server layer problem (DB is down) – Beta: font, images, post clearing cache first, del tags ***
  28. Restart ArcGIS Server service – Beta: font, images, del tags ***
  29. Calling ArcGIS Server service model - Beta: font, images, del tags ***
  30. Address Locator Style: Introduction – Draft: finish a working style before posting this
  31. Address Locator Style: Customizing Guide – Draft: finish a working style before posting this
  32. Address Locator Style: Testing – Draft: finish a working style before posting this
  33. FluentMigrator: Understanding the Framework Code – Draft: TODOs
  34. FluentMigrator: Extending the Framework – Draft: not finished
  35. ArcSDE/SQL Server Tables–What is Sde’s and what is Regular – Beta: SQL query, del tags, post after FM
  36. Stuck Database – the JOY! – Beta: font, images, maybe add a conclusion, post after intro to FM
  37. VS2010: “The project type is not supported by this... – Beta: cut the post (won’t continue to work on it), images, font ***
  38. Silverlight Memory Leak, Part 3 – ?????
  39. Silverlight Memory Leak, Part 4 – ?????

I have some more posts to which I haven’t decided the order:

  1. Coded UI testing for Silverlight application – Draft: really rough, font, images
  2. Silverlight ESRI App - IIS is dead–Error 5011 – Draft: TODOs, wrong date, del tags
  3. IntelliTrace: – Draft: still rough
  4. TODO: Using DotTrace
  5. Silverlight error connecting to WCF – ?????
  6. Rebuild All failed without any errors (Silverlight), Part 2 – ?????
  7. TODO: Probability, Why do I need it?
  8. Linq2Sql: Changing the Database Schema at Runtime (without XMLs) – Draft: still waiting for a solution
  9. Silverlight: Adding Google Streets View, part 2 – ?????
  10. LINQ and WCF – ?????
  11. Game Programming with XNA – ????
  12. HTTP Error 503. The service is unavailable. – ?????
  13. Combining WMV Videos – ????

Posts that I might not post:

  1. Writing a Windows Live Writer Plugin – Draft: didn’t write one…, images, del tags, ice tags
  2. Windows Live Writer: Tag providers, Part 2 – need to go over all the tag providers and need to get a response from some of them so it might take some time, Technorati is still giving me hard times.
  3. Offline Blog Writing Applications - Draft
  4. The Library example – Haven’t decided on posting this or not might change it to sheep and shepherds…
  5. Python – Introduction – Draft: just not written well
  6. Silverlight 4 – more for me…

I think that’s enough for now…

Refactoring ESRI: Making the Examples Readable, Part 1

This is the refactor for ESRI example for creating an Address Locator in a Personal GeoDatabase:

  1. public void AddressLocatorPersonalGDBViaArcObjects()
  2. {
  3.     // Open the default local locator workspace to get the locator style.
  4.     System.Object obj = Activator.CreateInstance(Type.GetTypeFromProgID(
  5.         "esriLocation.LocatorManager"));
  6.     ILocatorManager2 locatorManager2 = obj as ILocatorManager2;
  7.     ILocatorWorkspace locatorWorkspace = locatorManager2.GetLocatorWorkspaceFromPath
  8.         ("");
  9.  
  10.     // Get the locator style to base the new locator.
  11.     ILocatorStyle locatorStyle = locatorWorkspace.GetLocatorStyle(
  12.         "US Address - Dual Ranges");
  13.  
  14.     // Open the feature class to use as reference data.
  15.     IWorkspaceFactory2 workspaceFactory2 = new AccessWorkspaceFactoryClass();
  16.     IWorkspace workspace = workspaceFactory2.OpenFromFile(
  17.         @"D:\workspace\arcobjects\location\redlands.mdb",
  18.         0);
  19.     IFeatureWorkspace featureWorkspace = (IFeatureWorkspace) workspace;
  20.     IFeatureClass featureClass = featureWorkspace.OpenFeatureClass("Streets");
  21.  
  22.     // Set the feature class as the primary reference data table for the locator.
  23.     IDataset dataset = (IDataset) featureClass;
  24.  
  25.     IReferenceDataTables referenceDataTables = (IReferenceDataTables) locatorStyle;
  26.     IEnumReferenceDataTable enumReferenceDataTable = referenceDataTables.Tables;
  27.     enumReferenceDataTable.Reset();
  28.  
  29.     IReferenceDataTable referenceDataTable = enumReferenceDataTable.Next();
  30.     IReferenceDataTableEdit referenceDataTableEdit = (IReferenceDataTableEdit)
  31.                                                      referenceDataTable;
  32.     IName name = dataset.FullName;
  33.     referenceDataTableEdit.Name_2 = (ITableName) name;
  34.  
  35.     // Store the new locator in the same workspace as the reference data.
  36.     if (referenceDataTables.HasEnoughInfo)
  37.     {
  38.         locatorWorkspace = locatorManager2.GetLocatorWorkspaceFromPath(
  39.             @"D:\workspace\arcobjects\location\redlands.mdb");
  40.         ILocator locator = locatorWorkspace.AddLocator("New Redlands Locator",
  41.                                                        (ILocator) locatorStyle, "", null);
  42.     }
  43. }

The first order of business is refactoring this method.

The first thing to go was this:

  1. IFeatureClass featureClass = featureWorkspace.OpenFeatureClass("Streets");
  2.  
  3. // Set the feature class as the primary reference data table for the locator.
  4. IDataset dataset = (IDataset) featureClass;

To:

  1. IDataset dataset = (IDataset)featureWorkspace.OpenFeatureClass("Streets");

There was a few other things like: referenceDataTable->referenceDataTableEdit,

The end result was:

  1. public void AddressLocatorPersonalGDBViaArcObjects()
  2. {
  3.     // Open the default local locator workspace to get the locator style.
  4.     System.Object obj = Activator.CreateInstance(Type.GetTypeFromProgID(
  5.         "esriLocation.LocatorManager"));
  6.     var locatorManager2 = obj as ILocatorManager2;
  7.     var locatorWorkspace = locatorManager2.GetLocatorWorkspaceFromPath
  8.         ("");
  9.  
  10.     // Get the locator style to base the new locator.
  11.     var locatorStyle = locatorWorkspace.GetLocatorStyle(
  12.         "US Address - Dual Ranges");
  13.     // Open the feature class to use as reference data.
  14.     IWorkspaceFactory2 workspaceFactory2 = new AccessWorkspaceFactoryClass();
  15.     var featureWorkspace = (IFeatureWorkspace)workspaceFactory2.OpenFromFile(
  16.         @"D:\workspace\arcobjects\location\redlands.mdb",
  17.         0);
  18.  
  19.     // Set the feature class as the primary reference data table for the locator.
  20.     var dataset = (IDataset)featureWorkspace.OpenFeatureClass("Streets");
  21.     
  22.     var referenceDataTables = (IReferenceDataTables)locatorStyle;
  23.  
  24.     var enumReferenceDataTable = referenceDataTables.Tables;
  25.     enumReferenceDataTable.Reset();
  26.  
  27.     var referenceDataTableEdit = (IReferenceDataTableEdit) enumReferenceDataTable.Next();
  28.     
  29.     referenceDataTableEdit.Name_2 = (ITableName)dataset.FullName;
  30.  
  31.     // Store the new locator in the same workspace as the reference data.
  32.     if (referenceDataTables.HasEnoughInfo)
  33.     {
  34.         locatorWorkspace = locatorManager2.GetLocatorWorkspaceFromPath(
  35.             @"D:\workspace\arcobjects\location\redlands.mdb");
  36.         locatorWorkspace.AddLocator("New Redlands Locator",
  37.                                                        (ILocator) locatorStyle, "", null);
  38.     }
  39. }

39 lines without any loss of readability, not that it says much – it still not readable!

The next thing to look at is creating the ILocatorManager2, I don’t know about you but I prefer getting an exception about converting than about Null reference:

  1. (ILocatorManager2)Activator.CreateInstance(Type.GetTypeFromProgID("esriLocation.LocatorManager"));

(that also solved the problem of the ‘obj’ variable naming)

The next part is creating a Utils class for LocatorWorkspace:

  1. public class LocatorWorkspaceUtils
  2. {
  3.     private readonly ILocatorManager2 _locatorManager;
  4.  
  5.     #region Singleton
  6.  
  7.     private static readonly LocatorWorkspaceUtils instance = new LocatorWorkspaceUtils();
  8.  
  9.     // Explicit static constructor to tell C# compiler
  10.     // not to mark type as beforefieldinit
  11.     static LocatorWorkspaceUtils()
  12.     {
  13.     }
  14.  
  15.     private LocatorWorkspaceUtils()
  16.     {
  17.         _locatorManager =
  18.             (ILocatorManager2)Activator.CreateInstance(Type.GetTypeFromProgID("esriLocation.LocatorManager"));
  19.     }
  20.  
  21.     public static LocatorWorkspaceUtils Instance
  22.     {
  23.         get { return instance; }
  24.     }
  25.  
  26.     #endregion

It’s singleton with ILocatorManager already set, now the only thing we use it for is:

  1. private ILocatorWorkspace GetLocatorWorkspace(string fileLocation)
  2. {
  3.     return _locatorManager.GetLocatorWorkspaceFromPath(fileLocation);
  4. }

Getting the style:

  1. public ILocatorStyle GetLocatorStyle(string styleName)
  2. {
  3.     return GetLocatorStyle(styleName, "");
  4. }
  5.  
  6. public ILocatorStyle GetLocatorStyle(string styleName, string fileLocation)
  7. {
  8.     var locatorWorkspace = GetLocatorWorkspace(fileLocation);
  9.  
  10.     // Get the locator style to base the new locator.
  11.     return locatorWorkspace.GetLocatorStyle(styleName);
  12. }

Adding the locator style:

  1. public void AddLocatorStyle(string styleName, string fileLocation, ILocator locatorStyle)
  2. {
  3.     var locatorWorkspace = GetLocatorWorkspace(fileLocation);
  4.     locatorWorkspace.AddLocator(styleName, locatorStyle, "", null);
  5. }

Setting the dataset name in the locator:

  1. public void SetLocatorDatasetName(IName datasetName, ILocatorStyle locatorStyle)
  2. {
  3.     var referenceDataTables = (IReferenceDataTables)locatorStyle;
  4.  
  5.     var enumReferenceDataTable = referenceDataTables.Tables;
  6.     enumReferenceDataTable.Reset();
  7.  
  8.     var referenceDataTableEdit = (IReferenceDataTableEdit)enumReferenceDataTable.Next();
  9.  
  10.     //var name = new TableNameClass {Name = datasetName};
  11.  
  12.     referenceDataTableEdit.Name_2 = (ITableName)datasetName;
  13. }

Checking if the locator is good to be saved:

  1. public bool IsLocatorValidForSave(ILocatorStyle locatorStyle)
  2. {
  3.     return ((IReferenceDataTables)locatorStyle).HasEnoughInfo;
  4. }

Now the code looks like:

  1. public void AddressLocatorPersonalGDBViaArcObjects()
  2. {
  3.     // Get the locator style to base the new locator.
  4.     var locatorStyle = LocatorWorkspaceUtils.Instance.GetLocatorStyle("US Address - Dual Ranges");
  5.  
  6.     // Open the feature class to use as reference data.
  7.     IWorkspaceFactory2 workspaceFactory2 = new AccessWorkspaceFactoryClass();
  8.     var featureWorkspace = (IFeatureWorkspace)workspaceFactory2.OpenFromFile(
  9.         @"D:\workspace\arcobjects\location\redlands.mdb",
  10.         0);
  11.  
  12.     // Set the feature class as the primary reference data table for the locator.
  13.     var dataset = (IDataset)featureWorkspace.OpenFeatureClass("Streets");
  14.     LocatorWorkspaceUtils.Instance.SetLocatorDatasetName(dataset.FullName, locatorStyle);
  15.  
  16.     if (LocatorWorkspaceUtils.Instance.IsLocatorValidForSave(locatorStyle))
  17.     {
  18.         LocatorWorkspaceUtils.Instance.AddLocatorStyle("New Redlands Locator",
  19.             @"D:\workspace\arcobjects\location\redlands.mdb",
  20.             (ILocator)locatorStyle);
  21.     }
  22. }

Now something seems a bit off here, the only reason we open the workspace for the MDB file is to get the dataset FullName, WTF?!? Why not just pass the name as a… wait for it… string?

TODO: continue this

 

Keywords: ESRI, ArcObjects, refactor

IceRocket Tags: ,,

Bug: Using TimeStamp with Time Zone in (Oracle) Entity Framework

For the past week I have been working on the problem of correctly saving TimeStamp with Time Zone in (Oracle) Entity Framework. The problem is both DateTime and OffsetDateTime when saved in that field in the DB are missing a critical info – the location offset given to them. They always save the field with the offset of the client, in my case +02:00.

In the beginning I was sure it was something that will be easily fixed, simply because I came through this StackOverFlow question/answer which talked about changing the numeric data type so that bool is number(1,0). So I was sure it was a generic interface, well its not!

Looking into Oracle’s EFOracleProviderServices decompiled source (using JetBrains’s dotPeek) I tried looking for config values but instead found the bug in Oracle:

  1. new OracleParameter(name, (object) ((DateTimeOffset) value).DateTime))

Meaning that although you might have gone into the trouble of creating a DateTimeOffset (with no trouble at all), Oracle just knows what’s best and will convert it to DateTime (plus enable a boolean that will later convert it back to offset).

I have tried using Open Source Providers but so far no luck (had problem finding the provider or it wasn’t in .net 4.0), I will try again tomorrow.

http://eforacleodpprovider.codeplex.com

http://archive.msdn.microsoft.com/EFOracleProvider/Release/ProjectReleases.aspx?ReleaseId=1395

//TODO: Continue this

 

Resources:

StackOverFlow: Where do I put Oracle EntityFramework Mapping settings for LinqPad?