define([ "../../lib/object", "./_Class", "./surveyRuns", "dojo/Deferred", "dojo/_base/declare", "dojo/_base/json", "dojo/_base/lang", "dojo/_base/xhr" ], function(objectFuns, _Class, surveyRuns, Deferred, declare, json, lang, xhr) { var Responses = declare([_Class],{ _collection: 'responses', _type: 'Response', create: function() { var obj = { type: this._type, answers: {}, surveyRunId: null }; return obj; }, _deserialize: function(obj) { if (obj._surveyRun) { obj._surveyRun = surveyRuns._doDeserialize(obj._surveyRun); } if (obj.publicationDate) { obj.publicationDate = this._parseDate(obj.publicationDate); } }, _serialize: function(obj) { this._convertCheckAndRadio(obj.answers); if (obj._surveyRun) { obj._surveyRun = surveyRuns._doSerialize(obj._surveyRun); } if (obj.publicationDate) { obj.publicationDate = this._formatDate(obj.publicationDate); } }, getWithSecret: function(id,secret) { var query = xhr.objectToQuery({secret:secret}); return xhr('GET',{ url: '/api/open/responses/' + id + '?' + query, handleAs: 'json', contentType: false }).then(lang.hitch(this,'_doDeserialize'), lang.hitch(this,'_deserializeError')); }, postWithSecret: function(response,secret) { var query = xhr.objectToQuery({secret:secret}); var body = json.toJson(this._doSerialize(response)); return xhr('POST',{ url: '/api/open/responses?' + query, handleAs: 'json', contentType: 'application/json', rawBody: body }).then(lang.hitch(this,'_doDeserialize'), lang.hitch(this,'_deserializeError')); }, putWithSecret: function(response,secret) { var query = xhr.objectToQuery({secret:secret}); var body = json.toJson(this._doSerialize(response)); return xhr('PUT',{ url: '/api/open/responses/' + this.getId(response) + '?' + query, handleAs: 'json', contentType: 'application/json', rawBody: body }).then(lang.hitch(this,'_doDeserialize'), lang.hitch(this,'_deserializeError')); }, removeWithSecret: function(response,secret) { var query = xhr.objectToQuery({secret:secret}); var rev = this.getRev(response); var body = json.toJson(this._doSerialize(response)); var headers = {}; if ( rev ) { headers['If-Match'] = '"'+rev+'"'; } return xhr('DELETE',{ url: '/api/open/responses/' + this.getId(response) + '?' + query, headers: headers, handleAs: 'json', contentType: 'application/json', rawBody: body }).then(function(result){ return result; },lang.hitch(this,'_deserializeError')); }, _convertCheckAndRadio: function(answers) { // When we encounter an array, we assume it's really a // checkbox value. objectFuns.forEach(answers,function(v,prop){ if ( lang.isArray(v) ) { switch (v.length) { case 0: case 1: answers[prop] = v[0]; break; default: throw new Error("Responses cannot exist of array values."); } } },this); } }); return new Responses(); });