4272aab31a82adef433ff874e697ab146beec086
[idzebra-moved-to-github.git] / index / attribute.c
1 /* $Id: attribute.c,v 1.31 2007-05-08 12:50:04 adam Exp $
2    Copyright (C) 1995-2007
3    Index Data ApS
4
5 This file is part of the Zebra server.
6
7 Zebra is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
11
12 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20
21 */
22
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                                   int 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         if (use_value < 0)
115             zebra_setError(zh, YAZ_BIB1_UNSUPP_USE_ATTRIBUTE, use_string);
116         else
117             zebra_setError_zint(zh, YAZ_BIB1_UNSUPP_USE_ATTRIBUTE, use_value);
118         return ZEBRA_FAIL;
119     }
120     return ZEBRA_OK;
121 }
122
123 ZEBRA_RES zebra_apt_get_ord(ZebraHandle zh,
124                             Z_AttributesPlusTerm *zapt,
125                             int index_type,
126                             const char *xpath_use,
127                             const Odr_oid *curAttributeSet,
128                             int *ord)
129 {
130     ZEBRA_RES res = ZEBRA_OK;
131     AttrType relation;
132     int relation_value;
133     zinfo_index_category_t cat = zinfo_index_category_index;
134
135     attr_init_APT(&relation, zapt, 2);
136     relation_value = attr_find(&relation, NULL);
137
138     if (relation_value == 103) /* always matches */
139         cat = zinfo_index_category_alwaysmatches;
140     
141     if (!xpath_use)
142     {
143         res = zebra_attr_list_get_ord(zh, zapt->attributes,
144                                       cat, index_type,
145                                       curAttributeSet, ord);
146         /* use attribute not found. But it the relation is
147            always matches and the regulare index attribute is found
148            return a different diagnostic */
149         if (res != ZEBRA_OK && 
150             relation_value == 103
151             &&  zebra_attr_list_get_ord(
152                 zh, zapt->attributes, 
153                 zinfo_index_category_index, index_type,
154                 curAttributeSet, ord) == ZEBRA_OK)
155             zebra_setError_zint(zh, YAZ_BIB1_UNSUPP_RELATION_ATTRIBUTE, 103);
156     }
157     else
158     {
159         *ord = zebraExplain_lookup_attr_str(zh->reg->zei, cat, index_type,
160                                             xpath_use);
161         if (*ord == -1)
162         {
163             yaz_log(YLOG_LOG, "zebra_apt_get_ord FAILED xpath=%s index_type=%c",
164                     xpath_use, index_type);
165             zebra_setError(zh, YAZ_BIB1_UNSUPP_USE_ATTRIBUTE, 0);
166             res = ZEBRA_FAIL;
167         }
168         else
169         {
170             yaz_log(YLOG_LOG, "zebra_apt_get_ord OK xpath=%s index_type=%c",
171                     xpath_use, index_type);
172             
173         }
174     }
175     return res;
176 }
177
178 ZEBRA_RES zebra_sort_get_ord(ZebraHandle zh,
179                              Z_SortAttributes *sortAttributes,
180                              int *ord,
181                              int *numerical)
182 {
183     AttrType structure;
184     int structure_value;
185
186     attr_init_AttrList(&structure, sortAttributes->list, 4);
187
188     *numerical = 0;
189     structure_value = attr_find(&structure, 0);
190     if (structure_value == 109)
191         *numerical = 1;
192
193     if (zebra_attr_list_get_ord(
194             zh, sortAttributes->list,
195             zinfo_index_category_sort,
196             -1 /* any index */, yaz_oid_attset_bib_1, ord) == ZEBRA_OK)
197         return ZEBRA_OK;
198     return ZEBRA_FAIL;
199 }
200
201
202 /*
203  * Local variables:
204  * c-basic-offset: 4
205  * indent-tabs-mode: nil
206  * End:
207  * vim: shiftwidth=4 tabstop=8 expandtab
208  */
209