Android Open Source - opentraining Wger J S O N Parser






From Project

Back to project page opentraining.

License

The source code is released under:

GNU General Public License

If you think the Android project opentraining 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

/**
 * //from   w  w  w .  j  av  a 2s  . c o m
 * This is OpenTraining, an Android application for planning your your fitness training.
 * Copyright (C) 2012-2014 Christian Skubich
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */

package de.skubware.opentraining.activity.settings.sync;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.SortedSet;
import java.util.TreeSet;

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

import android.util.Log;
import android.util.SparseArray;

import de.skubware.opentraining.basic.ExerciseType;
import de.skubware.opentraining.basic.ExerciseType.ExerciseSource;
import de.skubware.opentraining.basic.License.LicenseType;
import de.skubware.opentraining.basic.Muscle;
import de.skubware.opentraining.basic.SportsEquipment;
import de.skubware.opentraining.db.IDataProvider;

/**
 * A class for parsing the JSON-data from wger.
 *
 */
public class WgerJSONParser {
  
  private List<ExerciseType> mNewExerciseList = new ArrayList<ExerciseType>();
  /* The exercise builder objects, for modifying the exercises after parsing (e.g. changing image) */
  private List<ExerciseType.Builder> mNewExerciseBuilderList = new ArrayList<ExerciseType.Builder>();

  //TODO Find better solution, remove static methods and fields
  private static IDataProvider mDataProvider;
  
  /** Tag for logging */
  private static final String TAG = "WgerJSONParser";
  
  /**
   * Constructor. Will start download immediately.
   * 
   * @param exerciseJSONString
   *            The exercises as JSON-String
   * @param languageJSONString
   *            The languages as JSON-String
   * @param muscleJSONString
   *            The muscles as JSON-String
   * @param dataProvider
   * @throws JSONException
   */
  public WgerJSONParser(String exerciseJSONString, String languageJSONString, String muscleJSONString, String equipmentJSONString, String licenseJSONString, IDataProvider dataProvider) throws JSONException{
    mDataProvider = dataProvider;
    
    // parse languages
    SparseArray<Locale> localeSparseArray = parseLanguages(languageJSONString);
    // parse muscles
    SparseArray<Muscle> muscleSparseArray = parseMuscles(muscleJSONString);
    // parse licenses
    SparseArray<LicenseType> licenseSparseArray = parseLicenses(licenseJSONString);

    // parse equipment (not required until REST-API supports this)
    // SparseArray<SportsEquipment> equipmentSparseArray = parseEquipment(equipmentJSONString);
    
    
    
    JSONObject mainObject = new JSONObject(exerciseJSONString);
    Log.d(TAG, mainObject.toString());
    JSONArray exerciseArray = mainObject.getJSONArray("objects");

    // parse each exercise of the JSON Array
    for (int i = 0; i < exerciseArray.length(); i++) {
      
      JSONObject jsonExercise = exerciseArray.getJSONObject(i);
      // get name and check if exercise already exists
      String name = jsonExercise.getString("name");
      if(dataProvider.exerciseExists(name))
        continue;
      
      ExerciseType.Builder builder = new ExerciseType.Builder(name, ExerciseSource.SYNCED);
      
      // category (unused)
      // String category = jsonExercise.getString("category");
      
      // comments (unused)
      // JSONArray commentArray = jsonExercise.getJSONArray("comments");
      // for (int k = 0; k < commentArray.length(); k++) {
      //  String comment = commentArray.getString(k);
      //}
      
      // description
      String description = jsonExercise.getString("description");
      builder.description(description);
      
      // id (unused)
      //String id = jsonExercise.getString("id");
      
      // language
      // the json-language String might look like this:
      // '/api/v1/language/1/'
      // only the number at the end is required
      String language = jsonExercise.getString("language");
      int languageNumber = getLastNumberOfJson(language);
      
      Map<Locale, String> translationMap = new HashMap<Locale, String>();
      translationMap.put(localeSparseArray.get(languageNumber), name);
      builder.translationMap(translationMap);
      
      // resource_uri (unused)
      //String resource_uri = jsonExercise.getString("resource_uri");
      

      // muscles
      SortedSet<Muscle> muscleSet = new TreeSet<Muscle>();
      JSONArray muscleArray = jsonExercise.getJSONArray("muscles");
      for (int l = 0; l < muscleArray.length(); l++) {
        String muscleString = muscleArray.getString(l);
        Muscle muscle = muscleSparseArray.get(getLastNumberOfJson(muscleString));
        muscleSet.add(muscle);
      }
      builder.activatedMuscles(muscleSet);
      
      // licenses
      // the json-language String might look like this:
      // '/api/v1/license/1/'
      // only the number at the end is required
      
      if(jsonExercise.has("license")){
        int licenseNumber = getLastNumberOfJson(jsonExercise.getString("license"));
        LicenseType licenseType = licenseSparseArray.get(licenseNumber);
        String license_author = jsonExercise.getString("license_author");
        Log.v(TAG, "license=" + licenseType + " license_author=" + license_author);
      }
      
      
      // equipment
      // not yet supported by REST-API
      /*SortedSet<SportsEquipment> equipmentSet = new TreeSet<SportsEquipment>();
      JSONArray equipmentArray = jsonExercise.getJSONArray("equipment");
      for (int l = 0; l < equipmentArray.length(); l++) {
        String equipmentString = equipmentArray.getString(l);
        SportsEquipment equipment = equipmentSparseArray.get(getLastNumberOfJson(equipmentString));
        equipmentSet.add(equipment);
      }*/
      
      
      builder.activatedMuscles(muscleSet);
      // images
      List<File> imageList= new ArrayList<File>();
      JSONArray imageArray = jsonExercise.getJSONArray("images");
      for (int l = 0; l < imageArray.length(); l++) {
        String imageString = imageArray.getString(l);
        imageList.add(new File(imageString));
      }
      builder.imagePath(imageList);
      
      
      mNewExerciseList.add(builder.build());
      mNewExerciseBuilderList.add(builder);
    }

  }
  
  /**
   * Returns the last number of a JSON-String.
   * 
   * E.g.: for '/api/v1/something/1/' '1' will be returned.
   * 
   * @param jsonString
   * 
   * @return the last number of a JSON-String
   */
  public static int getLastNumberOfJson(String jsonString){
    String[] split = jsonString.split("/");
    return Integer.parseInt(split[split.length - 1]);
  }
  
  /**
   * Parses the JSON-language(locale)-String and returns an SparseArray that maps the
   * language numbers to {@link Locale} objects
   */
  private SparseArray<Locale> parseLanguages(String languagesJSONString) throws JSONException{
    return parse(languagesJSONString, Locale.class);
  }
  
  /**
   * Parses the JSON-muscle-String and returns an SparseArray that maps the
   * muscle numbers to {@link Muscle} objects
   * 
   * Example for muscle JSON:
   * 
   * {"meta": {"limit": 20, "next": null, "offset": 0, "previous": null,
   * "total_count": 15}, "objects": [{"id": 2, "is_front": true, "name":
   * "Anterior deltoid", "resource_uri": "/api/v1/muscle/2/"}]}
   * 
   */
  private SparseArray<Muscle> parseMuscles(String musclesJSONString) throws JSONException{
    return parse(musclesJSONString, Muscle.class);
  }
  
  public static SparseArray<LicenseType> parseLicenses(String licenseJSONString) throws JSONException{
    return parse(licenseJSONString, LicenseType.class);
  }
  
  private SparseArray<SportsEquipment> parseEquipment(String equipmentJSONString) throws JSONException{
    return parse(equipmentJSONString, SportsEquipment.class);
  }
  
