예제 #1
0
  /**
   * Marks as deleted the entry with the given id
   *
   * @param id the entry id
   * @throws DataAccessException if an error occurs
   * @return the number of updated entries
   */
  public int markAsDeleted(long id) throws DataAccessException {

    Connection con = null;
    PreparedStatement ps = null;
    int rowsUpdated = 0;

    try {

      con = getConnection();

      ps = con.prepareStatement(queryDesc.getUpdateStatusQuery());

      ps.setLong(1, System.currentTimeMillis());
      ps.setString(2, RegistryEntryStatus.DELETED);
      ps.setLong(3, id);

      rowsUpdated = ps.executeUpdate();

    } catch (Exception ex) {

      throw new DataAccessException("Error marking entry with id '" + id + "' as deleted", ex);

    } finally {
      DBTools.close(con, ps, null);
    }
    return rowsUpdated;
  }
예제 #2
0
  /**
   * Reads active registry entries assigned to this server.
   *
   * @return the list of the active registry entries associated to this server
   * @throws com.funambol.pushlistener.service.registry.dao.DataAccessException if an error occurs
   */
  public List<RegistryEntry> getActiveEntries() throws DataAccessException {

    Connection con = null;
    PreparedStatement ps = null;
    ResultSet rs = null;

    List<RegistryEntry> entries = new ArrayList<RegistryEntry>();

    try {
      if (log.isTraceEnabled()) {
        log.trace("Executing '" + queryDesc.getReadActiveEntriesQuery() + "'");
      }

      con = getConnection();
      con.setReadOnly(true);

      ps = con.prepareStatement(queryDesc.getReadActiveEntriesQuery());

      ps.setString(1, "Y");
      rs = ps.executeQuery();

      while (rs.next()) {
        entries.add(resultSetToRegistryEntry(rs));
      }

    } catch (Exception e) {

      throw new DataAccessException(e);

    } finally {
      DBTools.close(con, ps, rs);
    }

    return entries;
  }
예제 #3
0
  /**
   * Returns the registry entry with the given id
   *
   * @return the read entry
   * @param id the id of the pim registry entry
   * @throws DataAccessException if an error occurs
   */
  public RegistryEntry getEntryById(long id) throws DataAccessException {

    Connection con = null;
    PreparedStatement ps = null;
    ResultSet rs = null;

    RegistryEntry entry = null;

    try {

      con = getConnection();
      con.setReadOnly(true);

      ps = con.prepareStatement(queryDesc.getReadEntryQuery());
      ps.setLong(1, id);
      rs = ps.executeQuery();

      while (rs.next()) {
        entry = resultSetToRegistryEntry(rs);
      }

    } catch (Exception e) {
      throw new DataAccessException("Error reading entry with id: " + id, e);
    } finally {
      DBTools.close(con, ps, rs);
    }

    return entry;
  }
예제 #4
0
  /**
   * Reads registry entries assigned to this server. If <code>lastUpdate</code> is different than 0,
   * just the changed entries with last_update bigger than the given one and the given status are
   * returned.
   *
   * @param lastUpdate the lastUpdate. 0 if all entries must be returned
   * @param status the wanted status
   * @throws com.funambol.pushlistener.service.registry.dao.DataAccessException if an error occurs
   * @return the read entries
   */
  public List<RegistryEntry> getEntries(long lastUpdate, String status) throws DataAccessException {

    Connection con = null;
    PreparedStatement ps = null;
    ResultSet rs = null;

    List<RegistryEntry> entries = new ArrayList<RegistryEntry>();

    try {

      con = getConnection();
      con.setReadOnly(true);

      if (lastUpdate != 0) {
        //
        // Read only changed registry entries
        //
        if (log.isTraceEnabled()) {
          log.trace("Executing '" + queryDesc.getReadChangedEntriesQuery() + "'");
        }

        ps = con.prepareStatement(queryDesc.getReadChangedEntriesQuery());
        ps.setLong(1, lastUpdate);
        ps.setString(2, status);

      } else {
        //
        // Read all registry entries
        //
        if (log.isTraceEnabled()) {
          log.trace("Executing '" + queryDesc.getReadActiveEntriesQuery() + "'");
        }

        ps = con.prepareStatement(queryDesc.getReadActiveEntriesQuery());
        ps.setString(1, "Y");
      }

      rs = ps.executeQuery();

      while (rs.next()) {
        entries.add(resultSetToRegistryEntry(rs));
      }

    } catch (Exception e) {

      throw new DataAccessException(e);

    } finally {
      DBTools.close(con, ps, rs);
    }

    return entries;
  }
예제 #5
0
 /**
  * This method is used to discover the extra properties defined in the push registry table.
  *
  * @param registryTableName
  * @throws DataAccessException
  */
 private void discoverExtraProperties(String registryTableName) throws DataAccessException {
   Connection connection = null;
   Statement statement = null;
   ResultSet rs = null;
   try {
     connection = this.getConnection();
     statement = connection.createStatement();
     String queryDiscoverExtraProperties =
         MessageFormat.format(SQL_DISCOVER_PROPERTIES, new Object[] {registryTableName});
     rs = statement.executeQuery(queryDiscoverExtraProperties);
     updateExtraPropertyInfo(rs);
   } catch (SQLException ex) {
     throw new DataAccessException(ex);
   } finally {
     DBTools.close(connection, statement, rs);
   }
 }
