summaryrefslogtreecommitdiff
path: root/src/com/memberwebs/ldapxml/LXSpecs.java
blob: 98f1d551127f16ee5774636f52d7ca1da8d73577 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
package com.memberwebs.ldapxml;

import java.util.*;

import com.memberwebs.ldapxml.helpers.*;
import com.familymembers.util.ldap.*;

/**
 * Additional specifications for retrieving data from an
 * LDAP directory.
 *
 * @author nielsen@memberwebs.com
 * @version 0.5
 */
public class LXSpecs
{
    /**
     * Don't retrieve any sub entries.
     */
    public static final int DEPTH_BASE = 0;

    /**
     * Retrive only direct children of the referenced entry.
     */
    public static final int DEPTH_SINGLE = 1;

    /**
     * Retrieve all sub entries.
     */
    public static final int DEPTH_INFINITE = 0xFFFFFFFF;


    private static final String LANG = "lang-";

    /**
     * Construct a new LXSpecs object.
     */
    public LXSpecs()
    {
        m_filter = LDAPUtil.FILTER_ALL;
        m_depth = DEPTH_BASE;
        m_sort = null;
        m_data = null;
        m_language = null;
        m_start = 0;
        m_limit = Integer.MAX_VALUE;
    }

    public final int getStart()
    {
        return m_start;
    }

    public final int getLimit()
    {
        return m_limit;
    }

    public final void setStart(int start)
    {
        m_start = start;
    }

    public final void setLimit(int limit)
    {
        m_limit = limit;
    }

    /**
     * Get preferred language.
     *
     * @return The language or null if none set.
     */
    public final String getLanguage()
    {
        return m_language;
    }

    /**
     * Set preferred language for LDAP retrieval.
     *
     * @param language The language or null for no preference.
     */
    public final void setLanguage(String language)
    {
        m_language = language;

        if(m_language != null && !m_language.startsWith(LANG))
            m_language = LANG + m_language;
    }

    /**
     * Returns the depth to which entries are recursively
     * retrieved from the LDAP directory.
     *
     * @return The depth.
     */
    public final int getDepth()
    {
        return m_depth;
    }

    /**
     * Sets the depth to which entries are recursively
     * retrieved from the LDAP directory.
     *
     * @return The depth.
     */
    public final void setDepth(int depth)
    {
        m_depth = depth;
    }

    /**
     * Gets the additional filter when searching for entries.
     *
     * @return The filter.
     */
    public final String getFilter()
    {
        return m_filter == null ? LDAPUtil.FILTER_ALL : m_filter;
    }

    /**
     * Gets the filter when searching for entries, combining it
     * with another filter.
     *
     * @param prevFilter The other filter.
     * @return The combined filter.
     */
    public final String getFilter(String prevFilter)
    {
        String filter = LDAPUtil.combineFilters(m_filter, prevFilter);
        return filter == null ? LDAPUtil.FILTER_ALL : filter;
    }

    /**
     * Set an additional filter used when searching for entries.
     *
     * @param filter The filter.
     */
    public final void setFilter(String filter)
    {
        m_filter = LDAPUtil.combineFilters(filter, null);
    }

    /**
     * Get the requested sort order.
     *
     * @return The sort order, or null if none present.
     */
    public final String[] getSort()
    {
        if(m_sort != null && m_sort.length > 0)
            return m_sort;
        else
            return null;
    }

    /**
     * Get the sort directions (ascending, descending) for the
     * sort order.
     *
     * @return An array of sort directions.
     */
    public final boolean[] getSortDirection()
    {
        if(m_direc != null && m_direc.length > 0)
            return m_direc;
        else
            return null;
    }

    /**
     * Set the sort order. Attribute names prefixed with '-' are
     * treated as descending.
     *
     * @param sort The sort order.
     */
    public final void setSort(String[] sort)
    {
        m_mappedSort = null;

        m_sort = new String[sort.length];
        m_direc = new boolean[sort.length];

        for(int i = 0; i < sort.length; i++)
        {
            boolean desc = sort[i].startsWith("-");
            if(desc)
                m_sort[i] = sort[i].substring(1);
            else
                m_sort[i] = sort[i];

            m_direc[i] = !desc;
        }
    }

    public final Object getData()
    {
        return m_data;
    }

    public final void setData(Object data)
    {
        m_data = data;
    }

    /**
     * Translate the sort order with mapped LDAP attribute
     * names retrieved from the map.
     *
     * @param map The LX Map to get the attribute names from.
     * @return The translated sort order.
     */
    protected final String[] getMappedSort(LXMap map)
    {
        if(m_mappedSort == null)
        {
            if(m_sort != null && m_sort.length > 0)
            {
                m_mappedSort = new String[m_sort.length];

                Map nameMap = map.getNameMap();
                for(int i = 0; i < m_sort.length; i++)
                {
                    String name = (String)nameMap.get(m_sort[i]);
                    m_mappedSort[i] = name == null ? m_sort[i] : name;
                }
            }
        }
        return m_mappedSort;
    }

    // The additional filter
    private String m_filter;

    // The depth to which to recursively retrieve sub entries
    private int m_depth;

    // The starting point to retrieve entries
    private int m_start;
    // The number of entries to retrieve
    private int m_limit;

    // The sort order and direction
    private String[] m_sort;
    private String[] m_mappedSort;
    private boolean[] m_direc;

    // The preferred language
    private String m_language;

    // Data for hooks
    private Object m_data;
}