Advertisement
Guest User

Untitled

a guest
Oct 24th, 2016
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.27 KB | None | 0 0
  1. package calymayor.chihuahuatic.model;
  2.  
  3. import android.content.ContentValues;
  4. import android.content.Context;
  5. import android.database.Cursor;
  6. import android.database.sqlite.SQLiteDatabase;
  7. import android.database.sqlite.SQLiteOpenHelper;
  8.  
  9. import java.util.ArrayList;
  10. import java.util.List;
  11.  
  12. /**
  13. * Created by miguel on 10/21/16.
  14. */
  15.  
  16. public class DBHandlerCTic extends SQLiteOpenHelper {
  17.  
  18. private static final String LOG = "DatabaseHelperCTic";
  19. private static final int DATABASE_VERSION = 1;
  20. private static final String DATABASE_NAME = "DBCTic";
  21.  
  22. //Table names
  23. private static final String TABLE_USERS = "users";
  24. private static final String TABLE_INSPECTIONS = "inspections";
  25. private static final String TABLE_INSPECTIONS_INCIDENTS = "inspectionsincidents";
  26.  
  27. //Column names
  28. private static final String KEY_USERNAME_ID = "idusername";
  29. private static final String KEY_CREATED = "created";
  30. private static final String KEY_EXPIRES = "expires";
  31. private static final String KEY_USERNAME = "username";
  32. private static final String KEY_PASSWORD = "password";
  33.  
  34. private static final String KEY_INSPECTION_ID = "idinspection";
  35. private static final String KEY_INSPECTION_DATE = "date";
  36. private static final String KEY_TYPE = "type";
  37. private static final String KEY_RESP = "resp";
  38. private static final String KEY_DETAIL = "detail";
  39.  
  40. private static final String KEY_INSPECTION_INCIDENTS_ID = "idinspectionsincidents";
  41. private static final String KEY_INCIDENT_STANDAR = "standar";
  42. private static final String KEY_INCIDENT_DATE = "date";
  43. private static final String KEY_LOCATION = "location";
  44. private static final String KEY_STATIONING = "stationing";
  45. private static final String KEY_JUNCTION = "junction";
  46. private static final String KEY_LOOP = "loop";
  47. private static final String KEY_WAY = "way";
  48. private static final String KEY_DISREPAIR = "disrepair";
  49. private static final String KEY_DISREPAIR_TYPE = "disrepairtype";
  50. private static final String KEY_DISREPAIR_LENGTH = "disrepairlength";
  51. private static final String KEY_DISREPAIR_DIMENSIONS = "disrepairdimensions";
  52. private static final String KEY_DISREPAIR_PHOTO = "disrepairphoto";
  53.  
  54. /*
  55. CREATE TABLE users (idusername INTEGER PRIMARY KEY,created DATETIME, expires TEXT, username TEXT, password TEXT)
  56. * */
  57.  
  58. //Table statements
  59. private static final String CREATE_TABLE_USERS =
  60. "CREATE TABLE " + TABLE_USERS
  61. + "(" + KEY_USERNAME_ID + " INTEGER PRIMARY KEY,"
  62. + KEY_CREATED + " TEXT,"
  63. + KEY_EXPIRES + " TEXT,"
  64. + KEY_USERNAME + " TEXT,"
  65. + KEY_PASSWORD + " TEXT" + ")";
  66.  
  67. private static final String CREATE_TABLE_INSPECTIONS =
  68. "CREATE TABLE " + TABLE_INSPECTIONS
  69. + "(" + KEY_INSPECTION_ID + " INTEGER PRIMARY KEY,"
  70. + KEY_INSPECTION_DATE + " TEXT,"
  71. + KEY_TYPE + " TEXT,"
  72. + KEY_RESP + " TEXT,"
  73. + KEY_DETAIL + " TEXT" + ")";
  74.  
  75. private static final String CREATE_TABLE_INSPECTIONS_INCIDENTS =
  76. "CREATE TABLE " + TABLE_INSPECTIONS_INCIDENTS
  77. + "(" + KEY_INSPECTION_INCIDENTS_ID + " INTEGER PRIMARY KEY,"
  78. + KEY_INCIDENT_STANDAR + " TEXT,"
  79. + KEY_INCIDENT_DATE + " TEXT,"
  80. + KEY_LOCATION + " TEXT,"
  81. + KEY_STATIONING + " TEXT,"
  82. + KEY_JUNCTION + " TEXT,"
  83. + KEY_LOOP + " TEXT,"
  84. + KEY_WAY + " TEXT,"
  85. + KEY_DISREPAIR + " TEXT,"
  86. + KEY_DISREPAIR_TYPE + " TEXT,"
  87. + KEY_DISREPAIR_LENGTH + " TEXT,"
  88. + KEY_DISREPAIR_DIMENSIONS + " TEXT,"
  89. + KEY_DISREPAIR_PHOTO + " TEXT" + ")";
  90.  
  91. // public DBHandlerCTic(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
  92. public DBHandlerCTic(Context context) {
  93. super(context, DATABASE_NAME, null, DATABASE_VERSION);
  94. }
  95.  
  96. @Override
  97. public void onCreate(SQLiteDatabase db) {
  98. db.execSQL(CREATE_TABLE_USERS);
  99. db.execSQL(CREATE_TABLE_INSPECTIONS);
  100. db.execSQL(CREATE_TABLE_INSPECTIONS_INCIDENTS);
  101. }
  102.  
  103. @Override
  104. public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  105. db.execSQL("DROP TABLE IF EXIST" + TABLE_USERS);
  106. db.execSQL("DROP TABLE IF EXIST" + TABLE_INSPECTIONS);
  107. db.execSQL("DROP TABLE IF EXIST" + TABLE_INSPECTIONS_INCIDENTS);
  108.  
  109. onCreate(db);
  110. }
  111.  
  112. //CRUD Operations
  113.  
  114. /*
  115. public long createToDo(Todo todo, long[] tag_ids) {
  116. SQLiteDatabase db = this.getWritableDatabase();
  117.  
  118. ContentValues values = new ContentValues();
  119. values.put(KEY_TODO, todo.getNote());
  120. values.put(KEY_STATUS, todo.getStatus());
  121. values.put(KEY_CREATED_AT, getDateTime());
  122.  
  123. // insert row
  124. long todo_id = db.insert(TABLE_TODO, null, values);
  125.  
  126. // assigning tags to todo
  127. for (long tag_id : tag_ids) {
  128. createTodoTag(todo_id, tag_id);
  129. }
  130.  
  131. return todo_id;
  132. }
  133. * */
  134.  
  135. //Insert Values
  136. public void addUser(Users users){
  137. SQLiteDatabase db = this.getWritableDatabase();
  138. ContentValues values = new ContentValues();
  139. values.put(KEY_CREATED, users.getCreated());
  140. values.put(KEY_EXPIRES, users.getExpires());
  141. values.put(KEY_USERNAME, users.getUsername());
  142. values.put(KEY_PASSWORD, users.getPassword());
  143.  
  144. db.insert(TABLE_USERS, null, values);
  145. db.close();
  146. }
  147.  
  148. public void addInspection(Inspections inspections){
  149. SQLiteDatabase db = this.getWritableDatabase();
  150. ContentValues values = new ContentValues();
  151. values.put(KEY_INSPECTION_DATE, inspections.getDate());
  152. values.put(KEY_TYPE, inspections.getType());
  153. values.put(KEY_RESP, inspections.getResp());
  154. values.put(KEY_DETAIL, inspections.getDetail());
  155.  
  156. db.insert(TABLE_INSPECTIONS, null, values);
  157. db.close();
  158. }
  159.  
  160. public void addInspectionIncidents(InspectionsIncidents inspectionsIncidents){
  161. SQLiteDatabase db = this.getWritableDatabase();
  162. ContentValues values = new ContentValues();
  163. values.put(KEY_INCIDENT_STANDAR, inspectionsIncidents.getStandar());
  164. values.put(KEY_INCIDENT_DATE, inspectionsIncidents.getDate());
  165. values.put(KEY_LOCATION, inspectionsIncidents.getLocation());
  166. values.put(KEY_STATIONING, inspectionsIncidents.getStationing());
  167. values.put(KEY_JUNCTION, inspectionsIncidents.getJunction());
  168. values.put(KEY_LOOP, inspectionsIncidents.getLoop());
  169. values.put(KEY_WAY, inspectionsIncidents.getWay());
  170. values.put(KEY_DISREPAIR, inspectionsIncidents.getDisrepair());
  171.  
  172. values.put(KEY_DISREPAIR_TYPE, inspectionsIncidents.getDisrepairtype());
  173. values.put(KEY_DISREPAIR_LENGTH, inspectionsIncidents.getDisrepairlength());
  174. values.put(KEY_DISREPAIR_DIMENSIONS, inspectionsIncidents.getDisrepairdimensions());
  175. values.put(KEY_DISREPAIR_PHOTO, inspectionsIncidents.getDisrepairphoto());
  176.  
  177. db.insert(TABLE_INSPECTIONS_INCIDENTS, null, values);
  178. db.close();
  179. }
  180.  
  181. //Read values
  182.  
  183. public Users getUser(int id){
  184. SQLiteDatabase db = this.getReadableDatabase();
  185.  
  186. Cursor cursor =
  187. db.query(TABLE_USERS,
  188. new String[]{KEY_USERNAME_ID, KEY_CREATED, KEY_EXPIRES, KEY_USERNAME, KEY_PASSWORD},
  189. KEY_USERNAME_ID +"=?",
  190. new String[] {String.valueOf(id)}, null, null, null, null);
  191.  
  192. if(cursor != null)
  193. cursor.moveToFirst();
  194.  
  195. Users user = new Users(
  196. Integer.parseInt(cursor.getString(0)),
  197. cursor.getString(1), cursor.getString(2),
  198. cursor.getString(3), cursor.getString(4));
  199.  
  200. return user;
  201. }
  202.  
  203. public List<Users> getAllUsers(){
  204. List<Users> usersList = new ArrayList<Users>();
  205. String selectQuery = "SELECT * FROM "+TABLE_USERS;
  206.  
  207. SQLiteDatabase db = this.getWritableDatabase();
  208. Cursor cursor = db.rawQuery(selectQuery, null);
  209.  
  210. if(cursor.moveToFirst()){
  211. do{
  212. Users users = new Users();
  213. users.setIdusername(Integer.parseInt(cursor.getString(0)));
  214. users.setCreated(cursor.getString(1));
  215. users.setExpires(cursor.getString(2));
  216. users.setUsername(cursor.getString(3));
  217. users.setPassword(cursor.getString(4));
  218.  
  219. usersList.add(users);
  220.  
  221. }while(cursor.moveToNext());
  222. }
  223.  
  224. return usersList;
  225. }
  226.  
  227. public int getUsersCount(){
  228. String countQuery = "SELECT * FROM " +TABLE_USERS;
  229. SQLiteDatabase db = this.getReadableDatabase();
  230. Cursor cursor = db.rawQuery(countQuery, null);
  231. cursor.close();
  232.  
  233. return cursor.getCount();
  234. }
  235.  
  236. public Inspections getInspections(int id){
  237. SQLiteDatabase db = this.getReadableDatabase();
  238.  
  239. Cursor cursor =
  240. db.query(TABLE_INSPECTIONS,
  241. new String[]{KEY_INSPECTION_ID, KEY_INSPECTION_DATE, KEY_TYPE, KEY_RESP, KEY_DETAIL},
  242. KEY_INSPECTION_ID +"=?",
  243. new String[] {String.valueOf(id)}, null, null, null, null);
  244.  
  245. if(cursor != null)
  246. cursor.moveToFirst();
  247.  
  248. Inspections inspection = new Inspections(
  249. Integer.parseInt(cursor.getString(0)),
  250. cursor.getString(1), cursor.getString(2),
  251. cursor.getString(3), cursor.getString(4));
  252.  
  253. return inspection;
  254. }
  255.  
  256. public List<Inspections> getAllInspections(){
  257. List<Inspections> inspectionList = new ArrayList<Inspections>();
  258. String selectQuery = "SELECT * FROM "+TABLE_INSPECTIONS;
  259.  
  260. SQLiteDatabase db = this.getWritableDatabase();
  261. Cursor cursor = db.rawQuery(selectQuery, null);
  262.  
  263. if(cursor.moveToFirst()){
  264. do{
  265. Inspections inspection = new Inspections();
  266. inspection.setIdinspection(Integer.parseInt(cursor.getString(0)));
  267. inspection.setDate(cursor.getString(1));
  268. inspection.setType(cursor.getString(2));
  269. inspection.setResp(cursor.getString(3));
  270. inspection.setDetail(cursor.getString(4));
  271.  
  272. inspectionList.add(inspection);
  273.  
  274. }while(cursor.moveToNext());
  275. }
  276.  
  277. return inspectionList;
  278. }
  279.  
  280. public List<InspectionsIncidents> getAllInspectionsIncidents(){
  281. List<InspectionsIncidents> inspectionIncidentsList = new ArrayList<InspectionsIncidents>();
  282. String selectQuery = "SELECT * FROM "+TABLE_INSPECTIONS_INCIDENTS;
  283.  
  284. SQLiteDatabase db = this.getWritableDatabase();
  285. Cursor cursor = db.rawQuery(selectQuery, null);
  286.  
  287. if(cursor.moveToFirst()){
  288. do{
  289. InspectionsIncidents inspectionIncidents = new InspectionsIncidents();
  290. inspectionIncidents.setIdinspectionsincidents(Integer.parseInt(cursor.getString(0)));
  291. inspectionIncidents.setStandar(cursor.getString(1));
  292. inspectionIncidents.setDate(cursor.getString(2));
  293. inspectionIncidents.setLocation(cursor.getString(3));
  294. inspectionIncidents.setStationing(cursor.getString(4));
  295. inspectionIncidents.setJunction(cursor.getString(5));
  296. inspectionIncidents.setLoop(cursor.getString(6));
  297. inspectionIncidents.setWay(cursor.getString(7));
  298. inspectionIncidents.setDisrepair(cursor.getString(8));
  299. inspectionIncidents.setDisrepairtype(cursor.getString(9));
  300. inspectionIncidents.setDisrepairlength(cursor.getString(10));
  301. inspectionIncidents.setDisrepairdimensions(cursor.getString(11));
  302. inspectionIncidents.setDisrepairphoto(cursor.getString(12));
  303.  
  304. inspectionIncidentsList.add(inspectionIncidents);
  305.  
  306. }while(cursor.moveToNext());
  307. }
  308.  
  309. return inspectionIncidentsList;
  310. }
  311.  
  312. public int getInspectionsCount(){
  313. String countQuery = "SELECT * FROM " +TABLE_INSPECTIONS;
  314. SQLiteDatabase db = this.getReadableDatabase();
  315. Cursor cursor = db.rawQuery(countQuery, null);
  316. cursor.close();
  317.  
  318. return cursor.getCount();
  319. }
  320. //Updating
  321. public int updateUsers(Users users){
  322. SQLiteDatabase db =this.getWritableDatabase();
  323. ContentValues values = new ContentValues();
  324. values.put(KEY_CREATED, users.getCreated());
  325. values.put(KEY_EXPIRES, users.getExpires());
  326. values.put(KEY_USERNAME, users.getUsername());
  327. values.put(KEY_PASSWORD, users.getPassword());
  328.  
  329. return db.update(TABLE_USERS, values, KEY_USERNAME_ID + " = ?",
  330. new String[]{String.valueOf(users.getIdusername())});
  331. }
  332.  
  333. public int updateInspections(Inspections inspections){
  334. SQLiteDatabase db =this.getWritableDatabase();
  335. ContentValues values = new ContentValues();
  336. values.put(KEY_INSPECTION_DATE, inspections.getDate());
  337. values.put(KEY_TYPE, inspections.getType());
  338. values.put(KEY_RESP, inspections.getResp());
  339. values.put(KEY_DETAIL, inspections.getDetail());
  340.  
  341. return db.update(TABLE_INSPECTIONS, values, KEY_INSPECTION_ID + " = ?",
  342. new String[]{String.valueOf(inspections.getIdinspection())});
  343. }
  344.  
  345. //Deleting
  346. public void deleteUsers(Users users){
  347. SQLiteDatabase db = this.getWritableDatabase();
  348. db.delete(TABLE_USERS, KEY_USERNAME_ID + " = ?",
  349. new String[]{String.valueOf(users.getIdusername())});
  350. db.close();
  351. }
  352.  
  353. public void deleteInspections(Inspections inspection){
  354. SQLiteDatabase db = this.getWritableDatabase();
  355. db.delete(TABLE_INSPECTIONS, KEY_INSPECTION_ID + " = ?",
  356. new String[]{String.valueOf(inspection.getIdinspection())});
  357. db.close();
  358. }
  359.  
  360. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement