Search in sources :

Example 1 with CourseGroup

use of com.mnnyang.gzuclassschedule.data.beanv2.CourseGroup in project GzuClassSchedule by mnnyang.

the class ImptPresenter method parseCoursesHtmlToDb.

private void parseCoursesHtmlToDb(final String html, final String courseTimeTerm) {
    try {
        Observable.create(new ObservableOnSubscribe<String>() {

            @Override
            public void subscribe(ObservableEmitter<String> emitter) throws Exception {
                ArrayList<CourseV2> courseV2s = ParseCourse.parse(html);
                // 删除旧数据
                CourseGroup group = Cache.instance().getCourseGroupDao().queryBuilder().where(CourseGroupDao.Properties.CgName.eq(courseTimeTerm)).unique();
                CourseV2Dao courseV2Dao = Cache.instance().getCourseV2Dao();
                if (null != group) {
                    courseV2Dao.queryBuilder().where(CourseV2Dao.Properties.CouCgId.eq(group.getCgId())).buildDelete().executeDeleteWithoutDetachingEntities();
                } else {
                    // 不存在旧数据 创建新的课表组
                    group = new CourseGroup();
                    group.setCgName(courseTimeTerm);
                    Cache.instance().getCourseGroupDao().insert(group);
                }
                for (CourseV2 courseV2 : courseV2s) {
                    courseV2.setCouCgId(group.getCgId());
                    courseV2Dao.insert(courseV2);
                }
                emitter.onNext("导入成功");
                emitter.onComplete();
            }
        }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<String>() {

            @Override
            public void onSubscribe(Disposable d) {
            }

            @Override
            public void onNext(String s) {
                if (mImptView == null) {
                    // view被销毁
                    return;
                }
                LogUtil.i(this, "导入成功:" + courseTimeTerm);
                Long curGroupId = Cache.instance().getCourseGroupDao().queryBuilder().where(CourseGroupDao.Properties.CgName.eq(courseTimeTerm)).unique().getCgId();
                Preferences.putLong(app.mContext.getString(R.string.app_preference_current_cs_name_id), curGroupId);
                mImptView.hideImpting();
                mImptView.showSucceed();
            }

            @Override
            public void onError(Throwable e) {
                if (mImptView == null) {
                    // view被销毁
                    return;
                }
                e.printStackTrace();
                mImptView.hideImpting();
                mImptView.showErrToast("插入数据库失败", true);
            }

            @Override
            public void onComplete() {
            }
        });
    } catch (Exception e) {
        e.printStackTrace();
        mImptView.hideImpting();
        mImptView.showErrToast("导入错误", true);
    }
}
Also used : Disposable(io.reactivex.disposables.Disposable) CourseV2Dao(com.mnnyang.gzuclassschedule.data.greendao.CourseV2Dao) ObservableOnSubscribe(io.reactivex.ObservableOnSubscribe) CourseGroup(com.mnnyang.gzuclassschedule.data.beanv2.CourseGroup) CourseV2(com.mnnyang.gzuclassschedule.data.beanv2.CourseV2) ObservableEmitter(io.reactivex.ObservableEmitter)

Example 2 with CourseGroup

use of com.mnnyang.gzuclassschedule.data.beanv2.CourseGroup in project GzuClassSchedule by mnnyang.

the class HomePresenter method buildJsonOfAllCourse.

/**
 * 对所有course建立json
 */
@NonNull
private JSONObject buildJsonOfAllCourse() {
    List<CourseGroup> groups = Cache.instance().getCourseGroupDao().queryBuilder().list();
    CourseV2Dao courseV2Dao = Cache.instance().getCourseV2Dao();
    JSONObject result = new JSONObject();
    JSONArray jsonArray = new JSONArray();
    try {
        result.put("data", jsonArray);
    } catch (JSONException e) {
        e.printStackTrace();
    }
    for (CourseGroup group : groups) {
        List<CourseV2> courseV2s = courseV2Dao.queryBuilder().where(CourseV2Dao.Properties.CouCgId.eq(group.getCgId())).list();
        for (CourseV2 course : courseV2s) {
            try {
                JSONObject jsonItem = new JSONObject();
                jsonItem.put("id", course.getCouId());
                jsonItem.put("name", course.getCouName());
                jsonItem.put("location", course.getCouLocation() == null ? "" : course.getCouLocation());
                jsonItem.put("week", course.getCouWeek());
                jsonItem.put("teacher", course.getCouTeacher() == null ? "" : course.getCouTeacher());
                jsonItem.put("all_week", course.getCouAllWeek());
                jsonItem.put("start_node", course.getCouStartNode());
                jsonItem.put("node_count", course.getCouNodeCount());
                jsonItem.put("color", course.getCouColor() == null ? "-1" : course.getCouColor());
                jsonItem.put("group_name", group.getCgName());
                jsonItem.put("only_id", course.getCouOnlyId());
                jsonItem.put("deleted", course.getCouDeleted());
                jsonArray.put(jsonItem);
            } catch (JSONException e) {
                LogUtil.e(this, "buildJsonOfAllCourse() failed--->" + course.toString());
                e.printStackTrace();
            }
        }
    }
    return result;
}
Also used : CourseV2Dao(com.mnnyang.gzuclassschedule.data.greendao.CourseV2Dao) CourseV2(com.mnnyang.gzuclassschedule.data.beanv2.CourseV2) JSONObject(org.json.JSONObject) CourseGroup(com.mnnyang.gzuclassschedule.data.beanv2.CourseGroup) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) NonNull(android.support.annotation.NonNull)

Example 3 with CourseGroup

use of com.mnnyang.gzuclassschedule.data.beanv2.CourseGroup in project GzuClassSchedule by mnnyang.

the class HomePresenter method getGroupId.

