Welcome to MSDN Blogs Sign in | Join | Help

Is programming a generic skill?

Came across a post by Justin Etheredge discussion whether changing between languages is just a matter of syntax.

Or, to pick a specific example, can a Java programmer quickly and easily learn to write C# code?

The answer is obviously "yes". Development is about a way of thinking and approaching problems, and given the similarity between Java and C#, a good Java developer should take a minimal amount of time to learn how to write functional code in C#. The biggest barrier is libraries, which are more different than the languages are.

The answer is equally as obviously "no". Sure, you can write functional code, but you will not be able to write idiomatic code. Like a high school senior with 4 years of French class on a trip to Paris, you can make yourself understood, but you aren't going to be mistaken as a native. You ask a question, somebody replies, "Ce ne sont pas vos oignons", and you just end thinking of soup.

So, yeah, you can write C# code, but it's going to be Java written in C#. Given the closeness of the languages, it may be sufficient, but you're going to force some refactoring on any idiomatic C# speakers who inherit your code.

It can be worse - when I first started writing in Perl, I wrote C code in Perl, which just doesn't work very well. And over time, I became at least functional, though perhaps not idiomatic in Perl (though, because of TMTOWTDI, it's hard to judge that in Perl).

However, if you can become idiomatic in multiple languages, your toolset broadens, and you become more useful in all your langauges.

Posted by ericgu | 5 Comments
Filed under:

Introduction to HealthVault Development #12: More than one person…

WeightTracker has gotten popular, and we have a new scenario. Our users would like us to extend our application so that they can easily enter weights for each member of the family.

This will require us to delve a bit more deeply into users, records, and authentication, and we’ll also explore the HealthVault shell a bit more…

Accounts and records

HealthVault separates the concept of accounts and records. An account is associated with a specific set of credentials, and a record contains the health information for a specific person.

It’s not uncommon for an account to have access to more than one record. A person might have access to their own record and the records of their children, spouse, or parents.

What data does your application use?

HealthVault supports two ways of dealing with records.

A single-record application – which is what WeightTracker is right now – is intended to work with only one person’s data (ie one record) at a time. The platform keeps track of which record the application is using, and when writing code, you just need to use PersonInfo.SelectedRecord.

A single-record application can switch between records by redirecting the user back to the HealthVault shell to choose the new record, and authorize it if necessary.

The alternate way of dealing with applications is to work with multiple records simultaneously.

We do this by adding the following entry to our web.config:

<add key="WCPage_IsMRA" value="true"/>

And we also need to tell the shell that we’re an MRA application whenever we ask it to do something. We do this in AuthorizeEmotion.aspx.cs, and we’ll modify the code there to the following:

string TargetQuery = "appid=cee3e0fc-03c6-40b4-9550-a151901b4a27&onopt1=Opt_Emotion&ismra=true";

Now, we can go back to the HealthVault PPE shell, create a second record, make sure our existing record no longer allows WeightTracker to use it (via the “sharing” tab). And when we run WeightTracker, we’ll see the following:

 

Note that there are now checkboxes next to the records. We select all of them, hit “continue”, and then we will need to authorize the application to access each record.

Displaying the list of authorized records

The list of records that the application is authorized is stored in PersonInfo.AuthorizedRecords. We’ll add a dropdown control to default.aspx:

Welcomes&nbsp;
    <asp:DropDownList ID="c_dropDownCurrentRecord" runat="server" Width="262px" OnSelectedIndexChanged="c_dropDownCurrentRecord_SelectedIndexChanged" AutoPostBack="True" />
    <br /><br />

and remove the references to c_labelUser from the code.

We will use the drop-down list to display the current list of authorized records and to select the one that we want. We’ll add the following code, and call PopulateRecordDropDown() at the beginning of Page_Prerender().

protected void c_dropDownCurrentRecord_SelectedIndexChanged(object sender, EventArgs e)
{
    foreach (Guid recordId in PersonInfo.AuthorizedRecords.Keys)
    {
        if (recordId.ToString() == c_dropDownCurrentRecord.SelectedValue)
        {
            PersonInfo.SelectedRecord = PersonInfo.AuthorizedRecords[recordId];
        }
    }
}

void PopulateRecordDropDown()
{
    c_dropDownCurrentRecord.Items.Clear();

    foreach (Guid recordId in PersonInfo.AuthorizedRecords.Keys)
    {
        HealthRecordInfo healthRecordInfo = PersonInfo.AuthorizedRecords[recordId];

        ListItem listItem = new ListItem(healthRecordInfo.Name, recordId.ToString());

        if (recordId == PersonInfo.SelectedRecord.Id)
        {
            listItem.Selected = true;
        }

        c_dropDownCurrentRecord.Items.Add(listItem);
    }
}

We then need to update our height code to deal with cases where the height isn’t there (or perhaps you already did this…)