  /**
   * A generic parsing method for parsing JSON to SportsEquipment, Muscle or Locale.
   */
  private static <T> SparseArray<T> parse(String jsonString, Class<T> c) throws JSONException{
    JSONObject mainObject = new JSONObject(jsonString);
    Log.d(TAG, "jsonString: " + mainObject.toString());
    JSONArray mainArray = mainObject.getJSONArray("objects");

    SparseArray<T> sparseArray = new SparseArray<T>();
    
    

    // parse each exercise of the JSON Array
    for (int i = 0; i < mainArray.length(); i++) {
      JSONObject singleObject = mainArray.getJSONObject(i);
      
      Integer id = singleObject.getInt("id");
      Object parsedObject;
      if(c.equals(Muscle.class)){
        // handle Muscles
        String name = singleObject.getString("name");
        parsedObject = mDataProvider.getMuscleByName(name);
        
        if(parsedObject == null)
          Log.e(TAG, "Could not find Muscle: " + name);
        
      }else if(c.equals(SportsEquipment.class)){
        // handle SportsEquipment
        String name = singleObject.getString("name");
        parsedObject = mDataProvider.getEquipmentByName(name);
        
        if(parsedObject == null)
          Log.e(TAG, "Could not find SportsEquipment: " + name);
        
      }else if(c.equals(Locale.class)){
        // handle Locales
        String short_name = singleObject.getString("short_name");
        parsedObject = new Locale(short_name);  
        
        if(short_name == null || short_name.equals(""))
          Log.e(TAG, "Error, no short_name=" + short_name);
        
      }else if(c.equals(LicenseType.class)){
        // handle licenses
        String short_name = singleObject.getString("short_name");
        
        parsedObject = mDataProvider.getLicenseTypeByName(short_name);  
        
        
        if(short_name == null || short_name.equals(""))
          Log.e(TAG, "Error, no short_name=" + short_name);
        
      }else{
        throw new IllegalStateException("parse(String, Class<T>) cannot be applied for class: " + c.toString());
      }

      sparseArray.put(id, (T) parsedObject);

      
    }
    
    return sparseArray;
  }

  
  /**
   * Returns the list with the new exercises.
   * 
   * 
   * @return A list containing all new exercises.
   */
  public ArrayList<ExerciseType> getNewExercises(){    
    return new ArrayList<ExerciseType>(mNewExerciseList);
  }
  
  /**
   * Returns the list with the builder objects for the new exercises
   * 
   * 
   * @return A list containing all builder objects for the new exercises
   */
  public ArrayList<ExerciseType.Builder> getNewExercisesBuilder(){    
    return new ArrayList<ExerciseType.Builder>(mNewExerciseBuilderList);
  }  
  
  
}




Java Source Code List

