source: contrib/funambol/trunk/modules/psync/src/main/java/br/com/prognus/psync/items/dao/PIMEntityDAO.java @ 2082

Revision 2082, 11.6 KB checked in by emersonfaria, 14 years ago (diff)

Ticket #927 - Reestruturacao dos diretorios do Funambol

Line 
1/**
2 * This class implements methods to access PIM data in a data store.
3 * @author Diorgenes Felipe Grzesiuk <diorgenes@prognus.com.br>
4 * @copyright Copyright 2007-2008 Prognus
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as published by
8 * the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with Foobar; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18 */
19package br.com.prognus.psync.items.dao;
20
21import java.sql.Connection;
22import java.sql.PreparedStatement;
23import java.sql.ResultSet;
24import java.sql.Timestamp;
25import java.util.Enumeration;
26import java.util.Iterator;
27import java.util.List;
28
29import javax.naming.NamingException;
30import javax.sql.DataSource;
31
32import br.com.prognus.psync.exception.PIMDBAccessException;
33import br.com.prognus.psync.util.Def;
34
35import com.funambol.common.pim.common.Property;
36import com.funambol.framework.logging.FunambolLogger;
37import com.funambol.framework.logging.FunambolLoggerFactory;
38import com.funambol.framework.tools.DBTools;
39import com.funambol.framework.tools.DataSourceTools;
40import com.novell.ldap.LDAPAttribute;
41import com.novell.ldap.LDAPAttributeSet;
42import com.novell.ldap.LDAPConnection;
43import com.novell.ldap.LDAPEntry;
44import com.novell.ldap.LDAPException;
45import com.novell.ldap.LDAPSearchResults;
46import com.novell.ldap.util.Base64;
47
48public abstract class PIMEntityDAO {
49
50        // ------------------------------------------------------------ Private data
51
52        protected static final FunambolLogger log = FunambolLoggerFactory
53                        .getLogger(Def.LOGGER_NAME);
54
55        protected String jndiDataSourceName;
56
57        protected DataSource dataSource = null;
58
59        // -------------------------------------------------------------- Properties
60
61        protected String userId;
62
63        public String getUserId() {
64                return userId;
65        }
66
67        // ------------------------------------------------------------ Constructors
68
69        /**
70         * Creates a new instance of PIMEntityDAO, ready to be linked to a given
71         * DataSource.
72         *
73         * @param jndiDataSourceName
74         *            the jndiname of the datasource to use
75         * @param userId
76         *            corresponding to the "userid" field of the fun_pim_contact
77         *            table
78         * @throws IllegalArgumentException
79         *             if there are errors looking up the dataSource with the given
80         *             jndiDataSourceName
81         */
82        public PIMEntityDAO(String jndiDataSourceName, String userId) {
83
84                Connection con = null;
85                PreparedStatement ps = null;
86
87                try {
88                        this.dataSource = DataSourceTools
89                                        .lookupDataSource(jndiDataSourceName);
90                } catch (NamingException ex) {
91                        throw new IllegalArgumentException("Error looking up datasource: "
92                                        + jndiDataSourceName, ex);
93                }
94
95                try {
96                        con = getDataSource().getConnection();
97
98                        PreparedStatement ldap_server = con
99                                        .prepareStatement("select config_value from phpgw_config WHERE config_name = 'ldap_host' AND config_app = 'phpgwapi'");
100                        ResultSet rs1 = ldap_server.executeQuery();
101
102                        PreparedStatement ldap_dc = con
103                                        .prepareStatement("select config_value from phpgw_config WHERE config_name = 'ldap_context' AND config_app = 'phpgwapi'");
104                        ResultSet rs2 = ldap_dc.executeQuery();
105
106                        rs1.next();
107                        rs2.next();
108
109                        String server = rs1.getString(1);
110                        String dc = rs2.getString(1);
111
112                        this.jndiDataSourceName = jndiDataSourceName;
113                        this.userId = LdapUID(server, dc, userId);
114
115                } catch (Exception e) {
116                        throw new IllegalArgumentException("Error UID ldap", e);
117                } finally {
118                        DBTools.close(con, ps, null);
119                }
120
121        }
122
123        // -----------------------------------------------------------Public methods
124
125        protected String LdapUID(String server, String dc, String user)
126                        throws Exception {
127
128                LDAPConnection ldap = new LDAPConnection();
129                String searchAttrs[] = { "uidNumber" };
130                String id_owner = null;
131
132                try {
133
134                        ldap.connect(server, 389);
135                        LDAPSearchResults searchResults = ldap.search(dc,
136                                        LDAPConnection.SCOPE_SUB, "(uid=" + user + ")",
137                                        searchAttrs, false);
138
139                        while (searchResults.hasMore()) {
140
141                                LDAPEntry nextEntry = null;
142                                try {
143                                        nextEntry = searchResults.next();
144                                } catch (LDAPException e) {
145                                        if (e.getResultCode() == LDAPException.LDAP_TIMEOUT
146                                                        || e.getResultCode() == LDAPException.CONNECT_ERROR)
147                                                break;
148                                        else
149                                                continue;
150                                }
151                                LDAPAttributeSet attributeSet = nextEntry.getAttributeSet();
152                                Iterator allAttributes = attributeSet.iterator();
153
154                                while (allAttributes.hasNext()) {
155
156                                        LDAPAttribute attribute = (LDAPAttribute) allAttributes
157                                                        .next();
158                                        String attributeName = attribute.getName();
159                                        Enumeration allValues = attribute.getStringValues();
160
161                                        if (allValues != null) {
162
163                                                while (allValues.hasMoreElements()) {
164
165                                                        String Value = (String) allValues.nextElement();
166
167                                                        if (Base64.isLDIFSafe(Value)) {
168                                                        } else {
169                                                                Value = Base64.encode(Value.getBytes());
170                                                        }
171
172                                                        if (attributeName.equalsIgnoreCase("uidNumber")) {
173                                                                id_owner = Value;
174                                                                break;
175                                                        }
176
177                                                }
178                                        }
179                                }
180                        }
181
182                        ldap.disconnect();
183
184                        if (id_owner == null) {
185                                throw new LDAPException();
186                        }
187
188                } catch (LDAPException e) {
189                        log.info("Error while connecting and binding to LDAP: "
190                                        + e.toString());
191                }
192
193                return id_owner;
194        }
195
196        /**
197         * Looks up the data source, making some guess attempts based on
198         * jndiDataSourceName.
199         *
200         * @throws Exception
201         */
202        protected DataSource getDataSource() throws Exception {
203                return dataSource;
204        }
205
206        public abstract List getAllItems() throws PIMDBAccessException;
207
208        public abstract void removeItem(String uid) throws PIMDBAccessException;
209
210        public abstract void removeAllItems() throws PIMDBAccessException;
211
212        public abstract char getItemState(String uid, Timestamp since)
213                        throws PIMDBAccessException;
214
215        /**
216         * Retrieves the UID list of the new items belonging to the user filtered
217         * according to the given time interval.
218         *
219         * @param since
220         *            the earliest allowed last-update Timestamp
221         * @param to
222         *            the latest allowed last-update Timestamp
223         * @throws PIMDBAccessException
224         * @return a List of UIDs (as String objects)
225         */
226        public List getNewItems(Timestamp since, Timestamp to)
227                        throws PIMDBAccessException {
228
229                log.info("\n\n==>DAO start getNewItems");
230                return getItemsHavingStatus(since, to, 'N', "");
231        }
232
233        /**
234         * Retrieves the UID list of the deleted items belonging to the user
235         * filtered according to the given time interval.
236         *
237         * @param since
238         *            the earliest allowed last-update Timestamp
239         * @param to
240         *            the latest allowed last-update Timestamp
241         * @throws PIMDBAccessException
242         * @return a List of UIDs (as String objects)
243         */
244//Correcao - Inserido o parametro sourceURI - emerson-faria.nobre@serpro.gov.br - 17/set/2009
245        public List getDeletedItems(Timestamp since, Timestamp to, String sourceURI)
246                        throws PIMDBAccessException {
247
248                log.info("\n\n==>DAO start getDeletedItems");
249                return getItemsHavingStatus(since, to, 'D', sourceURI);
250        }
251
252        /**
253         * Retrieves the UID list of the updated items belonging to the user
254         * filtered according to the given time interval.
255         *
256         * @param since
257         *            the earliest allowed last-update Timestamp
258         * @param to
259         *            the latest allowed last-update Timestamp
260         * @throws PIMDBAccessException
261         * @return a List of UIDs (as String objects)
262         */
263        public List getUpdatedItems(Timestamp since, Timestamp to)
264                        throws PIMDBAccessException {
265
266                log.info("\n\n==>DAO start getUpdatedItems");
267                return getItemsHavingStatus(since, to, 'U', "");
268        }
269
270        // ---------------------------------------------------------- Private
271        // methods
272
273        /**
274         * Retrieves the UID list of the items belonging to the user filtered
275         * according to the given time interval and status.
276         *
277         * @param since
278         *            the earliest allowed last-update Timestamp
279         * @param to
280         *            the latest allowed last-update Timestamp
281         * @param status
282         *            'D' for deleted items, 'N' for new items, 'U' for updated
283         *            items
284         * @throws PIMDBAccessException
285         * @return a List of UIDs (as String objects)
286         */
287// Alteracao - Adicionado parametro sourceURI - emerson-faria.nobre@serpro.gov.br - 17/set/2009
288        protected abstract List getItemsHavingStatus(Timestamp since, Timestamp to,
289                        char status, String sourceURI) throws PIMDBAccessException;
290
291         /**
292         * Checks (safely) whether the property is unset.
293         *
294         * @param property
295         *            may be null
296         * @return false only if the property value is a non-null, but possibly
297         *         empty (""), string
298         *
299         * @see PIMEntityDAO#stringFrom(Property)
300         */
301        protected static boolean isNullProperty(Property property) {
302
303                return (property == null || property.getPropertyValueAsString() == null);
304        }
305
306        /**
307         * Checks (safely) whether the property is unset or set to an empty string.
308         *
309         * @param property
310         *            may be null
311         * @return false only if the property value is a non-null non-empty string
312         *
313         * @see PIMEntityDAO#stringFrom(Property, boolean)
314         */
315        protected static boolean isEmptyProperty(Property property) {
316
317                if (property == null) {
318                        return true;
319                }
320                String string = property.getPropertyValueAsString();
321                if (string == null || string.length() == 0) {
322                        return true;
323                }
324                return false;
325        }
326
327        /**
328         * Extract a string from a property in a safe way. An empty string ("") is
329         * considered as an acceptable value for the property: in such a case, an
330         * empty String object will be returned.
331         *
332         * @param property
333         *            may be null
334         * @return if existing, the property value will be returned as a String
335         *         object
336         */
337        protected static String stringFrom(Property property) {
338
339                if (property == null) {
340                        return null;
341                }
342                return property.getPropertyValueAsString();
343        }
344
345        /**
346         * Extract a string from a property in a safe way. This method is not
347         * currently used, but it could be useful in the future for determining the
348         * behaviour of the connector when dealing with empty properties. A field
349         * whose value is extracted with stringFrom(..., true) will not be updated
350         * in case its value is set to ""; a field whose value is extracted with
351         * stringFrom(..., false) will be considered as explicitly kept blank if its
352         * value is "". This means that single field deletions can be made tunable.
353         *
354         * @param property
355         *            may be null
356         * @param emptyImpliesNull
357         *            if true, an empty string ("") will be treated as if it were
358         *            null; otherwise, in such a case an empty String object will be
359         *            returned
360         *
361         * @return if existing (and not empty if emptyImpliesNull is true), the
362         *         property value will be returned as a String object
363         */
364        protected static String stringFrom(Property property,
365                        boolean emptyImpliesNull) {
366
367                if (property == null) {
368                        return null;
369                }
370
371                String string = property.getPropertyValueAsString();
372                if (string == null || !emptyImpliesNull) {
373                        return string;
374                }
375
376                if (string.length() == 0) {
377                        return null;
378                }
379                return string;
380        }
381
382        /**
383         * Gets a substring in a safe way.
384         *
385         * @param string
386         *            may be null or longer than maxLength
387         * @param maxLength
388         * @return null if the string is null, a truncated substring of string
389         *         otherwise
390         */
391        protected static String truncate(String string, int maxLength) {
392                if (string == null || string.length() <= maxLength) {
393                        return string;
394                }
395                return string.substring(0, maxLength);
396        }
397}
Note: See TracBrowser for help on using the repository browser.