Height height = GetSingleValue<Height>(Height.TypeId);
if (height != null)
{
    if (height.Value.DisplayValue != null)
    {
        c_labelHeight.Text = height.Value.DisplayValue.ToString();
    }
    else
    {
        c_labelHeight.Text = height.Value.ToString();
    }
}

and

string bmiString = String.Empty;
if (height != null)
{
    double bmi = weight.Value.Kilograms / (height.Value.Meters * height.Value.Meters);
    bmiString = String.Format("{0:F2}", bmi);
}

After all of that, if you run the application, is should now allow you to select between the records. However, we still need to save the information about which record is current.

Saving the current record

To store the identifier of the current record, what we would like is a storage place that is associated with the person using the application rather than the a specific record. HealthVault provides that through the GetApplicationsSettings() and SetApplicationSettings() methods on the application connection. We could use those directly by putting some XML in there and then pulling it out, but we’re going to encapsulate that in a class. Here’s what we need to do:

First, we’ll create a new class named “WeightTrackerApplicationSettings”.

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml;
using System.Xml.XPath;
using System.Text;

using Microsoft.Health;

public class WeightTrackerApplicationSettings
{
    private Guid _selectedRecordId;

    public Guid SelectedRecordId
    {
        get { return _selectedRecordId; }
        set { _selectedRecordId = value; }
    }

    public void LoadFromHealthVault(AuthenticatedConnection authenticatedConnection)
    {
        IXPathNavigable settingsNavigable = authenticatedConnection.GetApplicationSettings();
        if (settingsNavigable == null)
        {
            return;
        }

        XPathNavigator navigator = settingsNavigable.CreateNavigator();

        XPathNavigator appSettingsNode = navigator.SelectSingleNode("app-settings");

        XPathNavigator selectedRecordNode = appSettingsNode.SelectSingleNode("selected-record-id");

        string value = selectedRecordNode.Value;
        _selectedRecordId = new Guid(value);
    }

    public void SaveToHealthVault(AuthenticatedConnection authenticatedConnection)
    {
        XmlDocument document = new XmlDocument();

        XmlElement nodeWeightTrackerSettings = document.CreateElement("app-settings");
        document.AppendChild(nodeWeightTrackerSettings);

        XmlElement nodeSelectedRecordId = document.CreateElement("selected-record-id");
        nodeWeightTrackerSettings.AppendChild(nodeSelectedRecordId);
        nodeSelectedRecordId.InnerText = _selectedRecordId.ToString();

        authenticatedConnection.SetApplicationSettings(document);
    }
}

Then, we add the code to load in the settings at the beginning of Page_Prerender():

_weightTrackerApplicationSettings.LoadFromHealthVault(AuthenticatedConnection);

and modify our methods:

protected void c_dropDownCurrentRecord_SelectedIndexChanged(object sender, EventArgs e)
{
    foreach (Guid recordId in PersonInfo.AuthorizedRecords.Keys)
    {
        if (recordId.ToString() == c_dropDownCurrentRecord.SelectedValue)
        {
            PersonInfo.SelectedRecord = PersonInfo.AuthorizedRecords[recordId];

            _weightTrackerApplicationSettings.SelectedRecordId = recordId;
            _weightTrackerApplicationSettings.SaveToHealthVault(AuthenticatedConnection);
        }
    }
}

void PopulateRecordDropDown()
{
    c_dropDownCurrentRecord.Items.Clear();

    foreach (Guid recordId in PersonInfo.AuthorizedRecords.Keys)
    {
        HealthRecordInfo healthRecordInfo = PersonInfo.AuthorizedRecords[recordId];

        ListItem listItem = new ListItem(healthRecordInfo.Name, recordId.ToString());

        if (recordId == PersonInfo.SelectedRecord.Id)
        {
            listItem.Selected = true;
        }

        c_dropDownCurrentRecord.Items.Add(listItem);
    }
}

That stores the currently selected record away and then restores it when the page starts up.

Next Time

Next time, we’ll display values from multiple records.

Posted by ericgu | 0 Comments
Filed under: ,

Rekeying the HealthVault/Amalga USB Key

If you attended the connected health conference last week, you got a USB key with a combination lock on it, so you could protect your important data against those with less than 10 minutes of free time, or those with very little imagination.

But to get this protection, you'll need to change the combination to some other number. Here's how you do it:

  1. Set the key to 0-0-0
  2. On the non-usb end, there's a small round button. Press it in with a pen.
  3. Change the combination
  4. Press the button on the USB end back in.

 

 

Posted by ericgu | 0 Comments
Filed under:

Suggest a HealthVault topic...

Is there something about HealthVault that you find confusing? If so, add a comment to this post, and I'll try to cover it in a future blog post.

Posted by ericgu | 1 Comments
Filed under:

