Created
July 31, 2013 20:07
-
-
Save alwx/6125674 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.content.ContentResolver; | |
import android.content.ContentValues; | |
import android.database.Cursor; | |
import android.net.Uri; | |
import com.bytelimit.dk.log.Logger; | |
import com.bytelimit.dk.rest.handler.AbstractSQLiteSyncHandler; | |
import com.bytelimit.dk.sqlite.SQLiteUtils; | |
import com.bytelimit.dk.utils.IOUtils; | |
import com.lessonsproject.core.api.parser.FeaturedLessonParser; | |
import com.lessonsproject.core.api.parser.JsonParser; | |
import com.lessonsproject.core.api.parser.ScheduleParser; | |
import com.lessonsproject.core.sqlite.Schedule; | |
import com.lessonsproject.core.sqlite.SearchLesson; | |
import com.lessonsproject.core.sqlite.SearchSchedule; | |
import org.json.JSONArray; | |
import org.json.JSONException; | |
import org.json.JSONObject; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.util.ArrayList; | |
import java.util.HashMap; | |
import java.util.List; | |
import java.util.Map; | |
/** | |
* @author alwx | |
* @version 1.5 | |
*/ | |
public class SearchLessonsHandler extends AbstractSQLiteSyncHandler { | |
private static final JsonParser<ContentValues> sParser = new FeaturedLessonParser(); | |
private static final JsonParser<ContentValues> sScheduleParser = new ScheduleParser(); | |
private static final Uri sLessonsUri = SQLiteUtils.getBaseUri(SearchLesson.class); | |
private static final Uri sSchedulesUri = SQLiteUtils.getBaseUri(SearchSchedule.class); | |
public SearchLessonsHandler(ContentResolver cr) { | |
super(cr); | |
} | |
@Override | |
public Cursor onLoadInBackground() { | |
return getContentResolver().query(sSchedulesUri, null, null, null, null); | |
} | |
@Override | |
protected void onBeforeUpdateInBackground(Uri uri, ContentValues[] values) { | |
getContentResolver().delete(uri, null, null); | |
} | |
@Override | |
protected Map<Uri, List<ContentValues>> onParseInBackground(InputStream is) { | |
try { | |
final JSONArray records = new JSONObject(IOUtils.toString(is)).getJSONArray("records"); | |
final Map<Uri, List<ContentValues>> result = new HashMap<Uri, List<ContentValues>>(); | |
final List<ContentValues> bulkValues = new ArrayList<ContentValues>(records.length()); | |
final List<ContentValues> bulkScheduleValues = new ArrayList<ContentValues>(); | |
for (int i = 0; i < records.length(); ++i) { | |
final JSONObject lesson = records.getJSONObject(i); | |
bulkValues.add(sParser.parse(lesson)); | |
// parse subject & schedule | |
final JSONObject subject = lesson.getJSONObject("subject"); | |
final JSONObject schedule = lesson.getJSONObject("schedule"); | |
final ContentValues scheduleValues = sScheduleParser.parse(schedule); | |
scheduleValues.put(Schedule.Columns.SUBJECT_ID, subject.getString("id")); | |
scheduleValues.put(Schedule.Columns.SUBJECT_NAME, subject.getString("name")); | |
if (!bulkScheduleValues.contains(scheduleValues)) { | |
bulkScheduleValues.add(scheduleValues); | |
} | |
} | |
result.put(sSchedulesUri, bulkScheduleValues); | |
result.put(sLessonsUri, bulkValues); | |
return result; | |
} catch (JSONException e) { | |
Logger.error(e); | |
} catch (IOException e) { | |
Logger.error(e); | |
} | |
return null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment