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