21st Century Breakdown

I picked up a copy of Green Day's newest this morning at Fred Meyer, paying a princely $12.99 for the priviledge to do so. I missed picking it up last Saturday because of being busy and all, so I missed 3 days of listening.

I've listened through a couple of times now. Musically, it's pretty wide-ranging - there are songs that are traditional Green Day, one song that sounds like it's directly from Money Money 2020 (a nice album if you like punk/new wave fusion), and one that's a little bit country (and more than a little bit rock and rock).

They manage to sound both very much like Green Day and not like Green Day at the same time, if that makes any sense. I think it's a nice album, perhaps not as strong as American Idiot, but we'll see how it holds up to repeated listening

 And yes, I'll be at Key arena with the wife and (now old enough for a Green Day concert) daughter on July 3rd.

 

 

Posted by ericgu | 2 Comments

Microsoft Connected Health Conference

The HealthVault Partner Conference is a year older and has a new name.

The Microsoft Connected Health Conference will be held on June 10-12th at Meydenbauer Center in Bellevue, WA.

If you enter the registration code on the page and choose "submit", you will get access to the details of the conference.

Posted by ericgu | 1 Comments

Progressio per Patientia

"Progress through suffering"

I have a friend (perhaps "riding acquaintance" is a more accurate term) named "Elden Fat Cyclist". Actually, that's his stage name. His real name is "Harold Fat Cyclist".

Harold has been involved in Livestrong for a while and - for reasons that will become obvious if you read his blog - this year has decided to organize "Team Fat Cyclist", to try to win the team competition of the Livestrong challenge.

This year, Livestrong has decided to hold a ride in Seattle. Their goal is apparently to do a shortened version of a Tour de France mountain stage, with an advertised 7900' of elevation gain over the 101 miles. That would, AFAIK, set a new standard for centuries in these parts, with the ever-popular (many would say "over-popular") Flying Wheels Century clocking in at 3500', and even the difficult 7 Hills century at only 7000'.

Or, to put it another way, this ride will involve a significant amount of pain and suffering. Since signing up for rides that are harder than I should is a bit of a tradition for me (not to mention being a tradition for Harold), I signed up last week.

That's where you come in.

The event is a fundraiser for cancer research, and I'm looking for people to sponsor me in this undertaking.

For the first $250 that's donated, I will match every dollar, and then Microsoft will match my dollar. So, if you donate $10, the total will be... well, it will be more than I can calculate on Sunny Seattle Spring Sunday.

If you can spare some money to help out a worthy cause (cancer research, not me suffering), please head on over to my fundraising page.

Posted by ericgu | 2 Comments
Filed under:

Introduction to HealthvaultDevelopment #11: Data filtering

The users are unhappy. The weight tracking application worked pretty well initially, but now that they’ve been using it daily for a few months, it’s showing them too much information. They would like it to show the recent results.

That will take us into filtering…

Filtering is done using the aptly-named HealthRecordFilter class. Thus far, we have only been using this class to filter to data that has the weight type id. In this episode we will explore additional ways to perform filtering.

Common Data Filtering

There are some properties that are shared across all data types. This section discusses filtering on those items.

Filtering based on the number of items

The simplest thing we can do is to only fetch a specific number of items rather than all of them. First, we add the following checkbox before the c_tableWeight table in default.aspx:

<asp:CheckBox ID="c_CheckboxLast5Items" runat="server" Text="Last 5 items only" AutoPostBack="True" /><br />

and then we change the filter definition in default.aspx.cs to:

filter.View.Sections |= HealthRecordItemSections.Audits;
if (c_CheckboxLast5Items.Checked)
{
    filter.MaxItemsReturned = 5;
}

If you run this, when you click the checkbox you will only see the last 5 weight measurements.

<aside>

The HealthRecordItemCollection class implements lazy fetching of results. It will fetch chunks of instances at a time, going back to the server when it needs another chunk. The chunk size is set by the MaxFullItemsReturnedPerRequest property of the filter, and is 30 by default.

This is transparent to the calling application, and the default value is usually fine.  Decreasing the value reduces the amount of work that the server has to do when processing each chunk, and might be required if your application deals objects that have significant amounts of data in them. This requires more trips to the server to fetch data, however, which will have performance implications.

Increasing the value reduces the number of trips to the server to fetch all the data, but there is a possibility that the longer operations may result in a timeout.

</aside>

Filtering based on dates

We can also filter based on the 

First, add a checkbox above the table that allows the user to toggle this setting. You can do that through the designer or by adding the following before the c_tableWeight table:

<asp:CheckBox ID="c_CheckboxLast3MonthsOnly" runat="server" Text="Last 3 months only" AutoPostBack="True" /><br />

and the following is added to the filter definition:

