Android Open Source - droidBBpush Device D A O






From Project

Back to project page droidBBpush.

License

The source code is released under:

This is free and unencumbered software released into the public domain. Anyone is free to copy, modify, publish, use, compile, sell, or distribute this software, either in source code form or as a co...

If you think the Android project droidBBpush listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.arg3.examples.push.dao;
/* w w  w .  ja  va 2 s .c  o  m*/
import com.arg3.examples.push.enums.DeviceType;
import com.arg3.examples.push.model.Device;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

/**
 * Created by c0der78 on 2014-09-19.
 */
public class DeviceDAO {

    private Connection conn;

    @Inject
    public DeviceDAO(Connection conn) {
        assert (conn != null);
        this.conn = conn;

    }

    private static Device queryToDevice(ResultSet query) throws SQLException {
        String deviceType = query.getString("type");

        Device device = new Device();

        device.setId(query.getInt("id"));

        device.setType(DeviceType.get(deviceType));

        device.setToken(query.getString("token"));

        return device;

    }

    public boolean remove(int deviceId) throws SQLException {
        PreparedStatement stmt = null;

        try {
            stmt = conn.prepareStatement("delete from devices where id = ?");

            stmt.setInt(1, deviceId);

            return stmt.executeUpdate() != 0;
        } finally {
            if (stmt != null)
                stmt.close();
        }
    }

    public List<Device> findAllDevices() throws SQLException {
        Statement stmt = null;

        List<Device> results = new ArrayList<Device>();

        try {
            stmt = conn.createStatement();

            ResultSet query = stmt.executeQuery("select * from devices");

            while (query.next()) {

                Device device = queryToDevice(query);

                results.add(device);
            }
        } finally {
            if (stmt != null)
                stmt.close();
        }

        return results;
    }

    public boolean exists(int id) throws SQLException {
        if (id == 0) return false;

        PreparedStatement stmt = null;

        try {
            stmt = conn.prepareStatement("select count(*) from devices where id = ?");

            stmt.setInt(1, id);

            ResultSet set = stmt.executeQuery();

            return set.getInt(0) > 0;
        } finally {
            if (stmt != null)
                stmt.close();
        }
    }

    public Device findByTypeAndToken(DeviceType type, String token) throws SQLException {
        if (token == null || token.isEmpty()) return null;

        PreparedStatement stmt = null;

        try {
            stmt = conn.prepareStatement("select * from devices where type = ? and token = ?");

            stmt.setString(1, type.toString());

            stmt.setString(2, token);

            ResultSet set = stmt.executeQuery();

            if (set.next()) {
                return queryToDevice(set);
            }
        } finally {
            if (stmt != null)
                stmt.close();
        }

        return null;
    }

    public Device find(int id) throws SQLException {
        if (id == 0) return null;

        PreparedStatement stmt = null;

        try {
            stmt = conn.prepareStatement("select * from devices where id = ?");

            stmt.setInt(1, id);

            ResultSet set = stmt.executeQuery();

            if (set.next()) {
                return queryToDevice(set);
            }
        } finally {
            if (stmt != null)
                stmt.close();
        }

        return null;
    }

    public void save(Device device) throws SQLException {
        PreparedStatement stmt = null;
        try {
            boolean deviceExists = exists(device.getId());

            if (deviceExists) {
                stmt = conn.prepareStatement("update devices set type=?, token=? where id=?");

                stmt.setInt(3, device.getId());
            } else {
                stmt = conn.prepareStatement("insert into devices(type, token) values(?,?)", Statement.RETURN_GENERATED_KEYS);
            }
            stmt.setString(1, device.getType().toString());

            stmt.setString(2, device.getToken());

            int id = stmt.executeUpdate();

            device.setId(id);
        } finally {
            if (stmt != null)
                stmt.close();
        }
    }
}




Java Source Code List

com.arg3.droid.bbpush.ApplicationTest.java
com.arg3.examples.droidbb.MainActivity.java
com.arg3.examples.droidbb.MainApplication.java
com.arg3.examples.droidbb.MainModule.java
com.arg3.examples.droidbb.PushNotifier.java
com.arg3.examples.droidbb.PushRegistrar.java
com.arg3.examples.droidbb.PushService.java
com.arg3.examples.droidbb.annotations.ForApplication.java
com.arg3.examples.droidbb.gcm.GCMBroadcastReceiver.java
com.arg3.examples.droidbb.gcm.GCMBroadcastReceiver.java
com.arg3.examples.droidbb.gcm.GCMHandler.java
com.arg3.examples.droidbb.gcm.GCMHandler.java
com.arg3.examples.droidbb.gcm.GCMIntentService.java
com.arg3.examples.droidbb.gcm.GCMIntentService.java
com.arg3.examples.push.AndroidPushService.java
com.arg3.examples.push.ApplicationListener.java
com.arg3.examples.push.BaseServlet.java
com.arg3.examples.push.BlackberryPushService.java
com.arg3.examples.push.MainModule.java
com.arg3.examples.push.PushServlet.java
com.arg3.examples.push.SendServlet.java
com.arg3.examples.push.ViewServlet.java
com.arg3.examples.push.api.BaseAPIServlet.java
com.arg3.examples.push.api.RegisterDeviceServlet.java
com.arg3.examples.push.dao.DeviceDAO.java
com.arg3.examples.push.enums.DeviceType.java
com.arg3.examples.push.model.Device.java