1 // $Id$
  2 
  3 /**
  4  * The Manager acts as the controller in a Model-View-Controller framework. All
  5  * public calls should be performed on the manager object.
  6  *
  7  * @param properties A map of fields to set. Refer to the list of public fields.
  8  * @class AbstractManager
  9  */
 10 AjaxSolr.AbstractManager = AjaxSolr.Class.extend(
 11   /** @lends AjaxSolr.AbstractManager.prototype */
 12   {
 13   /**
 14    * The fully-qualified URL of the Solr application. You must include the
 15    * trailing slash. Do not include the path to any Solr servlet.
 16    *
 17    * @field
 18    * @public
 19    * @type String
 20    * @default "http://localhost:8983/solr/"
 21    */
 22   solrUrl: 'http://localhost:8983/solr/',
 23 
 24   /**
 25    * If we want to proxy queries through a script, rather than send queries
 26    * to Solr directly, set this field to the fully-qualified URL of the script.
 27    *
 28    * @field
 29    * @public
 30    * @type String
 31    */
 32   proxyUrl: null,
 33 
 34   /**
 35    * The default Solr servlet. You may prepend the servlet with a core if using
 36    * multiple cores.
 37    *
 38    * @field
 39    * @public
 40    * @type String
 41    * @default "select"
 42    */
 43   servlet: 'select',
 44 
 45   /**
 46    * The most recent response from Solr.
 47    *
 48    * @field
 49    * @private
 50    * @type Object
 51    * @default {}
 52    */
 53   response: {},
 54 
 55   /** 
 56    * A collection of all registered widgets. For internal use only.
 57    *
 58    * @field
 59    * @private
 60    * @type Object
 61    * @default {}
 62    */
 63   widgets: {},
 64 
 65   /**
 66    * The parameter store for the manager and its widgets. For internal use only.
 67    *
 68    * @field
 69    * @private
 70    * @type Object
 71    */
 72   store: null,
 73 
 74   /**
 75    * Whether <tt>init()</tt> has been called yet. For internal use only.
 76    *
 77    * @field
 78    * @private
 79    * @type Boolean
 80    * @default false
 81    */
 82   initialized: false,
 83 
 84   /**
 85    * An abstract hook for child implementations.
 86    *
 87    * <p>This method should be called after the store and the widgets have been
 88    * added. It should initialize the widgets and the store, and do any other
 89    * one-time initializations, e.g., perform the first request to Solr.</p>
 90    *
 91    * <p>If no store has been set, it sets the store to the basic <tt>
 92    * AjaxSolr.ParameterStore</tt>.</p>
 93    */
 94   init: function () {
 95     this.initialized = true;
 96     if (this.store === null) {
 97       this.setStore(new AjaxSolr.ParameterStore());
 98     }
 99     this.store.load(false);
100     for (var widgetId in this.widgets) {
101       this.widgets[widgetId].init();
102     }
103     this.store.init();
104   },
105 
106   /**
107    * Set the manager's parameter store.
108    *
109    * @param {AjaxSolr.ParameterStore} store
110    */
111   setStore: function (store) { 
112     store.manager = this;
113     this.store = store;
114   },
115 
116   /** 
117    * Adds a widget to the manager.
118    *
119    * @param {AjaxSolr.AbstractWidget} widget
120    */
121   addWidget: function (widget) { 
122     widget.manager = this;
123     this.widgets[widget.id] = widget;
124   },
125 
126   /** 
127    * Stores the Solr parameters to be sent to Solr and sends a request to Solr.
128    *
129    * @param {Boolean} [start] The Solr start offset parameter.
130    * @param {String} [servlet] The Solr servlet to send the request to.
131    */
132   doRequest: function (start, servlet) {
133     if (this.initialized === false) {
134       this.init();
135     }
136     // Allow non-pagination widgets to reset the offset parameter.
137     if (start !== undefined) {
138       this.store.get('start').val(start);
139     }
140     if (servlet === undefined) {
141       servlet = this.servlet;
142     }
143 
144     this.store.save();
145 
146     for (var widgetId in this.widgets) {
147       this.widgets[widgetId].beforeRequest();
148     }
149 
150     this.executeRequest(servlet);
151   },
152 
153   /**
154    * An abstract hook for child implementations.
155    *
156    * <p>Sends the request to Solr, i.e. to <code>this.solrUrl</code> or <code>
157    * this.proxyUrl</code>, and receives Solr's response. It should send <code>
158    * this.store.string()</code> as the Solr query, and it should pass Solr's
159    * response to <code>handleResponse()</code> for handling.</p>
160    *
161    * <p>See <tt>managers/Manager.jquery.js</tt> for a jQuery implementation.</p>
162    *
163    * @param {String} servlet The Solr servlet to send the request to.
164    * @throws If not defined in child implementation.
165    */
166   executeRequest: function (servlet) {
167     throw 'Abstract method executeRequest must be overridden in a subclass.';
168   },
169 
170   /**
171    * This method is executed after the Solr response data arrives. Allows each
172    * widget to handle Solr's response separately.
173    *
174    * @param {Object} data The Solr response.
175    */
176   handleResponse: function (data) {
177     this.response = data;
178 
179     for (var widgetId in this.widgets) {
180       this.widgets[widgetId].afterRequest();
181     }
182   }
183 });
184