1 /**
  2  * @fileOverview JavaScript representation of the Finesse LayoutConfig object.
  3  *
  4  * @name finesse.restservices.LayoutConfig
  5  * @requires finesse.clientservices.ClientServices
  6  * @requires Class
  7  * @requires finesse.FinesseBase
  8  * @requires finesse.restservices.RestBase
  9  */
 10 
 11 var finesse = finesse || {};
 12 finesse.restservices = finesse.restservices || {};
 13 
 14 /** @private */
 15 finesse.restservices.TeamLayoutConfig = finesse.restservices.RestBase.extend(/** @lends finesse.restservices.LayoutConfig.prototype */{
 16     // Keep the restresponse so we can parse the layoutxml out of it in getLayoutXML()
 17     keepRestResponse: true,
 18 
 19     /**
 20      * @class
 21      * JavaScript representation of a LayoutConfig object. Also exposes
 22      * methods to operate on the object against the server.
 23      *
 24 	 * @param {Object} options
 25 	 *     An object with the following properties:<ul>
 26      *         <li><b>id:</b> The id of the object being constructed</li>
 27      *         <li><b>onLoad(this): (optional)</b> when the object is successfully loaded from the server</li>
 28      *         <li><b>onChange(this): (optional)</b> when an update notification of the object is received</li>
 29      *         <li><b>onAdd(this): (optional)</b> when a notification that the object is created is received</li>
 30      *         <li><b>onDelete(this): (optional)</b> when a notification that the object is deleted is received</li>
 31      *         <li><b>onError(rsp): (optional)</b> if loading of the object fails, invoked with the error response object:<ul>
 32      *             <li><b>status:</b> {Number} The HTTP status code returned</li>
 33      *             <li><b>content:</b> {String} Raw string of response</li>
 34      *             <li><b>object:</b> {Object} Parsed object of response</li>
 35      *             <li><b>error:</b> {Object} Wrapped exception that was caught:<ul>
 36      *                 <li><b>errorType:</b> {String} Type of error that was caught</li>
 37      *                 <li><b>errorMessage:</b> {String} Message associated with error</li>
 38      *             </ul></li>
 39      *         </ul></li>
 40      *         <li><b>parentObj: (optional)</b> The parent object</li></ul>
 41      * @constructs finesse.restservices.LayoutConfig
 42      **/
 43     init: function (options) {
 44         this._super(options);
 45     },
 46 
 47     /**
 48      * @private
 49      * Gets the REST class for the current object - this is the LayoutConfigs class.
 50      * @returns {Object} The LayoutConfigs class.
 51      */
 52     getRestClass: function () {
 53         return finesse.restservices.LayoutConfig;
 54     },
 55 
 56     /**
 57      * @private
 58      * Gets the REST type for the current object - this is a "LayoutConfig".
 59      * @returns {String} The LayoutConfig string.
 60      */
 61     getRestType: function () {
 62         return "TeamLayoutConfig";
 63     },
 64 
 65     /**
 66      * @private
 67      * Override default to indicate that this object doesn't support making
 68      * requests.
 69      */
 70     supportsRequests: false,
 71 
 72     /**
 73      * @private
 74      * Override default to indicate that this object doesn't support subscriptions.
 75      */
 76     supportsSubscriptions: false,
 77 
 78     /**
 79      * Getter for the category.
 80      * @returns {String} The category.
 81      */
 82     getLayoutXML: function () {
 83         this.isLoaded();
 84         var restResponse = this.getRestResponse(),
 85             domxml = jQuery.parseXML(restResponse),
 86             layoutxml = $(domxml).find("finesseLayout")[0];
 87         // get xml string differently depending on browser type
 88         // XMLSerializer is not present in IE
 89         return (typeof window.XMLSerializer !== "undefined") ?
 90             (new window.XMLSerializer()).serializeToString(layoutxml) :
 91             layoutxml.xml;
 92     },
 93 
 94     /**
 95      * Getter for the code.
 96      * @returns {String} The code.
 97      */
 98     getUseDefault: function () {
 99         this.isLoaded();
100         return this.getData().useDefault;
101     },
102     
103     /**
104      * Retrieve the TeamLayoutConfig.
105      *
106      * @returns {finesse.restservices.TeamLayoutConfig}
107      */
108     get: function () {
109         // this._id is needed, but is not used in this object.. we're overriding getRestUrl anyway
110         this._id = "0";
111         // set loaded to false so it will rebuild the collection after the get
112         this._loaded = false;
113         // reset collection
114         this._collection = {};
115         // perform get
116         this._synchronize();
117         return this;
118     },
119 
120     createPutSuccessHandler: function(contact, contentBody, successHandler){
121         return function (rsp) {
122             // Update internal structure based on response. Here we
123             // inject the contentBody from the PUT request into the
124             // rsp.object element to mimic a GET as a way to take
125             // advantage of the existing _processResponse method.
126             rsp.object = contentBody;
127             contact._processResponse(rsp);
128 
129             //Remove the injected Contact object before cascading response
130             rsp.object = {};
131             
132             //cascade response back to consumer's response handler
133             successHandler(rsp);
134         };
135     },
136     
137     put: function (newValues, handlers) {
138         // this._id is needed, but is not used in this object.. we're overriding getRestUrl anyway
139         this._id = "0";
140         this.isLoaded();
141         var contentBody = {};
142         contentBody[this.getRestType()] = {
143             "useDefault": newValues.useDefault,
144             "layoutxml": newValues.layoutXML
145         };
146 
147         // Protect against null dereferencing of options allowing its (nonexistant) keys to be read as undefined
148         handlers = handlers || {};
149 
150         this.restRequest(this.getRestUrl(), {
151             method: 'PUT',
152             success: this.createPutSuccessHandler(this, contentBody, handlers.success),
153             error: handlers.error,
154             content: contentBody
155         });
156 
157         return this; // Allow cascading
158     },
159 
160     getRestUrl: function(){
161         // return team's url + /LayoutConfig
162         // eg: /api/Team/1/LayoutConfig
163         if(this._restObj === undefined){
164             throw new Exception("TeamLayoutConfig instances must have a parent team object.");
165         }
166         return this._restObj.getRestUrl() + '/LayoutConfig';
167     }
168 
169 });
170 
171