Remove obsolete references
[idzebra-moved-to-github.git] / index / attribute.c
1 /* This file is part of the Zebra server.
2    Copyright (C) 1994-2011 Index Data
3
4 Zebra is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
7 version.
8
9 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17
18 */
19
20 #if HAVE_CONFIG_H
21 #include <config.h>
22 #endif
23 #include <stdio.h>
24
25 #include <yaz/log.h>
26 #include <yaz/diagbib1.h>
27 #include <idzebra/res.h>
28 #include <idzebra/util.h>
29 #include <attrfind.h>
30 #include "index.h"
31 #include <yaz/oid_db.h>
32
33 static data1_att *getatt(data1_attset *p, int att)
34 {
35     data1_att *a;
36     data1_attset_child *c;
37
38     /* scan local set */
39     for (a = p->atts; a; a = a->next)
40         if (a->value == att)
41             return a;
42     /* scan included sets */
43     for (c = p->children; c; c = c->next)
44         if ((a = getatt(c->child, att)))
45             return a;
46     return 0;
47 }
48
49 static int att_getentbyatt(ZebraHandle zi, const Odr_oid *set, int att,
50                            const char **name)
51 {
52     data1_att *r;
53     data1_attset *p;
54
55     if (!(p = data1_attset_search_id (zi->reg->dh, set)))
56     {
57         zebraExplain_loadAttsets (zi->reg->dh, zi->res);
58         p = data1_attset_search_id (zi->reg->dh, set);
59     }
60     if (!p)   /* set undefined */
61         return -2;
62     if (!(r = getatt(p, att)))
63         return -1;
64     *name = r->name;
65     return 0;
66 }
67
68
69 ZEBRA_RES zebra_attr_list_get_ord(ZebraHandle zh,
70                                   Z_AttributeList *attr_list,
71                                   zinfo_index_category_t cat,
72                                   const char *index_type,
73                                   const Odr_oid *curAttributeSet,
74                                   int *ord)
75 {
76     int use_value = -1;
77     const char *use_string = 0;
78     AttrType use;
79
80     attr_init_AttrList(&use, attr_list, 1);
81     use_value = attr_find_ex(&use, &curAttributeSet, &use_string);
82
83     if (use_value < 0)
84     {
85         if (!use_string)
86             use_string = "any";
87     }
88     else
89     {
90         /* we have a use attribute and attribute set */
91         int r;
92         
93         r = att_getentbyatt(zh, curAttributeSet, use_value, &use_string);
94         if (r == -2)
95         {
96             zebra_setError_zint(zh,  YAZ_BIB1_UNSUPP_ATTRIBUTE_SET, 0);
97             return ZEBRA_FAIL;
98         }
99         if (r == -1)
100         {
101             zebra_setError_zint(zh, YAZ_BIB1_UNSUPP_USE_ATTRIBUTE, use_value);
102             return ZEBRA_FAIL;
103         }
104     }
105     if (!use_string)
106     {
107         zebra_setError(zh, YAZ_BIB1_UNSUPP_USE_ATTRIBUTE, 0);
108         return ZEBRA_FAIL;
109     }
110     *ord = zebraExplain_lookup_attr_str(zh->reg->zei, cat, 
111                                         index_type, use_string);
112     if (*ord == -1)
113     {
114         /* attribute 14=1 does not issue a diagnostic even 
115            1) the attribute is numeric but listed in .att 
116            2) the use attribute is string
117         */
118         AttrType unsup;
119         int unsup_value = 0;
120         attr_init_AttrList(&unsup, attr_list, 14);
121         unsup_value = attr_find(&unsup, 0);
122
123         if (unsup_value != 1)
124         {
125             if (use_value < 0)
126                 zebra_setError(zh, YAZ_BIB1_UNSUPP_USE_ATTRIBUTE, use_string);
127             else
128                 zebra_setError_zint(zh, YAZ_BIB1_UNSUPP_USE_ATTRIBUTE, use_value);
129             return ZEBRA_FAIL;
130         }
131     }
132     return ZEBRA_OK;
133 }
134
135 ZEBRA_RES zebra_apt_get_ord(ZebraHandle zh,
136                             Z_AttributesPlusTerm *zapt,
137                             const char *index_type,
138                             const char *xpath_use,
139                             const Odr_oid *curAttributeSet,
140                             int *ord)
141 {
142     ZEBRA_RES res = ZEBRA_OK;
143     AttrType relation;
144     int relation_value;
145     zinfo_index_category_t cat = zinfo_index_category_index;
146
147     attr_init_APT(&relation, zapt, 2);
148     relation_value = attr_find(&relation, NULL);
149
150     if (relation_value == 103) /* always matches */
151         cat = zinfo_index_category_alwaysmatches;
152     
153     if (!xpath_use)
154     {
155         res = zebra_attr_list_get_ord(zh, zapt->attributes,
156                                       cat, index_type,
157                                       curAttributeSet, ord);
158         /* use attribute not found. But it the relation is
159            always matches and the regulare index attribute is found
160            return a different diagnostic */
161         if (res != ZEBRA_OK && 
162             relation_value == 103
163             &&  zebra_attr_list_get_ord(
164                 zh, zapt->attributes, 
165                 zinfo_index_category_index, index_type,
166                 curAttributeSet, ord) == ZEBRA_OK)
167             zebra_setError_zint(zh, YAZ_BIB1_UNSUPP_RELATION_ATTRIBUTE, 103);
168     }
169     else
170     {
171         *ord = zebraExplain_lookup_attr_str(zh->reg->zei, cat, index_type,
172                                             xpath_use);
173         if (*ord == -1)
174         {
175             yaz_log(YLOG_LOG, "zebra_apt_get_ord FAILED xpath=%s index_type=%s",
176                     xpath_use, index_type);
177             zebra_setError(zh, YAZ_BIB1_UNSUPP_USE_ATTRIBUTE, 0);
178             res = ZEBRA_FAIL;
179         }
180         else
181         {
182             yaz_log(YLOG_LOG, "zebra_apt_get_ord OK xpath=%s index_type=%s",
183                     xpath_use, index_type);
184             
185         }
186     }
187     return res;
188 }
189
190 ZEBRA_RES zebra_sort_get_ord(ZebraHandle zh,
191                              Z_SortAttributes *sortAttributes,
192                              int *ord,
193                              int *numerical)
194 {
195     AttrType structure;
196     int structure_value;
197
198     attr_init_AttrList(&structure, sortAttributes->list, 4);
199
200     *numerical = 0;
201     structure_value = attr_find(&structure, 0);
202     if (structure_value == 109)
203         *numerical = 1;
204
205     if (zebra_attr_list_get_ord(
206             zh, sortAttributes->list,
207             zinfo_index_category_sort,
208             0 /* any index */, yaz_oid_attset_bib_1, ord) == ZEBRA_OK)
209         return ZEBRA_OK;
210     return ZEBRA_FAIL;
211 }
212
213
214 /*
215  * Local variables:
216  * c-basic-offset: 4
217  * c-file-style: "Stroustrup"
218  * indent-tabs-mode: nil
219  * End:
220  * vim: shiftwidth=4 tabstop=8 expandtab
221  */
222