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