1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2010 Index Data
3 * See the file LICENSE for details.
8 * \brief Facet utilities
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>
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 */
26 void yaz_facet_attr_init(struct yaz_facet_attr *attr_values)
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;
36 static const char *stringattr(Z_ComplexAttribute *c)
39 Z_StringOrNumeric *son;
40 for (i = 0; i < c->num_list; i++)
43 if ( son->which == Z_StringOrNumeric_string)
49 /* Use attribute, @attr1, can be numeric or string */
50 static void useattr(Z_AttributeElement *ae, struct yaz_facet_attr *av)
53 if (ae->which == Z_AttributeValue_complex)
55 s = stringattr(ae->value.complex);
56 yaz_log(YLOG_DEBUG, "useattr %s %s", s, av->useattr);
62 { /* already seen one, can't have duplicates */
63 av->errcode = YAZ_BIB1_UNSUPP_ATTRIBUTE_COMBI;
64 av->errstring = "multiple use attributes";
68 { /* complex that did not return a string */
69 av->errcode = YAZ_BIB1_UNSUPP_ATTRIBUTE_COMBI;
70 av->errstring = "non-string complex attribute";
74 { /* numeric - could translate 4 to 'title' etc */
75 sprintf(av->useattrbuff, ODR_INT_PRINTF, *ae->value.numeric);
76 av->useattr = av->useattrbuff;
81 /* TODO rename to sortorder attr */
82 static void relationattr(Z_AttributeElement *ae, struct yaz_facet_attr *av)
84 if (ae->which == Z_AttributeValue_numeric)
86 if (*ae->value.numeric == 0)
87 av->relation = "desc";
88 else if (*ae->value.numeric == 1)
91 if (*ae->value.numeric == 3) {
92 av->relation = "unknown/unordered";
94 av->errcode = YAZ_BIB1_UNSUPP_RELATION_ATTRIBUTE;
95 sprintf(av->useattrbuff, ODR_INT_PRINTF,
97 av->errstring = av->useattrbuff;
102 av->errcode = YAZ_BIB1_UNSUPP_RELATION_ATTRIBUTE;
103 av->errstring = "non-numeric relation attribute";
107 static void limitattr(Z_AttributeElement *ae, struct yaz_facet_attr *av)
109 if (ae->which == Z_AttributeValue_numeric)
111 av->limit = *ae->value.numeric;
112 yaz_log(YLOG_DEBUG, "limitattr %d ", av->limit);
116 av->errcode = YAZ_BIB1_UNSUPP_ATTRIBUTE;
117 av->errstring = "non-numeric limit attribute";
121 /* Get the index to be searched from the attributes.
123 can be either "string", or some well-known value like
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!
131 void yaz_facet_attr_get_z_attributes(const Z_AttributeList *attributes,
132 struct yaz_facet_attr *av)
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 */
146 else if (*ae->attributeType == 2)
148 relationattr(ae, av);
150 else if (*ae->attributeType == 3)
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 */
165 return; /* no need to dig deeper, return on first error */
171 Z_FacetList *extract_facet_request(ODR odr, Z_OtherInformation *search_input)
173 Z_FacetList *facet_list =
174 yaz_oi_get_facetlist_oid(&search_input, odr,
175 yaz_oid_userinfo_facet_1, 1, 0);
179 Z_Term *term_create(ODR odr, const char *cstr)
181 Z_Term *term = odr_malloc(odr, sizeof(*term));
182 term->which = Z_Term_characterString;
183 term->u.characterString = odr_strdup(odr, cstr);
187 Z_FacetTerm* facet_term_create(ODR odr, Z_Term *term, int freq)
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;
196 Z_FacetField* facet_field_create(ODR odr, Z_AttributeList *attributes,
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));
206 void facet_field_term_set(ODR odr, Z_FacetField *field,
207 Z_FacetTerm *facet_term, int index)
209 assert(0 <= index && index < field->num_terms);
210 field->terms[index] = facet_term;
213 Z_FacetList* facet_list_create(ODR odr, int num_facets)
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));
222 void facet_list_field_set(ODR odr, Z_FacetList *list, Z_FacetField *field,
225 assert(0 <= index && index < list->num);
226 list->elements[index] = field;
232 * c-file-style: "Stroustrup"
233 * indent-tabs-mode: nil
235 * vim: shiftwidth=4 tabstop=8 expandtab