Spring Cleaning

I received a letter from my landlord this week notifying me that they would be coming through on Monday and spraying for bugs. The letter said that I should clear off my kitchen counters, and move everything out from the cabinets below the counters, and from under the bathroom sink. I only recall them sending out a notice like this once before, maybe seven or eight years ago. As far as I can tell, we don’t actually have a pest problem, but maybe there’s an issue elsewhere in the building.

Either way, I took this as an opportunity to do some spring cleaning this morning. In addition to throwing out a bunch of random stuff from the kitchen, I also decided to toss out a bunch of old computer books. I threw out maybe 30 or so books. I’ve done this before, and probably blogged about it, so I’m not sure if I’l be saying anything new here. But I wanted to take a break from cleaning and write up a blog post, so here we are.

I haven’t been buying a lot of hard-copy computer books lately, preferring DRM-free e-books instead. At my current job, I have exactly one hard-copy book at my desk. I also have three e-books on my computer that I refer to regularly. At my previous job, I kept maybe a half-dozen hard-copy books on my desk, and a few e-books. At the job before that, I had a whole bookshelf unit, probably six feet tall and three feet wide, full with books (and admittedly a large array of random knick-knacks), most of which I’d bought myself. I left quite a few of them behind when we went out of business, but I took a couple of boxes of them home; most of those went into the recycling dumpster today.

I’ve been buying most of my e-books from O’Reilly. Generally, I wait for them to have a good sale, as they did last week, on the Day Against DRM. Packt had a really good sale too, any book for $10. I ended up buying three books from Packt. They publish such a wide variety of stuff, I found myself with maybe 15 tabs open while I was shopping, looking at all the cool stuff I’d like to learn about. But I know, at this stage in my life, and with the speed that technology changes, that I’m not going to read 15 programming-related books before at least 12 of them are hopelessly out of date. So I settled on buying just three.

I should make a point of checking back in a year, and seeing how many of them I’ve actually read. But, hey, if I don’t read them, I’m only out $10 each and there’s nothing to drag out to the recycling bin!

F# on the Mac

I’ve been messing around with F# lately. It works fine in VS 2013 Express on my Windows 8 laptop, but I wanted to be able to play around with it on my Mac too. I have Xamarin Studio installed on the Mac, and I added the F# support to it, and that works OK. I wanted to be able to use FSI from the terminal too, though. That turns out to be pretty easy too, though it’s called “fsharpi” instead of “fsi” like it is on the PC.

base 36 conversion

I haven’t posted any code on here in a while, so here’s a quick little bit of code that might be useful to somebody. I had to write some code this week to do conversion from base 10 to base 36 and back, in Dynamics AX. So I just took some simple code from the Wikipedia article on base 36 and converted it to X++. Nothing fancy. The code below is all in a job, but I’ll probably put it in a utility class when I actually implement it.