if (c_CheckboxLast3MonthsOnly.Checked)
{
    filter.UpdatedDateMin = DateTime.Now - new TimeSpan(30, 0, 0, 0);
}

Filtering based on application id

In a previous episode, we added some code to fetch the name of the application that last updated the data in HealthVault. We can filter on the application that last updated this data item so we only retrieve that items that our application retrieved (in general, apps should try to read all the data a user has, but it may make sense to only read this subset at times).

This is done with the following:

filter.UpdatedApplication = ApplicationConnection.ApplicationId;

Adding the checkbox and the appropriate logic is left as an exercise to the reader.

Type-Specific Filtering

Filtering on a value that is specific to a type – the value of a weight measurement, the name of a medication is a bit more complex. The type-specific data for an instance is stored in HealthVault XML, so a filter needs to be able to find a specific piece of data inside of a larger chunk of XML and then compare it to a desired value.

This is done using a query language that is known as XPath. To write an xpath expression for a specific HealthVault data type requires that you understand that XML format that is uses to store the information, which either requires getting the XML for a specific instance (by calling GetItemXml() on the instance), or understanding the XSD schema for the data type.

To make this easier, I wrote the HealthVault XPath Explorer. This utility will let you easily walk the XML for instances that are in a specific HealthVault record, though you will have to figure out how to write the condition yourself. There are a number of good XPath tutorial available on the net.

For our application, if we want to only return items where the weight value is less than 50 kilograms, we can use the following:

filter.XPath = "/thing/data-xml/weight/value/kg[.> 50]";

In the “[. > 50]”, the “.” refers to the current node, so it means “kg > 50”.

Automatic user sign in

You may have noticed that our demo app requires us to sign in every time we run it. This behavior is configurable:

  1. Go to the application configuration center
  2. Select the demo application
  3. Go to the “Misc” tab
  4. Toggle the “automatic user sign-in” radio button to “yes”
  5. Set a sign-in duration in seconds (I chose 3600).
  6. Click “Make changes”.

That will start the configuration change process. Within 30 minutes, the update will make it to the servers, and a new “stay signed in” checkbox will appear on the log in page for the application.

Posted by ericgu | 2 Comments
Filed under: ,

HealthVault XPath Explorer

By default, when a query is made to HealthVault, it returns all instances of a specific type. If you wish to filter to a subset of those instances, you can specify some properties in the HealthRecordFilter class. For example, you can set the CreatedApplication property to filter on the application that created the health record item.

The filter properties, however, only works for properties that are shared across all data types. If you want to filter based on properties that are data-type-specific, you can do that by setting the XPath property to an appropriate XPath query.

Creating that query is a bit inconvenient – perhaps a lot inconvenient – so I wrote a little utility that you can use to easily create an xpath query.

You can find the code here.

Make a copy of the HelloWorld sample, put the two files into it, and run the sample.

Posted by ericgu | 4 Comments

Introduction to HealthVault Development #10: Adding additional data to healthvault

We have a new user request. One of our users would like to store how hungry he whenever he enters a weight value.

We head off to the HealthVault MSDN page, go to the learn page and then the reference page, and then choose HealthRecordItem Type Schema Browser. This page talks to the developer platform to find out what types are currently supported by the platform.

Looking through all the data types available on the page, we don’t find one related to hunger. So, we can just head home for the day and spend some quality time with our XBox.

Or, we could explore other ways to add the hunger data to HealthVault.

The first way is to ask the HealthVault team to add a new data type. We regularly add new data types and revise existing ones to meet the needs of our partners. If you think that the data you want to store is of general interest, please start a conversation with us.

The downside of this is that it’s going to take us a while to figure out what the data type should be, design it, and then add it to a release. At best, it’s about 6 weeks from when we start working on it to when it shows up on your doorstep, gift-wrapped in a small box with a big red bow (gift wrap, box, and bow not included). If the type is complex, requires a lot of research, or if we’re especially busy, it could take longer.

While you wait for us – or if the information is not of general interest – there are three ways to add information:

  • All data types provide the CommonData.Note and CommonData.Tags properties for your use.
  • All data types support adding extension data in XML format.
  • A custom application-specific data type can be created.

Application-specific data types are only accessible by the application that created them, and there is therefore no data liquidity using such types. While there are scenarios where that makes sense, our guideline is that applications should make as much data as possible accessible to other applications, and therefore limit the use of application-specific types.

Extensions

Extensions are created and accessed using the HealthRecordItemExtension class, and there is a collection of these classes on the CommonData property of the data type instance.

The XML for the extension is in the following format:

<extension>
    <extension-name>
        xml data goes here
    </extension-name>
</extension>

The meaning of the XML data inside is purely up to the application.

For hunger, we’ll use an entry like this:

<extension><hunger>Moderate</hunger></extension>

Adding the extension to our project

