Happy new year
[yaz-moved-to-github.git] / src / facet.c
1 /* This file is part of the YAZ toolkit.
2  * Copyright (C) 1995-2011 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 void yaz_oi_set_facetlist(
23     Z_OtherInformation **otherInformation, ODR odr,
24     Z_FacetList *facet_list)
25 {
26     int categoryValue = 1;
27     Z_External *z_external = 0;
28     Z_OtherInformationUnit *oi =
29         yaz_oi_update(otherInformation, odr, yaz_oid_userinfo_facet_1,
30                       categoryValue, 0);
31     if (!oi)
32         return;
33     oi->which = Z_OtherInfo_externallyDefinedInfo;
34     z_external = odr_malloc(odr, sizeof(*z_external));
35     z_external->which = Z_External_userFacets;
36     z_external->direct_reference = odr_oiddup(odr, yaz_oid_userinfo_facet_1);
37     z_external->indirect_reference = 0;
38     z_external->descriptor = 0;
39     z_external->u.facetList = facet_list;
40     oi->information.externallyDefinedInfo = z_external;
41 }
42
43 Z_FacetList *yaz_oi_get_facetlist(Z_OtherInformation **otherInformation)
44 {
45     int categoryValue = 1;
46     Z_External *z_external = 0;
47     Z_OtherInformationUnit *oi =
48         yaz_oi_update(otherInformation, 0, yaz_oid_userinfo_facet_1,
49                       categoryValue, 0);
50     if (!oi)
51         return 0;
52     z_external = oi->information.externallyDefinedInfo;
53
54     if (z_external && z_external->which == Z_External_userFacets) {
55         return z_external->u.facetList;
56     }
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         yaz_log(YLOG_DEBUG, "useattr %s %s", s, av->useattr);
95         if (s)
96         {
97             if (!av->useattr)
98                 av->useattr = s;
99             else
100             { /* already seen one, can't have duplicates */
101                 av->errcode = YAZ_BIB1_UNSUPP_ATTRIBUTE_COMBI;
102                 av->errstring = "multiple use attributes";
103             }
104         }
105         else
106         { /* complex that did not return a string */
107             av->errcode = YAZ_BIB1_UNSUPP_ATTRIBUTE_COMBI;
108             av->errstring = "non-string complex attribute";
109         }
110     }
111     else
112     { /* numeric - could translate 4 to 'title' etc */
113         sprintf(av->useattrbuff, ODR_INT_PRINTF, *ae->value.numeric);
114         av->useattr = av->useattrbuff;
115     }
116 } /* useattr */
117
118
119 /* TODO rename to sortorder attr */
120 static void relationattr(Z_AttributeElement *ae, struct yaz_facet_attr *av)
121 {
122     if (ae->which == Z_AttributeValue_numeric)
123     {
124         if (*ae->value.numeric == 0)
125             av->relation = "desc";
126         else if (*ae->value.numeric == 1)
127                 av->relation = "asc";
128             else
129         if (*ae->value.numeric == 3) {
130             av->relation = "unknown/unordered";
131         } else {
132             av->errcode = YAZ_BIB1_UNSUPP_RELATION_ATTRIBUTE;
133             sprintf(av->useattrbuff, ODR_INT_PRINTF,
134                         *ae-> attributeType);
135             av->errstring = av->useattrbuff;
136         }
137     }
138     else
139     {
140         av->errcode = YAZ_BIB1_UNSUPP_RELATION_ATTRIBUTE;
141         av->errstring = "non-numeric relation attribute";
142     }
143 } /* relationattr */
144
145 static void limitattr(Z_AttributeElement *ae, struct yaz_facet_attr *av)
146 {
147     if (ae->which == Z_AttributeValue_numeric)
148     {
149         av->limit = *ae->value.numeric;
150         yaz_log(YLOG_DEBUG, "limitattr %d ", av->limit);
151     }
152     else
153     {
154         av->errcode = YAZ_BIB1_UNSUPP_ATTRIBUTE;
155         av->errstring = "non-numeric limit attribute";
156     }
157 } /* relationattr */
158
159 /* Get the index to be searched from the attributes.
160    @attr 1
161      can be either "string", or some well-known value like
162      4 for title
163    Returns a null and sets errors in rr,
164    emtpy string if no attr found,
165    or the string itself - always a pointer to the Z-structs,
166    so no need to free that string!
167 */
168
169 void yaz_facet_attr_get_z_attributes(const Z_AttributeList *attributes,
170                                      struct yaz_facet_attr *av)
171 {
172     int i;
173     Z_AttributeElement *ae;
174     yaz_log(YLOG_DEBUG, "Attribute num attributes: %d",
175             attributes->num_attributes);
176     for (i=0; i < attributes->num_attributes; i++) {
177         ae = attributes->attributes[i];
178         /* ignoring the attributeSet here */
179         yaz_log(YLOG_DEBUG, "Attribute type %d", (int) *ae->attributeType);
180         if (*ae->attributeType == 1)
181         { /* use attribute */
182             useattr(ae, av);
183         }
184         else if (*ae->attributeType == 2)
185         { /* sortorder */
186             relationattr(ae, av);
187         }
188         else if (*ae->attributeType == 3)
189         { /* limit */
190             limitattr(ae, av);
191         }
192         else
193         { /* unknown attribute */
194             av->errcode = YAZ_BIB1_UNSUPP_ATTRIBUTE_TYPE;
195             sprintf(av->useattrbuff, ODR_INT_PRINTF,
196                         *ae-> attributeType);
197             av->errstring = av->useattrbuff;
198             yaz_log(YLOG_DEBUG, "Unsupported attribute type %s", av->useattrbuff);
199             /* would like to give a better message, but the standard */
200             /* tells me to return the attribute type */
201         }
202         if (av->errcode)
203             return; /* no need to dig deeper, return on first error */
204     }
205     return;
206 } /* facetattrs */
207
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
225 Z_FacetField* facet_field_create(ODR odr, Z_AttributeList *attributes,
226                                  int num_terms)
227 {
228     Z_FacetField *facet_field = odr_malloc(odr, sizeof(*facet_field));
229     facet_field->attributes = attributes;
230     facet_field->num_terms = num_terms;
231     facet_field->terms = odr_malloc(odr, num_terms * sizeof(*facet_field->terms));
232     return facet_field;
233 }
234
235 void facet_field_term_set(ODR odr, Z_FacetField *field,
236                           Z_FacetTerm *facet_term, int index)
237 {
238     assert(0 <= index && index < field->num_terms);
239     field->terms[index] = facet_term;
240 }
241
242 Z_FacetList* facet_list_create(ODR odr, int num_facets)
243 {
244     Z_FacetList *facet_list = odr_malloc(odr, sizeof(*facet_list));
245     facet_list->num = num_facets;
246     facet_list->elements =
247         odr_malloc(odr, facet_list->num * sizeof(*facet_list->elements));
248     return facet_list;
249 }
250
251 void facet_list_field_set(ODR odr, Z_FacetList *list, Z_FacetField *field,
252                           int index)
253 {
254     assert(0 <= index && index < list->num);
255     list->elements[index] = field;
256 }
257
258 /*
259  * Local variables:
260  * c-basic-offset: 4
261  * c-file-style: "Stroustrup"
262  * indent-tabs-mode: nil
263  * End:
264  * vim: shiftwidth=4 tabstop=8 expandtab
265  */
266