// getting all incidents under single incident type public List<Incident> getAllIncidentsByIncidentType(IncidentType incidentType) { List<Incident> incidents = new ArrayList<>(); String selectQuery = "SELECT Id, IncidentTypeId, CreatedByContactId, IncidentDate, Description, InitialAction, Reportable FROM " + Constants.TABLE_INCIDENT; Log.e(LOG, selectQuery); db = dbHelper.getReadableDatabase(); // Cursor c = db.rawQuery(selectQuery, null); Cursor c = db.query( Constants.TABLE_INCIDENT, // a. table new String[] { "Id", "IncidentTypeId", "CreatedByContactId", "IncidentDate", "Description", "InitialAction", "Reportable" }, // b. column names " IncidentTypeId = ?", // c. selections new String[] {String.valueOf(incidentType.getId())}, // d. selections args null, // e. group by null, // f. having null, // g. order by null); // looping through all rows and adding to list if (c.moveToFirst()) { do { Incident anIncident = new Incident(); anIncident.setId(c.getInt((c.getColumnIndex("Id")))); anIncident.setIncidentType( incidentType_dao.getIncidentType(c.getInt(c.getColumnIndex("IncidentTypeId")))); anIncident.setCreatedBy( contact_dao.getContact(c.getInt(c.getColumnIndex("CreatedByContactId")))); anIncident.setIncidentDate( DateTimeHelper.parseFromISO8601(c.getString(c.getColumnIndex("IncidentDate")))); anIncident.setDescription((c.getString(c.getColumnIndex("Description")))); anIncident.setInitialAction((c.getString(c.getColumnIndex("InitialAction")))); anIncident.setReportable(c.getInt(c.getColumnIndex("Reportable")) > 0); // anIncident.setNotifyContacts(); // adding to incident list incidents.add(anIncident); } while (c.moveToNext()); } c.close(); return incidents; }
// get single Incident public Incident getIncident(long incident_id) { Incident anIncident = new Incident(); db = dbHelper.getReadableDatabase(); String selectQuery = "SELECT * FROM " + Constants.TABLE_INCIDENT + " WHERE Id = " + incident_id; Log.e(LOG, selectQuery); // Cursor c = db.rawQuery(selectQuery, null); Cursor c = db.query( Constants.TABLE_INCIDENT, // a. table new String[] { "Id", "IncidentTypeId", "CreatedByContactId", "IncidentDate", "Description", "InitialAction", "Reportable" }, // b. column names " Id = ?", // c. selections new String[] {String.valueOf(incident_id)}, // d. selections args null, // e. group by null, // f. having null, // g. order by null); // if (c != null) // c.moveToFirst(); if (c.moveToFirst()) { anIncident.setId(c.getInt(c.getColumnIndex("Id"))); anIncident.setIncidentType( incidentType_dao.getIncidentType(c.getInt(c.getColumnIndex("IncidentTypeId")))); anIncident.setCreatedBy(contact_dao.getContact(c.getColumnIndex("CreatedByContactId"))); anIncident.setIncidentDate( DateTimeHelper.parseFromISO8601(c.getString(c.getColumnIndex("IncidentDate")))); anIncident.setDescription((c.getString(c.getColumnIndex("Description")))); anIncident.setInitialAction((c.getString(c.getColumnIndex("InitialAction")))); anIncident.setReportable(c.getInt(c.getColumnIndex("Reportable")) > 0); anIncident.setNotifyContacts(getAllNotifyContactsForIncident(incident_id)); anIncident.setAttachments(getAllAttachmentsForIncident(incident_id)); } c.close(); return anIncident; }
// *** Creating an Incident public long createIncident(Incident incident) { db = dbHelper.getWritableDatabase(); ContentValues values = new ContentValues(); values.put("IncidentTypeId", incident.getIncidentType().getId()); values.put("CreatedByContactId", incident.getCreatedBy().getId()); values.put("IncidentDate", DateTimeHelper.formatToIS08601(incident.getIncidentDate())); values.put("Description", incident.getDescription()); values.put("InitialAction", incident.getInitialAction()); values.put("Reportable", incident.getReportable()); // insert row long incident_id = db.insert(Constants.TABLE_INCIDENT, null, values); incident.setId((int) incident_id); if (incident.getNotifyContacts().size() > 0) { for (Contact c : incident.getNotifyContacts()) { addNotifyContactToIncident(incident, c); } } return incident_id; }