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