In default.aspx, make the c_lableHunger and c_dropDownListHunger controls visible. Here’s the code we need to parse the values, create the extension, and add it to the weight object:

void AddHungerToWeight(Weight weight)
{
    HealthRecordItemExtension extension = new HealthRecordItemExtension(ExtensionWeightTrackerHunger);
    XPathNavigator navigator = extension.ExtensionData.CreateNavigator();
    string innerXml = @"<extension><hunger>" + c_dropDownListHunger.SelectedItem.Value +
        @"</hunger></extension>";
    navigator.InnerXml = innerXml;

    weight.CommonData.Extensions.Add(extension);
}

That helper is already in the project, so you can just add the call right before the weight value is saved:

AddHungerToWeight(weight);
PersonInfo.SelectedRecord.NewItem(weight);

The ExtensionWeightTrackHunger constant is used to uniquely identify the extension. Because there is a possibility for collision here, it would be a good idea to use a “<application>.<extension-name>” format. The constant that is currently in the project is incorrect – change it to “WeightTracker.Hunger”.

In addition to the actual data stored in the extension (the ExtensionData property), there are a few other items that you can store:

  • Logo is the url of a logo to display with the extension.
  • Source is the source identifier.
  • Transform is the URL of a transform that converts the extension data into an HTML form.
  • Version is the version number of the extension

We’ll follow our usual pattern in modifying our fetching code:

First, add a “Hunger” header as the second-to-last entry.

Then, we need some code to iterate over all extensions, similar to how we iterated over the related items. There is a helper in the project, but I don’t like the way it works any more, so we’ll use this code instead:

string hunger = String.Empty;
foreach (HealthRecordItemExtension extension in weight.CommonData.Extensions)
{
    if (extension.Source == ExtensionWeightTrackerHunger)
    {
        XPathNavigator navigator = extension.ExtensionData.CreateNavigator();
        XPathNavigator value = navigator.SelectSingleNode("extension/hunger");
        if (value != null)
        {
            hunger = value.Value;
            break;
        }
    }
}

Finally, add hunger to the call to AddCellsToTable(). Run the project and verify that it works.

A Nicer Approach

We can make things better by using a derived class for the extension.

Add a new class to the project, and name it “Hunger Extension”. Visual studio will ask you if you want to put it in App_Code – tell it “yes, that would be lovely”.

Replace the class with the following:

using System;
using System.Xml;
using System.Xml.XPath;
using Microsoft.Health;
using Microsoft.Health.ItemTypes;

public class HungerExtension : HealthRecordItemExtension
{
    static readonly string ExtensionSource = "WeightTracker.Hunger" ;

    string m_hunger;

    public HungerExtension()
    {
    }

    public HungerExtension(string hunger)
                   : base(ExtensionSource)
    {
        m_hunger = hunger;
        Source = ExtensionSource;
    }

    public string Hunger
    {
        get { return m_hunger; }
        set { m_hunger = value; }
    }

    protected override void ParseXml(IXPathNavigable extensionData)
    {
        XPathNavigator navigator = extensionData.CreateNavigator();
        XPathNavigator hungerNode = navigator.SelectSingleNode("extension/Hunger" );

        if (hungerNode != null)
        {
            m_hunger = hungerNode.Value;
        }
    }

    protected override void WriteXml(System.Xml.XmlWriter writer)
    {
        writer.WriteStartElement("Hunger");
        writer.WriteString(m_hunger);
        writer.WriteEndElement();
    }

    public static void RegisterExtensionHandler()
    {
        ItemTypeManager.RegisterExtensionHandler(ExtensionSource, typeof(HungerExtension), true);
    }
}

The class is pretty simple. The ParseXml() method pulls the data out of XML and puts it in the class, and the WriteXml() method does the opposite. The RegisterExtensionHandler() method registers this extension with the SDK, so that when it sees that extension, it creates an instance of our derived type instead of a generic HealthRecordItemExtension instance.

Now that we have the class, it’s much easier to use our extension.

First, add the call to register the extension before the weight values are loaded:

HungerExtension.RegisterExtensionHandler();

Modify the code to fetch the hunger value to the following:

string hunger = String.Empty;
foreach (HealthRecordItemExtension extension in weight.CommonData.Extensions)
{
    HungerExtension hungerExtension = extension as HungerExtension;
    if (hungerExtension != null)
    {
        hunger = hungerExtension.Hunger;
    }
}

Change the code to save the hunger to the following:

weight.CommonData.Extensions.Add(
        new HungerExtension(c_dropDownListHunger.SelectedItem.Value));
PersonInfo.SelectedRecord.NewItem(weight);

Run the application, and verify that it works. Then delete the unnecessary helper methods and string constant from default.aspx.cs

Other resources:

There is another set of pages where you can look at the schemas for the item types, and schemas for all the platform methods. We will be unifying these two sources in the future.

