Android Open Source - bv-android-sdk Bazaar Functions






From Project

Back to project page bv-android-sdk.

License

The source code is released under:

Apache License

If you think the Android project bv-android-sdk 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.bazaarvoice.example.reviewsubmission;
/*w ww  .  j  av  a 2 s .  c o m*/
import java.io.File;

import org.json.JSONException;
import org.json.JSONObject;

import android.graphics.Bitmap;
import android.util.Log;

import com.bazaarvoice.types.*;
import com.bazaarvoice.BazaarRequest;
import com.bazaarvoice.OnBazaarResponse;
import com.bazaarvoice.SubmissionMediaParams;
import com.bazaarvoice.SubmissionParams;
import com.bazaarvoice.types.ApiVersion;

/**
 * BazaarFunctions.java <br>
 * ReviewSubmissionExample<br>
 * 
 * This is a suite of functions that leverage the BazaarvoiceSDK. This class
 * consolidates the usage of these functions for easier understanding of how to
 * use the SDK.
 * 
 * <p>
 * Created on 6/29/12. Copyright (c) 2012 BazaarVoice. All rights reserved.
 * 
 * @author Bazaarvoice Engineering
 * 
 */
public class BazaarFunctions {

  private static final String TAG = "BazaarFunctions";
  private static final String API_URL = "reviews.apitestcustomer.bazaarvoice.com/bvstaging";
  private static final String API_KEY = "2cpdrhohmgmwfz8vqyo48f52g";
  private static final ApiVersion API_VERSION = ApiVersion.FIVE_FOUR;
  private static final int MIN_IMAGE_DIMENSIONS = 600;


  public static String photoUrl = "";

  /**
   * A wrapper for uploadPhoto() that doesn't pass a callback function for
   * upload completion.
   * 
   * @param file
   *            the file location of the photo
   */
  public static void uploadPhoto(File file) {
    uploadPhoto(file, null);
  }

  /**
   * Actually sends the photo upload request -- this is a helper function for uploadPhoto.
   * 
   * @param params
   *            the params of the request
   * @param listener
   *            an <code>OnImageUploadComplete</code> object
   */
  private static void sendPhotoRequest(SubmissionMediaParams params, final OnImageUploadComplete listener){
    OnBazaarResponse response = new OnBazaarResponse() {

      @Override
      public void onException(String message, Throwable exception) {
        Log.e(TAG,
            "Error = " + message + "\n"
                + Log.getStackTraceString(exception));
      }

      @Override
      public void onResponse(JSONObject json) {
        Log.i(TAG, "Response = \n" + json);
        if (listener != null) {
          listener.onFinish();
        }
        try {
          photoUrl = json.getJSONObject("Photo")
              .getJSONObject("Sizes").getJSONObject("normal")
              .getString("Url");
        } catch (JSONException exception) {
          Log.e(TAG, Log.getStackTraceString(exception));
        }
      }

    };

    BazaarRequest submitMedia = new BazaarRequest(API_URL, API_KEY,
        API_VERSION);
    submitMedia.postSubmission(RequestType.PHOTOS, params, response);
  }
  
  /**
   * Creates a request to upload a photo to the Bazaarvoice image store for
   * use with a review. This function also allows you to pass a callback
   * function to be called when the upload completes.
   * 
   * @param file
   *            the file location of the photo
   * @param listener
   *            an <code>OnImageUploadComplete</code> object
   */
  public static void uploadPhoto(File file,
      final OnImageUploadComplete listener) {
    try {
      SubmissionMediaParams params = new SubmissionMediaParams(MediaParamsContentType.REVIEW);
      params.setPhoto(file);
      params.setUserId("test1");

      sendPhotoRequest(params, listener);

    } catch (Exception exception) {
      Log.e(TAG, Log.getStackTraceString(exception));
    }
  }

  /**
   * Creates a request to upload a photo to the Bazaarvoice image store for
   * use with a review. This function also allows you to pass a callback
   * function to be called when the upload completes.
   * 
   * @param bitmap
   *            the bitmap representation of the image to upload
   * @param filename
   *            the filename of the photo to upload -- this is necessary to determine mime type
   */
  public static void uploadPhoto(Bitmap bitmap, String filenname, final OnImageUploadComplete listener) {
    try {
      SubmissionMediaParams params = new SubmissionMediaParams(MediaParamsContentType.REVIEW);
      if(bitmap.getHeight() < MIN_IMAGE_DIMENSIONS || bitmap.getWidth() < MIN_IMAGE_DIMENSIONS){
        float scale = Math.max(MIN_IMAGE_DIMENSIONS / (float)  bitmap.getHeight(), MIN_IMAGE_DIMENSIONS / (float) bitmap.getWidth());
        bitmap = Bitmap.createScaledBitmap(bitmap, (int)(bitmap.getWidth() * scale), (int)(bitmap.getHeight() * scale), true);
      }
      
      params.setPhoto(bitmap, filenname);
      params.setUserId("test1");

      sendPhotoRequest(params, listener);
    } catch (Exception exception) {
      Log.e(TAG, Log.getStackTraceString(exception));
    }
  }

  
  /**
   * Submits the given review for the given product as a preview. This means
   * that it will not actually be submitted but will be tested against the API
   * and any errors will be reported.
   * 
   * @param prodId
   *            the product ID
   * @param review
   *            the full review
   * @param listener
   *            the callback function for handling the response
   */
  public static void previewReview(String prodId, BazaarReview review,
      OnBazaarResponse listener) {
    reviewAction(prodId, review, listener, false);
  }

  /**
   * Submits the given review for the given product as a submission. This
   * means that it will be entered into the system and be ready for display
   * soon.
   * 
   * @param prodId
   *            the product ID
   * @param review
   *            the full review
   * @param listener
   *            the callback function for handling the response
   */
  public static void submitReview(String prodId, BazaarReview review,
      OnBazaarResponse listener) {
    reviewAction(prodId, review, listener, true);
  }

  /**
   * Builds a review request and sends it off as either a preview or a
   * submission.
   * 
   * @param prodId
   *            the product ID
   * @param review
   *            the full review
   * @param listener
   *            the callback function for handling the response
   * @param submit
   *            true to submit, false to preview
   */
  private static void reviewAction(String prodId, BazaarReview review,
      OnBazaarResponse listener, boolean submit) {
    SubmissionParams params = new SubmissionParams();
    if (submit)
      params.setAction(Action.SUBMIT);
    else
      params.setAction(Action.PREVIEW);

    params.setProductId(prodId);
    params.setRating(review.getRating());
    params.setReviewText(review.getReviewText());
    params.setTitle(review.getTitle());
    params.setUserNickname(review.getNickname());
    params.setUserEmail("bv@bv.com");

    if (!photoUrl.equals(""))
      params.addPhotoUrl(photoUrl);

    if (!review.getAuthorId().equals("null"))
      params.setUserId(review.getAuthorId());
    else if ( !(review.getNickname().equals("null") || "".equals(review.getNickname().trim())) )
      params.setUserId(review.getNickname());
    else
      params.setUserId("Anonymous");

    BazaarRequest submission = new BazaarRequest(API_URL, API_KEY,
        API_VERSION);
    submission.postSubmission(RequestType.REVIEWS, params, listener);
  }

}




Java Source Code List

com.bazaarvoice.BazaarException.java
com.bazaarvoice.BazaarParams.java
com.bazaarvoice.BazaarRequest.java
com.bazaarvoice.DisplayParams.java
com.bazaarvoice.Media.java
com.bazaarvoice.OnBazaarResponse.java
com.bazaarvoice.SubmissionMediaParams.java
com.bazaarvoice.SubmissionParams.java
com.bazaarvoice.example.browseproducts.BazaarFunctions.java
com.bazaarvoice.example.browseproducts.BazaarProduct.java
com.bazaarvoice.example.browseproducts.BazaarReview.java
com.bazaarvoice.example.browseproducts.BazaarUIThreadResponse.java
com.bazaarvoice.example.browseproducts.ImageDownloader.java
com.bazaarvoice.example.browseproducts.MainActivity.java
com.bazaarvoice.example.browseproducts.OnImageDownloadComplete.java
com.bazaarvoice.example.browseproducts.ProductAdapter.java
com.bazaarvoice.example.browseproducts.ProductsActivity.java
com.bazaarvoice.example.browseproducts.ReviewAdapter.java
com.bazaarvoice.example.browseproducts.ReviewDisplayActivity.java
com.bazaarvoice.example.browseproducts.ReviewsActivity.java
com.bazaarvoice.example.reviewsubmission.BazaarFunctions.java
com.bazaarvoice.example.reviewsubmission.BazaarReview.java
com.bazaarvoice.example.reviewsubmission.CameraUtils.java
com.bazaarvoice.example.reviewsubmission.ImageDownloader.java
com.bazaarvoice.example.reviewsubmission.MainActivity.java
com.bazaarvoice.example.reviewsubmission.OnImageDownloadComplete.java
com.bazaarvoice.example.reviewsubmission.OnImageUploadComplete.java
com.bazaarvoice.example.reviewsubmission.RatingActivity.java
com.bazaarvoice.example.reviewsubmission.RatingPreviewActivity.java
com.bazaarvoice.example.reviewsubmission.ReviewSubmissionApp.java
com.bazaarvoice.intentexample.BazaarFunctions.java
com.bazaarvoice.intentexample.BazaarUIThreadResponse.java
com.bazaarvoice.intentexample.CameraUtils.java
com.bazaarvoice.intentexample.MainActivity.java
com.bazaarvoice.types.Action.java
com.bazaarvoice.types.ApiVersion.java
com.bazaarvoice.types.Equality.java
com.bazaarvoice.types.FeedbackContentType.java
com.bazaarvoice.types.FeedbackType.java
com.bazaarvoice.types.FeedbackVoteType.java
com.bazaarvoice.types.IncludeStatsType.java
com.bazaarvoice.types.IncludeType.java
com.bazaarvoice.types.MediaParamsContentType.java
com.bazaarvoice.types.RequestType.java
com.chute.android.photopickerplus.adapter.AlbumsAdapter.java
com.chute.android.photopickerplus.adapter.PhotoSelectCursorAdapter.java
com.chute.android.photopickerplus.adapter.PhotosAdapter.java
com.chute.android.photopickerplus.app.AlbumsActivity.java
com.chute.android.photopickerplus.app.ChooseServiceActivity.java
com.chute.android.photopickerplus.app.GridActivity.java
com.chute.android.photopickerplus.app.PhotoPickerPlusApp.java
com.chute.android.photopickerplus.dao.MediaDAO.java
com.chute.android.photopickerplus.util.AppUtil.java
com.chute.android.photopickerplus.util.Constants.java
com.chute.android.photopickerplus.util.NotificationUtil.java
com.chute.android.photopickerplus.util.intent.AlbumsActivityIntentWrapper.java
com.chute.android.photopickerplus.util.intent.IntentUtil.java
com.chute.android.photopickerplus.util.intent.IntentWrapper.java
com.chute.android.photopickerplus.util.intent.PhotoActivityIntentWrapper.java
com.chute.android.photopickerplus.util.intent.PhotoPickerPlusIntentWrapper.java
com.chute.android.photopickerplus.util.intent.PhotosIntentWrapper.java
com.example.productwidgetexample.BazaarFunctions.java
com.example.productwidgetexample.BazaarProduct.java
com.example.productwidgetexample.BazaarReview.java
com.example.productwidgetexample.BazaarUIThreadResponse.java
com.example.productwidgetexample.ImageDownloader.java
com.example.productwidgetexample.OnImageDownloadComplete.java
com.example.productwidgetexample.ProductWidgetProvider.java
com.example.productwidgetexample.ReviewAdapter.java
com.example.productwidgetexample.ReviewsActivity.java