223be7e4d393bf431913fecf8eafc813e9fd68a8
[yaz-moved-to-github.git] / src / facet.c
1 /* This file is part of the YAZ toolkit.
2  * Copyright (C) 1995-2012 Index Data
3  * See the file LICENSE for details.
4  */
5
6 /**
7  * \file facet.c
8  * \brief Facet utilities
9  */
10
11 #if HAVE_CONFIG_H
12 #include <config.h>
13 #endif
14
15 #include <yaz/facet.h>
16 #include <yaz/diagbib1.h>
17 #include <yaz/oid_db.h>
18 #include <yaz/oid_std.h>
19 #include <yaz/otherinfo.h>
20 #include <yaz/pquery.h>
21 #include <assert.h>
22
23 void yaz_oi_set_facetlist(
24     Z_OtherInformation **otherInformation, ODR odr,
25     Z_FacetList *facet_list)
26 {
27     int categoryValue = 1;
28     Z_External *z_external = 0;
29     Z_OtherInformationUnit *oi =
30         yaz_oi_update(otherInformation, odr, yaz_oid_userinfo_facet_1,
31                       categoryValue, 0);
32     if (!oi)
33         return;
34     oi->which = Z_OtherInfo_externallyDefinedInfo;
35     z_external = odr_malloc(odr, sizeof(*z_external));
36     z_external->which = Z_External_userFacets;
37     z_external->direct_reference = odr_oiddup(odr, yaz_oid_userinfo_facet_1);
38     z_external->indirect_reference = 0;
39     z_external->descriptor = 0;
40     z_external->u.facetList = facet_list;
41     oi->information.externallyDefinedInfo = z_external;
42 }
43
44 Z_FacetList *yaz_oi_get_facetlist(Z_OtherInformation **otherInformation)
45 {
46     int categoryValue = 1;
47     Z_External *z_external = 0;
48     Z_OtherInformationUnit *oi =
49         yaz_oi_update(otherInformation, 0, yaz_oid_userinfo_facet_1,
50                       categoryValue, 0);
51     if (!oi)
52         return 0;
53     z_external = oi->information.externallyDefinedInfo;
54
55     if (z_external && z_external->which == Z_External_userFacets)
56         return z_external->u.facetList;
57     return 0;
58 }
59
60 /* Little helper to extract a string attribute */
61 /* Gets the first string, there is usually only one */
62 /* in case of errors, returns null */
63
64 void yaz_facet_attr_init(struct yaz_facet_attr *attr_values)
65 {
66     attr_values->errcode   = 0;
67     attr_values->errstring = 0;
68     attr_values->relation  = 0;
69     attr_values->useattr   = 0;
70     attr_values->useattrbuff[0] = 0;
71     attr_values->limit     = 0;
72 }
73
74 static const char *stringattr(Z_ComplexAttribute *c)
75 {
76     int i;
77      Z_StringOrNumeric *son;
78     for (i = 0; i < c->num_list; i++)
79     {
80         son = c->list[i];
81         if ( son->which == Z_StringOrNumeric_string)
82             return son->u.string;
83     }
84     return 0;
85 }
86
87 /* Use attribute, @attr1, can be numeric or string */
88 static void useattr(Z_AttributeElement *ae, struct yaz_facet_attr *av)
89 {
90     const char *s;
91     if (ae->which == Z_AttributeValue_complex)
92     {
93         s = stringattr(ae->value.complex);
94         if (s)
95         {
96             if (!av->useattr)
97                 av->useattr = s;
98             else
99             { /* already seen one, can't have duplicates */
100                 av->errcode = YAZ_BIB1_UNSUPP_ATTRIBUTE_COMBI;
101                 av->errstring = "multiple use attributes";
102             }
103         }
104         else
105         { /* complex that did not return a string */
106             av->errcode = YAZ_BIB1_UNSUPP_ATTRIBUTE_COMBI;
107             av->errstring = "non-string complex attribute";
108         }
109     }
110     else
111     { /* numeric - could translate 4 to 'title' etc */
112         sprintf(av->useattrbuff, ODR_INT_PRINTF, *ae->value.numeric);
113         av->useattr = av->useattrbuff;
114     }
115 }
116
117
118 static void sortorderattr(Z_AttributeElement *ae, struct yaz_facet_attr *av)
119 {
120     if (ae->which == Z_AttributeValue_numeric)
121     {
122         if (*ae->value.numeric == 0)
123             av->relation = "desc";
124         else if (*ae->value.numeric == 1)
125             av->relation = "asc";
126         else if (*ae->value.numeric == 3)
127             av->relation = "unknown/unordered";
128         else
129         {
130             av->errcode = YAZ_BIB1_UNSUPP_RELATION_ATTRIBUTE;
131             sprintf(av->useattrbuff, ODR_INT_PRINTF,
132                     *ae-> attributeType);
133             av->errstring = av->useattrbuff;
134         }
135     }
136     else
137     {
138         av->errcode = YAZ_BIB1_UNSUPP_RELATION_ATTRIBUTE;
139         av->errstring = "non-numeric relation attribute";
140     }
141 }
142
143 static void limitattr(Z_AttributeElement *ae, struct yaz_facet_attr *av)
144 {
145     if (ae->which == Z_AttributeValue_numeric)
146     {
147         av->limit = *ae->value.numeric;
148     }
149     else
150     {
151         av->errcode = YAZ_BIB1_UNSUPP_ATTRIBUTE;
152         av->errstring = "non-numeric limit attribute";
153     }
154 }
155
156 /* Get the index to be searched from the attributes.
157    @attr 1
158      can be either "string", or some well-known value like
159      4 for title
160    Returns a null and sets errors in rr,
161    emtpy string if no attr found,
162    or the string itself - always a pointer to the Z-structs,
163    so no need to free that string!
164 */
165
166 void yaz_facet_attr_get_z_attributes(const Z_AttributeList *attributes,
167                                      struct yaz_facet_attr *av)
168 {
169     int i;
170     Z_AttributeElement *ae;
171     for (i = 0; i < attributes->num_attributes; i++)
172     {
173         ae = attributes->attributes[i];
174         /* ignoring the attributeSet here */
175         if (*ae->attributeType == 1)
176         { /* use attribute */
177             useattr(ae, av);
178         }
179         else if (*ae->attributeType == 2)
180         { /* sortorder */
181             sortorderattr(ae, av);
182         }
183         else if (*ae->attributeType == 3)
184         { /* limit */
185             limitattr(ae, av);
186         }
187         else
188         { /* unknown attribute */
189             av->errcode = YAZ_BIB1_UNSUPP_ATTRIBUTE_TYPE;
190             sprintf(av->useattrbuff, ODR_INT_PRINTF,
191                         *ae-> attributeType);
192             av->errstring = av->useattrbuff;
193             yaz_log(YLOG_WARN, "Unsupported attribute type %s", av->useattrbuff);
194             /* would like to give a better message, but the standard */
195             /* tells me to return the attribute type */
196         }
197         if (av->errcode)
198             return; /* no need to dig deeper, return on first error */
199     }
200     return;
201 } /* facetattrs */
202
203 #if 0
204 Z_Term *term_create(ODR odr, const char *cstr)
205 {
206     Z_Term *term = odr_malloc(odr, sizeof(*term));
207     term->which = Z_Term_characterString;
208     term->u.characterString = odr_strdup(odr, cstr);
209     return term;
210 }
211
212 Z_FacetTerm* facet_term_create(ODR odr, Z_Term *term, int freq)
213 {
214     Z_FacetTerm *facet_term = odr_malloc(odr, sizeof(*facet_term));
215     facet_term->count = odr_malloc(odr, sizeof(*facet_term->count));
216     facet_term->term = term;
217     *facet_term->count = freq;
218     return facet_term;
219 }
220 #endif
221
222 Z_FacetTerm *facet_term_create_cstr(ODR odr, const char *cstr, Odr_int freq)
223 {
224     Z_FacetTerm *facet_term = odr_malloc(odr, sizeof(*facet_term));
225     Z_Term *term = z_Term_create(odr, Z_Term_general, cstr, strlen(cstr));
226     facet_term->term = term;
227     facet_term->count = odr_intdup(odr, freq);
228     return facet_term;
229 }
230
231 Z_FacetField* facet_field_create(ODR odr, Z_AttributeList *attributes,
232                                  int num_terms)
233 {
234     Z_FacetField *facet_field = odr_malloc(odr, sizeof(*facet_field));
235     facet_field->attributes = attributes;
236     facet_field->num_terms = num_terms;
237     facet_field->terms = odr_malloc(odr, num_terms * sizeof(*facet_field->terms));
238     return facet_field;
239 }
240
241 void facet_field_term_set(ODR odr, Z_FacetField *field,
242                           Z_FacetTerm *facet_term, int index)
243 {
244     assert(0 <= index && index < field->num_terms);
245     field->terms[index] = facet_term;
246 }
247
248 Z_FacetList* facet_list_create(ODR odr, int num_facets)
249 {
250     Z_FacetList *facet_list = odr_malloc(odr, sizeof(*facet_list));
251     facet_list->num = num_facets;
252     facet_list->elements =
253         odr_malloc(odr, facet_list->num * sizeof(*facet_list->elements));
254     return facet_list;
255 }
256
257 void facet_list_field_set(ODR odr, Z_FacetList *list, Z_FacetField *field,
258                           int index)
259 {
260     assert(0 <= index && index < list->num);
261     list->elements[index] = field;
262 }
263
264 /*
265  * Local variables:
266  * c-basic-offset: 4
267  * c-file-style: "Stroustrup"
268  * indent-tabs-mode: nil
269  * End:
270  * vim: shiftwidth=4 tabstop=8 expandtab
271  */
272