82a8365eef9ac65275c3f169126b8dcf68a2f6d6
[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 facet_struct_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 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 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 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 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 facetattrs(Z_AttributeList *attributes, struct yaz_facet_attr *av)
132 {
133     int i;
134     Z_AttributeElement *ae;
135     yaz_log(YLOG_DEBUG, "Attribute num attributes: %d",
136             attributes->num_attributes);
137     for (i=0; i < attributes->num_attributes; i++) {
138         ae = attributes->attributes[i];
139         /* ignoring the attributeSet here */
140         yaz_log(YLOG_DEBUG, "Attribute type %d", (int) *ae->attributeType);
141         if (*ae->attributeType == 1)
142         { /* use attribute */
143             useattr(ae, av);
144         }
145         else if (*ae->attributeType == 2)
146         { /* sortorder */
147             relationattr(ae, av);
148         }
149         else if (*ae->attributeType == 3)
150         { /* limit */
151             limitattr(ae, av);
152         }
153         else
154         { /* unknown attribute */
155             av->errcode = YAZ_BIB1_UNSUPP_ATTRIBUTE_TYPE;
156             sprintf(av->useattrbuff, ODR_INT_PRINTF,
157                         *ae-> attributeType);
158             av->errstring = av->useattrbuff;
159             yaz_log(YLOG_DEBUG, "Unsupported attribute type %s", av->useattrbuff);
160             /* would like to give a better message, but the standard */
161             /* tells me to return the attribute type */
162         }
163         if (av->errcode)
164             return; /* no need to dig deeper, return on first error */
165     }
166     return;
167 } /* facetattrs */
168
169
170 Z_FacetList *extract_facet_request(ODR odr, Z_OtherInformation *search_input)
171 {
172     Z_FacetList *facet_list =
173         yaz_oi_get_facetlist_oid(&search_input, odr,
174                                  yaz_oid_userinfo_facet_1, 1, 0);
175     return facet_list;
176 }
177
178 Z_Term *term_create(ODR odr, const char *cstr)
179 {
180     Z_Term *term = odr_malloc(odr, sizeof(*term));
181     term->which = Z_Term_characterString;
182     term->u.characterString = odr_strdup(odr, cstr);
183     return term;
184 }
185
186 Z_FacetTerm* facet_term_create(ODR odr, Z_Term *term, int freq)
187 {
188     Z_FacetTerm *facet_term = odr_malloc(odr, sizeof(*facet_term));
189     facet_term->count = odr_malloc(odr, sizeof(*facet_term->count));
190     facet_term->term = term;
191     *facet_term->count = freq;
192     return facet_term;
193 }
194
195 Z_FacetField* facet_field_create(ODR odr, Z_AttributeList *attributes,
196                                  int num_terms)
197 {
198     Z_FacetField *facet_field = odr_malloc(odr, sizeof(*facet_field));
199     facet_field->attributes = attributes;
200     facet_field->num_terms = num_terms;
201     facet_field->terms = odr_malloc(odr, num_terms * sizeof(*facet_field->terms));
202     return facet_field;
203 }
204
205 void facet_field_term_set(ODR odr, Z_FacetField *field,
206                           Z_FacetTerm *facet_term, int index)
207 {
208     assert(0 <= index && index < field->num_terms);
209     field->terms[index] = facet_term;
210 }
211
212 Z_FacetList* facet_list_create(ODR odr, int num_facets)
213 {
214     Z_FacetList *facet_list = odr_malloc(odr, sizeof(*facet_list));
215     facet_list->num = num_facets;
216     facet_list->elements =
217         odr_malloc(odr, facet_list->num * sizeof(*facet_list->elements));
218     return facet_list;
219 }
220
221 void facet_list_field_set(ODR odr, Z_FacetList *list, Z_FacetField *field,
222                           int index)
223 {
224     assert(0 <= index && index < list->num);
225     list->elements[index] = field;
226 }
227
228 /*
229  * Local variables:
230  * c-basic-offset: 4
231  * c-file-style: "Stroustrup"
232  * indent-tabs-mode: nil
233  * End:
234  * vim: shiftwidth=4 tabstop=8 expandtab
235  */
236