Next Time

Next time, we’ll talk about data filtering.

Posted by ericgu | 3 Comments

After SchooL chocLate cookies

  • 2 cups chocLate Syrip
  • 2 cups flour
  • 1/4 cup maLLases
  • 5 cups oatmeaL
  • 5 TabLespoons SaLt
  • 5 teaspoons BakingSoda

Put OatmeaL, SaLt, fLour and Soda in bowl

Stir in syrip and MaLLases

Bake ten minits

Temp 345

Eric, age 7

(I was cleaning out some old stuff this weekend, and thought I would share it with you)

Posted by ericgu | 4 Comments

Introduction to HealthVault Development #9: Optional Authorization

This week, we have a new customer request. One of our customers wants to track her mood on a daily basis. We can do this by using the Emotion data type, but after talking with our broader customer base, we find that some of them don’t want this information displayed by the application.

What to do? What to do?

Enter optional auth

Optional auth allows different users to grant our application different amounts of data access. All users are requested to grant us the base auth for the application, but our application decides when to ask users for additional data access.

We will use the specific access granted by a user to determine what information to query for and what to display in our user interface.

Setting up our optional authorization rule

Go to the application configuration center, pick your application, and add a new rule, named “Opt_Emotion”, make it optional, ask for Create/Read permissions, and choose the “Emotional State” data type. Save your rule.

Determining type permissions

We can ask the platform for information about our access to the Emotion data type with the following code:

List<Guid> items = new List<Guid>();
items.Add(Emotion.TypeId);
Collection<HealthRecordItemTypePermission> permissions =
        PersonInfo.SelectedRecord.QueryPermissions(items);

This code asks the selected record whether it has access to Emotion. If there is no access, the permissions collection will be empty.

Since we’re only concerned with one rather than several types, I wrote a little wrapper to make our code a bit simpler. Put this at the beginning of Page_Prerender:

HealthRecordItemTypePermission emotionPermission =
                          GetItemPermission(Emotion.TypeId);

If you look at the HealthRecordItemTypePermission class, you’ll see that it stores the permissions for both online and offline access. In our case, we want to look for online access.

if (emotionPermission.OnlineAccessPermissions == HealthRecordItemPermissions.None)
{
    c_linkEnableMoodTracking.Visible = true;
}
else
{
    c_labelMood.Visible = true;
    c_dropDownListMood.Visible = true;
    PopulateDropDownFromEnum(c_dropDownListMood, typeof(Mood));
}

Because I’m going to be defining the rule set, I know ahead of time that the user is either going to have enough access to Emotion for me to do what I want, or no access at all. I can therefore simply test whether there is any access to control my UI.

It’s possible, however, for me to ask for read and write access separately, and if I had done that, I would need to check for specific permissions (Create, Read, Update, or Delete).

It would probably be a good time to run the project to see what happens. There should be a link below the Save button that says “Enable Mood Tracking”.

Asking the user for access

You might remember when you first ran the application, you were redirected to HealthVault so that you could authorize access for the application. For optional authorization, we’re going to send the user back to that same page.

But first, we’d like to explain to the user the advantages of providing us access. That is done with a landing page named AuthorizeEmotion.aspx.

Here’s the code that will do the redirect:

string TargetQuery =
    "appid=f36debe2-c2a3-434a-b822-f8c294fdecf9&onopt1=Opt_Emotion";
RedirectToShellUrl("APPAUTH", TargetQuery);

The appid parameter must be set your application’s application id, and the onopt1 parameter is set to the name of the rule we want to ask for. It is possible to ask for multiple rules and to ask for offline access as well – the details are on the HealthVault Shell Redirect Interfaces page.

Run your application, and choose the “Enable Mood Tracking” button, and click “Authorize”. When you get back to the authorization page, you will now find that it now asks for access to the Emotional State data type, with a checkbox next to it. Click the checkbox, approve access, and you’ll now find that the UI shifts into the “mood-enabled” mode.

Storing and displaying Mood values…

Storing and display Mood values is similar to the code we wrote to store the exercise values. We add the following code after we save the Exercise instance:

Emotion emotion = null;
if (c_labelMood.Visible)
{
    emotion = new Emotion(new HealthServiceDateTime(DateTime.Now));
    emotion.Mood = (Mood) Int32.Parse(c_dropDownListMood.SelectedItem.Value);
    PersonInfo.SelectedRecord.NewItem(emotion);
}

and then modify the code that sets up the relationship:

weight.CommonData.RelatedItems.Add(
             new HealthRecordItemRelationship(exercise.Key));
if (emotion != null)
{
    weight.CommonData.RelatedItems.Add(
            new HealthRecordItemRelationship(emotion.Key));
}

PersonInfo.SelectedRecord.NewItem(weight);

Displaying mood values in the table

