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