Collaboration Store, Part LXXXI

“The trouble with programmers is that you can never tell what a programmer is doing until it’s too late.”
Seymour Cray

Last time, we started building a widget for the application details pop-up and today we need to wrap that up. We left off with a rough layout of what the pop-up might contain, and now we need to gather up all of the data necessary to populate the screen. The first thing that we need to do is get the primary application record.

data.sysId = input.sys_id;
data.record = {};
var appGR = new GlideRecord('x_11556_col_store_member_application');
appGR.query();
if (appGR.get(data.sysId)) {
	var item = {};
	data.record.name = appGR.getDisplayValue('name');
	data.record.description = appGR.getDisplayValue('description');
	data.record.applicationId = appGR.getValue('application');
	data.record.logo = appGR.getValue('logo');
	data.record.version = appGR.getDisplayValue('current_version');
	data.record.provider = appGR.getDisplayValue('provider.name');
	data.record.providerId = appGR.getValue('provider');
	data.record.providerLogo = appGR.provider.getRefRecord().getValue('logo');
	data.record.local = appGR.getDisplayValue('provider.instance') == gs.getProperty('instance_name');
	data.record.state = 0;
	if (data.record.applicationId) {
		data.record.state = 1;
		data.record.installedVersion = appGR.getDisplayValue('application.version');
		if (data.record.version == data.record.installedVersion) {
			data.record.state = 2;
		}
	}
	if (!data.record.local && data.record.state != 2) {
		data.record.attachmentId = getAttachmentId(data.record.sys_id, data.record.version);
	}
	data.record.versionList = getVersionRecords(data.sysId);
}

Then we need the functions that fetch the attachment ID and all of the version records.

function getAttachmentId(applicationId, version) {
	var attachmentId = '';

	var versionGR = new GlideRecord('x_11556_col_store_member_application_version');
	versionGR.addQuery('member_application', applicationId);
	versionGR.addQuery('version', version);
	versionGR.query();
	if (versionGR.next()) {
		var attachmentGR = new GlideRecord('sys_attachment');
		attachmentGR.addQuery('table_name', 'x_11556_col_store_member_application_version');
		attachmentGR.addQuery('table_sys_id', versionGR.getUniqueValue());
		attachmentGR.addQuery('content_type', 'CONTAINS', 'xml');
		attachmentGR.query();
		if (attachmentGR.next()) {
			attachmentId = attachmentGR.getUniqueValue();
		}
	}
		
	return attachmentId;
}

function getVersionRecords(applicationId) {
	var versionList = [];

	var versionGR = new GlideRecord('x_11556_col_store_member_application_version');
	versionGR.addQuery('member_application', applicationId);
	versionGR.orderByDesc('sys_created_on');
	versionGR.query();
	while (versionGR.next()) {
		var thisVersion = {};
		thisVersion.date = formatDate(versionGR.getDisplayValue('sys_created_on'));
		thisVersion.builtOn = versionGR.getDisplayValue('built_on');
		thisVersion.version = versionGR.getDisplayValue('version');
		versionList.push(thisVersion);
	}
		
	return versionList;
}

The version records are dated, and the date format that I have chosen is month day, year (‘MMM d, yyyy’); however, for today’s date and yesterday’s date, I replace the date with the words Today and Yesterday. To pull that off, I need to create some variables for those two dates right at the top.

var gd = new GlideDate();
var today = gd.getByFormat('MMM d, yyyy');
var gdt = new GlideDateTime();
gdt.addDaysLocalTime(-1);
gd.setValue(gdt.getDate());
var yesterday = gd.getByFormat('MMM d, yyyy');

Once those values have been establish, I can reference them in the date format function.

function formatDate(dateString) {
	var response = '';
	if (dateString) {
		var date = new GlideDate();
		date.setValue(dateString);
		response = date.getByFormat('MMM d, yyyy');
		if (response == today) {
			response = 'Today';
		} else if (response == yesterday) {
			response = 'Yesterday';
		}
	}
	return response;
}

That’s pretty much it for the server side code. Here is the whole thing all put together.

