Login | Register
My pages Projects Community openCollabNet

Discussions > cvs > CVS update: /openpsa/addons/FunambolOpenPSAConnector_Maven2_Eclipse/src/java/org/openpsa/sync4j/ContactSyncSource.java

openpsa
Discussion topic

Back to topic list

CVS update: /openpsa/addons/FunambolOpenPSAConnector_Maven2_Eclipse/src/java/org/openpsa/sync4j/ContactSyncSource.java

Author juhana
Full name Juhana Hirvilahti
Date 2007-03-07 04:47:58 PST
Message User: juhana
Date: 2007-03-07 04:47:58-0800
Log:
 Testing

File Changes:

Directory: /openpsa/addons/Funa​mbolOpenPSAConnector​_Maven2_Eclipse/src/​java/org/openpsa/syn​c4j/
====================​====================​====================​====================​===============

File [changed]: ContactSyncSource.java
Url: http://openpsa.tigri​s.org/source/browse/​openpsa/addons/Funam​bolOpenPSAConnector_​Maven2_Eclipse/src/j​ava/org/openpsa/sync​4j/ContactSyncSource​.java?r1=1.1&r2=​1.2
Delta lines: +1 -1
-------------------
--- ContactSyncSource.java 2007-03-06 07:31:12-0800 1.1
+++ ContactSyncSource.java 2007-03-07 04:47:55-0800 1.2
@@ -1 +1 @@
-/*
 * Copyright 2006 Jukka Zitting <jz at yukatan dot fi>
 * Copyright 2006 Nemein Oy <info at nemein dot com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */
package org.openpsa.sync4j;

import java.io.ByteArrayInputStream;
import java.io.UnsupportedE​ncodingException;
import java.security.Principal;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import java.util.TimeZone;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import sync4j.foundation.pd​i.converter.ContactT​oVcard;
import sync4j.foundation.pd​i.converter.Converte​rException;
import sync4j.foundation.pd​i.parser.VcardParser​;
import sync4j.framework.eng​ine.SyncItem;
import sync4j.framework.eng​ine.source.SyncSourc​eException;

public class ContactSyncSource extends OpenPSASyncSource {

    static final Logger logger =
        LoggerFactory.getLog​ger(ContactSyncSourc​e.class);

    public ContactSyncSource() {
        logger.debug("Contac​tSyncSource()");
        setName("OpenPSA Contact Synchronization Source");
        setType("text/x-vcard");
    }

    protected byte[] encode(Object data)
            throws ConverterException, UnsupportedEncodingException {
        ContactToVcard converter =
            new ContactToVcard(TimeZ​one.getDefault(), "UTF-8");
        return converter.convert(da​ta).getBytes("UTF-8"​);
    }

    protected Object decode(byte[] data) throws SyncSourceException {
        try {
            VcardParser parser =
                new VcardParser(new ByteArrayInputStream(data), "UTC", "UTF-8");
            return parser.vCard();
        } catch (Exception e) {
            throw new SyncSourceException(​e.getMessage(), e);
        }
    }

    private MidgardPerson getPerson(String guid)
            throws SQLException, SyncSourceException {
        logger.debug("getPerson({})", guid);
        String sql =
            "SELECT p.id, p.sitegroup, NULLIF(p.created,'00​00-00-00'),"
            + " NULLIF(r.changed,'0000-00-00'), NULLIF(r.updated,'00​00-00-00'),"
            + " p.firstname, p.lastname, p.email, p.homepage,"
            + " p.handphone, p.workphone, p.homephone"
            + " FROM person AS p, repligard AS r"
            + " WHERE r.realm = 'person' AND r.id = p.id AND r.guid = ?";
        PreparedStatement ps = prepare(sql);
        try {
            ps.setString(1, guid);
            ResultSet rs = ps.executeQuery();
            try {
                if (rs.next()) {
                    MidgardPerson person = new MidgardPerson();
                    person.setTable("person");
                    person.setId(rs.getInt(1));
                    person.setGuid(guid);
                    person.setSitegroup(​rs.getInt(2));
                    Date created = rs.getDate(3);
                    Date c = rs.getDate(4);
                    Date u = rs.getDate(5);
                    if (created == null) {
                        if (c != null) {
                            if (u != null && u.before(c)) {
                                created = u;
                            } else {
                                created = c;
                            }
                        } else if (u != null) {
                            created = u;
                        } else {
                            created = new Date();
                        }
                    }
                    person.setCreated(created);
                    if (c == null) {
                        c = person.getCreated();
                    }
                    if (u == null || u.before(c)) {
                        person.setUpdated(c);
                    } else {
                        person.setUpdated(u);
                    }
                    person.setFirstname(​rs.getString(6));
                    person.setLastname(r​s.getString(7));
                    person.setEmail(rs.g​etString(8));
                    person.setHomepage(r​s.getString(9));
                    person.setHandphone(​rs.getString(10));
                    person.setWorkphone(​rs.getString(11));
                    person.setHomephone(​rs.getString(12));
                    return person;
                } else {
                    throw new SyncSourceException("Person not found: " + guid);
                }
            } finally {
                rs.close();
            }
        } finally {
            ps.close();
        }
    }

