@Background
  void fetchData() {
    TeamListResponse teams = connection.getTeams();
    MatchesListResponse matches = connection.getMatches();

    ActiveAndroid.beginTransaction();
    try {
      for (Team team : teams.getTeams()) {
        team.save();
      }

      for (Match match : matches.getMatches()) {
        Team awayTeam = match.getAwayTeam();
        match.setAwayTeam(awayTeam.getById());
        Team homeTeam = match.getHomeTeam();
        match.setHomeTeam(homeTeam.getById());
        match.save();
      }
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      ActiveAndroid.setTransactionSuccessful();
    }
    ActiveAndroid.endTransaction();

    setupViewPager();
  }
  public static void saveAlerts(JSONArray alerts) throws JSONException {
    ActiveAndroid.beginTransaction();

    try {
      new Delete().from(Alert.class).execute();

      JSONObject currentAlert;
      for (int i = 0; i < alerts.length(); i++) {
        currentAlert = alerts.getJSONObject(i);

        Alert alert = new Alert();
        alert.alertId = currentAlert.getLong("id");
        alert.description = currentAlert.getString("alert");
        alert.date = currentAlert.getString("date");
        alert.username = currentAlert.getString("username");
        alert.latitude = currentAlert.getDouble("latitude");
        alert.longitude = currentAlert.getDouble("longitude");
        alert.line = currentAlert.getString("line");
        alert.city = currentAlert.getString("city");
        alert.station = currentAlert.getString("station");
        alert.transport = currentAlert.getString("transport");
        alert.icon = currentAlert.getString("transport_icon");
        alert.save();
      }

      ActiveAndroid.setTransactionSuccessful();

    } finally {

      ActiveAndroid.endTransaction();
    }
  }
Example #3
0
 @Override
 public final void run() {
   ActiveAndroid.beginTransaction();
   execute();
   ActiveAndroid.setTransactionSuccessful();
   ActiveAndroid.endTransaction();
 }
  public static void saveTweets(JSONArray alerts) throws JSONException {
    ActiveAndroid.beginTransaction();

    try {
      new Delete().from(Tweet.class).execute();

      JSONObject currentAlert;
      for (int i = 0; i < alerts.length(); i++) {
        currentAlert = alerts.getJSONObject(i);

        Tweet tweet = new Tweet();
        tweet.text = currentAlert.getString("text");
        tweet.user = currentAlert.getString("user");
        tweet.rtUser = currentAlert.getString("rt_user");
        tweet.date = currentAlert.getString("date");
        tweet.save();
      }

      ActiveAndroid.setTransactionSuccessful();

    } finally {

      ActiveAndroid.endTransaction();
    }
  }
  @Override
  protected Void doInBackground(Void... params) {
    /* Get all items from each machine */
    Reader reader = API.getData("machines/stock/");
    Machine machine =
        new GsonBuilder()
            .excludeFieldsWithModifiers(Modifier.FINAL, Modifier.TRANSIENT, Modifier.STATIC)
            .create()
            .fromJson(reader, Machine.class);
    MachineData machines = machine.getMachineData();

    /* Store all items in the DB */
    ActiveAndroid.beginTransaction();
    try {
      List<ItemInfo> itemsList = new Select().from(ItemInfo.class).execute();
      for (ItemInfo item : itemsList) {
        item.delete();
      }
      for (ItemInfo item : machines.getBigItemInfo()) {
        item.save();
      }
      for (ItemInfo item : machines.getLittleItemInfo()) {
        item.save();
      }
      for (ItemInfo item : machines.getSnacktemInfo()) {
        item.save();
      }
      ActiveAndroid.setTransactionSuccessful();
    } finally {
      ActiveAndroid.endTransaction();
    }
    return null;
  }
    @Override
    protected Boolean doInBackground(String... params) {
      OAuth OAuth = new OAuth(context);
      Utils Utils = new Utils();

      Map<String, String> postParams = new HashMap<String, String>(1);
      postParams.put("client_id", Config.OAUTHCLIENTID);
      postParams.put("client_secret", Config.OAUTHCLIENTSECRET);
      JSONObject result = OAuth.call("StaticData", "", postParams);

      if (Config.DEBUG) Log.d("SyncStaticData", result.toString());

      try {
        // Save sync data
        JSONObject data = result.getJSONObject("data");

        JSONObject currentData;
        ActiveAndroid.beginTransaction();
        try {
          // Save countries
          new Delete().from(Country.class).execute();
          JSONArray countries = data.getJSONObject("country").getJSONArray("data");

          for (int i = 0; i < countries.length(); i++) {
            currentData = countries.getJSONObject(i);
            Country country = new Country();
            country.name = currentData.getString("name");
            country.countryId = currentData.getLong("id");
            country.save();
          }

          // Save cities
          new Delete().from(City.class).execute();
          JSONArray cities = data.getJSONObject("city").getJSONArray("data");

          for (int i = 0; i < cities.length(); i++) {
            currentData = cities.getJSONObject(i);
            City city = new City();
            city.name = currentData.getString("name");
            city.cityId = currentData.getLong("id");
            city.country_id = currentData.getLong("country_id");
            city.save();
          }

          ActiveAndroid.setTransactionSuccessful();
        } finally {
          ActiveAndroid.endTransaction();
        }
        return true;
      } catch (Exception e) {
        if (Config.DEBUG) Log.d("Sync", "Sync failed. Cause: " + e.toString());
        e.printStackTrace();
        return false;
      }
    }
