Package org.openmhealth.reference.exception

Examples of org.openmhealth.reference.exception.OmhException


    final ThirdParty thirdParty)
    throws OmhException {
   
    // Validate the parameter.
    if(thirdParty == null) {
      throw new OmhException("The third-party is null.");
    }
   
    // Get the connection to the third-party bin with the Jackson wrapper.
    JacksonDBCollection<ThirdParty, Object> collection =
      JacksonDBCollection
        .wrap(
          MongoDao
            .getInstance()
            .getDb()
            .getCollection(DB_NAME),
          ThirdParty.class);
   
    // Save it.
    try {
      collection.insert(thirdParty);
    }
    catch(MongoException.DuplicateKey e) {
      throw
        new OmhException(
          "A third-party with the given ID already exists.");
    }
  }
View Full Code Here


   
    // If multiple authentication tokens were returned, that is a violation
    // of the system.
    if(result.count() > 1) {
      throw
        new OmhException(
          "Multiple third-parties have the same ID: " + thirdParty);
    }
   
    // If no third-parties were returned, then return null.
    if(result.count() == 0) {
View Full Code Here

   */
  @Override
  public void storeData(final List<Data> data) {
    // Validate the parameter.
    if(data == null) {
      throw new OmhException("The token is null.");
    }
   
    // Get the DAO.
    SqlDao dao = SqlDao.getInstance();

    // Get the transaction manager.
    PlatformTransactionManager transactionManager =
      dao.getTransactionManager();
   
    // Create a new transaction definition and name it.
    DefaultTransactionDefinition transactionDefinition =
      new DefaultTransactionDefinition();
    transactionDefinition.setName("Adding a data point.");
   
    // Create the new transaction.
    TransactionStatus transactionStatus =
      transactionManager.getTransaction(transactionDefinition);
   
    // Get the JDBC template.
    JdbcTemplate jdbcTemplate = dao.getJdbcTemplate();
   
    // Create the list of points to be inserted into the database.
    List<Object[]> points = new ArrayList<Object[]>(data.size());
   
    // Add each of the points to the array.
    try {
      for(Data point : data) {
        Object[] pointData = new Object[6];
        pointData[0] = point.getOwner();
        pointData[1] = point.getSchemaId();
        pointData[2] = point.getSchemaVersion();
       
        MetaData metaData = point.getMetaData();
        if(metaData == null) {
          pointData[3] = null;
          pointData[4] = null;
        }
        else {
          pointData[3] = metaData.getId();
          pointData[4] =
            ISOW3CDateTimeFormat
              .any()
              .print(metaData.getTimestamp());
        }
       
        pointData[5] = JSON_MAPPER.writeValueAsString(point.getData());
       
        points.add(pointData);
      }
    }
    catch(JsonProcessingException e) {
      throw new OmhException("Could not convert some data to JSON.", e);
    }
   
    // Add the data.
    try {
      jdbcTemplate
        .batchUpdate(
          "INSERT INTO " + DataSet.DB_NAME + " (" +
              UserBin.DB_NAME + "_id" + ", " +
              Registry.DB_NAME + "_id" + ", " +
              Data.JSON_KEY_METADATA + "_" +
                MetaData.JSON_KEY_ID + ", " +
              Data.JSON_KEY_METADATA + "_" +
                MetaData.JSON_KEY_TIMESTAMP + ", " +
              Data.JSON_KEY_DATA + " " +
            ") VALUES (" +
              "(" +
                "SELECT " + SqlDao.KEY_DATABASE_ID + " " +
                "FROM " + UserBin.DB_NAME + " " +
                "WHERE " + User.JSON_KEY_USERNAME + " = ?" +
              "), " +
              "(" +
                "SELECT " + SqlDao.KEY_DATABASE_ID + " " +
                "FROM " + Registry.DB_NAME + " " +
                "WHERE " + Schema.JSON_KEY_ID + " = ? " +
                "AND " + Schema.JSON_KEY_VERSION + " = ?" +
              "), " +
              "?, " +
              "?, " +
              "?" +
            ")",
          points);
     
      // Commit the transaction.
      transactionManager.commit(transactionStatus);
    }
    catch(DataAccessException e) {
      transactionManager.rollback(transactionStatus);
      throw new OmhException("There was a problem storing the data.", e);
    }
  }
View Full Code Here

    final long numToSkip,
    final long numToReturn) {
   
    // Validate the parameters.
    if(owner == null) {
      throw new OmhException("The data is null.");
    }
    else if(schemaId == null) {
      throw new OmhException("The schema ID is null.");
    }
   
    // Retrieve the list of results.
    List<Data> list;
    try {
      list =
        SqlDao
          .getInstance()
          .getJdbcTemplate()
          .query(
            // Get the required columns to build the object.
            "SELECT " +
                User.JSON_KEY_USERNAME + ", " +
                Schema.JSON_KEY_ID + ", " +
                Schema.JSON_KEY_VERSION + ", " +
                Data.JSON_KEY_METADATA + "_" +
                  MetaData.JSON_KEY_ID + ", " +
                Data.JSON_KEY_METADATA + "_" +
                  MetaData.JSON_KEY_TIMESTAMP + ", " +
                Data.JSON_KEY_DATA + " " +
              // Include all of the required tables.
              "FROM " +
                UserBin.DB_NAME + ", " +
                Registry.DB_NAME + ", " +
                DataSet.DB_NAME + " " +
              // Link the user table to the data table.
              "WHERE " +
                  UserBin.DB_NAME +
                  "." +
                  SqlDao.KEY_DATABASE_ID +
                " = " +
                  DataSet.DB_NAME +
                  "." +
                  UserBin.DB_NAME + "_id " +
              // Limit the results based on the required
              // username.
              "AND " + User.JSON_KEY_USERNAME + " = ? " +
              // Link the registry table to the data table.
              "AND " +
                  Registry.DB_NAME +
                  "." +
                  SqlDao.KEY_DATABASE_ID +
                " = " +
                  DataSet.DB_NAME +
                  "." +
                  Registry.DB_NAME + "_id " +
              // Limit the results based on the required schema
              // ID and version.
              "AND " + Schema.JSON_KEY_ID + " = ? " +
              "AND " + Schema.JSON_KEY_VERSION + " = ? " +
              "LIMIT ?, ?",
            new Object[] {
              owner,
              schemaId,
              version,
              numToSkip,
              numToReturn },
            new RowMapper<Data>() {
              /**
               * Maps the row to a {@link Data} object.
               */
              @Override
              public Data mapRow(
                final ResultSet resultSet,
                final int rowNum)
                throws SQLException {
               
                // Get the username.
                String username =
                  resultSet
                    .getString(User.JSON_KEY_USERNAME);
                // Get the schema's ID.
                String id =
                  resultSet.getString(Schema.JSON_KEY_ID);
                // Get the stream's version.
                long version =
                  resultSet.getLong(Schema.JSON_KEY_VERSION);
               
                // Build the meta-data.
                MetaData.Builder metaDataBuilder =
                  new MetaData.Builder();
                // Get and set the ID, even if it is null.
                metaDataBuilder
                  .setId(
                    resultSet
                      .getString(
                        Data.JSON_KEY_METADATA +
                          "_" +
                          MetaData.JSON_KEY_ID));
                // Get the timestamp.
                String metaDataTimestampString =
                  resultSet
                    .getString(
                      Data.JSON_KEY_METADATA +
                        "_" +
                        MetaData.JSON_KEY_TIMESTAMP);
                // If the timestamp is not null, decode it and
                // set it.
                if(metaDataTimestampString != null) {
                  metaDataBuilder
                    .setTimestamp(
                      ISOW3CDateTimeFormat
                        .any()
                        .parseDateTime(
                          metaDataTimestampString));
                }
                // If the builder has no non-null members,
                // create a MetaData object; otherwise, just
                // leave it as null.
                MetaData metaData =
                  ((metaDataBuilder.isNull()) ?
                    null :
                    metaDataBuilder.build());
               
                // Get the data.
                JsonNode data;
                try {
                  data =
                    JSON_MAPPER
                      .readTree(
                        resultSet
                          .getString(
                            Data.JSON_KEY_DATA));
                }
                catch(IOException e) {
                  throw
                    new SQLException(
                      "Error decoding the data.",
                      e);
                }
                // FIXME: Apply the column list.
               
                // Create a Data object and return it.
                return
                  new Data(
                    username,
                    id,
                    version,
                    metaData,
                    data);
              }
            });
    }
    // For all issues, we simply propagate the exception.
    catch(DataAccessException e) {
      throw
        new OmhException(
          "There was an error querying for schemas.",
          e);
    }
   
    // Retrieve the total count of results.
    int count;
    try {
      count =
        SqlDao
          .getInstance()
          .getJdbcTemplate()
          .queryForInt(
            "SELECT COUNT(1) " +
              // Include all of the required tables.
              "FROM " +
                UserBin.DB_NAME + ", " +
                Registry.DB_NAME + ", " +
                DataSet.DB_NAME + " " +
              // Link the user table to the data table.
              "WHERE " +
                  UserBin.DB_NAME +
                  "." +
                  SqlDao.KEY_DATABASE_ID +
                " = " +
                  DataSet.DB_NAME +
                  "." +
                  UserBin.DB_NAME + "_id " +
              // Limit the results based on the required
              // username.
              "AND " + User.JSON_KEY_USERNAME + " = ? " +
              // Link the registry table to the data table.
              "AND " +
                  Registry.DB_NAME +
                  "." +
                  SqlDao.KEY_DATABASE_ID +
                " = " +
                  DataSet.DB_NAME +
                  "." +
                  Registry.DB_NAME + "_id " +
              // Limit the results based on the required schema
              // ID and version.
              "AND " + Schema.JSON_KEY_ID + " = ? " +
              "AND " + Schema.JSON_KEY_VERSION + " = ? ",
            new Object[] { owner, schemaId, version });
    }
    // For all issues, we simply propagate the exception.
    catch(DataAccessException e) {
      throw
        new OmhException(
          "There was an error querying for schemas count.",
          e);
    }
   
    return new SqlMultiValueResult<Data>(list, count);
View Full Code Here

    final AuthenticationToken token)
    throws OmhException {
   
    // Validate the parameter.
    if(token == null) {
      throw new OmhException("The token is null.");
    }
   
    // Get the authentication token collection.
    JacksonDBCollection<AuthenticationToken, Object> collection =
      JacksonDBCollection
        .wrap(
          MongoDao
            .getInstance()
            .getDb()
            .getCollection(DB_NAME),
          AuthenticationToken.class);
   
    // Make sure the token doesn't already exist.
    if(collection
      .count(
        new BasicDBObject(
          AuthenticationToken.JSON_KEY_TOKEN,
          token.getToken())) > 0) {
     
      throw new OmhException("The token already exists.");
    }
   
    // Save it.
    collection.insert(token);
  }
View Full Code Here

   
    // If multiple authentication tokens were returned, that is a violation
    // of the system.
    if(result.count() > 1) {
      throw
        new OmhException(
          "Multiple copies of the same authentication token " +
            "exist: " +
            token);
    }
   
View Full Code Here

            new SingleColumnRowMapper<String>());
    }
    // For all issues, we simply propagate the exception.
    catch(DataAccessException e) {
      throw
        new OmhException(
          "There was an error querying for the schema IDs.",
          e);
    }
   
    // Retrieve the total count of results.
    int count;
    try {
      count =
        SqlDao
          .getInstance()
          .getJdbcTemplate()
          .queryForInt(
            "SELECT COUNT(" + SqlDao.KEY_DATABASE_ID + ") " +
              "FROM " + Registry.DB_NAME);
    }
    // If the problem is that the number of results isn't what we expected,
    // we may still be alright.
    catch(IncorrectResultSizeDataAccessException e) {     
      // Otherwise, we throw an exception.
      throw
        new OmhException(
          "A count query returned more than one result.",
          e);
    }
    // For all other issues, we simply propagate the exception.
    catch(DataAccessException e) {
      throw
        new OmhException(
          "There was an error querying for the schema IDs count.",
          e);
    }
   
    // Return the result
View Full Code Here

    final long numToSkip,
    final long numToReturn) {
   
    // Validate the schema ID.
    if(schemaId == null) {
      throw new OmhException("The schema ID is null.");
    }
   
    // Retrieve the list of results.
    List<Long> list;
    try {
      list =
        SqlDao
          .getInstance()
          .getJdbcTemplate()
          .query(
            "SELECT DISTINCT(" + Schema.JSON_KEY_VERSION + ") " +
              "FROM " + Registry.DB_NAME + " " +
              "WHERE " + Schema.JSON_KEY_ID + " = ? " +
              "ORDER BY " + Schema.JSON_KEY_VERSION + " " +
              "LIMIT ?, ?",
            new Object[] { schemaId, numToSkip, numToReturn },
            new SingleColumnRowMapper<Long>());
    }
    // For all issues, we simply propagate the exception.
    catch(DataAccessException e) {
      throw
        new OmhException(
          "There was an error querying for the schema versions.",
          e);
    }
   
    // Retrieve the total count of results.
    int count;
    try {
      count =
        SqlDao
          .getInstance()
          .getJdbcTemplate()
          .queryForInt(
            "SELECT COUNT(" + SqlDao.KEY_DATABASE_ID + ") " +
              "FROM " + Registry.DB_NAME + " " +
              "WHERE " + Schema.JSON_KEY_ID + " = ?",
            new Object[] { schemaId });
    }
    // For all issues, we simply propagate the exception.
    catch(DataAccessException e) {
      throw
        new OmhException(
          "There was an error querying for schema versions count.",
          e);
    }
   
    return new SqlMultiValueResult<Long>(list, count);
View Full Code Here

   * (non-Javadoc)
   * @see org.openmhealth.reference.data.Registry#getSchema(java.lang.String, long)
   */
  public Schema getSchema(final String schemaId, final long schemaVersion) {
    if(schemaId == null) {
      throw new OmhException("The schema ID is null.");
    }
   
    try {
      return
        SqlDao
          .getInstance()
          .getJdbcTemplate()
          .queryForObject(
            "SELECT " +
                Schema.JSON_KEY_ID + ", " +
                Schema.JSON_KEY_VERSION + ", " +
                "`" + Schema.JSON_KEY_SCHEMA + "` " +
              "FROM " +
                Registry.DB_NAME + " " +
              "WHERE " + Schema.JSON_KEY_ID + " = ? " +
              "AND " + Schema.JSON_KEY_VERSION + " = ?",
            new Object[] { schemaId, schemaVersion },
            new RowMapper<Schema>() {
              /**
               * Maps the row to an {@link Schema} object.
               */
              @Override
              public Schema mapRow(
                final ResultSet resultSet,
                final int rowNum)
                throws SQLException {
               
                String id =
                  resultSet.getString(Schema.JSON_KEY_ID);
                long version =
                  resultSet.getLong(Schema.JSON_KEY_VERSION);
                JsonNode schema;
                try {
                  schema = JSON_MAPPER.
                    readTree(
                      resultSet
                        .getString(
                          Schema.JSON_KEY_SCHEMA));
                }
                catch(IOException e) {
                  throw
                    new SQLException(
                      "Error reading the schema.",
                      e);
                }
               
                return
                  new Schema(
                    id,
                    version,
                    schema,
                    OmhValidationController
                      .VALIDATION_CONTROLLER);
              }
            });
    }
    // If the problem is that the number of results isn't what we expected,
    // we may still be alright.
    catch(IncorrectResultSizeDataAccessException e) {
      // If there weren't any tokens with the given token value, then we
      // simply return null.
      if(e.getActualSize() == 0) {
        return null;
      }
     
      // Otherwise, we throw an exception.
      throw
        new OmhException(
          "Multiple schemas have the same ID-version pair: " +
            schemaId + ", " +
            schemaVersion,
          e);
    }
    // For all other issues, we simply propagate the exception.
    catch(DataAccessException e) {
      throw
        new OmhException(
          "There was an error querying for a schema.",
          e);
    }
  }
View Full Code Here

TOP

Related Classes of org.openmhealth.reference.exception.OmhException

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.