1 /**
  2 * @fileOverview JavaScript representation of the Finesse TeamNotReadyReasonCodes collection
  3 * object which contains a list of TeamNotReadyReasonCode objects.
  4  *
  5  * @name finesse.restservices.TeamNotReadyReasonCodes
  6  * @requires finesse.clientservices.ClientServices
  7  * @requires Class
  8  * @requires finesse.FinesseBase
  9  * @requires finesse.restservices.RestBase
 10  * @requires finesse.restservices.Dialog
 11  * @requires finesse.restservices.RestCollectionBase
 12  */
 13 
 14 var finesse = finesse || {};
 15 finesse.restservices = finesse.restservices || {};
 16 
 17 /** @private */
 18 finesse.restservices.TeamNotReadyReasonCodes = finesse.restservices.RestCollectionBase.extend(/** @lends finesse.restservices.TeamNotReadyReasonCodes.prototype */{
 19 
 20     /**
 21      * @class
 22      * JavaScript representation of a TeamNotReadyReasonCodes collection object. Also exposes
 23      * methods to operate on the object against the server.
 24      *
 25      * @param {Object} options
 26      *     An object with the following properties:<ul>
 27      *         <li><b>id:</b> The id of the object being constructed</li>
 28      *         <li><b>onLoad(this): (optional)</b> when the object is successfully loaded from the server</li>
 29      *         <li><b>onChange(this): (optional)</b> when an update notification of the object is received</li>
 30      *         <li><b>onAdd(this): (optional)</b> when a notification that the object is created is received</li>
 31      *         <li><b>onDelete(this): (optional)</b> when a notification that the object is deleted is received</li>
 32      *         <li><b>onError(rsp): (optional)</b> if loading of the object fails, invoked with the error response object:<ul>
 33      *             <li><b>status:</b> {Number} The HTTP status code returned</li>
 34      *             <li><b>content:</b> {String} Raw string of response</li>
 35      *             <li><b>object:</b> {Object} Parsed object of response</li>
 36      *             <li><b>error:</b> {Object} Wrapped exception that was caught:<ul>
 37      *                 <li><b>errorType:</b> {String} Type of error that was caught</li>
 38      *                 <li><b>errorMessage:</b> {String} Message associated with error</li>
 39      *             </ul></li>
 40      *         </ul></li>
 41      *         <li><b>parentObj: (optional)</b> The parent object</li></ul>
 42      * @constructs finesse.restservices.TeamNotReadyReasonCodes
 43      **/
 44     init: function (options) {
 45         this._super(options);
 46     },
 47 
 48     /**
 49      * @private
 50      * Gets the REST class for the current object - this is the TeamNotReadyReasonCodes class.
 51      */
 52     getRestClass: function () {
 53         return finesse.restservices.TeamNotReadyReasonCodes;
 54     },
 55 
 56     /**
 57      * @private
 58      * Gets the REST class for the objects that make up the collection. - this
 59      * is the TeamNotReadyReasonCode class.
 60      */
 61     getRestItemClass: function () {
 62         return finesse.restservices.TeamNotReadyReasonCode;
 63     },
 64 
 65     /**
 66      * @private
 67      * Gets the REST type for the current object - this is a "ReasonCodes".
 68      */
 69     getRestType: function () {
 70         return "ReasonCodes";
 71     },
 72 
 73     /**
 74      * Overrides the parent class.  Returns the url for the NotReadyReasonCodes resource
 75      */
 76     getRestUrl: function () {
 77         // return ("/finesse/api/" + this.getRestType() + "?category=NOT_READY");
 78         var restObj = this._restObj,
 79             restUrl = "";
 80         //Prepend the base REST object if one was provided.
 81         //Otherwise prepend with the default webapp name.
 82         if (restObj instanceof finesse.restservices.RestBase) {
 83             restUrl += restObj.getRestUrl();
 84         }
 85         else {
 86             restUrl += "/finesse/api";
 87         }
 88         //Append the REST type.
 89         restUrl += "/ReasonCodes?category=NOT_READY";
 90         //Append ID if it is not undefined, null, or empty.
 91         if (this._id) {
 92             restUrl += "/" + this._id;
 93         }
 94         return restUrl;
 95     },
 96 
 97     /**
 98      * @private
 99      * Gets the REST type for the objects that make up the collection - this is "ReasonCode".
100      */
101     getRestItemType: function () {
102         return "ReasonCode";
103     },
104 
105     /**
106      * @private
107      * Override default to indicates that the collection supports making
108      * requests.
109      */
110     supportsRequests: true,
111 
112     /**
113      * @private
114      * Override default to indicate that this object doesn't support subscriptions.
115      */
116     supportsRestItemSubscriptions: false,
117 
118     /**
119      * Retrieve the Not Ready Reason Codes.
120      *
121      * @returns {finesse.restservices.TeamNotReadyReasonCodes}
122      *     This TeamNotReadyReasonCodes object to allow cascading.
123      */
124     get: function () {
125         // set loaded to false so it will rebuild the collection after the get
126         this._loaded = false;
127         // reset collection
128         this._collection = {};
129         // perform get
130         this._synchronize();
131         return this;
132     },
133 
134     /**
135      * Set up the PutSuccessHandler for TeamNotReadyReasonCodes
136      * @param {Object} reasonCodes
137      * @param {String} contentBody
138      * @param successHandler    
139      * @return {function}
140      */
141     createPutSuccessHandler: function (reasonCodes, contentBody, successHandler) {
142         return function (rsp) {
143             // Update internal structure based on response. Here we
144             // inject the contentBody from the PUT request into the
145             // rsp.object element to mimic a GET as a way to take
146             // advantage of the existing _processResponse method.
147             rsp.object = contentBody;
148             reasonCodes._processResponse(rsp);
149 
150             //Remove the injected contentBody object before cascading response
151             rsp.object = {};
152 
153             //cascade response back to consumer's response handler
154             successHandler(rsp);
155         };
156     },
157 
158     /**
159      * Perform the REST API PUT call to update the reason code assignments for the team
160      * @param {string[]} newValues
161      * @param handlers     
162      */
163     update: function (newValues, handlers) {
164         this.isLoaded();
165         var contentBody = {}, contentBodyInner = [], i, innerObject = {};
166 
167         contentBody[this.getRestType()] = {
168         };
169 
170         for (i in newValues) {
171             if (newValues.hasOwnProperty(i)) {
172                 innerObject = {
173                     "uri": newValues[i]
174                 };
175                 contentBodyInner.push(innerObject);
176             }
177         }
178 
179         contentBody[this.getRestType()] = {
180             "ReasonCode" : contentBodyInner
181         };
182 
183         // Protect against null dereferencing of options allowing its (nonexistant) keys to be read as undefined
184         handlers = handlers || {};
185 
186         this.restRequest(this.getRestUrl(), {
187             method: 'PUT',
188             success: this.createPutSuccessHandler(this, contentBody, handlers.success),
189             error: handlers.error,
190             content: contentBody
191         });
192 
193         return this; // Allow cascading
194     }
195 });