Example #7
0
 @Subscribe
 public void onUsersLoaded(TaskEvent event) {
   if (event.getEvantType() == EventEnum.RELOAD) {
     ActiveAndroid.beginTransaction();
     try {
       new Delete().from(GitHubUser.class).execute();
       ActiveAndroid.setTransactionSuccessful();
     } finally {
       ActiveAndroid.endTransaction();
     }
   }
 }
 public static void parse(Feed feed, String str) throws XmlPullParserException, IOException {
   try {
     ActiveAndroid.beginTransaction();
     XmlPullParser parser = Xml.newPullParser();
     parser.setInput(new StringReader(str));
     parser.nextTag();
     readFeed(feed, parser);
     ActiveAndroid.setTransactionSuccessful();
   } catch (Exception e) {
     e.printStackTrace();
   } finally {
     ActiveAndroid.endTransaction();
   }
 }
Example #9
0
 @Subscribe
 public void onUsersLoaded(LoadUsersEvent event) {
   if (event.getUsers() != null) {
     ActiveAndroid.beginTransaction();
     try {
       for (GitHubUser user : event.getUsers()) {
         user.save();
       }
       ActiveAndroid.setTransactionSuccessful();
     } finally {
       ActiveAndroid.endTransaction();
     }
   }
 }
Example #10
0
  public void dasei(View v) {
    try {
      ActiveAndroid.beginTransaction();

      Item item = new Item();

      item.save();

      ActiveAndroid.setTransactionSuccessful();
    } finally {
      ActiveAndroid.endTransaction();
    }
    Intent intent = new Intent();
    intent.setAction(intent.ACTION_MAIN);
    intent.setClass(MainActivity.this, OtherActivity.class);
    startActivity(intent);
  }
    @Override
    protected Boolean doInBackground(String... params) {
      OAuth OAuth = new OAuth(context);
      Utils Utils = new Utils();

      Map<String, String> postParams = new HashMap<String, String>(1);
      postParams.put("access_token", Utils.getToken(context));
      JSONObject result = OAuth.call("synchronize", "", postParams);

      if (Config.DEBUG) Log.d("Sync", result.toString());

      try {
        // Save sync data
        JSONObject data = result.getJSONObject("data");

        JSONObject currentData;
        ActiveAndroid.beginTransaction();
        try {

          // Save user data
          new Delete().from(User.class).execute();
          JSONObject userData = data.getJSONObject("user");
          Log.d("User", "User data > " + userData);
          User user = new User();
          user.username = userData.getString("username");
          user.name = userData.getString("name");
          if (userData.isNull("name")) {
            user.name = "";
          }
          String email = "";
          if (!userData.isNull("email")) {
            email = userData.getString("email");
          }
          user.email = email;

          String twitter = "";
          if (!userData.isNull("twittername")) {
            twitter = userData.getString("twittername");
          }
          user.twittername = twitter;

          String aboutme = "";
          if (!userData.isNull("aboutme")) {
            aboutme = userData.getString("aboutme");
          }
          user.aboutme = aboutme;
          user.cityId = userData.getLong("city_id");
          user.save();

          // Save countries
          new Delete().from(Country.class).execute();
          JSONArray countries = data.getJSONObject("country").getJSONArray("data");

          for (int i = 0; i < countries.length(); i++) {
            currentData = countries.getJSONObject(i);
            Country country = new Country();
            country.name = currentData.getString("name");
            country.countryId = currentData.getLong("id");
            country.save();
          }

          // Save cities
          new Delete().from(City.class).execute();
          JSONArray cities = data.getJSONObject("city").getJSONArray("data");

          for (int i = 0; i < cities.length(); i++) {
            currentData = cities.getJSONObject(i);
            City city = new City();
            city.name = currentData.getString("name");
            city.cityId = currentData.getLong("id");
            city.country_id = currentData.getLong("country_id");
            city.save();
          }

          // Save Lines
          new Delete().from(Line.class).execute();
          JSONArray lines = data.getJSONObject("line").getJSONArray("data");
          for (int i = 0; i < lines.length(); i++) {
            currentData = lines.getJSONObject(i);
            Line line = new Line();
            line.name = currentData.getString("name");
            // line.cityId = currentData.getLong("city_id");
            line.lineId = currentData.getLong("id");
            line.transport_id = currentData.getInt("transport_id");
            line.save();
          }

          // Save Transports
          new Delete().from(Transport.class).execute();
          JSONArray transports = data.getJSONObject("transport").getJSONArray("data");
          for (int i = 0; i < transports.length(); i++) {
            currentData = transports.getJSONObject(i);
            Transport transport = new Transport();
            transport.transportId = currentData.getLong("id");
            transport.name = currentData.getString("name");
            transport.icon = currentData.getString("icon");
            transport.save();
          }

          // Save station
          new Delete().from(Station.class).execute();
          JSONArray stations = data.getJSONObject("station").getJSONArray("data");
          for (int i = 0; i < stations.length(); i++) {
            currentData = stations.getJSONObject(i);
            Station station = new Station();
            station.name = currentData.getString("name");
            station.longitude = currentData.getDouble("longitude");
            station.stationId = currentData.getLong("id");
            station.latitude = currentData.getDouble("latitude");
            // station.lineId = currentData.getLong("line_id");
            station.save();
          }

          // Save CitiesTransports
          new Delete().from(CitiesTransport.class).execute();
          JSONArray citiesTransports = data.getJSONObject("citiestransport").getJSONArray("data");
          for (int i = 0; i < citiesTransports.length(); i++) {
            currentData = citiesTransports.getJSONObject(i);
            CitiesTransport citiesTransport = new CitiesTransport();
            citiesTransport.transportId = currentData.getLong("transport_id");
            citiesTransport.cityId = currentData.getLong("city_id");
            citiesTransport.save();
          }

          // Save LineStations
          new Delete().from(LinesStation.class).execute();
          JSONArray lineStations = data.getJSONObject("linesstations").getJSONArray("data");
          for (int i = 0; i < lineStations.length(); i++) {
            currentData = lineStations.getJSONObject(i);
            LinesStation linesStation = new LinesStation();
            linesStation.lineId = currentData.getLong("line_id");
            linesStation.stationId = currentData.getLong("station_id");
            linesStation.save();
          }

          ActiveAndroid.setTransactionSuccessful();
        } finally {
          ActiveAndroid.endTransaction();
        }
        return true;
      } catch (Exception e) {
        if (Config.DEBUG) Log.d("Sync", "Sync failed. Cause: " + e.toString());
        e.printStackTrace();
        return false;
      }
    }