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.
 36    *
 37    * @field
 38    * @public
 39    * @type String
 40    * @default "select"
 41    */
 42   servlet: 'select',
 43 
 44   /**
 45    * The most recent response from Solr.
 46    *
 47    * @field
 48    * @private
 49    * @type Object
 50    * @default {}
 51    */
 52   response: {},
 53 
 54   /** 
 55    * A collection of all registered widgets. For internal use only.
 56    *
 57    * @field
 58    * @private
 59    * @type Object
 60    * @default {}
 61    */
 62   widgets: {},
 63 
 64   /**
 65    * The parameter store for the manager and its widgets. For internal use only.
 66    *
 67    * @field
 68    * @private
 69    * @type Object
 70    */
 71   store: null,
 72 
 73   /**
 74    * Whether <tt>init()</tt> has been called yet. For internal use only.
 75    *
 76    * @field
 77    * @private
 78    * @type Boolean
 79    * @default false
 80    */
 81   initialized: false,
 82 
 83   /**
 84    * An abstract hook for child implementations.
 85    *
 86    * <p>This method should be called after the store and the widgets have been
 87    * added. It should initialize the widgets and the store, and do any other
 88    * one-time initializations, e.g., perform the first request to Solr.</p>
 89    *
 90    * <p>If no store has been set, it sets the store to the basic <tt>
 91    * AjaxSolr.ParameterStore</tt>.</p>
 92    */
 93   init: function () {
 94     this.initialized = true;
 95     if (this.store === null) {
 96       this.setStore(new AjaxSolr.ParameterStore());
 97     }
 98     this.store.load(false);
 99     for (var widgetId in this.widgets) {
100       this.widgets[widgetId].init();
101     }
102     this.store.init();
103   },
104 
105   /**
106    * Set the manager's parameter store.
107    *
108    * @param {AjaxSolr.ParameterStore} store
109    */
110   setStore: function (store) { 
111     store.manager = this;
112     this.store = store;
113   },
114 
115   /** 
116    * Adds a widget to the manager.
117    *
118    * @param {AjaxSolr.AbstractWidget} widget
119    */
120   addWidget: function (widget) { 
121     widget.manager = this;
122     this.widgets[widget.id] = widget;
123   },
124 
125   /** 
126    * Stores the Solr parameters to be sent to Solr and sends a request to Solr.
127    *
128    * @param {Boolean} [start] The Solr start offset parameter.
129    * @param {String} [servlet] The Solr servlet to send the request to.
130    */
131   doRequest: function (start, servlet) {
132     if (this.initialized === false) {
133       this.init();
134     }
135     // Allow non-pagination widgets to reset the offset parameter.
136     if (start !== undefined) {
137       this.store.get('start').val(start);
138     }
139     if (servlet === undefined) {
140       servlet = this.servlet;
141     }
142 
143     this.store.save();
144 
145     for (var widgetId in this.widgets) {
146       this.widgets[widgetId].beforeRequest();
147     }
148 
149     this.executeRequest(servlet);
150   },
151 
152   /**
153    * An abstract hook for child implementations.
154    *
155    * <p>Sends the request to Solr, i.e. to <code>this.solrUrl</code> or <code>
156    * this.proxyUrl</code>, and receives Solr's response. It should send <code>
157    * this.store.string()</code> as the Solr query, and it should pass Solr's
158    * response to <code>handleResponse()</code> for handling.</p>
159    *
160    * <p>See <tt>managers/Manager.jquery.js</tt> for a jQuery implementation.</p>
161    *
162    * @param {String} servlet The Solr servlet to send the request to.
163    * @throws If not defined in child implementation.
164    */
165   executeRequest: function (servlet) {
166     throw 'Abstract method executeRequest must be overridden in a subclass.';
167   },
168 
169   /**
170    * This method is executed after the Solr response data arrives. Allows each
171    * widget to handle Solr's response separately.
172    *
173    * @param {Object} data The Solr response.
174    */
175   handleResponse: function (data) {
176     this.response = data;
177 
178     for (var widgetId in this.widgets) {
179       this.widgets[widgetId].afterRequest();
180     }
181   }
182 });
183