/* global define */
define(['jquery', 'underscore', 'backbone', 'uuid', 'collections/ObjectFormats', 'models/BaseDataONEObject', 'md5'],
function ($, _, Backbone, uuid, ObjectFormats, BaseDataONEObject, md5) {
/*
A DataONEObject represents a DataONE object that has a format
type of DATA, METADATA, or RESOURCE. It stores the system
metadata attributes for the object at a minimum.
*/
var DataONEObject = BaseDataONEObject.extend({
/**
* Override defaults
* @returns {*}
*/
defaults: function () {
return _.extend(BaseDataONEObject.prototype.defaults(), {
accessPolicy: MetacatUI.appModel.get("defaultAccessPolicy"),
id: DataONEObject.generateEssDiveId()
});
},
/**
* Override the updateID() method to pass in and ESS-DIVE identifier
*
* @param id
* @returns {*}
*/
updateID: function(id){
BaseDataONEObject.prototype.updateID.apply(this,[DataONEObject.generateEssDiveId(this)])
}
},
{
/***
* Generates an ESS-DIVE identifier.
*
* The format is `ess-dive-{hash}-{timestamp}`.
*
* The `hash` is generated from the seriesId, if it exists.
*
* @param thisDataONEObject
* @returns {string}
*/
generateEssDiveId: function (thisDataONEObject) {
// Create the Date suffix of the ID
// Metacat does not like colons, removing
// them along with Z
var suffix_date = new Date().toISOString().replace(/:|Z|\.|-/g,"");
// Hash is generated from Math.random if there is no series id
var hash;
if (typeof thisDataONEObject != "undefined" && thisDataONEObject.get("seriesId")) {
// Generate an MD5 hash from the seriesID. Take the first 12 chars
hash = md5(thisDataONEObject.get("seriesId"));
}
else {
hash = md5(suffix_date+Math.random().toString());
}
// Truncating the hash at 15 characters means collisions will start
// happening at ~4.2 trillion (collisions = 2^2n where n = # hex digits)
return "ess-dive-" + hash.substr(0, 15) + "-" + suffix_date;
},
});
return DataONEObject;
}
);