The header on the table needs to change based on whether we are tracking mood or not:

if (c_labelMood.Visible)
{
    AddHeaderCells(c_tableWeight, "Date", "Weight", "BMI", "App Name",
                           "Minutes", "Mood", "&nbsp;");
}
else
{
    AddHeaderCells(c_tableWeight, "Date", "Weight", "BMI", "App Name",
                           "Minutes", "&nbsp;");
}

We add the following to our relationship-fetching code:

Emotion emotion = relatedItem as Emotion;
if (emotion != null)
{
    mood = emotion.Mood.ToString();
}

after declaring “string mood” right below “string minutes”.

And finally, we modify our code that adds the data to the table:

if (c_labelMood.Visible)
{
    AddCellsToTable(c_tableWeight, weight.When.ToString(),
               weight.Value.DisplayValue.ToString(), bmiString, appName, minutes, mood);
}
else
{
    AddCellsToTable(c_tableWeight, weight.When.ToString(),
               weight.Value.DisplayValue.ToString(), bmiString, appName, minutes);
}

With that code, the application should now support entering and displaying mood information.

De-authorization

If we want to give the user the opportunity to turn off mood tracking, we can do it by using the same call as we used to authorize the access.

Add a page named DeauthorizeEmotion with appropriate text and code, and then modify the main page code so that it shows the authorize or deauthorize link as applicable.

Current Code

To help keep your code in sync with mine, I’ve uploaded my current code to MSDN Code Gallery. You will need to change my source so that it uses your application ID rather than mine.

Suggestions

I have a list of topics that I’m planning on covering, but if you have a suggestion for what you’d like to see, let me know.

Next Time

Next time, we’ll be looking at how to store hunger information in our application.

Posted by ericgu | 2 Comments

Introduction to HealthVault Development #8: Related items

Our current application allows users to track their weight, but some of our users also want to track the time they spend walking. In this episode, we’ll extend our application to store and display that information.

We will be storing that information in the Exercise type. First, we’ll need to head off to the Application Configuration Center (there’s a link at the bottom of the HealthVault Application Manager if you forget the URL…)

Locate your application, and look at the online access that is currently defined. We could add a new rule named “Exercise”, but that would get unwieldy pretty quickly with lots of types. Instead, we are are going to group our rules based upon the type of access required. What we are defining right now is known as the application’s “base auth”, so we’ll put that concept in our names to make it easier later, when we explore the other kind of auth (if you’re too excited to wait, it’s called “optional” auth…)

  1. Edit the Weight rule. Change the name to “Base_All”.
  2. Add access to Exercise to this rule.
  3. Rename the Height rule to be “Base_read”.

After you save the configuration (and perhaps wait a short time for the new configuration to be activated), run the application.

You will be redirected to re-authorize the application, since your application is now asking for more data access than you had previously authorized it to have.

That can make things a bit inconvenient (and confusing) for your users. You deploy a new version of your app that uses more types, and they have to reauthorize. The answer to that is the aforementioned optional auth, which I promise we’ll cover very soon.

Now we can modify our code to record that exercise information.

Housekeeping

A little bit of housekeeping first, to fix a bug that you might have noticed. If you enter a new weight value, it doesn’t show up right away. This is because of a mistake I made when I wrote the first version.

When the user enters a new weight, the data is posted back to the asp page, and Page_Load is called, then c_buttonSave_Click, then Page_Prerender, in that order (other stuff happens too). Which means that we’ve already rendered out the “current” set of information before we add new new value, which is why it doesn’t show up.

The fix is to take all of the code that is currently in Page_Load, and move it into Page_Prerender.

I assure you that this bug was in the code to provide a chance for you to debug your code, and certainly not an oversight from when I wrote the original code.

Saving Exercise Information

Go to default.aspx, and make label2 and c_textboxWalkingTime visible. This provides the UI that users use to enter their walking time.

We’ll start by creating an Exercise instance:

Exercise exercise = new Exercise();

Next, we need to record when the exercise happened. We do this with the following:

DateTime now = DateTime.Now;
ApproximateDateTime approximateDateTime = new ApproximateDateTime(
    new ApproximateDate(now.Year, now.Month, now.Day),
    new ApproximateTime(now.Hour, now.Minute, now.Second));

exercise.When = approximateDateTime;

That’s an ugly bit of code, but that’s what you have to write right now. Our next release will let you write this as:

exercise.When = new ApproximateDateTime(DateTime.Now);

so you might want to give that a try.

Now, we want to store the number of minutes that our user walked:

int minutes = Int32.Parse(c_textboxWalkingTime.Text);
exercise.Duration = minutes;

