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