    protected SyncItem getSyncItemFromGuid(MidgardUser user, String guid)
            throws SQLException, SyncSourceException {
        logger.debug("getSyn​cItemFromGuid({})", guid);
        MidgardPerson person = getPerson(guid);
        Date timestamp = person.getUpdated();
        String sql = "SELECT MAX(added) FROM net_nehmer_buddylist_entry_db"
            + " WHERE account = ? AND buddy = ? AND added > ?";
        PreparedStatement ps = prepare(sql);
        try {
            ps.setString(1, user.getGuid());
            ps.setString(2, guid);
            ps.setLong(3, timestamp.getTime() / 1000);
            ResultSet rs = ps.executeQuery();
            try {
                if (rs.next()) {
                    timestamp = new Date(rs.getLong(1) * 1000);
                }
                return getSyncItem(guid, person.getContact(), timestamp);
            } finally {
                rs.close();
            }
        } finally {
            ps.close();
        }
    }

    protected Set getNewGuids(MidgardUser user, Date since)
            throws SyncSourceException, SQLException {
        logger.debug("getNewGuids({})", user);
        String sql = "SELECT DISTINCT buddy FROM net_nehmer_buddylist_entry_db"
            + " WHERE account = ? AND sitegroup = ? AND added > ?";
        PreparedStatement ps = prepare(sql);
        try {
            ps.setString(1, user.getGuid());
            ps.setInt(2, user.getSitegroup());
            ps.setLong(3, since.getTime() / 1000);
            ResultSet rs = ps.executeQuery();
            try {
                Set guids = new HashSet();
                while (rs.next()) {
                    guids.add(rs.getString(1));
                }
                return guids;
            } finally {
                rs.close();
            }
        } finally {
            ps.close();
        }
    }

    protected Set getUpdatedGuids(MidgardUser user, Date since)
           throws SyncSourceException, SQLException {
        logger.debug("getUpd​atedGuids({})", user);
        String sql =
            "SELECT DISTINCT b.buddy"
            + " FROM net_nehmer_buddylist_entry_db AS b, repligard AS r"
            + " WHERE b.account=? AND b.buddy = r.guid AND r.realm='person'"
            + " AND (r.changed > ? OR r.updated > ?)";
        PreparedStatement ps = prepare(sql);
        try {
            ps.setString(1, user.getGuid());
            ps.setTimestamp(2, new Timestamp(since.getTime()));
            ps.setTimestamp(3, new Timestamp(since.getTime()));
            ResultSet rs = ps.executeQuery();
            try {
                Set guids = new HashSet();
                while (rs.next()) {
                    guids.add(rs.getString(1));
                }
                return guids;
            } finally {
                rs.close();
            }
        } finally {
            ps.close();
        }
    }

    protected Set getAllGuids(MidgardUser user)
            throws SyncSourceException, SQLException {
        logger.debug("getAllGuids({})", user);
        String sql = "SELECT DISTINCT buddy FROM net_nehmer_buddylist_entry_db"
            + " WHERE account = ? AND sitegroup = ?";
        PreparedStatement ps = prepare(sql);
        try {
            ps.setString(1, user.getGuid());
            ps.setInt(2, user.getSitegroup());
            ResultSet rs = ps.executeQuery();
            try {
                Set guids = new HashSet();
                while (rs.next()) {
                    guids.add(rs.getString(1));
                }
                return guids;
            } finally {
                rs.close();
            }
        } finally {
            ps.close();
        }
    }