We’re still missing an important piece of data – the kind of exercise. We would like to store this information so that programs can make decisions based on the specific activity. That can easily be done by defining a set of different activities (which is known as a “vocabulary” in HealthVault, and referencing one of those with the exercise.

But that would miss an important piece of data – what the user said the activity was. Perhaps they want to enter “race walking” or “fitness walking”, or “walk in the park” and have the system show that as the activity when it displays the exercise information.

To accommodate both of those uses, HealthVault uses a concept of a codable value, which is similar to the measurement type discussed in an earlier episode. It stores a Text value – which is whatever the user or program thinks it should be – and one or more coded values, which provide the specifics that programs would read.

We’ll start by defining the CodedValue part. Looking at the docs for Exercise.Activity, we find it recommends the exercise-activities vocabulary (we could also have looked in the list of vocabularies).

Each vocabulary item has a code, a display text, and an abbreviation. So, to code to walking, we use the following coded value:

CodedValue codedValue = new CodedValue(“Walking”, “exercise-activities”);

Where "Walking” is the code and “exercise-activities” is the vocabulary.

We’ll use that to create the whole codable value:

exercise.Activity =
    new CodableValue("Walking", new CodedValue("Walking", "exercise-activities"));

In this case the Text and the codable value are the same, but in most cases the code is going to be considerably more terse than the Text value, or it may be a numeric value.

If it was important to our application to store more information, or if we wanted to refer to more than one vocabulary, we could add additional coded values to the codable value. This is useful if there is more than one standard set of codes for a particular property.

The codable values that are stored are not validated by the platform, which gives applications the flexibility to code to vocabularies that are not on the platform. We’d appreciate it, however, that you let us know if you’re coding to a vocabulary that you think should be on the platform.

After all of that, we can save the Exercise instance to the record:

PersonInfo.SelectedRecord.NewItem(exercise);

Constructors

In the last bit of code, I created the Exercise instance and then set the properties. That works fine, but all of the types in HealthVault declare constructors that take all the required values as parameters, and using that constructor is a good way of making sure you set all the required properties. Using that approach, our code would look like this:

DateTime now = DateTime.Now;
ApproximateDateTime approximateDateTime = new ApproximateDateTime(
    new ApproximateDate(now.Year, now.Month, now.Day),
    new ApproximateTime(now.Hour, now.Minute, now.Second));

Exercise exercise = new Exercise(approximateDateTime, 
                 new CodableValue("Walking",
                                            new CodedValue("Walking", "exercise-activities")));

int minutes = Int32.Parse(c_textboxWalkingTime.Text);
exercise.Duration = minutes;

PersonInfo.SelectedRecord.NewItem(exercise);

Related Items

There’s a problem in the code that I just wrote. The Exercise instance and the Weight instance were entered at the same time by the user, but that relationship isn’t stored in HealthVault. This is going to complicate our display code considerably, as we have no easy way of finding the Exercise instance that goes with a specific weight.

We’ll use the Related items feature to connect them together.  We’ll simply add the Exercise instance to the related items list of the weight before we save it to the database:

weight.CommonData.RelatedItems.Add(new HealthRecordItemRelationship(weight.Key));
PersonInfo.SelectedRecord.NewItem(weight);

Related items is pretty low-tech – the id of the related object is merely saved on the object. The platform doesn’t provide referential integrity or any of that database stuff, so applications must be prepared to come across related item links that refer to items that aren’t accessible to the application.

Displaying the Exercise information in the table

Whenever we fetch a weight, we want to see if there is a related exercise instance. Here’s the code that we’ll use:

string minutes = String.Empty;
foreach (HealthRecordItemRelationship relationship in weight.CommonData.RelatedItems)
{
    if (relationship.ItemKey != null)
    {
        HealthRecordItem relatedItem =
            PersonInfo.SelectedRecord.GetItem(relationship.ItemKey.Id,
            HealthRecordItemSections.Core | HealthRecordItemSections.Xml);

        Exercise exercise = relatedItem as Exercise;
        if (exercise != null)
        {
            minutes = exercise.Duration.ToString();
        }
    }
}

This code walks through all the related items for an instance, and if there’s a key, it fetches the related item instance. It checks to see if the item fetched is an exercise, and if it is, formats the duration.

The task of adding a new column named “Minutes” to the table and the minutes value to the body of the table is left as an exercise to the reader.

Other Stuff

There are a few other topics that out of scope of this episode, but worth mentioning anyway.

Exercise is the new version of the AerobicSession type. Because of the type of data they store and the differences between the types, applications that will process AerobicSession data will likely want to handle it separately from Exercise data.  There is more information on this issue here.

There is a second method of relating items together known as client ID, which is used by HealthVault connection center when it needs to link instances together (related items doesn’t work for it because of some architectural issues). I’ll cover client IDs in the future.

Next Time

Next time, we’ll do something with optional auth.

Posted by ericgu | 4 Comments
Filed under: ,
More Posts Next page »
 
Page view tracker