Search in sources :

Example 1 with StudyRoom

use of de.tum.in.tumcampusapp.component.ui.studyroom.model.StudyRoom in project TumCampusApp by TCA-Team.

the class StudyRoomGroupManager method downloadFromExternal.

public void downloadFromExternal() throws JSONException {
    Optional<JSONObject> jsonObject = new NetUtils(mContext).downloadJsonObject(STUDYROOM_URL, CacheManager.VALIDITY_DO_NOT_CACHE, true);
    if (!jsonObject.isPresent()) {
        return;
    }
    dao.removeCache();
    groupDao.removeCache();
    List<StudyRoomGroup> groups = getAllFromJson(jsonObject.get());
    for (StudyRoomGroup group : groups) {
        groupDao.insert(group);
        for (StudyRoom room : group.getRooms()) {
            room.setStudyRoomGroup(group.getId());
            dao.insert(room);
        }
    }
    new SyncManager(mContext).replaceIntoDb(this);
}
Also used : StudyRoom(de.tum.in.tumcampusapp.component.ui.studyroom.model.StudyRoom) NetUtils(de.tum.in.tumcampusapp.utils.NetUtils) JSONObject(org.json.JSONObject) SyncManager(de.tum.in.tumcampusapp.utils.sync.SyncManager) StudyRoomGroup(de.tum.in.tumcampusapp.component.ui.studyroom.model.StudyRoomGroup)

Example 2 with StudyRoom

use of de.tum.in.tumcampusapp.component.ui.studyroom.model.StudyRoom in project TumCampusApp by TCA-Team.

the class StudyRoomGroupManager method getAllFromJson.

public static List<StudyRoomGroup> getAllFromJson(JSONObject jsonObject) throws JSONException {
    JSONArray groupsJsonArray = jsonObject.getJSONArray("gruppen");
    JSONArray roomsJsonArray = jsonObject.getJSONArray("raeume");
    List<StudyRoomGroup> studyRoomGroups = new ArrayList<>();
    for (int i = 0; i < groupsJsonArray.length(); i++) {
        JSONObject groupJsonObject = groupsJsonArray.getJSONObject(i);
        List<StudyRoom> studyRoomList = getStudyRoomsFromJson(groupJsonObject.getJSONArray("raeume"), roomsJsonArray);
        studyRoomGroups.add(new StudyRoomGroup(groupJsonObject.getInt("nr"), groupJsonObject.getString("name"), groupJsonObject.getString("detail"), studyRoomList));
    }
    return studyRoomGroups;
}
Also used : StudyRoom(de.tum.in.tumcampusapp.component.ui.studyroom.model.StudyRoom) JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) ArrayList(java.util.ArrayList) StudyRoomGroup(de.tum.in.tumcampusapp.component.ui.studyroom.model.StudyRoomGroup)

Example 3 with StudyRoom

use of de.tum.in.tumcampusapp.component.ui.studyroom.model.StudyRoom in project TumCampusApp by TCA-Team.

the class StudyRoomGroupManager method getStudyRoomsFromJson.

private static List<StudyRoom> getStudyRoomsFromJson(JSONArray groupRoomList, JSONArray allRooms) throws JSONException {
    List<StudyRoom> studyRooms = new ArrayList<>();
    for (int i = 0; i < allRooms.length(); i++) {
        int roomNumber = allRooms.getJSONObject(i).getInt("raum_nr");
        for (int j = 0; j < groupRoomList.length(); j++) {
            if (groupRoomList.getInt(j) == roomNumber) {
                StudyRoom studyRoom;
                try {
                    studyRoom = new StudyRoom(roomNumber, allRooms.getJSONObject(i).getString("raum_code"), allRooms.getJSONObject(i).getString("raum_name"), allRooms.getJSONObject(i).getString("gebaeude_name"), -1, new SimpleDateFormat(DATEFORMAT, Locale.US).parse(allRooms.getJSONObject(i).getString("belegung_bis")));
                } catch (ParseException e) {
                    // Room is not occupied
                    studyRoom = new StudyRoom(roomNumber, allRooms.getJSONObject(i).getString("raum_code"), allRooms.getJSONObject(i).getString("raum_name"), allRooms.getJSONObject(i).getString("gebaeude_name"), -1, new Date());
                }
                studyRooms.add(studyRoom);
            }
        }
    }
    return studyRooms;
}
Also used : StudyRoom(de.tum.in.tumcampusapp.component.ui.studyroom.model.StudyRoom) ArrayList(java.util.ArrayList) ParseException(java.text.ParseException) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date)

Aggregations

StudyRoom (de.tum.in.tumcampusapp.component.ui.studyroom.model.StudyRoom)3 StudyRoomGroup (de.tum.in.tumcampusapp.component.ui.studyroom.model.StudyRoomGroup)2 ArrayList (java.util.ArrayList)2 JSONObject (org.json.JSONObject)2 NetUtils (de.tum.in.tumcampusapp.utils.NetUtils)1 SyncManager (de.tum.in.tumcampusapp.utils.sync.SyncManager)1 ParseException (java.text.ParseException)1 SimpleDateFormat (java.text.SimpleDateFormat)1 Date (java.util.Date)1 JSONArray (org.json.JSONArray)1