// https://gist.github.com/andyhuey/5c2404b65939b5fccab8
static void AjhBase36Test(Args _args)
{
    // ajh 2014-05-07: 611.23
    // adapted from http://en.wikipedia.org/wiki/Base_36.
    // note: handles non-negative integers only.
    #define.NBASE(36)
    #define.CLIST("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ")

    int64 base36_decode(str base36_input)
    {
        int64 result = 0;
        real pow = 0;
        int i, pos;
        str c;

        for (i = strLen(base36_input); i > 0; i--)
        {
            c = subStr(base36_input, i, 1);
            pos = strScan(#CLIST, c, 1, #NBASE) - 1;
            if (pos == -1)
                return pos;  // error
            result += pos * power(#NBASE, pow);
            pow++;
        }
        return result;
    }
    str base36_encode(int64 input_num)
    {
        int64 work_num = input_num;
        str result = "";
        int digitidx;
        str c;

        do {
            digitidx = int642int(work_num mod #NBASE);
            c = subStr(#CLIST, digitidx + 1, 1);
            result += c;
            work_num = work_num div #NBASE;
        } while (work_num > 0);
        return strReverse(result);
    }

    print base36_decode("");
    print base36_decode("0");
    print base36_decode("A"); // 10
    print base36_decode("7PS"); // 10,000
    print base36_decode("255S"); // 100,000
    print base36_decode("!@#$"); // error
    print base36_encode(0);
    print base36_encode(123);
    print base36_encode(10000);
    print base36_encode(100000);
    pause;
}

more Project Euler stuff

This weekend’s main entertainment has been working on Project Euler problems. I’ve been trying to learn F#, so I decided to go back and re-do the first several Euler problems in F#. That exercise definitely helped me start to get a handle on doing things “the F# way”. I still have a long way to go, in terms of functional programming, but I’m learning.
I also solved problem 27, which was the next one up for me, in C#, as usual. Just because I wanted to make some progress, overall. This is only the second Euler problem I’ve solved this year, but I’m starting to get the “bug” again, so maybe I’ll knock a few more out this month.

checking user roles in AX 2012

It’s been a while since I’ve posted anything related to Dynamics AX / X++, so I thought I’d write up something I stumbled across recently. I had created a custom form, with a number of buttons on it. Two of the buttons needed to be available only to users in a certain role.

Well, first, I should point out that this can be done without any code. See here and here for information on that. And there are good reasons to do it this way, in many cases.

But there are also some good reasons to do this in code. It allows you to document what you’re doing and why, and it gives you more flexibility than just doing it through properties in the AOT. In my case, the business rules around this didn’t really fit into the permissions available in the AOT (Read, Update, Create, and Delete), so while I could have picked one of those and used it, it wouldn’t have accurately reflected the actual use case.

So I first wanted to find a method in X++ that would tell me if a given user was in a given role. I’m familiar with Roles.IsUserInRole from the .NET Framework, and have used it frequently in the context of ASP.NET sites using custom membership providers. So I looked for something similar in AX. That led me to the SysUserManagement Class.

I wound up writing a utility method that made use of this class:

// https://gist.github.com/andyhuey/9326912
/// <summary>
/// return true is the specified user is in any of the roles in the roleNames container.
/// </summary>
/// <param name="axUserId">
/// AX user id, e.g. curUserId()
/// </param>
/// <param name="roleNames">
/// container of role names to check. (use role NAME, not label.)
/// </param>
/// <returns>
/// true if user is in ANY of the specified roles.
/// </returns>
/// <remarks>
///
/// </remarks>

public static boolean isUserInRole(UserId axUserId, container roleNames)
{
    SysUserManagement userManagement = new SysUserManagement();
    List roleList = userManagement.getRolesForUser(axUserId);
    ListEnumerator listEnum = null;
    boolean isInRole = false;
    str roleStr = '';

    if (!roleList)
        return false;

    listEnum = roleList.getEnumerator();
    while (listEnum.moveNext())
    {
        if (conFind(roleNames, listEnum.current()))
            return true;
    }

    return false;
}

It worked fine on my VM, when logged in under my own ID. But, after deployment, it quickly became apparent that X++ code running under a normal user account (without SYSADMIN rights) can’t call methods in the SysUserManagement class. Now, there’s nothing I can see in the documentation that indicates that, but I should of course have tested my code under a normal user account.

So I rewrote my code to access the appropriate role-related tables directly, and it turns out that a normal user can do that, no problem:

// https://gist.github.com/andyhuey/9326939
/// <summary>
/// return true is the specified user is in any of the roles in the roleNames container.
/// </summary>
/// <param name="axUserId">
/// AX user id, e.g. curUserId()
/// </param>
/// <param name="roleNames">
/// container of role names to check. (use role NAME, not label.)
/// </param>
/// <returns>
/// true if user is in ANY of the specified roles.
/// </returns>
/// <remarks>
/// ajh 2014-02-12: previous method req'd admin perm. to run. Doh!
/// </remarks>

public static boolean isUserInRole(UserId axUserId, container roleNames)
{
    SecurityUserRole securityUserRole;
    SecurityRole securityRole;

    while select AotName from securityRole
    join securityUserRole
    where securityUserRole.User == axUserId
    && securityUserRole.SecurityRole == securityRole.RecId
    {
        if (conFind(roleNames, securityRole.AotName))
            return true;
    }
    return false;
}

So I guess the lesson here is to always test your code under a normal user account, and not to assume that the MSDN page for a given AX class will tell you everything you need to know about that class.

And, as with a lot of stuff in AX, I have a feeling that I’m still doing this “the wrong way”, even though my code works and is fairly simple. I’m guessing that, a year from now, I’ll have figured out that there’s a better way to do this.

Automation

I don’t think I’ve ever linked to an XKCD comic on this blog before, but (of course) I read it regularly. This one made me chuckle, thinking about a project I’m currently working on. I have a task that involves generating output files and sending them to a few other companies via SFTP, weekly. For “phase one,” I was going to manually run my program to generate the files, then send them out with WinSCP. Then, once that was running smoothly, I was going to add SFTP capability to the program, using SSH.NET.
This is kind of a long story, but to make it short, let’s just say that I’ve spent more time than is really reasonable taking care of the SFTP automation, and have yet to put it into production. Meanwhile, I just keep sending out the files every week, manually.
I’ve actually learned a lot about a couple of things in the process of working on this automation, but the amount of effort that’s gone into the automation, vs. the five minutes it takes to send the files off manually once a week is a bit silly.

Happy New Year

I thought I would write up a quick New Year’s post today, with a few status updates. I mostly write this kind of post for my own future reference, just to see where I’ve been, and what progress (if any) I’ve made in certain areas.

First, I’m happy to say that I stepped on the scale this morning, and it read 200 pounds. I started my diet around Sept 1, at 230 lbs, with the goal of losing a pound a week, until I hit 200. So I hit my goal, and I hit it earlier than expected. I guess my next goal will be to get down to 180, again at one pound per week. We’ll see if I can manage that. I’m still logging all my calories with the Lose It app on my phone. I think that’s really been the key factor in being successful. I’m not sure when I was last under 200 lbs. Maybe back in college?

On another front, I haven’t done quite so good. I blogged, back in November, about Coursera. I had enrolled in two courses, Algorithms, Parts I and II. Part I was technically done before I enrolled, and Part II was just starting up. My plan was to breeze through the first part, then catch up with the second. I was doing OK up through the end of November, and gotten through about 80% of the material in Part I, but then I got busy with other stuff in December, and never went back to it. Meanwhile, Part II wrapped up, so here it is, 2014, and I haven’t done anything on Coursera in a month. It looks like I can still watch the lectures, and even submit the programming assignments for auto-grading, so I may just pick it up again this month, and finish Part I. Or I may wait until it “officially” starts up again, on January 30, and try to take it in “real-time”. Then, I can take Part II in real-time, when it’s next offered in March. Or maybe I’ll just try something different this year, either from Coursera or EdX.

Professionally, I started my job at SHI in January 2013, so I’m at just about the one year mark there. I had my performance review with my boss yesterday, and it went pretty well. I’ll likely stick with SHI for another year. Given that I’m doing almost 100% Dynamics AX work though, I want to see about doing more web stuff on the side this year. Last year, I did a project for my former employer, Electric Vine, that allowed me to exercise my ASP.NET and JavaScript skills a bit, but I don’t have any consulting work planned for 2014. I should really find some, or maybe get involved in an open source project, or something like that.

Coursera

I recently decided that it would be a good idea to take an online course or two, from Coursera or EdX. I noticed that Algorithms II, a Princeton course with Robert Sedgewick as lecturer, was just starting up, so I signed up for that. I then also noticed that, while the Algorithms I class had ended recently, it was still possible to sign up for it and work through it. So I decided to do that first, and see if I could then move onto Algorithms II before it ended. Well, I’m getting through the material, but it’s taking some time, so I don’t think I’ll necessarily “catch up” prior to the official end of the Algorithms II class, but that’s not really necessary, though it would be nice.

The class uses Java, which I wasn’t initially that thrilled about, but I’m not really having any trouble with it. It’s similar enough to languages I know, like C++ and C#, that it’s easy enough to pick it up as I go. And the kind of work we’re doing in the class is such that I don’t need to worry much about user interface details or anything like that. Everything we’ve done so far is pretty much command-line stuff, with a little simple graphics work. So I haven’t had to worry about learning whatever the Java equivalent of Windows Forms or WPF is. (Swing maybe?)

The course material consists primarily of lecture videos (with Robert Sedgewick), programming assignments, and quizzes. For the first class, I’m choosing to watch the videos and do the programming assignments, but I’m skipping the quizzes.

The textbook for the Course is Algorithms (4th Edition), by Sedgewick and Kevin Wayne, who was also involved in creating the class, though he hasn’t shown up as a lecturer yet. (I’m not sure if all the lectures are by Sedgewick, or if they switch over to Wayne at some point.) The textbook is fairly expensive, but it’s not required. There’s a “booksite” for it that has all the material you’d need from the textbook.

The course recommends, but doesn’t require, that you use an IDE called “DrJava.” They even have a nice little installer that will quickly set up an environment for you with the JRE, DrJava, and some libraries that are used as part of the class. Very convenient and simple. I’m using the Windows version, but they have versions for Windows, Mac, and Linux.

DrJava isn’t a great IDE, but it’s simple and easy to use. I wouldn’t want to use it for day-to-day work, but just to write and test the kind of programs you need to create for an Intro to Algorithms course, it’s fine. (I do find myself falling back on Komodo Edit sometimes, though, when I need to do some “major” editing.)

So, basically, everything you need for the course is free (Coursera stuff, the textbook web site, and the development environment), which is great.

The lectures are quite good. I’m guessing that this is basically the same material that’s actually used for the Princeton undergrad Algorithms course, with some tweaks made to accommodate the requirements and limitations of the online course environment. Good use is made of visualizations for the algorithms, which is really key in understanding this kind of stuff.

The programming assignments are well thought-out too. They need to be structured in such a way that they can be run through an “auto-grader” that can evaluate and grade them in a reasonable way, so that imposes some limitations on them, but that’s fine. Basically, the programs need to be written to a well-defined API that the auto-grader will exercise, and then report on. It tests boundary conditions, large inputs, timing, memory use, and so on. The assignments are generally written so that there are levels of correctness that can be evaluated. There may be, say, 50 tests the grader can run your code through. Maybe the first time you submit your code, you’ll pass 25 of them. Then, you can tweak it and re-submit, and maybe get 35 right, and so on. So, the process of working through an assignment can be iterative, and you can learn as you go.

I think it’s great that stuff like this is available on the internet for free. I’m not sure where all this will lead though. More access to quality higher education for more people? Or will this stuff all be monetized at some point, leading to some kind of new status quo in higher education, where things are far more centralized than they are now, and the educational experience is far less personal? I don’t know; I’m sure people smarter than me have probably spend a lot of time thinking about that. For me, right now, it means I can take something vaguely equivalent to a Princeton undergrad course for free, in my living room!

Visual Studio 2013

I haven’t been paying too much attention to the VS 2013 launch, but I did read a few blog posts about it yesterday, including this one. Honestly, I haven’t really done much with VS 2012 yet, other than using it to work on some Project Euler problems at home, and using it as a front-end to TFS 2012 at work. (My day-to-day programming work is mostly done in the AX IDE environment right now.) Some of the stuff they’re doing sounds interesting, but it doesn’t really apply to me right now. One of these days, I’d like to get back into some serious .NET work, possibly including some Azure stuff, and ASP.NET MVC, and maybe get a chance to mess around with stuff like Unity. I do have one new interesting side project going on right now, though, which I want to write up in more detail later.

Project Euler

I haven’t mentioned Project Euler on here in a while. Earlier this year, I got on a roll and solved the first 20 or so problems. Then, I got too busy with other stuff, and didn’t make any more progress. Well, I got interested in it again and finished through to problem 25. I’m hoping I can find time to get a few more done before the end of the year. I’m finding that Project Euler is a pretty good way to keep up some basic skills. The kind of programming I do at work is interesting, but it doesn’t really exercise certain “muscles”.