Bump copyright year
[idzebra-moved-to-github.git] / index / attribute.c
1 /* This file is part of the Zebra server.
2    Copyright (C) 1994-2010 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 #include <stdio.h>
21
22 #include <yaz/log.h>
23 #include <yaz/diagbib1.h>
24 #include <idzebra/res.h>
25 #include <idzebra/util.h>
26 #include <attrfind.h>
27 #include "index.h"
28 #include <yaz/oid_db.h>
29
30 static data1_att *getatt(data1_attset *p, int att)
31 {
32     data1_att *a;
33     data1_attset_child *c;
34
35     /* scan local set */
36     for (a = p->atts; a; a = a->next)
37         if (a->value == att)
38             return a;
39     /* scan included sets */
40     for (c = p->children; c; c = c->next)
41         if ((a = getatt(c->child, att)))
42             return a;
43     return 0;
44 }
45
46 static int att_getentbyatt(ZebraHandle zi, const Odr_oid *set, int att,
47                            const char **name)
48 {
49     data1_att *r;
50     data1_attset *p;
51
52     if (!(p = data1_attset_search_id (zi->reg->dh, set)))
53     {
54         zebraExplain_loadAttsets (zi->reg->dh, zi->res);
55         p = data1_attset_search_id (zi->reg->dh, set);
56     }
57     if (!p)   /* set undefined */
58         return -2;
59     if (!(r = getatt(p, att)))
60         return -1;
61     *name = r->name;
62     return 0;
63 }
64
65
66 ZEBRA_RES zebra_attr_list_get_ord(ZebraHandle zh,
67                                   Z_AttributeList *attr_list,
68                                   zinfo_index_category_t cat,
69                                   const char *index_type,
70                                   const Odr_oid *curAttributeSet,
71                                   int *ord)
72 {
73     int use_value = -1;
74     const char *use_string = 0;
75     AttrType use;
76
77     attr_init_AttrList(&use, attr_list, 1);
78     use_value = attr_find_ex(&use, &curAttributeSet, &use_string);
79
80     if (use_value < 0)
81     {
82         if (!use_string)
83             use_string = "any";
84     }
85     else
86     {
87         /* we have a use attribute and attribute set */
88         int r;
89         
90         r = att_getentbyatt(zh, curAttributeSet, use_value, &use_string);
91         if (r == -2)
92         {
93             zebra_setError_zint(zh,  YAZ_BIB1_UNSUPP_ATTRIBUTE_SET, 0);
94             return ZEBRA_FAIL;
95         }
96         if (r == -1)
97         {
98             zebra_setError_zint(zh, YAZ_BIB1_UNSUPP_USE_ATTRIBUTE, use_value);
99             return ZEBRA_FAIL;
100         }
101     }
102     if (!use_string)
103     {
104         zebra_setError(zh, YAZ_BIB1_UNSUPP_USE_ATTRIBUTE, 0);
105         return ZEBRA_FAIL;
106     }
107     *ord = zebraExplain_lookup_attr_str(zh->reg->zei, cat, 
108                                         index_type, use_string);
109     if (*ord == -1)
110     {
111         /* attribute 14=1 does not issue a diagnostic even 
112            1) the attribute is numeric but listed in .att 
113            2) the use attribute is string
114         */
115         AttrType unsup;
116         int unsup_value = 0;
117         attr_init_AttrList(&unsup, attr_list, 14);
118         unsup_value = attr_find(&unsup, 0);
119
120         if (unsup_value != 1)
121         {
122             if (use_value < 0)
123                 zebra_setError(zh, YAZ_BIB1_UNSUPP_USE_ATTRIBUTE, use_string);
124             else
125                 zebra_setError_zint(zh, YAZ_BIB1_UNSUPP_USE_ATTRIBUTE, use_value);
126             return ZEBRA_FAIL;
127         }
128     }
129     return ZEBRA_OK;
130 }
131
132 ZEBRA_RES zebra_apt_get_ord(ZebraHandle zh,
133                             Z_AttributesPlusTerm *zapt,
134                             const char *index_type,
135                             const char *xpath_use,
136                             const Odr_oid *curAttributeSet,
137                             int *ord)
138 {
139     ZEBRA_RES res = ZEBRA_OK;
140     AttrType relation;
141     int relation_value;
142     zinfo_index_category_t cat = zinfo_index_category_index;
143
144     attr_init_APT(&relation, zapt, 2);
145     relation_value = attr_find(&relation, NULL);
146
147     if (relation_value == 103) /* always matches */
148         cat = zinfo_index_category_alwaysmatches;
149     
150     if (!xpath_use)
151     {
152         res = zebra_attr_list_get_ord(zh, zapt->attributes,
153                                       cat, index_type,
154                                       curAttributeSet, ord);
155         /* use attribute not found. But it the relation is
156            always matches and the regulare index attribute is found
157            return a different diagnostic */
158         if (res != ZEBRA_OK && 
159             relation_value == 103
160             &&  zebra_attr_list_get_ord(
161                 zh, zapt->attributes, 
162                 zinfo_index_category_index, index_type,
163                 curAttributeSet, ord) == ZEBRA_OK)
164             zebra_setError_zint(zh, YAZ_BIB1_UNSUPP_RELATION_ATTRIBUTE, 103);
165     }
166     else
167     {
168         *ord = zebraExplain_lookup_attr_str(zh->reg->zei, cat, index_type,
169                                             xpath_use);
170         if (*ord == -1)
171         {
172             yaz_log(YLOG_LOG, "zebra_apt_get_ord FAILED xpath=%s index_type=%s",
173                     xpath_use, index_type);
174             zebra_setError(zh, YAZ_BIB1_UNSUPP_USE_ATTRIBUTE, 0);
175             res = ZEBRA_FAIL;
176         }
177         else
178         {
179             yaz_log(YLOG_LOG, "zebra_apt_get_ord OK xpath=%s index_type=%s",
180                     xpath_use, index_type);
181             
182         }
183     }
184     return res;
185 }
186
187 ZEBRA_RES zebra_sort_get_ord(ZebraHandle zh,
188                              Z_SortAttributes *sortAttributes,
189                              int *ord,
190                              int *numerical)
191 {
192     AttrType structure;
193     int structure_value;
194
195     attr_init_AttrList(&structure, sortAttributes->list, 4);
196
197     *numerical = 0;
198     structure_value = attr_find(&structure, 0);
199     if (structure_value == 109)
200         *numerical = 1;
201
202     if (zebra_attr_list_get_ord(
203             zh, sortAttributes->list,
204             zinfo_index_category_sort,
205             0 /* any index */, yaz_oid_attset_bib_1, ord) == ZEBRA_OK)
206         return ZEBRA_OK;
207     return ZEBRA_FAIL;
208 }
209
210
211 /*
212  * Local variables:
213  * c-basic-offset: 4
214  * c-file-style: "Stroustrup"
215  * indent-tabs-mode: nil
216  * End:
217  * vim: shiftwidth=4 tabstop=8 expandtab
218  */
219