Exemplo n.º 1
0
  /**
   * Construct a DatabaseTableRecord whit the values of the a FermatMessage pass by parameter
   *
   * @param incomingTemplateNetworkServiceMessage the contains the values
   * @return DatabaseTableRecord whit the values
   */
  private DatabaseTableRecord constructFrom(FermatMessage incomingTemplateNetworkServiceMessage) {

    /*
     * Create the record to the entity
     */
    DatabaseTableRecord entityRecord = getDatabaseTable().getEmptyRecord();

    /*
     * Set the entity values
     */
    entityRecord.setStringValue(
        CommunicationNetworkServiceDatabaseConstants.INCOMING_MESSAGES_ID_COLUMN_NAME,
        incomingTemplateNetworkServiceMessage.getId().toString());
    entityRecord.setStringValue(
        CommunicationNetworkServiceDatabaseConstants.INCOMING_MESSAGES_SENDER_ID_COLUMN_NAME,
        incomingTemplateNetworkServiceMessage.getSender().toString());
    entityRecord.setStringValue(
        CommunicationNetworkServiceDatabaseConstants.INCOMING_MESSAGES_RECEIVER_ID_COLUMN_NAME,
        incomingTemplateNetworkServiceMessage.getReceiver().toString());
    entityRecord.setStringValue(
        CommunicationNetworkServiceDatabaseConstants.INCOMING_MESSAGES_TEXT_CONTENT_COLUMN_NAME,
        incomingTemplateNetworkServiceMessage.getContent());
    entityRecord.setStringValue(
        CommunicationNetworkServiceDatabaseConstants.INCOMING_MESSAGES_TYPE_COLUMN_NAME,
        incomingTemplateNetworkServiceMessage.getFermatMessageContentType().getCode());

    if (incomingTemplateNetworkServiceMessage.getShippingTimestamp() != null) {
      entityRecord.setLongValue(
          CommunicationNetworkServiceDatabaseConstants
              .OUTGOING_MESSAGES_SHIPPING_TIMESTAMP_COLUMN_NAME,
          incomingTemplateNetworkServiceMessage.getShippingTimestamp().getTime());
    } else {
      entityRecord.setLongValue(
          CommunicationNetworkServiceDatabaseConstants
              .OUTGOING_MESSAGES_SHIPPING_TIMESTAMP_COLUMN_NAME,
          new Long(0));
    }

    if (incomingTemplateNetworkServiceMessage.getDeliveryTimestamp() != null) {
      entityRecord.setLongValue(
          CommunicationNetworkServiceDatabaseConstants
              .INCOMING_MESSAGES_SHIPPING_TIMESTAMP_COLUMN_NAME,
          incomingTemplateNetworkServiceMessage.getDeliveryTimestamp().getTime());
    } else {
      entityRecord.setLongValue(
          CommunicationNetworkServiceDatabaseConstants
              .INCOMING_MESSAGES_DELIVERY_TIMESTAMP_COLUMN_NAME,
          new Long(0));
    }

    entityRecord.setStringValue(
        CommunicationNetworkServiceDatabaseConstants.INCOMING_MESSAGES_STATUS_COLUMN_NAME,
        incomingTemplateNetworkServiceMessage.getFermatMessagesStatus().getCode());

    /*
     * return the new table record
     */
    return entityRecord;
  }
  /**
   * Check fail send message
   *
   * @param destinationPublicKey
   */
  private void checkFailedSendMessage(String destinationPublicKey) {

    try {

      /*
       * Read all pending message from database
       */
      Map<String, Object> filters = new HashMap<>();
      filters.put(
          CommunicationNetworkServiceDatabaseConstants.OUTGOING_MESSAGES_RECEIVER_ID_COLUMN_NAME,
          destinationPublicKey);
      filters.put(
          CommunicationNetworkServiceDatabaseConstants.OUTGOING_MESSAGES_STATUS_COLUMN_NAME,
          MessagesStatus.PENDING_TO_SEND.getCode());
      List<FermatMessage> messages =
          getCommunicationNetworkServiceConnectionManager()
              .getOutgoingMessageDao()
              .findAll(filters);

      for (FermatMessage fermatMessage : messages) {

        /*
         * Increment the fail count field
         */
        FermatMessageCommunication fermatMessageCommunication =
            (FermatMessageCommunication) fermatMessage;
        fermatMessageCommunication.setFailCount(fermatMessageCommunication.getFailCount() + 1);

        if (fermatMessageCommunication.getFailCount() > 10) {

          /*
           * Calculate the date
           */
          long sentDate = fermatMessageCommunication.getShippingTimestamp().getTime();
          long currentTime = System.currentTimeMillis();
          long dif = currentTime - sentDate;
          double dias = Math.floor(dif / (1000 * 60 * 60 * 24));

          /*
           * if have mora that 3 days
           */
          if ((int) dias > 3) {
            getCommunicationNetworkServiceConnectionManager()
                .getOutgoingMessageDao()
                .delete(fermatMessage.getId());
          }
        } else {
          getCommunicationNetworkServiceConnectionManager()
              .getOutgoingMessageDao()
              .update(fermatMessage);
        }
      }

    } catch (Exception e) {
      e.printStackTrace();
    }
  }