    protected Set getDeletedGuids(MidgardUser user, Date since)
            throws SyncSourceException, SQLException {
        String sql = "SELECT DISTINCT guid FROM repligard"
            + " WHERE realm='person' AND action='delete'"
            + " AND (updated > ? OR changed > ?) AND sitegroup = ?";
        PreparedStatement ps = prepare(sql);
        try {
            ps.setDate(1, new java.sql.Date(since.​getTime()));
            ps.setDate(2, new java.sql.Date(since.​getTime()));
            ps.setInt(3, user.getSitegroup());
            ResultSet rs = ps.executeQuery();
            try {
                Set guids = new HashSet();
                while (rs.next()) {
                    guids.add(rs.getString(1));
                }
                return guids;
            } finally {
                rs.close();
            }
        } finally {
            ps.close();
        }
    }

    protected String setSyncItem(
            MidgardUser user, String guid, Object data, Date time)
            throws SQLException, SyncSourceException {
        logger.debug("setSyn​cItem({},{},data,tim​e)", user, guid);
        String sql = "SELECT b.id"
            + " FROM net_nehmer_buddylist_entry_db AS b, repligard AS r"
            + " WHERE b.id = r.id AND r.realm='net_nehmer_​buddylist_entry_db'"​
            + " AND b.sitegroup = ? AND b.account = ? AND b.buddy = ?";
        PreparedStatement ps = prepare(sql);
        try {
            ps.setInt(1, user.getSitegroup());
            ps.setString(2, user.getGuid());
            ps.setString(3, guid);
            ResultSet rs = ps.executeQuery();
            try {
                while (rs.next()) {
                    // TODO
                }
                return guid;
            } finally {
                rs.close();
            }
        } finally {
            ps.close();
        }
    }

    public SyncItem setSyncItem(Principal principal, SyncItem syncInstance)
            throws SyncSourceException {
        logger.debug("setSyn​cItem({},syncitem)",​ principal);
        return syncInstance;
    }

    protected final void removeSyncItem(MidgardUser user, String guid, Date time)
            throws SQLException, SyncSourceException {
        logger.debug("remove​SyncItem({},{})", user, guid);
        String sql = "SELECT b.id"
            + " FROM net_nehmer_buddylist_entry_db AS b, repligard AS r"
            + " WHERE b.id = r.id AND r.realm='net_nehmer_​buddylist_entry_db'"​
            + " AND b.sitegroup = ? AND b.account = ? AND b.buddy = ?";
        PreparedStatement ps = prepare(sql);
        try {
            ps.setInt(1, user.getSitegroup());
            ps.setString(2, user.getGuid());
            ps.setString(3, guid);
            ResultSet rs = ps.executeQuery();
            try {
                while (rs.next()) {
                    delete(user, "net_nehmer_buddylis​t_entry_db", rs.getInt(1), time);
                }
            } finally {
                rs.close();
            }
        } finally {
            ps.close();
        }
    }

}
\ No newline at end of file
+/*
 * Copyright 2006 Jukka Zitting <jz at yukatan dot fi>
 * Copyright 2006 Nemein Oy <info at nemein dot com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */
package org.openpsa.sync4j;

import java.io.*;
import java.io.ByteArrayInputStream;
import java.io.UnsupportedE​ncodingException;
import java.security.Principal;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import java.util.TimeZone;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import sync4j.foundation.pd​i.converter.ContactT​oVcard;
import sync4j.foundation.pd​i.converter.Converte​rException;
import sync4j.foundation.pd​i.parser.VcardParser​;
import sync4j.framework.eng​ine.SyncItem;
import sync4j.framework.eng​ine.source.SyncSourc​eException;

public class ContactSyncSource extends OpenPSASyncSource {

    static final Logger logger =
        LoggerFactory.getLog​ger(ContactSyncSourc​e.class);

    public ContactSyncSource() {
        logger.debug("Contac​tSyncSource()");
        setName("OpenPSA Contact Synchronization Source");
        setType("text/x-vcard");
    }

    protected byte[] encode(Object data)
            throws ConverterException, UnsupportedEncodingException {
        ContactToVcard converter =
            new ContactToVcard(TimeZ​one.getDefault(), "UTF-8");
        return converter.convert(da​ta).getBytes("UTF-8"​);
    }

    protected Object decode(byte[] data) throws SyncSourceException {
        try {
            VcardParser parser =
                new VcardParser(new ByteArrayInputStream(data), "UTC", "UTF-8");
            return parser.vCard();
        } catch (Exception e) {
            throw new SyncSourceException(​e.getMessage(), e);
        }
    }

