1 /**
  2 * @fileOverview JavaScript representation of the Finesse Team Wrap-Up Reasons collection
  3 * object which contains a list of Wrap-Up Reasons objects.
  4  *
  5  * @name finesse.restservices.TeamWrapUpReasons
  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.TeamWrapUpReasons = finesse.restservices.RestCollectionBase.extend(/** @lends finesse.restservices.TeamWrapUpReasons.prototype */{
 19 
 20     /**
 21      * @class
 22      * JavaScript representation of a TeamWrapUpReasons 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.TeamWrapUpReasons
 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 TeamWrapUpReasons class.
 51      */
 52     getRestClass: function () {
 53         return finesse.restservices.TeamWrapUpReasons;
 54     },
 55 
 56     /**
 57      * @private
 58      * Gets the REST class for the objects that make up the collection. - this
 59      * is the TeamWrapUpReason class.
 60      */
 61     getRestItemClass: function () {
 62         return finesse.restservices.TeamWrapUpReason;
 63     },
 64 
 65     /**
 66      * @private
 67      * Gets the REST type for the current object - this is a "WrapUpReasons".
 68      */
 69     getRestType: function () {
 70         return "WrapUpReasons";
 71     },
 72 
 73     /**
 74      * @private
 75      * Gets the REST type for the objects that make up the collection - this is "WrapUpReason".
 76      */
 77     getRestItemType: function () {
 78         return "WrapUpReason";
 79     },
 80 
 81     /**
 82      * @private
 83      * Override default to indicates that the collection supports making
 84      * requests.
 85      */
 86     supportsRequests: true,
 87 
 88     /**
 89      * @private
 90      * Override default to indicate that this object doesn't support subscriptions.
 91      */
 92     supportsRestItemSubscriptions: false,
 93 
 94     /**
 95      * Retrieve the Team Wrap Up Reasons.
 96      *
 97      * @returns {finesse.restservices.TeamWrapUpReasons}
 98      *     This TeamWrapUpReasons object to allow cascading.
 99      */
100     get: function () {
101         // set loaded to false so it will rebuild the collection after the get
102         this._loaded = false;
103         // reset collection
104         this._collection = {};
105         // perform get
106         this._synchronize();
107         return this;
108     },
109 
110     /**
111      * Set up the PutSuccessHandler for TeamWrapUpReasons
112      * @param {Object} wrapUpReasons
113      * @param {Object} contentBody
114      * @param successHandler
115      * @returns response
116      */
117     createPutSuccessHandler: function (wrapUpReasons, contentBody, successHandler) {
118         return function (rsp) {
119             // Update internal structure based on response. Here we
120             // inject the contentBody from the PUT request into the
121             // rsp.object element to mimic a GET as a way to take
122             // advantage of the existing _processResponse method.
123             rsp.object = contentBody;
124             
125             wrapUpReasons._processResponse(rsp);
126 
127             //Remove the injected contentBody object before cascading response
128             rsp.object = {};
129 
130             //cascade response back to consumer's response handler
131             successHandler(rsp);
132         };
133     },
134 
135     /**    
136      * Perform the REST API PUT call to update the reason code assignments for the team
137      * @param {String Array} newValues
138      * @param handlers
139      * @returns {Object} this
140      */
141     update: function (newValues, handlers) {
142         this.isLoaded();
143         var contentBody = {}, contentBodyInner = [], i, innerObject = {};
144 
145         contentBody[this.getRestType()] = {
146         };
147 
148         for (i in newValues) {
149             if (newValues.hasOwnProperty(i)) {
150                 innerObject = {
151                     "uri": newValues[i]
152                 };
153                 contentBodyInner.push(innerObject);
154             }
155         }
156 
157         contentBody[this.getRestType()] = {
158             "WrapUpReason" : contentBodyInner
159         };
160 
161         // Protect against null dereferencing of options allowing its (nonexistant) keys to be read as undefined
162         handlers = handlers || {};
163 
164         this.restRequest(this.getRestUrl(), {
165             method: 'PUT',
166             success: this.createPutSuccessHandler(this, contentBody, handlers.success),
167             error: handlers.error,
168             content: contentBody
169         });
170 
171         return this; // Allow cascading
172     }
173 });