// Get all attachments for incident public List<Attachment> getAllAttachmentsForIncident(long incident_id) { List<Attachment> attachments = new ArrayList<>(); String selectQuery = "SELECT Id, Incident_Id, Attachment_Id FROM " + Constants.TABLE_INCIDENT_ATTACHMENT; Log.e(LOG, selectQuery); db = dbHelper.getReadableDatabase(); // Cursor c = db.rawQuery(selectQuery, null); Cursor c = db.query( Constants.TABLE_INCIDENT_ATTACHMENT, // a. table new String[] {"Id", "Incident_Id", "Attachment_Id"}, // b. column names " Incident_Id = ?", // c. selections new String[] {String.valueOf(incident_id)}, // 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 { Attachment anAttachment = attachment_dao.getAttachment(c.getInt(c.getColumnIndex("Attachment_Id"))); // adding to incident list attachments.add(anAttachment); } while (c.moveToNext()); } c.close(); return attachments; }
// getting Incident count public int getIncidentCount() { String countQuery = "SELECT Id FROM " + Constants.TABLE_INCIDENT; db = dbHelper.getReadableDatabase(); Cursor cursor = db.rawQuery(countQuery, null); int count = cursor.getCount(); cursor.close(); // return count return count; }
// 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; }
// Getting Attachments Count public int getAttachmentsCountForIncident(long incident_id) { String countQuery = "SELECT Id FROM " + Constants.TABLE_INCIDENT_ATTACHMENT + " WHERE Incident_Id=" + String.valueOf(incident_id); db = dbHelper.getReadableDatabase(); Cursor cursor = db.rawQuery(countQuery, null); int count = cursor.getCount(); cursor.close(); // return count return count; }
// 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; }