예제 #6
0
  /**
   * Inserts the given entry. If no error occurs, the entry id is returned and set as id in the
   * given entry object.
   *
   * @param entry the entry to updated
   * @return the entry id
   * @throws DataAccessException if an error occurs
   */
  public long insertRegistryEntry(RegistryEntry entry) throws DataAccessException {

    Connection con = null;
    PreparedStatement ps = null;

    long id = getNextEntryId();

    try {

      con = getConnection();

      ps = con.prepareStatement(queryDesc.getInsertEntryQuery());

      ps.setLong(1, id);
      ps.setLong(2, entry.getPeriod());
      ps.setString(3, entry.getActive() ? "Y" : "N");
      ps.setString(4, entry.getTaskBeanFile());
      ps.setLong(5, entry.getLastUpdate());
      ps.setString(6, RegistryEntryStatus.NEW);

      short numberOfExtraProperties = queryDesc.getNumberOfExtraColumns();
      String[] extraProperties = queryDesc.getExtraProperties();
      if (numberOfExtraProperties > 0) {
        for (short j = 0; j < numberOfExtraProperties; j++) {
          Object value = null;
          if (entry.hasProperty(extraProperties[j])) {
            value = entry.getProperty(extraProperties[j]);
          }
          ps.setObject(7 + j, value);
        }
      }

      ps.executeUpdate();

      entry.setId(id);

    } catch (Exception e) {

      throw new DataAccessException("Error inserting a registry entry", e);

    } finally {
      DBTools.close(con, ps, null);
    }

    return id;
  }
예제 #7
0
  /**
   * Updates the given entry. If no error occurs, the number of updated rows is returned.
   *
   * @param entry the entry to updated
   * @return the number of updated rows
   * @throws DataAccessException if an error occurs
   */
  public int updateRegistryEntry(RegistryEntry entry) throws DataAccessException {

    Connection con = null;
    PreparedStatement ps = null;
    int rowsUpdated = 0;

    try {

      con = getConnection();

      ps = con.prepareStatement(queryDesc.getUpdateEntryQuery());

      ps.setLong(1, entry.getPeriod());
      ps.setString(2, entry.getActive() ? "Y" : "N");
      ps.setString(3, entry.getTaskBeanFile());
      ps.setLong(4, entry.getLastUpdate());
      ps.setString(5, RegistryEntryStatus.UPDATED);

      short numberOfExtraProperties = queryDesc.getNumberOfExtraColumns();
      String[] extraProperties = queryDesc.getExtraProperties();
      if (numberOfExtraProperties > 0) {
        short j = 0;
        for (; j < numberOfExtraProperties; j++) {
          Object value = null;
          if (entry.hasProperty(extraProperties[j])) {
            value = entry.getProperty(extraProperties[j]);
          }
          ps.setObject(6 + j, value);
        }
        ps.setLong(6 + j, entry.getId());
      } else {
        ps.setLong(6, entry.getId());
      }
      rowsUpdated = ps.executeUpdate();

    } catch (Exception e) {

      throw new DataAccessException("Error updating registry entry '" + entry.getId() + "'", e);

    } finally {
      DBTools.close(con, ps, null);
    }
    return rowsUpdated;
  }
예제 #8
0
  /**
   * Removes the entry with status 'D' associated to this server
   *
   * @return the number of the removed entries
   * @throws com.funambol.pushlistener.service.registry.dao.DataAccessException if an error occurs
   */
  public int removeAllDeletedRegistryEntry() throws DataAccessException {

    Connection con = null;
    PreparedStatement ps = null;
    int rowsDeleted = 0;

    try {

      con = getConnection();

      ps = con.prepareStatement(queryDesc.getDeleteEntriesQuery());
      rowsDeleted = ps.executeUpdate();
      return rowsDeleted;

    } catch (Exception e) {

      throw new DataAccessException(e);

    } finally {
      DBTools.close(con, ps, null);
    }
  }
예제 #9
0
  /**
   * Deletes the entry with the given id
   *
   * @param id the id of the entry to delete
   * @return true if the entry is deleted, false otherwise
   * @throws com.funambol.pushlistener.service.registry.dao.DataAccessException if an error occurs
   */
  public boolean deleteRegistryEntry(long id) throws DataAccessException {

    Connection con = null;
    PreparedStatement ps = null;
    int rowsDeleted = 0;

    try {

      con = getConnection();

      ps = con.prepareStatement(queryDesc.getDeleteEntryQuery());
      ps.setLong(1, id);
      rowsDeleted = ps.executeUpdate();

      return (rowsDeleted > 0);

    } catch (Exception e) {

      throw new DataAccessException(e);

    } finally {
      DBTools.close(con, ps, null);
    }
  }