(function() {
	var gd = new GlideDate();
	var today = gd.getByFormat('MMM d, yyyy');
	var gdt = new GlideDateTime();
	gdt.addDaysLocalTime(-1);
	gd.setValue(gdt.getDate());
	var yesterday = gd.getByFormat('MMM d, yyyy');
	if (input) {
		data.sysId = input.sys_id;
		data.record = {};
		var appGR = new GlideRecord('x_11556_col_store_member_application');
		appGR.query();
		if (appGR.get(data.sysId)) {
			var item = {};
			data.record.name = appGR.getDisplayValue('name');
			data.record.description = appGR.getDisplayValue('description');
			data.record.applicationId = appGR.getValue('application');
			data.record.logo = appGR.getValue('logo');
			data.record.version = appGR.getDisplayValue('current_version');
			data.record.provider = appGR.getDisplayValue('provider.name');
			data.record.providerId = appGR.getValue('provider');
			data.record.providerLogo = appGR.provider.getRefRecord().getValue('logo');
			data.record.local = appGR.getDisplayValue('provider.instance') == gs.getProperty('instance_name');
			data.record.state = 0;
			if (data.record.applicationId) {
				data.record.state = 1;
				data.record.installedVersion = appGR.getDisplayValue('application.version');
				if (data.record.version == data.record.installedVersion) {
					data.record.state = 2;
				}
			}
			if (!data.record.local && data.record.state != 2) {
				data.record.attachmentId = getAttachmentId(data.record.sys_id, data.record.version);
			}
			data.record.versionList = getVersionRecords(data.sysId);
		}
	}

	function getAttachmentId(applicationId, version) {
		var attachmentId = '';

		var versionGR = new GlideRecord('x_11556_col_store_member_application_version');
		versionGR.addQuery('member_application', applicationId);
		versionGR.addQuery('version', version);
		versionGR.query();
		if (versionGR.next()) {
			var attachmentGR = new GlideRecord('sys_attachment');
			attachmentGR.addQuery('table_name', 'x_11556_col_store_member_application_version');
			attachmentGR.addQuery('table_sys_id', versionGR.getUniqueValue());
			attachmentGR.addQuery('content_type', 'CONTAINS', 'xml');
			attachmentGR.query();
			if (attachmentGR.next()) {
				attachmentId = attachmentGR.getUniqueValue();
			}
		}
		
		return attachmentId;
	}

	function getVersionRecords(applicationId) {
		var versionList = [];

		var versionGR = new GlideRecord('x_11556_col_store_member_application_version');
		versionGR.addQuery('member_application', applicationId);
		versionGR.orderByDesc('sys_created_on');
		versionGR.query();
		while (versionGR.next()) {
			var thisVersion = {};
			thisVersion.date = formatDate(versionGR.getDisplayValue('sys_created_on'));
			thisVersion.builtOn = versionGR.getDisplayValue('built_on');
			thisVersion.version = versionGR.getDisplayValue('version');
			versionList.push(thisVersion);
		}
		
		return versionList;
	}

	function formatDate(dateString) {
		var response = '';
		if (dateString) {
			var date = new GlideDate();
			date.setValue(dateString);
			response = date.getByFormat('MMM d, yyyy');
			if (response == today) {
				response = 'Today';
			} else if (response == yesterday) {
				response = 'Yesterday';
			}
		}
		return response;
	}
})();

To format all of this data, we use the following HTML.

<div class="panel{{c.data.record.local?' local-app':''}}">
  <img ng-src="{{::c.data.record.logo}}.iix?t=small" ng-if="c.data.record.logo" alt="" class="m-r-sm m-b-sm pull-left" aria-hidden="true"/>
  <h3>{{c.data.record.name}}</h3>
  <div>
    <p>{{::c.data.record.description}}</p>
    <strong>${Version History}</strong>
    <table>
      <thead>
        <tr>
          <th>${Version}</th>
          <th>${Published}</th>
          <th>${Built on}</th>
          <th>${Install}</th>
        </tr>
      </thead>
      <tbody>
        <tr ng-repeat="version in c.data.record.versionList">
          <td>{{::version.version}}</td>
          <td>{{::version.date}}</td>
          <td>{{::version.builtOn}}</td>
          <td ng-if="version.version == c.data.record.installedVersion">${Installed}</td>
          <td ng-if="version.version == c.data.record.version && c.data.record.state != 2">
            <button ng-click="alert('OK');">${Install version} {{::version.version}}</button>
          </td>
        </tr>
      </tbody>
    </table>
    <p>
      <a href="/x_11556_col_store_member_application.do?sys_id={{::c.data.sysId}}">${View Collaboration Store application record}</a><br/>
      <a ng-if="c.data.record.state > 0" href="/sys_app.do?sys_id={{::c.data.record.applicationId}}">${View installed application record}</a>
    </p>
    <p ng-if="!c.data.record.local">
      <span style="display: inline-flex;" class="pull-right">
        <span ng-if="c.data.record.provider">${This application provided by} <a href="/x_11556_col_store_member_organization.do?sys_id={{::c.data.record.providerId}}">{{::c.data.record.provider}}</a></span>
        &nbsp;
        <img ng-src="{{::c.data.record.providerLogo}}.iix?t=small" ng-if="c.data.record.providerLogo" alt="{{::c.data.record.provider}}" class="avatar-small" style="display: inline-flex; width: 16px; height: 16px;"/>
        &nbsp;
      </span>
    </p>
    <p>&nbsp;</p>
  </div>
</div>

We don’t need any client side code, but to pretty things up just a bit, we do need a wee bit of CSS.

  padding: 5px;
}

th {
  color: #ccc;
  font-style: italic;
  text-align: center;
  border-bottom: 1px solid #ccc;
}

.local-app {
  background-color: #f5f5f5;
  padding: 5px;
}

Someone who actually knows what they are doing could probably do a much better job with the prettying up part, but this will do for now.

So now all that is left is to bundle the whole thing up into yet another pre-release Update Set for testing purposes, so here you go:

This is another drop-in replacement for any previous 0.7.x version. If you have been already been testing with any other version, just install this one over the one that you have been using. If you installing for the first time, you will need the other prerequisites, which you can read about here and here and here. As always, feedback of any kind in the comments section is welcome, encouraged, and very much appreciated. Also, any ideas on the shopping experience in general, or on the search widget that we have yet to add to the other side of the page, would be great as well. Next time, we may start taking a look at that unless we have some test results to review. Thanks to everyone who has taken the time to take this out for a spin, and if you haven’t done it yet, please give it a try and let us know what you find.