/**
 * 获取group_id
 */
private Long getGroupId(DownCourseWrapper.DownCourse downCourse) {
    if (downCourse == null) {
        return null;
    }
    CourseGroupDao groupDao = Cache.instance().getCourseGroupDao();
    String groupName = downCourse.getGroup_name();
    if (!TextUtils.isEmpty(groupName)) {
        Long groupId = mCacheGroup.get(groupName);
        if (null == groupId) {
            CourseGroup dbGroup = groupDao.queryBuilder().where(CourseGroupDao.Properties.CgName.eq(groupName)).unique();
            if (dbGroup == null) {
                CourseGroup newGroup = new CourseGroup().setCgName(groupName);
                groupId = groupDao.insert(newGroup);
            } else {
                groupId = dbGroup.getCgId();
            }
            mCacheGroup.put(groupName, groupId);
        }
        return groupId;
    } else {
        LogUtil.e(this, "下载的数据未找到group_name");
        return null;
    }
}
Also used : CourseGroupDao(com.mnnyang.gzuclassschedule.data.greendao.CourseGroupDao) CourseGroup(com.mnnyang.gzuclassschedule.data.beanv2.CourseGroup)

Example 4 with CourseGroup

use of com.mnnyang.gzuclassschedule.data.beanv2.CourseGroup in project GzuClassSchedule by mnnyang.

the class HomePresenter method writeShare.

/**
 * 分享写入本地
 */
private long writeShare(List<DownCourseWrapper.DownCourse> data) {
    CourseV2Dao courseV2Dao = Cache.instance().getCourseV2Dao();
    CourseGroup group = new CourseGroup();
    group.setCgName("来自热心网友分享" + AppUtils.createUUID().substring(0, 8));
    long newGroupId = Cache.instance().getCourseGroupDao().insert(group);
    for (DownCourseWrapper.DownCourse downCourse : data) {
        CourseV2 courseV2 = new CourseV2().setCouOnlyId(// new only_id
        AppUtils.createUUID()).setCouCgId(// new group
        newGroupId).setCouName(downCourse.getName()).setCouTeacher(downCourse.getTeacher()).setCouLocation(downCourse.getLocation()).setCouColor(downCourse.getColor()).setCouWeek(downCourse.getWeek()).setCouStartNode(downCourse.getStart_node()).setCouNodeCount(downCourse.getNode_count()).setCouAllWeek(downCourse.getAll_week());
        courseV2Dao.insert(courseV2);
    }
    return newGroupId;
}
Also used : CourseV2Dao(com.mnnyang.gzuclassschedule.data.greendao.CourseV2Dao) CourseV2(com.mnnyang.gzuclassschedule.data.beanv2.CourseV2) CourseGroup(com.mnnyang.gzuclassschedule.data.beanv2.CourseGroup) DownCourseWrapper(com.mnnyang.gzuclassschedule.data.beanv2.DownCourseWrapper)

Example 5 with CourseGroup

use of com.mnnyang.gzuclassschedule.data.beanv2.CourseGroup in project GzuClassSchedule by mnnyang.

the class CourseActivity method initFirstStart.

@Override
public void initFirstStart() {
    boolean isFirst = Preferences.getBoolean(getString(R.string.app_preference_app_is_first_start), true);
    if (!isFirst) {
        return;
    }
    Preferences.putBoolean(getString(R.string.app_preference_app_is_first_start), false);
    CourseGroupDao groupDao = Cache.instance().getCourseGroupDao();
    CourseGroup defaultGroup = groupDao.queryBuilder().where(CourseGroupDao.Properties.CgName.eq("默认课表")).unique();
    long insert;
    if (defaultGroup == null) {
        insert = groupDao.insert(new CourseGroup(0L, "默认", ""));
    } else {
        insert = defaultGroup.getCgId();
    }
    // migrate old data
    AppUtils.copyOldData(this);
    Preferences.putLong(getString(R.string.app_preference_current_cs_name_id), insert);
    showOnceSplash();
}
Also used : CourseGroupDao(com.mnnyang.gzuclassschedule.data.greendao.CourseGroupDao) CourseGroup(com.mnnyang.gzuclassschedule.data.beanv2.CourseGroup)

Aggregations

CourseGroup (com.mnnyang.gzuclassschedule.data.beanv2.CourseGroup)9 CourseGroupDao (com.mnnyang.gzuclassschedule.data.greendao.CourseGroupDao)5 CourseV2 (com.mnnyang.gzuclassschedule.data.beanv2.CourseV2)4 CourseV2Dao (com.mnnyang.gzuclassschedule.data.greendao.CourseV2Dao)4 DialogInterface (android.content.DialogInterface)1 NonNull (android.support.annotation.NonNull)1 RecyclerView (android.support.v7.widget.RecyclerView)1 View (android.view.View)1 EditText (android.widget.EditText)1 Course (com.mnnyang.gzuclassschedule.data.bean.Course)1 CsItem (com.mnnyang.gzuclassschedule.data.bean.CsItem)1 DownCourseWrapper (com.mnnyang.gzuclassschedule.data.beanv2.DownCourseWrapper)1 DaoMaster (com.mnnyang.gzuclassschedule.data.greendao.DaoMaster)1 DaoSession (com.mnnyang.gzuclassschedule.data.greendao.DaoSession)1 MyOpenHelper (com.mnnyang.gzuclassschedule.data.greendao.MyOpenHelper)1 DialogHelper (com.mnnyang.gzuclassschedule.utils.DialogHelper)1 DialogListener (com.mnnyang.gzuclassschedule.utils.DialogListener)1 ObservableEmitter (io.reactivex.ObservableEmitter)1 ObservableOnSubscribe (io.reactivex.ObservableOnSubscribe)1 Disposable (io.reactivex.disposables.Disposable)1