at.technikum.mti.fancycoverflow.FancyCoverFlowAdapter.java
at.technikum.mti.fancycoverflow.FancyCoverFlowItemWrapper.java
at.technikum.mti.fancycoverflow.FancyCoverFlow.java
de.skubware.opentraining.activity.ChangeLogDialog.java
de.skubware.opentraining.activity.DisclaimerDialog.java
de.skubware.opentraining.activity.MainActivity.java
de.skubware.opentraining.activity.NavigationGalleryAdapter.java
de.skubware.opentraining.activity.SelectWorkoutDialogFragment.java
de.skubware.opentraining.activity.WhatsNewDialog.java
de.skubware.opentraining.activity.acra.ACRACrashReportMailer.java
de.skubware.opentraining.activity.acra.ACRAFeedbackMailer.java
de.skubware.opentraining.activity.acra.OpenTrainingApplication.java
de.skubware.opentraining.activity.acra.RequestExerciseUpdate.java
de.skubware.opentraining.activity.create_exercise.CreateExerciseActivity.java
de.skubware.opentraining.activity.create_exercise.CustomSpinner.java
de.skubware.opentraining.activity.create_exercise.DescriptionFragment.java
de.skubware.opentraining.activity.create_exercise.EditImageMetadataDialog.java
de.skubware.opentraining.activity.create_exercise.EquipmentDataFragment.java
de.skubware.opentraining.activity.create_exercise.ExerciseImageListAdapter.java
de.skubware.opentraining.activity.create_exercise.ImageFragment.java
de.skubware.opentraining.activity.create_exercise.MuscleDataFragment.java
de.skubware.opentraining.activity.create_exercise.NameFragment.java
de.skubware.opentraining.activity.create_exercise.SimpleDataFragment.java
de.skubware.opentraining.activity.create_exercise.SpinnerDataFragment.java
de.skubware.opentraining.activity.create_workout.DialogFilterMusclesAndEquipment.java
de.skubware.opentraining.activity.create_workout.DialogWorkoutOverviewFragment.java
de.skubware.opentraining.activity.create_workout.ExerciseDetailOnGestureListener.java
de.skubware.opentraining.activity.create_workout.ExerciseTypeDetailActivity.java
de.skubware.opentraining.activity.create_workout.ExerciseTypeDetailFragment.java
de.skubware.opentraining.activity.create_workout.ExerciseTypeListActivity.java
de.skubware.opentraining.activity.create_workout.ExerciseTypeListAdapter.java
de.skubware.opentraining.activity.create_workout.ExerciseTypeListFragment.java
de.skubware.opentraining.activity.create_workout.SelectMuscleDialog.java
de.skubware.opentraining.activity.create_workout.SendExerciseFeedbackDialogFragment.java
de.skubware.opentraining.activity.create_workout.upload_exercise.ExerciseImage.java
de.skubware.opentraining.activity.create_workout.upload_exercise.UploadExerciseAsyncTask.java
de.skubware.opentraining.activity.create_workout.upload_exercise.UploadExerciseImagesAsyncTask.java
de.skubware.opentraining.activity.create_workout.upload_exercise.WgerRestService.java
de.skubware.opentraining.activity.manage_workouts.RenameWorkoutDialogFragment.java
de.skubware.opentraining.activity.manage_workouts.WorkoutDetailActivity.java
de.skubware.opentraining.activity.manage_workouts.WorkoutDetailFragment.java
de.skubware.opentraining.activity.manage_workouts.WorkoutListActivity.java
de.skubware.opentraining.activity.manage_workouts.WorkoutListFragment.java
de.skubware.opentraining.activity.settings.LicenseDialog.java
de.skubware.opentraining.activity.settings.SettingsActivity.java
de.skubware.opentraining.activity.settings.sync.OpenTrainingSyncResultReceiver.java
de.skubware.opentraining.activity.settings.sync.OpenTrainingSyncService.java
de.skubware.opentraining.activity.settings.sync.RestClient.java
de.skubware.opentraining.activity.settings.sync.SyncFinishedDialog.java
de.skubware.opentraining.activity.settings.sync.WgerImageDownloader.java
de.skubware.opentraining.activity.settings.sync.WgerJSONParser.java
de.skubware.opentraining.activity.start_training.DialogFragmentAddEntry.java
de.skubware.opentraining.activity.start_training.DialogFragmentTrainingEntryTable.java
de.skubware.opentraining.activity.start_training.FExDetailActivity.java
de.skubware.opentraining.activity.start_training.FExDetailFragment.java
de.skubware.opentraining.activity.start_training.FExListActivity.java
de.skubware.opentraining.activity.start_training.FExListAdapter.java
de.skubware.opentraining.activity.start_training.FExListFragment.java
de.skubware.opentraining.activity.start_training.RecoveryTimerManager.java
de.skubware.opentraining.activity.start_training.SwipeDismissListViewTouchListener.java
de.skubware.opentraining.activity.start_training.SwipeDismissTouchListener.java
de.skubware.opentraining.activity.start_training.TrainingEntryListAdapter.java
de.skubware.opentraining.basic.ActivationLevel.java
de.skubware.opentraining.basic.ExerciseTag.java
de.skubware.opentraining.basic.ExerciseType.java
de.skubware.opentraining.basic.FSet.java
de.skubware.opentraining.basic.FitnessExercise.java
de.skubware.opentraining.basic.IExercise.java
de.skubware.opentraining.basic.License.java
de.skubware.opentraining.basic.Muscle.java
de.skubware.opentraining.basic.SportsEquipment.java
de.skubware.opentraining.basic.TrainingEntry.java
de.skubware.opentraining.basic.Translatable.java
de.skubware.opentraining.basic.Workout.java
de.skubware.opentraining.db.Cache.java
de.skubware.opentraining.db.DataHelper.java
de.skubware.opentraining.db.DataProvider.java
de.skubware.opentraining.db.IDataProvider.java
de.skubware.opentraining.db.parser.AbstractJSONParser.java
de.skubware.opentraining.db.parser.ExerciseTagJSONParser.java
de.skubware.opentraining.db.parser.ExerciseTypeXMLParser.java
de.skubware.opentraining.db.parser.IParser.java
de.skubware.opentraining.db.parser.MuscleJSONParser.java
de.skubware.opentraining.db.parser.SportsEquipmentJSONParser.java
de.skubware.opentraining.db.parser.WorkoutXMLParser.java
de.skubware.opentraining.db.parser.XMLSaver.java
de.skubware.opentraining.db.rest.ExerciseImageGSONSerializer.java
de.skubware.opentraining.db.rest.ExerciseTypeGSONSerializer.java
de.skubware.opentraining.db.rest.LanguageGSONDeserializer.java
de.skubware.opentraining.db.rest.MuscleGSONDeserializer.java
de.skubware.opentraining.db.rest.ServerModel.java
de.skubware.opentraining.db.rest.SportsEquipmentGSONDeserializer.java
de.skubware.opentraining.exporter.HTMLExporter.java
de.skubware.opentraining.exporter.LaTeXExporter.java
de.skubware.opentraining.exporter.WorkoutExporter.java