public IncidentDAO(Context context) { dbHelper = DatabaseHelper.getInstance(context); incidentType_dao = new IncidentTypeDAO(context); contact_dao = new ContactDAO(context); attachment_dao = new AttachmentDAO(context); }
// Remove notify contact from incident public void removeAllNotifyContactsFromIncident(long incident_id) { db = dbHelper.getWritableDatabase(); db.delete( Constants.TABLE_INCIDENT_NOTIFY_CONTACT, "Incident_Id" + " = ?", new String[] {String.valueOf(incident_id)}); }
// 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; }
// Adding new attachment to incident public void addAttachmentToIncident(Attachment attachment, Incident incident) { db = dbHelper.getWritableDatabase(); ContentValues values = new ContentValues(); values.put("Incident_Id", incident.getId()); values.put("Attachment_Id", attachment.getId()); // updating row db.insert(Constants.TABLE_INCIDENT_ATTACHMENT, null, values); }
// Add notify contact to incident public void addNotifyContactToIncident(Incident anIncident, Contact aContact) { db = dbHelper.getWritableDatabase(); ContentValues values = new ContentValues(); values.put("Incident_Id", anIncident.getId()); values.put("Contact_Id", aContact.getId()); // updating row db.insert(Constants.TABLE_INCIDENT_NOTIFY_CONTACT, null, values); }
// 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; }
// Deleting all attachments for an Incident public void deleteAllAttachmentsForIncident(Incident incident) { db = dbHelper.getWritableDatabase(); if (incident.getAttachments().size() > 0) { for (Attachment anAttachment : incident.getAttachments()) { attachment_dao.deleteSingleAttachment(anAttachment); } } db.delete( Constants.TABLE_INCIDENT_ATTACHMENT, "Incident_Id" + " = ?", new String[] {String.valueOf(incident.getId())}); }
// 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; }
// Updating a Incident public void updateIncident(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()); // updating row db.update( Constants.TABLE_INCIDENT, values, "Id=?", new String[] {String.valueOf(incident.getId())}); removeAllNotifyContactsFromIncident(incident.getId()); if (incident.getNotifyContacts().size() > 0) { for (Contact c : incident.getNotifyContacts()) { addNotifyContactToIncident(incident, c); } } }
// *** 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; }
// Deleting a Incident public static void deleteIncident(long incident_id) { db = dbHelper.getWritableDatabase(); db.delete(Constants.TABLE_INCIDENT, "Id" + " = ?", new String[] {String.valueOf(incident_id)}); }