Exemplo n.º 3
0
  /**
   * Method that update an entity in the data base.
   *
   * @param entity FermatMessage to update.
   * @throws CantUpdateRecordDataBaseException
   */
  public void update(FermatMessage entity) throws CantUpdateRecordDataBaseException {

    if (entity == null) {
      throw new IllegalArgumentException("The entity is required, can not be null");
    }

    try {

      /*
       * 1- Create the record to the entity
       */
      DatabaseTableRecord entityRecord = constructFrom(entity);

      /*
       * 2.- Create a new transaction and execute
       */
      DatabaseTable outgoinMessageTable = getDatabaseTable();
      DatabaseTransaction transaction = getDataBase().newTransaction();

      // set filter
      outgoinMessageTable.addUUIDFilter(
          CommunicationNetworkServiceDatabaseConstants.OUTGOING_MESSAGES_ID_COLUMN_NAME,
          entity.getId(),
          DatabaseFilterType.EQUAL);

      transaction.addRecordToUpdate(outgoinMessageTable, entityRecord);
      getDataBase().executeTransaction(transaction);

    } catch (DatabaseTransactionFailedException databaseTransactionFailedException) {

      StringBuffer contextBuffer = new StringBuffer();
      contextBuffer.append(
          "Database Name: " + CommunicationNetworkServiceDatabaseConstants.DATA_BASE_NAME);

      String context = contextBuffer.toString();
      String possibleCause = "The record do not exist";
      CantUpdateRecordDataBaseException cantUpdateRecordDataBaseException =
          new CantUpdateRecordDataBaseException(
              CantUpdateRecordDataBaseException.DEFAULT_MESSAGE,
              databaseTransactionFailedException,
              context,
              possibleCause);
      throw cantUpdateRecordDataBaseException;
    }
  }
Exemplo n.º 4
0
  /**
   * Method that create a new entity in the data base.
   *
   * @param entity FermatMessage to create.
   * @throws CantInsertRecordDataBaseException
   */
  public void create(FermatMessage entity) throws CantInsertRecordDataBaseException {

    if (entity == null) {
      throw new IllegalArgumentException("The entity is required, can not be null");
    }

    try {

      if (findById(String.valueOf(entity.getId())) == null) {
        /*
         * 1- Create the record to the entity
         */
        DatabaseTableRecord entityRecord = constructFrom(entity);

        /** 2.- Create a new transaction and execute */
        DatabaseTransaction transaction = getDataBase().newTransaction();
        transaction.addRecordToInsert(getDatabaseTable(), entityRecord);
        getDataBase().executeTransaction(transaction);
      }

    } catch (DatabaseTransactionFailedException databaseTransactionFailedException) {

      // Register the failure.
      StringBuffer contextBuffer = new StringBuffer();
      contextBuffer.append(
          "Database Name: " + CommunicationNetworkServiceDatabaseConstants.DATA_BASE_NAME);

      String context = contextBuffer.toString();
      String possibleCause =
          "The Template Database triggered an unexpected problem that wasn't able to solve by itself";
      CantInsertRecordDataBaseException cantInsertRecordDataBaseException =
          new CantInsertRecordDataBaseException(
              CantInsertRecordDataBaseException.DEFAULT_MESSAGE,
              databaseTransactionFailedException,
              context,
              possibleCause);
      throw cantInsertRecordDataBaseException;
    } catch (CantReadRecordDataBaseException e) {
      CantInsertRecordDataBaseException cantInsertRecordDataBaseException =
          new CantInsertRecordDataBaseException(
              CantInsertRecordDataBaseException.DEFAULT_MESSAGE, e, "", "Cant Get record");
      throw cantInsertRecordDataBaseException;
    }
  }