helper for init. attribute values struct. Add some debug logging.
[yaz-moved-to-github.git] / src / facet.c
1
2
3 #include <yaz/facet.h>
4 #include <yaz/diagbib1.h>
5 #include <yaz/oid_db.h>
6 #include <yaz/oid_std.h>
7 #include <yaz/otherinfo.h>
8 #include <assert.h>
9
10 /* Little helper to extract a string attribute */
11 /* Gets the first string, there is usually only one */
12 /* in case of errors, returns null */
13
14 void facet_struct_init(struct attrvalues *attr_values) {
15     attr_values->errcode   = 0;
16     attr_values->errstring = 0;
17     attr_values->relation  = 0;
18     attr_values->useattr   = 0;
19     attr_values->useattrbuff[0] = 0;
20     attr_values->limit     = 0;
21 }
22
23 const char *stringattr( Z_ComplexAttribute *c ) {
24     int i;
25      Z_StringOrNumeric *son;
26     for ( i = 0; i < c->num_list; i++ ) {
27         son = c->list[i];
28         if ( son->which == Z_StringOrNumeric_string )
29             return son->u.string;
30     }
31     return 0;
32 }
33
34 /* Use attribute, @attr1, can be numeric or string */
35 void useattr ( Z_AttributeElement *ae,
36                        struct attrvalues *av )
37 {
38     const char *s;
39     if ( ae->which == Z_AttributeValue_complex ) {
40         s = stringattr( ae->value.complex );
41         yaz_log(YLOG_DEBUG, "useattr %s %s", s, av->useattr);
42         if (s) {
43             if (!av->useattr)
44                 av->useattr = s;
45             else { /* already seen one, can't have duplicates */
46                 av->errcode = YAZ_BIB1_UNSUPP_ATTRIBUTE_COMBI;
47                 av->errstring = "multiple use attributes";
48             }
49         } else { /* complex that did not return a string */
50             av->errcode = YAZ_BIB1_UNSUPP_ATTRIBUTE_COMBI;
51             av->errstring = "non-string complex attribute";
52         }
53     } else { /* numeric - could translate 4 to 'title' etc */
54         sprintf(av->useattrbuff, ODR_INT_PRINTF, *ae->value.numeric );
55         av->useattr = av->useattrbuff;
56     }
57 } /* useattr */
58
59
60 /* TODO rename to sortorder attr */
61 void relationattr ( Z_AttributeElement *ae,
62                            struct attrvalues *av )
63 {
64     if ( ae->which == Z_AttributeValue_numeric ) {
65         if ( *ae->value.numeric == 0 )
66             av->relation = "desc";
67         else if ( *ae->value.numeric == 1 )
68                 av->relation = "asc";
69             else
70         if ( *ae->value.numeric == 3 ) {
71             av->relation = "unknown/unordered";
72         } else {
73             av->errcode = YAZ_BIB1_UNSUPP_RELATION_ATTRIBUTE;
74             sprintf(av->useattrbuff, ODR_INT_PRINTF,
75                         *ae-> attributeType);
76             av->errstring = av->useattrbuff;
77         }
78     } else {
79         av->errcode = YAZ_BIB1_UNSUPP_RELATION_ATTRIBUTE;
80         av->errstring = "non-numeric relation attribute";
81     }
82 } /* relationattr */
83
84 void limitattr ( Z_AttributeElement *ae,
85                         struct attrvalues *av )
86 {
87   /* TODO - check numeric first, then value! */
88     if ( ae->which == Z_AttributeValue_numeric ) {
89         av->limit = *ae->value.numeric;
90     } else {
91         av->errcode = YAZ_BIB1_UNSUPP_ATTRIBUTE;
92         av->errstring = "non-numeric limit attribute";
93     }
94 } /* relationattr */
95
96 /* Get the index to be searched from the attributes.
97    @attr 1
98      can be either "string", or some well-known value like
99      4 for title
100    Returns a null and sets errors in rr,
101    emtpy string if no attr found,
102    or the string itself - always a pointer to the Z-structs,
103    so no need to free that string!
104 */
105
106 void facetattrs( Z_AttributeList *attributes,
107                           struct attrvalues *av )
108 {
109     int i;
110     Z_AttributeElement *ae;
111     yaz_log(YLOG_DEBUG, "Attribute num attributes: %d", attributes->num_attributes);
112     for ( i=0; i < attributes->num_attributes; i++ ) {
113         ae = attributes->attributes[i];
114         /* ignoring the attributeSet here */
115         yaz_log(YLOG_DEBUG, "Attribute type %d", (int) *ae->attributeType);
116         if ( *ae->attributeType == 1 ) { /* use attribute */
117             useattr(ae, av);
118         } else if ( *ae->attributeType == 2 ) { /* sortorder */
119             relationattr(ae, av);
120         } else if ( *ae->attributeType == 3 ) { /* limit */
121             limitattr(ae, av);
122         } else { /* unknown attribute */
123             av->errcode = YAZ_BIB1_UNSUPP_ATTRIBUTE_TYPE;
124             sprintf(av->useattrbuff, ODR_INT_PRINTF,
125                         *ae-> attributeType);
126             av->errstring = av->useattrbuff;
127             yaz_log(YLOG_DEBUG, "Unsupported attribute type %s", av->useattrbuff);
128             /* would like to give a better message, but the standard */
129             /* tells me to return the attribute type */
130         }
131         if ( av->errcode )
132             return; /* no need to dig deeper, return on first error */
133     }
134     return;
135 } /* facetattrs */
136
137
138 Z_FacetList *extract_facet_request(ODR odr, Z_OtherInformation *search_input) {
139     Z_FacetList *facet_list = yaz_oi_get_facetlist_oid(&search_input, odr, yaz_oid_userinfo_facet_1, 1, 0);
140     return facet_list;
141 }
142
143 Z_Term *term_create(ODR odr, const char *cstr) {
144     Z_Term *term = odr_malloc(odr, sizeof(*term));
145     term->which = Z_Term_characterString;
146     term->u.characterString = odr_strdup(odr, cstr);
147     return term;
148 }
149
150 Z_FacetTerm* facet_term_create(ODR odr, Z_Term *term, int freq) {
151     Z_FacetTerm *facet_term = odr_malloc(odr, sizeof(*facet_term));
152     facet_term->count = odr_malloc(odr, sizeof(*facet_term->count));
153     facet_term->term = term;
154     *facet_term->count = freq;
155     return facet_term;
156 }
157
158 Z_FacetField* facet_field_create(ODR odr, Z_AttributeList *attributes, int num_terms) {
159     Z_FacetField *facet_field = odr_malloc(odr, sizeof(*facet_field));
160     facet_field->attributes = attributes;
161     facet_field->num_terms = num_terms;
162     facet_field->terms = odr_malloc(odr, num_terms * sizeof(*facet_field->terms));
163     return facet_field;
164 }
165
166 void facet_field_term_set(ODR odr, Z_FacetField *field, Z_FacetTerm *facet_term, int index) {
167     assert(0 <= index && index < field->num_terms);
168     field->terms[index] = facet_term;
169 }
170
171 Z_FacetList* facet_list_create(ODR odr, int num_facets) {
172     Z_FacetList *facet_list = odr_malloc(odr, sizeof(*facet_list));
173     facet_list->num = num_facets;
174     facet_list->elements = odr_malloc(odr, facet_list->num * sizeof(*facet_list->elements));
175     return facet_list;
176 }
177
178 void facet_list_field_set(ODR odr, Z_FacetList *list, Z_FacetField *field, int index) {
179     assert(0 <= index && index < list->num);
180     list->elements[index] = field;
181 }
182