    private MidgardPerson getPerson(String guid)
            throws SQLException, SyncSourceException {
        logger.debug("getPerson({})", guid);
        String sql =
            "SELECT p.id, p.sitegroup, NULLIF(p.created,'00​00-00-00'),"
            + " NULLIF(r.changed,'0000-00-00'), NULLIF(r.updated,'00​00-00-00'),"
            + " p.firstname, p.lastname, p.email, p.homepage,"
            + " p.handphone, p.workphone, p.homephone"
            + " FROM person AS p, repligard AS r"
            + " WHERE p.metadata_deleted=0 AND r.realm = 'person' AND r.id = p.id AND r.guid = ?";
        PreparedStatement ps = prepare(sql);
        try {
            ps.setString(1, guid);
            ResultSet rs = ps.executeQuery();
            try {
                if (rs.next()) {
                    MidgardPerson person = new MidgardPerson();
                    person.setTable("person");
                    person.setId(rs.getInt(1));
                    person.setGuid(guid);
                    person.setSitegroup(​rs.getInt(2));
                    Date created = rs.getDate(3);
                    Date c = rs.getDate(4);
                    Date u = rs.getDate(5);
                    if (created == null) {
                        if (c != null) {
                            if (u != null && u.before(c)) {
                                created = u;
                            } else {
                                created = c;
                            }
                        } else if (u != null) {
                            created = u;
                        } else {
                            created = new Date();
                        }
                    }
                    person.setCreated(created);
                    if (c == null) {
                        c = person.getCreated();
                    }
                    if (u == null || u.before(c)) {
                        person.setUpdated(c);
                    } else {
                        person.setUpdated(u);
                    }
                    person.setFirstname(​rs.getString(6));
                    person.setLastname(r​s.getString(7));
                    person.setEmail(rs.g​etString(8));
                    person.setHomepage(r​s.getString(9));
                    person.setHandphone(​rs.getString(10));
                    person.setWorkphone(​rs.getString(11));
                    person.setHomephone(​rs.getString(12));
                    return person;
                } else {
                    throw new SyncSourceException("Person not found: " + guid);
                }
            } finally {
                rs.close();
            }
        } finally {
            ps.close();
        }
    }

    protected SyncItem getSyncItemFromGuid(MidgardUser user, String guid)
            throws SQLException, SyncSourceException {
        logger.debug("getSyn​cItemFromGuid({})", guid);
        MidgardPerson person = getPerson(guid);
        Date timestamp = person.getUpdated();
        String sql = "SELECT MAX(added) FROM net_nehmer_buddylist_entry_db"
            + " WHERE account = ? AND buddy = ? AND added > ?";
        PreparedStatement ps = prepare(sql);
        try {
            ps.setString(1, user.getGuid());
            ps.setString(2, guid);
            ps.setLong(3, timestamp.getTime() / 1000);
            ResultSet rs = ps.executeQuery();
            try {
                if (rs.next()) {
                    timestamp = new Date(rs.getLong(1) * 1000);
                }
                return getSyncItem(guid, person.getContact(), timestamp);
            } finally {
                rs.close();
            }
        } finally {
            ps.close();
        }
    }

    protected Set getNewGuids(MidgardUser user, Date since)
            throws SyncSourceException, SQLException {
        logger.debug("getNewGuids({})", user);
        String sql = "SELECT DISTINCT buddy FROM net_nehmer_buddylist_entry_db"
            + " WHERE metadata_deleted=0 AND account = ? AND sitegroup = ? AND added > ?";
        PreparedStatement ps = prepare(sql);
        try {
            ps.setString(1, user.getGuid());
            ps.setInt(2, user.getSitegroup());
            ps.setLong(3, since.getTime() / 1000);
            ResultSet rs = ps.executeQuery();
            try {
                Set guids = new HashSet();
                while (rs.next()) {
                    guids.add(rs.getString(1));
                }
                return guids;
            } finally {
                rs.close();
            }
        } finally {
            ps.close();
        }
    }

    protected Set getUpdatedGuids(MidgardUser user, Date since)
           throws SyncSourceException, SQLException {
        logger.debug("getUpd​atedGuids({})", user);
        String sql =
            "SELECT DISTINCT b.buddy"
            + " FROM net_nehmer_buddylist_entry_db AS b, repligard AS r"
            + " WHERE b.account=? AND b.buddy = r.guid AND r.realm='person'"
            + " AND (r.changed > ? OR r.updated > ?)";
        PreparedStatement ps = prepare(sql);
        try {
            ps.setString(1, user.getGuid());
            ps.setTimestamp(2, new Timestamp(since.getTime()));
            ps.setTimestamp(3, new Timestamp(since.getTime()));
            ResultSet rs = ps.executeQuery();
            try {
                Set guids = new HashSet();
                while (rs.next()) {
                    guids.add(rs.getString(1));
                }
                return guids;
            } finally {
                rs.close();
            }
        } finally {
            ps.close();
        }
    }

    protected Set getAllGuids(MidgardUser user)
            throws SyncSourceException, SQLException {
        logger.debug("getAllGuids({})", user);
        String sql = "SELECT DISTINCT buddy FROM net_nehmer_buddylist_entry_db"
            + " WHERE account = ? AND sitegroup = ?";
        PreparedStatement ps = prepare(sql);
        try {
            ps.setString(1, user.getGuid());
            ps.setInt(2, user.getSitegroup());
            ResultSet rs = ps.executeQuery();
            try {
                Set guids = new HashSet();
                while (rs.next()) {
                    guids.add(rs.getString(1));
                }
                return guids;
            } finally {
                rs.close();
            }
        } finally {
            ps.close();
        }
    }

    protected Set getDeletedGuids(MidgardUser user, Date since)
            throws SyncSourceException, SQLException {
        String sql = "SELECT DISTINCT guid FROM repligard"
            + " WHERE realm='person' AND action='delete'"
            + " AND (updated > ? OR changed > ?) AND sitegroup = ?";
        PreparedStatement ps = prepare(sql);
        try {
            ps.setDate(1, new java.sql.Date(since.​getTime()));
            ps.setDate(2, new java.sql.Date(since.​getTime()));
            ps.setInt(3, user.getSitegroup());
            ResultSet rs = ps.executeQuery();
            try {
                Set guids = new HashSet();
                while (rs.next()) {
                    guids.add(rs.getString(1));
                }
                return guids;
            } finally {
                rs.close();
            }
        } finally {
            ps.close();
        }
    }

    protected String setSyncItem(
            MidgardUser user, String guid, Object data, Date time)
            throws SQLException, SyncSourceException {
        
                try{
                    // Create file
                    FileWriter fstream = new FileWriter("ex.txt");
                BufferedWriter out = new BufferedWriter(fstream);
                out.write("Setting Sync Item");
                //Close the output stream
                out.close();
            }catch (Exception e){//Catch exception if any
                System.err.println("Error: " + e.getMessage());
            }
        
        logger.debug("setSyn​cItem({},{},data,tim​e)", user, guid);
        String sql = "SELECT b.id"
            + " FROM net_nehmer_buddylist_entry_db AS b, repligard AS r"
            + " WHERE b.id = r.id AND r.realm='net_nehmer_​buddylist_entry_db'"​
            + " AND b.sitegroup = ? AND b.account = ? AND b.buddy = ?";
        PreparedStatement ps = prepare(sql);
        try {
            ps.setInt(1, user.getSitegroup());
            ps.setString(2, user.getGuid());
            ps.setString(3, guid);
            ResultSet rs = ps.executeQuery();
            try {
                while (rs.next()) {
                    // TODO
                }
                return guid;
            } finally {
                rs.close();
            }
        } finally {
            ps.close();
        }
    }

    public SyncItem setSyncItem(Principal principal, SyncItem syncInstance)
            throws SyncSourceException {
        logger.debug("setSyn​cItem({},syncitem)",​ principal);
        return syncInstance;
    }

    protected final void removeSyncItem(MidgardUser user, String guid, Date time)
            throws SQLException, SyncSourceException {
        logger.debug("remove​SyncItem({},{})", user, guid);
        String sql = "SELECT b.id"
            + " FROM net_nehmer_buddylist_entry_db AS b, repligard AS r"
            + " WHERE b.id = r.id AND r.realm='net_nehmer_​buddylist_entry_db'"​
            + " AND b.sitegroup = ? AND b.account = ? AND b.buddy = ?";
        PreparedStatement ps = prepare(sql);
        try {
            ps.setInt(1, user.getSitegroup());
            ps.setString(2, user.getGuid());
            ps.setString(3, guid);
            ResultSet rs = ps.executeQuery();
            try {
                while (rs.next()) {
                    delete(user, "net_nehmer_buddylis​t_entry_db", rs.getInt(1), time);
                }
            } finally {
                rs.close();
            }
        } finally {
            ps.close();
        }
    }

}
\ No newline at end of file

« Previous message in topic | 1 of 1 | Next message in topic »

Messages

Show all messages in topic

CVS update: /openpsa/addons/FunambolOpenPSAConnector_Maven2_Eclipse/src/java/org/openpsa/sync4j/ContactSyncSource.java juhana Juhana Hirvilahti 2007-03-07 04:47:58 PST
Messages per page: