Search in sources :

Example 1 with DatasetMetadata

use of com.amazonaws.mobileconnectors.cognito.DatasetMetadata in project amazon-cognito-android by aws.

the class CognitoSyncStorage method getDatasets.

/*
     * (non-Javadoc)
     * @see com.amazonaws.cognitov2.RemoteStorage#listDatasets()
     */
@Override
public List<DatasetMetadata> getDatasets() {
    List<DatasetMetadata> datasets = new ArrayList<DatasetMetadata>();
    String nextToken = null;
    do {
        ListDatasetsRequest request = new ListDatasetsRequest();
        appendUserAgent(request, userAgent);
        request.setIdentityPoolId(identityPoolId);
        // a large enough number to reduce # of requests
        request.setMaxResults("64");
        request.setNextToken(nextToken);
        ListDatasetsResult result = null;
        try {
            request.setIdentityId(getIdentityId());
            result = client.listDatasets(request);
        } catch (AmazonClientException ace) {
            throw handleException(ace, "Failed to list dataset metadata");
        }
        for (com.amazonaws.services.cognitosync.model.Dataset dataset : result.getDatasets()) {
            datasets.add(modelToDatasetMetadata(dataset));
        }
        nextToken = result.getNextToken();
    } while (nextToken != null);
    return datasets;
}
Also used : DatasetMetadata(com.amazonaws.mobileconnectors.cognito.DatasetMetadata) AmazonClientException(com.amazonaws.AmazonClientException) ArrayList(java.util.ArrayList) ListDatasetsResult(com.amazonaws.services.cognitosync.model.ListDatasetsResult) ListDatasetsRequest(com.amazonaws.services.cognitosync.model.ListDatasetsRequest)

Example 2 with DatasetMetadata

use of com.amazonaws.mobileconnectors.cognito.DatasetMetadata in project amazon-cognito-android by aws.

the class SQLiteLocalStorage method getDatasetMetadata.

@Override
public DatasetMetadata getDatasetMetadata(String identityId, String datasetName) throws DataStorageException {
    DatasetMetadata dataset = null;
    SQLiteDatabase db = helper.getWritableDatabase();
    Cursor c = db.query(TABLE_DATASETS, DatasetColumns.ALL, DatasetColumns.IDENTITY_ID + " = ? AND " + DatasetColumns.DATASET_NAME + " = ?", new String[] { identityId, datasetName }, null, null, null);
    if (c.moveToFirst()) {
        dataset = cursorToDatasetMetadata(c);
    }
    c.close();
    return dataset;
}
Also used : SQLiteDatabase(android.database.sqlite.SQLiteDatabase) DatasetMetadata(com.amazonaws.mobileconnectors.cognito.DatasetMetadata) Cursor(android.database.Cursor)

Example 3 with DatasetMetadata

use of com.amazonaws.mobileconnectors.cognito.DatasetMetadata in project amazon-cognito-android by aws.

the class SQLiteLocalStorage method getCommonDatasetNames.

/**
 * Checks the metadata of all datasets for one identity id and another to
 * see if any have the same name.
 *
 * @param oldIdentityId the old identity id, which the user is changing from
 * @param newIdentityId the new identity id, which the user is changing to
 * @return a set of all of the non unique names
 */
Set<String> getCommonDatasetNames(String oldIdentityId, String newIdentityId) {
    Set<String> newNameSet = new HashSet<String>();
    Set<String> oldNameSet = new HashSet<String>();
    if (oldIdentityId != null && newIdentityId != null) {
        List<DatasetMetadata> newDatasets = getDatasets(newIdentityId);
        List<DatasetMetadata> oldDatasets = getDatasets(oldIdentityId);
        for (DatasetMetadata oldMetaData : oldDatasets) {
            oldNameSet.add(oldMetaData.getDatasetName());
        }
        for (DatasetMetadata newMetaData : newDatasets) {
            newNameSet.add(newMetaData.getDatasetName());
        }
        oldNameSet.retainAll(newNameSet);
    }
    return oldNameSet;
}
Also used : DatasetMetadata(com.amazonaws.mobileconnectors.cognito.DatasetMetadata) HashSet(java.util.HashSet)

Example 4 with DatasetMetadata

use of com.amazonaws.mobileconnectors.cognito.DatasetMetadata in project amazon-cognito-android by aws.

the class SQLiteLocalStorage method createDataset.

/**
 * Creates a new {@link Dataset}. Stores its information in datasets table.
 * Nothing will happen if a dataset with the same name exists. This should
 * be called before any operation related to this dataset name is performed.
 * Otherwise this dataset won't be visible in listDatasets and will result
 * in dangled records.
 *
 * @param identityId identity id
 * @param datasetName dataset name
 */
public void createDataset(String identityId, String datasetName) {
    SQLiteDatabase db = helper.getWritableDatabase();
    db.beginTransaction();
    try {
        // check if the dataset exists or not
        DatasetMetadata metadata = getMetadataInternal(db, identityId, datasetName);
        if (metadata == null) {
            // add a new entry if it doesn't exist
            ContentValues values = new ContentValues();
            values.put(DatasetColumns.IDENTITY_ID, identityId);
            values.put(DatasetColumns.DATASET_NAME, datasetName);
            values.put(DatasetColumns.CREATION_TIMESTAMP, String.valueOf(new Date().getTime()));
            values.put(DatasetColumns.LAST_MODIFIED_TIMESTAMP, String.valueOf(new Date().getTime()));
            // use default values for other fields
            long row = db.insert(TABLE_DATASETS, null, values);
            if (row == -1) {
                Log.e(TAG, String.format("couldn't create dataset %s", datasetName));
            }
        }
        db.setTransactionSuccessful();
    } finally {
        db.endTransaction();
    }
}
Also used : ContentValues(android.content.ContentValues) SQLiteDatabase(android.database.sqlite.SQLiteDatabase) DatasetMetadata(com.amazonaws.mobileconnectors.cognito.DatasetMetadata) Date(java.util.Date)

Example 5 with DatasetMetadata

use of com.amazonaws.mobileconnectors.cognito.DatasetMetadata in project amazon-cognito-android by aws.

the class CognitoSyncStorage method getDatasetMetadata.

@Override
public DatasetMetadata getDatasetMetadata(String datasetName) throws DataStorageException {
    DescribeDatasetRequest request = new DescribeDatasetRequest();
    appendUserAgent(request, userAgent);
    request.setIdentityPoolId(identityPoolId);
    DatasetMetadata dataset = null;
    try {
        request.setIdentityId(getIdentityId());
        request.setDatasetName(datasetName);
        DescribeDatasetResult result = client.describeDataset(request);
        dataset = modelToDatasetMetadata(result.getDataset());
    } catch (AmazonClientException ace) {
        throw handleException(ace, "Failed to get metadata of dataset: " + datasetName);
    }
    return dataset;
}
Also used : DescribeDatasetRequest(com.amazonaws.services.cognitosync.model.DescribeDatasetRequest) DatasetMetadata(com.amazonaws.mobileconnectors.cognito.DatasetMetadata) AmazonClientException(com.amazonaws.AmazonClientException) DescribeDatasetResult(com.amazonaws.services.cognitosync.model.DescribeDatasetResult)

Aggregations

DatasetMetadata (com.amazonaws.mobileconnectors.cognito.DatasetMetadata)9 SQLiteDatabase (android.database.sqlite.SQLiteDatabase)4 Cursor (android.database.Cursor)3 ContentValues (android.content.ContentValues)2 AmazonClientException (com.amazonaws.AmazonClientException)2 ArrayList (java.util.ArrayList)2 DescribeDatasetRequest (com.amazonaws.services.cognitosync.model.DescribeDatasetRequest)1 DescribeDatasetResult (com.amazonaws.services.cognitosync.model.DescribeDatasetResult)1 ListDatasetsRequest (com.amazonaws.services.cognitosync.model.ListDatasetsRequest)1 ListDatasetsResult (com.amazonaws.services.cognitosync.model.ListDatasetsResult)1 Date (java.util.Date)1 HashSet (java.util.HashSet)1