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