Files
doneit-web/src/app/services/sqlite.service.ts
T

255 lines
9.4 KiB
TypeScript
Raw Normal View History

2021-09-21 06:09:41 +01:00
import { Injectable } from '@angular/core';
import { Platform } from '@ionic/angular';
import { SQLite, SQLiteObject } from '@ionic-native/sqlite/ngx';
@Injectable({
providedIn: 'root'
})
export class SqliteService {
private dbInstance: SQLiteObject;
readonly db_name: string = "gabinetedigital.db";
readonly events: string = "Events";
readonly expedientes: string = "Expedientes";
EVENTS: Array<any>;
EXPEDIENTES: Array<any>;
constructor(private platform: Platform,
private sqlite: SQLite) {
this.databaseConn();
}
databaseConn() {
this.platform.ready().then(async () => {
await this.sqlite.create({
name: this.db_name,
location: 'default'
}).then(async (sqLite: SQLiteObject) => {
this.dbInstance = sqLite;
await sqLite.executeSql(`
CREATE TABLE IF NOT EXISTS ${this.events} (
EventId varchar(255) PRIMARY KEY,
Subject varchar(255),
HasAttachments BIT,
Location varchar(255),
CalendarId varchar(255),
CalendarName varchar(255),
StartDate varchar(255),
EndDate varchar(255),
EventType varchar(255),
Attendees Text,
IsMeeting BIT,
IsRecurring BIT,
IsAllDayEvent BIT,
AppointmentState INTERGER,
TimeZone varchar(255),
Organizer Text,
Category varchar(255),
EventRecurrence Text,
Attachments Text,
Body Text
)`, [])
.then((res) => {
console.log("Sucess Events Table created: ", res)
})
.catch((error) => console.log(JSON.stringify(error)));
await sqLite.executeSql(`
CREATE TABLE IF NOT EXISTS ${this.expedientes} (
serialNumber varchar(255) PRIMARY KEY,
workflowInstanceFolio varchar(255),
Documents Text,
actions Text,
activityInstanceName varchar(255),
formURL varchar(255),
originator Text,
taskStartDate varchar(255),
totalDocuments INTERGER,
workflowDisplayName varchar(255),
workflowID INTERGER,
workflowInstanceDataFields Text,
workflowInstanceID INTERGER,
workflowName varchar(255)
)`, [])
.then((res) => {
console.log("Sucess Espedientes Table created: ", res)
})
.catch((error) => console.log(JSON.stringify(error)));
})
.catch((error) => console.log(JSON.stringify(error)));
});
}
//addEvent
public addEvent(data) {
this.dbInstance.executeSql(`
INSERT OR REPLACE INTO ${this.events} (EventId,Subject,HasAttachments,Location,CalendarId,CalendarName,StartDate,EndDate,EventType,Attendees,IsMeeting,IsRecurring,IsAllDayEvent,AppointmentState,TimeZone,Organizer,Category,EventRecurrence,Attachments,Body )
VALUES ('${data.EventId}','${data.Subject}', '${data.HasAttachments}','${data.Location}','${data.CalendarId}','${data.CalendarName}','${data.StartDate}','${data.EndDate}','${data.EventType}','${data.Attendees}','${data.IsMeeting}','${data.IsRecurring}',
'${data.IsAllDayEvent}','${data.AppointmentState}','${data.TimeZone}','${data.Organizer}','${data.Category}','${data.EventRecurrence}','${data.Attachments}','${data.Body}')`, [])
.then(() => {
console.log("event add with Success");
}, (e) => {
console.log(JSON.stringify(e.err));
});
}
//addExpediente
public addExpediente(data) {
this.dbInstance.executeSql(`
INSERT OR REPLACE INTO ${this.expedientes} (serialNumber,workflowInstanceFolio,Documents,actions,activityInstanceName,formURL,originator,taskStartDate,totalDocuments,workflowDisplayName,workflowID,workflowInstanceDataFields,workflowInstanceID,workflowName)
VALUES ('${data.serialNumber}','${data.workflowInstanceFolio}', '${data.Documents}','${data.actions}','${data.activityInstanceName}','${data.formURL}','${data.originator}','${data.taskStartDate}','${data.totalDocuments}','${data.workflowDisplayName}','${data.workflowID}',
'${data.workflowInstanceDataFields}','${data.workflowInstanceID}','${data.workflowName}')`, [])
.then(() => {
console.log("expediente add with Success");
}, (e) => {
console.log(JSON.stringify(e.err));
});
}
//getAllEvents
getAllEvents() {
var hashattachment = false;
var ismeeting = false;
var isrecurring = false;
var isallday = false;
return this.dbInstance.executeSql(`SELECT * FROM ${this.events}`, []).then((res) => {
this.EVENTS = [];
if (res.rows.length > 0) {
for (var i = 0; i < res.rows.length; i++) {
if (res.rows.item(i).HasAttachments === "true") {
hashattachment = true
}
if (res.rows.item(i).IsMeeting === "true") {
ismeeting = true
}
if (res.rows.item(i).IsRecurring === "true") {
isrecurring = true
}
if (res.rows.item(i).IsAllDayEvent === "true") {
isallday = true
}
let event = {
EventId: res.rows.item(i).EventId,
HasAttachments: hashattachment,
Subject: res.rows.item(i).Subject,
Location: res.rows.item(i).Location,
CalendarId: res.rows.item(i).CalendarId,
CalendarName: res.rows.item(i).CalendarName,
StartDate: res.rows.item(i).StartDate,
EndDate: res.rows.item(i).EndDate,
EventType: res.rows.item(i).EventType,
Attendees: res.rows.item(i).Attendees,
IsMeeting: ismeeting,
IsRecurring: isrecurring,
IsAllDayEvent: isallday,
AppointmentState: res.rows.item(i).AppointmentState,
TimeZone: res.rows.item(i).TimeZone,
Organizer: res.rows.item(i).Organizer,
Category: res.rows.item(i).Category,
EventRecurrence: res.rows.item(i).EventRecurrence,
Attachments: res.rows.item(i).Attachments
}
this.EVENTS.push(event);
}
return this.EVENTS;
}
}, (e) => {
console.log(" Get all events error", JSON.stringify(e));
});
}
getAllExpedientes() {
return this.dbInstance.executeSql(`SELECT * FROM ${this.expedientes}`, []).then((res) => {
this.EXPEDIENTES = [];
if (res.rows.length > 0) {
for (var i = 0; i < res.rows.length; i++) {
this.EXPEDIENTES.push(res.rows.item(i));
}
return this.EXPEDIENTES;
}
}, (e) => {
console.log(" Get all expedientes error", JSON.stringify(e));
});
}
//getEventBy id
getEventById(id) {
var hashattachment = false;
var ismeeting = false;
var isrecurring = false;
var isallday = false;
var body;
var attendes;
var organizer;
var eventrecurrence;
var attachment;
return this.dbInstance.executeSql(`SELECT * FROM ${this.events} WHERE EventId = ? `, [id]).then((res) => {
this.EVENTS = [];
if (res.rows.length > 0) {
for (var i = 0; i < res.rows.length; i++) {
if (res.rows.item(i).HasAttachments === "true") {
hashattachment = true
}
if (res.rows.item(i).IsMeeting === "true") {
ismeeting = true
}
if (res.rows.item(i).IsRecurring === "true") {
isrecurring = true
}
if (res.rows.item(i).IsAllDayEvent === "true") {
isallday = true
}
/* if (res.rows.item(i).Body != "") {
body = JSON.parse(res.rows.item(i).Body);
}
if (res.rows.item(i).Attendees !="") {
attendes = JSON.parse(res.rows.item(i).Attendees);
}
if(res.rows.item(i).Organizer !=""){
organizer = JSON.parse(res.rows.item(i).Organizer);
}
if(res.row.item(i).EventRecurrence != ""){
eventrecurrence = JSON.parse(res.row.item(i).EventRecurrence);
}
if(res.row.item(i).Attachments != ""){
attachment = JSON.parse(res.row.item(i).Attachments);
}
*/
let event = {
EventId: res.rows.item(i).EventId,
HasAttachments: hashattachment,
Subject: res.rows.item(i).Subject,
Location: res.rows.item(i).Location,
CalendarId: res.rows.item(i).CalendarId,
CalendarName: res.rows.item(i).CalendarName,
StartDate: res.rows.item(i).StartDate,
EndDate: res.rows.item(i).EndDate,
EventType: res.rows.item(i).EventType,
Attendees: res.rows.item(i).Attendees,
IsMeeting: ismeeting,
IsRecurring: isrecurring,
IsAllDayEvent: isallday,
Body: res.rows.item(i).Body,
AppointmentState: res.rows.item(i).AppointmentState,
TimeZone: res.rows.item(i).TimeZone,
Organizer: res.rows.item(i).Organizer,
Category: res.rows.item(i).Category,
EventRecurrence: res.rows.item(i).EventRecurrence,
Attachments: res.rows.item(i).Attachments
}
this.EVENTS.push(event);
}
return this.EVENTS;
}
}, (e) => {
console.log(" Get events by id error", JSON.stringify(e));
});
}
}