caadd3e9d1ee24072a20116dc7493b3de453950e
[working/Evergreen.git] / 1.6 / development / datamodelsandaccess.xml
1 <?xml version="1.0" encoding="UTF-8"?>\r
2 <chapter xml:id="data_models_and_access" xmlns="http://docbook.org/ns/docbook" version="5.0" xml:lang="EN"\r
3     xmlns:xi="http://www.w3.org/2001/XInclude" xmlns:xlink="http://www.w3.org/1999/xlink">\r
4         <chapterinfo>\r
5         <title>Evergreen Data Models and Access</title>\r
6         </chapterinfo>\r
7         <section id="_database_schema">\r
8                 <title>Database schema</title>\r
9                 <simpara>The database schema is tied pretty tightly to PostgreSQL. Although PostgreSQL\r
10                 adheres closely to ANSI SQL standards, the use of schemas, SQL functions\r
11                 implemented in both plpgsql and plperl, and PostgreSQL&#8217;s native full-text\r
12                 search would make it&#8230; challenging&#8230; to port to other database platforms.</simpara>\r
13                 <simpara>A few common PostgreSQL interfaces for poking around the schema and\r
14                 manipulating data are:</simpara>\r
15                 <itemizedlist>\r
16                 <listitem>\r
17                 <simpara>\r
18                 psql (the command line client)\r
19                 </simpara>\r
20                 </listitem>\r
21                 <listitem>\r
22                 <simpara>\r
23                 pgadminIII (a GUI client).\r
24                 </simpara>\r
25                 </listitem>\r
26                 </itemizedlist>\r
27                 <simpara>Or you can read through the source files in Open-ILS/src/sql/Pg.</simpara>\r
28                 <simpara>Let&#8217;s take a quick tour through the schemas, pointing out some highlights\r
29                 and some key interdependencies:</simpara>\r
30                 <itemizedlist>\r
31                 <listitem>\r
32                 <simpara>\r
33                 actor.org_unit &#8594; asset.copy_location\r
34                 </simpara>\r
35                 </listitem>\r
36                 <listitem>\r
37                 <simpara>\r
38                 actor.usr &#8594; actor.card\r
39                 </simpara>\r
40                 </listitem>\r
41                 <listitem>\r
42                 <simpara>\r
43                 biblio.record_entry &#8594; asset.call_number &#8594; asset.copy\r
44                 </simpara>\r
45                 </listitem>\r
46                 <listitem>\r
47                 <simpara>\r
48                 config.metabib_field &#8594; metabib.*_field_entry\r
49                 </simpara>\r
50                 </listitem>\r
51                 </itemizedlist>\r
52         </section>\r
53         <section id="_database_access_methods">\r
54                 <title>Database access methods</title>\r
55                 <simpara>You could use direct access to the database via Perl DBI, JDBC, etc,\r
56                 but Evergreen offers several database CRUD services for\r
57                 creating / retrieving / updating / deleting data. These avoid tying\r
58                 you too tightly to the current database schema and they funnel database\r
59                 access through the same mechanism, rather than tying up connections\r
60                 with other interfaces.</simpara>\r
61         </section>\r
62         <section id="_evergreen_interface_definition_language_idl">\r
63                 <title>Evergreen Interface Definition Language (IDL)</title>\r
64                 <simpara>Defines properties and required permissions for Evergreen classes.\r
65                 To reduce network overhead, a given object is identified via a\r
66                 class-hint and serialized as a JSON array of properties (no named properties).</simpara>\r
67                 <simpara>As of 1.6, fields will be serialized in the order in which they appear\r
68                 in the IDL definition file, and the is_new / is_changed / is_deleted\r
69                 properties are automatically added. This has greatly reduced the size of\r
70                 the <literal>fm_IDL.xml</literal> file and makes DRY people happier :)</simpara>\r
71                 <itemizedlist>\r
72                 <listitem>\r
73                 <simpara>\r
74                 &#8230; oils_persist:readonly tells us, if true, that the data lives in the database, but is pulled from the SELECT statement defined in the &lt;oils_persist:source_definition&gt; child element\r
75                 </simpara>\r
76                 </listitem>\r
77                 </itemizedlist>\r
78                 <simplesect id="_idl_basic_example_config_language_map">\r
79                         <title>IDL basic example (config.language_map)</title>\r
80                         <programlisting language="xml" linenumbering="unnumbered">&lt;class id="clm" controller="open-ils.cstore open-ils.pcrud"\r
81                                 oils_obj:fieldmapper="config::language_map"\r
82                                 oils_persist:tablename="config.language_map"\r
83                                 reporter:label="Language Map" oils_persist:field_safe="true"&gt; <co id="dmCO5-1"/> <co id="dmCO5-2"/> <co id="dmCO5-3"/> <co id="dmCO5-4"/>\r
84                             &lt;fields oils_persist:primary="code" oils_persist:sequence=""&gt;  <co id="dmCO5-5"/>\r
85                                 &lt;field reporter:label="Language Code" name="code"\r
86                                     reporter:selector="value" reporter:datatype="text"/&gt; <co id="dmCO5-6"/>\r
87                                 &lt;field reporter:label="Language" name="value"\r
88                                     reporter:datatype="text" oils_persist:i18n="true"/&gt; <co id="dmCO5-7"/>\r
89                             &lt;/fields&gt;\r
90                             &lt;links/&gt;\r
91                             &lt;permacrud xmlns="http://open-ils.org/spec/opensrf/IDL/permacrud/v1"&gt; <co id="dmCO5-8"/>\r
92                                 &lt;actions&gt;\r
93                                     &lt;create global_required="true" permission="CREATE_MARC_CODE"&gt; <co id="dmCO5-9"/>\r
94                                     &lt;retrieve global_required="true"\r
95                                         permission="CREATE_MARC_CODE UPDATE_MARC_CODE DELETE_MARC_CODE"&gt;\r
96                                     &lt;update global_required="true" permission="UPDATE_MARC_CODE"&gt;\r
97                                     &lt;delete global_required="true" permission="DELETE_MARC_CODE"&gt;\r
98                                 &lt;/actions&gt;\r
99                             &lt;/permacrud&gt;\r
100                         &lt;/class&gt;</programlisting>\r
101                         <calloutlist>\r
102                         <callout arearefs="dmCO5-1">\r
103                         <simpara>\r
104                         The <literal>class</literal> element defines the attributes and permissions for classes,\r
105                         and relationships between classes.\r
106                         </simpara>\r
107                         <itemizedlist>\r
108                         <listitem>\r
109                         <simpara>\r
110                         The <literal>id</literal> attribute on the <literal>class</literal> element defines the class hint that is\r
111                         used everywhere in Evergreen.\r
112                         </simpara>\r
113                         </listitem>\r
114                         <listitem>\r
115                         <simpara>\r
116                         The <literal>controller</literal> attribute defines the OpenSRF\r
117                         services that provide access to the data for the class objects.\r
118                         </simpara>\r
119                         </listitem>\r
120                         </itemizedlist>\r
121                         </callout>\r
122                         <callout arearefs="dmCO5-2">\r
123                         <simpara>\r
124                         The <literal>oils_obj::fieldmapper</literal> attribute defines the name of the class that\r
125                         is generated by <literal>OpenILS::Utils::Fieldmapper</literal>.\r
126                         </simpara>\r
127                         </callout>\r
128                         <callout arearefs="dmCO5-3">\r
129                         <simpara>\r
130                         The <literal>oils_persist:tablename</literal> attribute defines the name of the table\r
131                         that contains the data for the class objects.\r
132                         </simpara>\r
133                         </callout>\r
134                         <callout arearefs="dmCO5-4">\r
135                         <simpara>\r
136                         The reporter interface uses <literal>reporter:label</literal> attribute values in\r
137                         the source list to provide meaningful class and attribute names. The\r
138                         <literal>open-ils.fielder</literal> service generates a set of methods that provide direct\r
139                         access to the classes for which <literal>oils_persist:field_safe</literal> is <literal>true</literal>. For\r
140                         example,\r
141                         </simpara>\r
142                         <screen>srfsh# request open-ils.fielder open-ils.fielder.clm.atomic \\r
143                             {"query":{"code":{"=":"eng"}}}\r
144 \r
145                         Received Data: [\r
146                           {\r
147                             "value":"English",\r
148                             "code":"eng"\r
149                           }\r
150                         ]</screen>\r
151                         </callout>\r
152                         <callout arearefs="dmCO5-5">\r
153                         <simpara>\r
154                         The <literal>fields</literal> element defines the list of fields for the class.\r
155                         </simpara>\r
156                         <itemizedlist>\r
157                         <listitem>\r
158                         <simpara>\r
159                         The <literal>oils_persist:primary</literal> attribute defines the column that acts as\r
160                         the primary key for the table.\r
161                         </simpara>\r
162                         </listitem>\r
163                         <listitem>\r
164                         <simpara>\r
165                         The <literal>oils_persist:sequence</literal> attribute holds the name of the database\r
166                         sequence.\r
167                         </simpara>\r
168                         </listitem>\r
169                         </itemizedlist>\r
170                         </callout>\r
171                         <callout arearefs="dmCO5-6">\r
172                         <simpara>\r
173                         Each <literal>field</literal> element defines one property of the class.\r
174                         </simpara>\r
175                         <itemizedlist>\r
176                         <listitem>\r
177                         <simpara>\r
178                         The <literal>name</literal> attribute defines the getter/setter method name for the field.\r
179                         </simpara>\r
180                         </listitem>\r
181                         <listitem>\r
182                         <simpara>\r
183                         The <literal>reporter:label</literal> attribute defines the attribute name as used in\r
184                         the reporter interface.\r
185                         </simpara>\r
186                         </listitem>\r
187                         <listitem>\r
188                         <simpara>\r
189                         The <literal>reporter:selector</literal> attribute defines the field used in the reporter\r
190                         filter interface to provide a selectable list. This gives the user a more\r
191                         meaningful access point than the raw numeric ID or abstract code.\r
192                         </simpara>\r
193                         </listitem>\r
194                         <listitem>\r
195                         <simpara>\r
196                         The <literal>reporter:datatype</literal> attribute defines the type of data held by\r
197                         this property for the purposes of the reporter.\r
198                         </simpara>\r
199                         </listitem>\r
200                         </itemizedlist>\r
201                         </callout>\r
202                         <callout arearefs="dmCO5-7">\r
203                         <simpara>\r
204                         The <literal>oils_persist:i18n</literal> attribute, when <literal>true</literal>, means that\r
205                         translated values for the field&#8217;s contents may be accessible in\r
206                         different locales.\r
207                         </simpara>\r
208                         </callout>\r
209                         <callout arearefs="dmCO5-8">\r
210                         <simpara>\r
211                         The <literal>permacrud</literal> element defines the permissions (if any) required\r
212                         to <emphasis role="strong">c</emphasis>reate, <emphasis role="strong">r</emphasis>etrieve, <emphasis role="strong">u</emphasis>pdate, and <emphasis role="strong">d</emphasis>elete data for this\r
213                         class. <literal>open-ils.permacrud</literal> must be defined as a controller for the class\r
214                         for the permissions to be applied.\r
215                         </simpara>\r
216                         </callout>\r
217                         <callout arearefs="dmCO5-9">\r
218                         <simpara>\r
219                         Each action requires one or more <literal>permission</literal> values that the\r
220                         user must possess to perform the action.\r
221                         </simpara>\r
222                         <itemizedlist>\r
223                         <listitem>\r
224                         <simpara>\r
225                         If the <literal>global_required</literal> attribute is <literal>true</literal>, then the user must\r
226                         have been granted that permission globally (depth = 0) to perform\r
227                         the action.\r
228                         </simpara>\r
229                         </listitem>\r
230                         <listitem>\r
231                         <simpara>\r
232                         The <literal>context_field</literal> attribute denotes the <literal>&lt;field&gt;</literal> that identifies\r
233                         the org_unit at which the user must have the pertinent permission.\r
234                         </simpara>\r
235                         </listitem>\r
236                         <listitem>\r
237                         <simpara>\r
238                         An action element may contain a <literal>&lt;context_field&gt;</literal> element that\r
239                         defines the linked class (identified by the <literal>link</literal> attribute) and\r
240                         the field in the linked class that identifies the org_unit where\r
241                         the permission must be held.\r
242                         </simpara>\r
243                         <itemizedlist>\r
244                         <listitem>\r
245                         <simpara>\r
246                         If the <literal>&lt;context_field&gt;</literal> element contains a <literal>jump</literal> attribute,\r
247                         then it defines a link to a link to a class with a field identifying\r
248                         the org_unit where the permission must be held.\r
249                         </simpara>\r
250                         </listitem>\r
251                         </itemizedlist>\r
252                         </listitem>\r
253                         </itemizedlist>\r
254                         </callout>\r
255                         </calloutlist>\r
256                 </simplesect>\r
257                 <simplesect id="_reporter_data_types_and_their_possible_values">\r
258                         <title>Reporter data types and their possible values</title>\r
259                         <itemizedlist>\r
260                         <listitem>\r
261                         <simpara>\r
262                         <literal>bool</literal>: Boolean <literal>true</literal> or <literal>false</literal>\r
263                         </simpara>\r
264                         </listitem>\r
265                         <listitem>\r
266                         <simpara>\r
267                         <literal>id</literal>: ID of the row in the database\r
268                         </simpara>\r
269                         </listitem>\r
270                         <listitem>\r
271                         <simpara>\r
272                         <literal>int</literal>: integer value\r
273                         </simpara>\r
274                         </listitem>\r
275                         <listitem>\r
276                         <simpara>\r
277                         <literal>interval</literal>: PostgreSQL time interval\r
278                         </simpara>\r
279                         </listitem>\r
280                         <listitem>\r
281                         <simpara>\r
282                         <literal>link</literal>: link to another class, as defined in the <literal>&lt;links&gt;</literal>\r
283                         element of the class definition\r
284                         </simpara>\r
285                         </listitem>\r
286                         <listitem>\r
287                         <simpara>\r
288                         <literal>money</literal>: currency amount\r
289                         </simpara>\r
290                         </listitem>\r
291                         <listitem>\r
292                         <simpara>\r
293                         <literal>org_unit</literal>: list of org_units\r
294                         </simpara>\r
295                         </listitem>\r
296                         <listitem>\r
297                         <simpara>\r
298                         <literal>text</literal>: text value\r
299                         </simpara>\r
300                         </listitem>\r
301                         <listitem>\r
302                         <simpara>\r
303                         <literal>timestamp</literal>: PostgreSQL timestamp\r
304                         </simpara>\r
305                         </listitem>\r
306                         </itemizedlist>\r
307                 </simplesect>\r
308                 <simplesect id="_idl_example_with_linked_fields_actor_workstation">\r
309                         <title>IDL example with linked fields (actor.workstation)</title>\r
310                         <simpara>Just as tables often include columns with foreign keys that point\r
311                         to values stored in the column of a different table, IDL classes\r
312                         can contain fields that link to fields in other classes. The <literal>&lt;links&gt;</literal>\r
313                         element defines which fields link to fields in other classes, and\r
314                         the nature of the relationship:</simpara>\r
315                         <programlisting language="xml" linenumbering="unnumbered">&lt;class id="aws" controller="open-ils.cstore"\r
316                                 oils_obj:fieldmapper="actor::workstation"\r
317                                 oils_persist:tablename="actor.workstation"\r
318                                 reporter:label="Workstation"&gt;\r
319                             &lt;fields oils_persist:primary="id"\r
320                                     oils_persist:sequence="actor.workstation_id_seq"&gt;\r
321                                 &lt;field reporter:label="Workstation ID" name="id"\r
322                                         reporter:datatype="id"/&gt;\r
323                                 &lt;field reporter:label="Workstation Name" name="name"\r
324                                         reporter:datatype="text"/&gt;\r
325                                 &lt;field reporter:label="Owning Library" name="owning_lib"\r
326                                         reporter:datatype="org_unit"/&gt;\r
327                                 &lt;field reporter:label="Circulations" name="circulations"\r
328                                         oils_persist:virtual="true" reporter:datatype="link"/&gt; <co id="dmCO6-1"/>\r
329                             &lt;/fields&gt;\r
330                             &lt;links&gt;  <co id="dmCO6-2"/>\r
331                                 &lt;link field="owning_lib" reltype="has_a" key="id"\r
332                                         map="" class="aou"/&gt;  <co id="dmCO6-3"/>\r
333                                 &lt;link field="circulations" reltype="has_many" key="workstation"\r
334                                         map="" class="circ"/&gt;\r
335                                 &lt;link field="circulation_checkins" reltype="has_many"\r
336                                         key="checkin_workstation" map="" class="circ"/&gt;\r
337                             &lt;/links&gt;\r
338                         &lt;/class&gt;</programlisting>\r
339                         <calloutlist>\r
340                         <callout arearefs="dmCO6-1">\r
341                         <simpara>\r
342                         This field includes an <literal>oils_persist:virtual</literal> attribute with the value of\r
343                         <literal>true</literal>, meaning that the linked class <literal>circ</literal> is a virtual class.\r
344                         </simpara>\r
345                         </callout>\r
346                         <callout arearefs="dmCO6-2">\r
347                         <simpara>\r
348                         The <literal>&lt;links&gt;</literal> element contains 0 or more <literal>&lt;link&gt;</literal> elements.\r
349                         </simpara>\r
350                         </callout>\r
351                         <callout arearefs="dmCO6-3">\r
352                         <simpara>\r
353                         Each <literal>&lt;link&gt;</literal> element defines the field (<literal>field</literal>) that links to a different\r
354                         class (<literal>class</literal>), the relationship (<literal>rel_type</literal>) between this field and the target\r
355                         field (<literal>key</literal>). If the field in this class links to a virtual class, the (<literal>map</literal>)\r
356                         attribute defines the field in the target class that returns a list of matching\r
357                         objects for each object in this class.\r
358                         </simpara>\r
359                         </callout>\r
360                         </calloutlist>\r
361                 </simplesect>\r
362         </section>\r
363         <section id="open_ils_cstore_literal_data_access_interfaces">\r
364                 <title><literal>open-ils.cstore</literal> data access interfaces</title>\r
365                 <simpara>For each class documented in the IDL, the <literal>open-ils.cstore</literal> service\r
366                 automatically generates a set of data access methods, based on the\r
367                 <literal>oils_persist:tablename</literal> class attribute.</simpara>\r
368                 <simpara>For example, for the class hint <literal>clm</literal>, cstore generates the following\r
369                 methods with the <literal>config.language_map</literal> qualifer:</simpara>\r
370                 <itemizedlist>\r
371                 <listitem>\r
372                 <simpara>\r
373                 <literal>open-ils.cstore.direct.config.language_map.id_list {"code" { "like": "e%" } }</literal>\r
374                 </simpara>\r
375                 <simpara>Retrieves a list composed only of the IDs that match the query.</simpara>\r
376                 </listitem>\r
377                 <listitem>\r
378                 <simpara>\r
379                 <literal>open-ils.cstore.direct.config.language_map.retrieve "eng"</literal>\r
380                 </simpara>\r
381                 <simpara>Retrieves the object that matches a specific ID.</simpara>\r
382                 </listitem>\r
383                 <listitem>\r
384                 <simpara>\r
385                 <literal>open-ils.cstore.direct.config.language_map.search {"code" : "eng"}</literal>\r
386                 </simpara>\r
387                 <simpara>Retrieves a list of objects that match the query.</simpara>\r
388                 </listitem>\r
389                 <listitem>\r
390                 <simpara>\r
391                 <literal>open-ils.cstore.direct.config.language_map.create &lt;_object_&gt;</literal>\r
392                 </simpara>\r
393                 <simpara>Creates a new object from the passed in object.</simpara>\r
394                 </listitem>\r
395                 <listitem>\r
396                 <simpara>\r
397                 <literal>open-ils.cstore.direct.config.language_map.update &lt;_object_&gt;</literal>\r
398                 </simpara>\r
399                 <simpara>Updates the object that has been passed in.</simpara>\r
400                 </listitem>\r
401                 <listitem>\r
402                 <simpara>\r
403                 <literal>open-ils.cstore.direct.config.language_map.delete "eng"</literal>\r
404                 </simpara>\r
405                 <simpara>Deletes the object that matches the query.</simpara>\r
406                 </listitem>\r
407                 </itemizedlist>\r
408         </section>\r
409         <section id="_open_ils_pcrud_data_access_interfaces">\r
410                 <title>open-ils.pcrud data access interfaces</title>\r
411                 <simpara>For each class documented in the IDL, the <literal>open-ils.pcrud</literal> service\r
412                 automatically generates a set of data access methods, based on the\r
413                 <literal>oils_persist:tablename</literal> class attribute.</simpara>\r
414                 <simpara>For example, for the class hint <literal>clm</literal>, <literal>open-ils.pcrud</literal> generates the following\r
415                 methods that parallel the <literal>open-ils.cstore</literal> interface:</simpara>\r
416                 <itemizedlist>\r
417                 <listitem>\r
418                 <simpara>\r
419                 <literal>open-ils.pcrud.id_list.clm &lt;_authtoken_&gt;, { "code": { "like": "e%" } }</literal>\r
420                 </simpara>\r
421                 </listitem>\r
422                 <listitem>\r
423                 <simpara>\r
424                 <literal>open-ils.pcrud.retrieve.clm &lt;_authtoken_&gt;, "eng"</literal>\r
425                 </simpara>\r
426                 </listitem>\r
427                 <listitem>\r
428                 <simpara>\r
429                 <literal>open-ils.pcrud.search.clm &lt;_authtoken_&gt;, { "code": "eng" }</literal>\r
430                 </simpara>\r
431                 </listitem>\r
432                 <listitem>\r
433                 <simpara>\r
434                 <literal>open-ils.pcrud.create.clm &lt;_authtoken_&gt;, &lt;_object_&gt;</literal>\r
435                 </simpara>\r
436                 </listitem>\r
437                 <listitem>\r
438                 <simpara>\r
439                 <literal>open-ils.pcrud.update.clm &lt;_authtoken_&gt;, &lt;_object_&gt;</literal>\r
440                 </simpara>\r
441                 </listitem>\r
442                 <listitem>\r
443                 <simpara>\r
444                 <literal>open-ils.pcrud.delete.clm &lt;_authtoken_&gt;, "eng"</literal>\r
445                 </simpara>\r
446                 </listitem>\r
447                 </itemizedlist>\r
448         </section>\r
449         <section id="_transaction_and_savepoint_control">\r
450                 <title>Transaction and savepoint control</title>\r
451                 <simpara>Both <literal>open-ils.cstore</literal> and <literal>open-ils.pcrud</literal> enable you to control database transactions\r
452                 to ensure that a set of operations either all succeed, or all fail,\r
453                 atomically:</simpara>\r
454                 <itemizedlist>\r
455                 <listitem>\r
456                 <simpara>\r
457                 <literal>open-ils.cstore.transaction.begin</literal>\r
458                 </simpara>\r
459                 </listitem>\r
460                 <listitem>\r
461                 <simpara>\r
462                 <literal>open-ils.cstore.transaction.commit</literal>\r
463                 </simpara>\r
464                 </listitem>\r
465                 <listitem>\r
466                 <simpara>\r
467                 <literal>open-ils.cstore.transaction.rollback</literal>\r
468                 </simpara>\r
469                 </listitem>\r
470                 <listitem>\r
471                 <simpara>\r
472                 <literal>open-ils.pcrud.transaction.begin</literal>\r
473                 </simpara>\r
474                 </listitem>\r
475                 <listitem>\r
476                 <simpara>\r
477                 <literal>open-ils.pcrud.transaction.commit</literal>\r
478                 </simpara>\r
479                 </listitem>\r
480                 <listitem>\r
481                 <simpara>\r
482                 <literal>open-ils.pcrud.transaction.rollback</literal>\r
483                 </simpara>\r
484                 </listitem>\r
485                 </itemizedlist>\r
486                 <simpara>At a more granular level, <literal>open-ils.cstore</literal> and <literal>open-ils.pcrud</literal> enable you to set database\r
487                 savepoints to ensure that a set of operations either all succeed, or all\r
488                 fail, atomically, within a given transaction:</simpara>\r
489                 <itemizedlist>\r
490                 <listitem>\r
491                 <simpara>\r
492                 <literal>open-ils.cstore.savepoint.begin</literal>\r
493                 </simpara>\r
494                 </listitem>\r
495                 <listitem>\r
496                 <simpara>\r
497                 <literal>open-ils.cstore.savepoint.commit</literal>\r
498                 </simpara>\r
499                 </listitem>\r
500                 <listitem>\r
501                 <simpara>\r
502                 <literal>open-ils.cstore.savepoint.rollback</literal>\r
503                 </simpara>\r
504                 </listitem>\r
505                 <listitem>\r
506                 <simpara>\r
507                 <literal>open-ils.pcrud.savepoint.begin</literal>\r
508                 </simpara>\r
509                 </listitem>\r
510                 <listitem>\r
511                 <simpara>\r
512                 <literal>open-ils.pcrud.savepoint.commit</literal>\r
513                 </simpara>\r
514                 </listitem>\r
515                 <listitem>\r
516                 <simpara>\r
517                 <literal>open-ils.pcrud.savepoint.rollback</literal>\r
518                 </simpara>\r
519                 </listitem>\r
520                 </itemizedlist>\r
521                 <simpara>Transactions and savepoints must be performed within a stateful\r
522                 connection to the <literal>open-ils.cstore</literal> and <literal>open-ils.pcrud</literal> services.\r
523                 In <literal>srfsh</literal>, you can open a stateful connection using the <literal>open</literal>\r
524                 command, and then close the stateful connection using the <literal>close</literal>\r
525                 command - for example:</simpara>\r
526                 <screen>srfsh# open open-ils.cstore\r
527                 ... perform various transaction-related work\r
528                 srfsh# close open-ils.cstore</screen>\r
529                 <simplesect id="_json_queries">\r
530                         <title>JSON Queries</title>\r
531                         <simpara>Beyond simply retrieving objects by their ID using the <literal>\*.retrieve</literal>\r
532                         methods, you can issue queries against the <literal>\*.delete</literal> and <literal>\*.search</literal>\r
533                         methods using JSON to filter results with simple or complex search\r
534                         conditions.</simpara>\r
535                         <simpara>For example, to generate a list of barcodes that are held in a\r
536                         copy location that allows holds and is visible in the OPAC:</simpara>\r
537                         <programlisting language="sh" linenumbering="unnumbered">srfsh# request open-ils.cstore open-ils.cstore.json_query  <co id="dmCO7-1"/>\r
538                             {"select": {"acp":["barcode"], "acpl":["name"]}, <co id="dmCO7-2"/>\r
539                              "from":   {"acp":"acpl"},   <co id="dmCO7-3"/>\r
540                              "where":  [     <co id="dmCO7-4"/>\r
541                                  {"+acpl": "holdable"},   <co id="dmCO7-5"/>\r
542                                  {"+acpl": "opac_visible"}     <co id="dmCO7-6"/>\r
543                              ]}\r
544 \r
545                         Received Data: {\r
546                           "barcode":"BARCODE1",\r
547                           "name":"Stacks"\r
548                         }\r
549 \r
550                         Received Data: {\r
551                           "barcode":"BARCODE2",\r
552                           "name":"Stacks"\r
553                         }</programlisting>\r
554                         <calloutlist>\r
555                         <callout arearefs="dmCO7-1">\r
556                         <simpara>\r
557                         Invoke the <literal>json_query</literal> service.\r
558                         </simpara>\r
559                         </callout>\r
560                         <callout arearefs="dmCO7-2">\r
561                         <simpara>\r
562                         Select the <literal>barcode</literal> field from the <literal>acp</literal> class and the <literal>name</literal>\r
563                         field from the <literal>acpl</literal> class.\r
564                         </simpara>\r
565                         </callout>\r
566                         <callout arearefs="dmCO7-3">\r
567                         <simpara>\r
568                         Join the <literal>acp</literal> class to the <literal>acpl</literal> class based on the linked field\r
569                         defined in the IDL.\r
570                         </simpara>\r
571                         </callout>\r
572                         <callout arearefs="dmCO7-4">\r
573                         <simpara>\r
574                         Add a <literal>where</literal> clause to filter the results. We have more than one\r
575                         condition beginning with the same key, so we wrap the conditions inside\r
576                         an array.\r
577                         </simpara>\r
578                         </callout>\r
579                         <callout arearefs="dmCO7-5">\r
580                         <simpara>\r
581                         The first condition tests whether the boolean value of the <literal>holdable</literal>\r
582                         field on the <literal>acpl</literal> class is true.\r
583                         </simpara>\r
584                         </callout>\r
585                         <callout arearefs="dmCO7-6">\r
586                         <simpara>\r
587                         The second condition tests whether the boolean value of the\r
588                         <literal>opac_visible</literal> field on the <literal>acpl</literal> class is true.\r
589                         </simpara>\r
590                         </callout>\r
591                         </calloutlist>\r
592                         <simpara>For thorough coverage of the breadth of support offered by JSON\r
593                         query syntax, see <ulink url="http://open-ils.org/dokuwiki/doku.php?id=documentation:technical:jsontutorial">JSON Queries: A Tutorial</ulink>.</simpara>\r
594                 </simplesect>\r
595                 <simplesect id="_fleshing_linked_objects">\r
596                         <title>Fleshing linked objects</title>\r
597                         <simpara>A simplistic approach to retrieving a set of objects that are linked to\r
598                         an object that you are retrieving - for example, a set of call numbers\r
599                         linked to the barcodes that a given user has borrowed - would be to:\r
600                           1. Retrieve the list of circulation objects (<literal>circ</literal> class)\r
601                         for a given user (<literal>usr</literal> class).\r
602                           2. For each circulation object, look up the target copy (<literal>target_copy</literal>\r
603                         field, linked to the <literal>acp</literal> class).\r
604                           3. For each copy, look up the call number for that copy (<literal>call_number</literal>\r
605                         field, linked to the <literal>acn</literal> class).</simpara>\r
606                         <simpara>However, this would result in potentially hundreds of round-trip\r
607                         queries from the client to the server. Even with low-latency connections,\r
608                         the network overhead would be considerable. So, built into the <literal>open-ils.cstore</literal> and\r
609                         <literal>open-ils.pcrud</literal> access methods is the ability to <emphasis>flesh</emphasis> linked fields -\r
610                         that is, rather than return an identifier to a given linked field,\r
611                         the method can return the entire object as part of the initial response.</simpara>\r
612                         <simpara>Most of the interfaces that return class instances from the IDL offer the\r
613                         ability to flesh returned fields. For example, the\r
614                         <literal>open-ils.cstore.direct.\*.retrieve</literal> methods allow you to specify a\r
615                         JSON structure defining the fields you wish to flesh in the returned object.</simpara>\r
616                         <formalpara><title>Fleshing fields in objects returned by <literal>open-ils.cstore</literal></title><para>\r
617                         <programlisting language="sh" linenumbering="unnumbered">srfsh# request open-ils.cstore open-ils.cstore.direct.asset.copy.retrieve 1, \\r
618                             {\r
619                                 "flesh": 1,    <co id="dmCO8-1"/>\r
620                                 "flesh_fields": {   <co id="dmCO8-2"/>\r
621                                     "acp": ["location"]\r
622                                 }\r
623                             }</programlisting>\r
624                         </para></formalpara>\r
625                         <calloutlist>\r
626                         <callout arearefs="dmCO8-1">\r
627                         <simpara>\r
628                         The <literal>flesh</literal> argument is the depth at which objects should be fleshed.\r
629                         For example, to flesh out a field that links to another object that includes\r
630                         a field that links to another object, you would specify a depth of 2.\r
631                         </simpara>\r
632                         </callout>\r
633                         <callout arearefs="dmCO8-2">\r
634                         <simpara>\r
635                         The <literal>flesh_fields</literal> argument contains a list of objects with the fields\r
636                         to flesh for each object.\r
637                         </simpara>\r
638                         </callout>\r
639                         </calloutlist>\r
640                         <simpara>Let&#8217;s flesh things a little deeper. In addition to the copy location,\r
641                         let&#8217;s also flesh the call number attached to the copy, and then flesh\r
642                         the bibliographic record attached to the call number.</simpara>\r
643                         <formalpara><title>Fleshing fields in fields of objects returned by <literal>open-ils.cstore</literal></title><para>\r
644                         <programlisting language="java" linenumbering="unnumbered">request open-ils.cstore open-ils.cstore.direct.asset.copy.retrieve 1, \\r
645                             {\r
646                                 "flesh": 2,\r
647                                 "flesh_fields": {\r
648                                     "acp": ["location", "call_number"],\r
649                                     "acn": ["record"]\r
650                                  }\r
651                             }</programlisting>\r
652                         </para></formalpara>\r
653                 </simplesect>\r
654         </section>\r
655         <section id="_adding_an_idl_entry_for_resolverresolver">\r
656                 <title>Adding an IDL entry for ResolverResolver</title>\r
657                 <simpara>Most OpenSRF methods in Evergreen define their object interface in the\r
658                 IDL. Without an entry in the IDL, the prospective caller of a given\r
659                 method is forced to either call the method and inspect the returned\r
660                 contents, or read the source to work out the structure of the JSON\r
661                 payload. At this stage of the tutorial, we have not defined an entry\r
662                 in the IDL to represent the object returned by the\r
663                 <literal>open-ils.resolver.resolve_holdings</literal> method. It is time to complete\r
664                 that task.</simpara>\r
665                 <simpara>The <literal>open-ils.resolver</literal> service is unlike many of the other classes\r
666                 defined in the IDL because its data is not stored in the Evergreen\r
667                 database. Instead, the data is requested from an external Web service\r
668                 and only temporarily cached in <literal>memcached</literal>. Fortunately, the IDL\r
669                 enables us to represent this kind of class by setting the\r
670                 <literal>oils_persist:virtual</literal> class attribute to <literal>true</literal>.</simpara>\r
671                 <simpara>So, let&#8217;s add an entry to the IDL for the <literal>open-ils.resolver.resolve_holdings</literal>\r
672                 service:</simpara>\r
673                 <programlisting language="xml" linenumbering="unnumbered"></programlisting>\r
674                 <simpara>And let&#8217;s make <literal>ResolverResolver.pm</literal> return an array composed of our new\r
675                 <literal>rhr</literal> classes rather than raw JSON objects:</simpara>\r
676                 <programlisting language="perl" linenumbering="unnumbered"></programlisting>\r
677                 <simpara>Once we add the new entry to the IDL and copy the revised <literal>ResolverResolver.pm</literal>\r
678                 Perl module to <literal>/openils/lib/perl5/OpenILS/Application/</literal>, we need to:</simpara>\r
679                 <orderedlist numeration="arabic">\r
680                 <listitem>\r
681                 <simpara>\r
682                 Copy the updated IDL to both the <literal>/openils/conf/</literal> and\r
683                 <literal>/openils/var/web/reports/</literal> directories. The Dojo approach to\r
684                 parsing the IDL uses the IDL stored in the reports directory.\r
685                 </simpara>\r
686                 </listitem>\r
687                 <listitem>\r
688                 <simpara>\r
689                 Restart the Perl services to make the new IDL visible to the services\r
690                 and refresh the <literal>open-ils.resolver</literal> implementation\r
691                 </simpara>\r
692                 </listitem>\r
693                 <listitem>\r
694                 <simpara>\r
695                 Rerun <literal>/openils/bin/autogen.sh</literal> to regenerate the JavaScript versions\r
696                 of the IDL required by the HTTP translator and gateway.\r
697                 </simpara>\r
698                 </listitem>\r
699                 </orderedlist>\r
700                 <simpara>We also need to adjust our JavaScript client to use the nifty new\r
701                 objects that <literal>open-ils.resolver.resolve_holdings</literal> now returns.\r
702                 The best approach is to use the support in Evergreen&#8217;s Dojo extensions\r
703                 to generate the JavaScript classes directly from the IDL XML file.</simpara>\r
704                 <formalpara><title>Accessing classes defined in the IDL via Fieldmapper</title><para>\r
705                 <programlisting language="html" linenumbering="unnumbered"></programlisting>\r
706                 </para></formalpara>\r
707                 <calloutlist>\r
708                 <callout arearefs="">\r
709                 <simpara>\r
710                 Load the Dojo core.\r
711                 </simpara>\r
712                 </callout>\r
713                 <callout arearefs="">\r
714                 <simpara>\r
715                 <literal>fieldmapper.AutoIDL</literal> reads <literal>/openils/var/reports/fm_IDL.xml</literal> to\r
716                 generate a list of class properties.\r
717                 </simpara>\r
718                 </callout>\r
719                 <callout arearefs="">\r
720                 <simpara>\r
721                 <literal>fieldmapper.dojoData</literal> seems to provide a store for Evergreen data\r
722                 accessed via Dojo.\r
723                 </simpara>\r
724                 </callout>\r
725                 <callout arearefs="">\r
726                 <simpara>\r
727                 <literal>fieldmapper.Fieldmapper</literal> converts the list of class properties into\r
728                 actual classes.\r
729                 </simpara>\r
730                 </callout>\r
731                 <callout arearefs="">\r
732                 <simpara>\r
733                 <literal>fieldmapper.standardRequest</literal> invokes an OpenSRF method and returns\r
734                 an array of objects.\r
735                 </simpara>\r
736                 </callout>\r
737                 <callout arearefs="">\r
738                 <simpara>\r
739                 The first argument to <literal>fieldmapper.standardRequest</literal> is an array\r
740                 containing the OpenSRF service name and method name.\r
741                 </simpara>\r
742                 </callout>\r
743                 <callout arearefs="">\r
744                 <simpara>\r
745                 The second argument to <literal>fieldmapper.standardRequest</literal> is an array\r
746                 containing the arguments to pass to the OpenSRF method.\r
747                 </simpara>\r
748                 </callout>\r
749                 <callout arearefs="">\r
750                 <simpara>\r
751                 As Fieldmapper has instantiated the returned objects based on their\r
752                 class hints, we can invoke getter/setter methods on the objects.\r
753                 </simpara>\r
754                 </callout>\r
755                 </calloutlist>\r
756         </section>\r
757         <section id="dm_attribution">\r
758                 <simpara>This chapter was taken from Dan Scott's <emphasis>Developer Workshop</emphasis>, February 2010.</simpara>\r
759         </section>\r
760 </chapter>\r