// 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())}); }
// 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; }
// 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; }
// 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); }
// 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; }