· 8 years ago · Feb 27, 2018, 01:56 PM
1package com.example.test65.utilities;
2
3/**
4 * Created by l1maginaire on 2/25/18.
5 */
6
7import android.content.ContentValues;
8import android.content.Context;
9import android.database.Cursor;
10import android.database.SQLException;
11import android.database.sqlite.SQLiteDatabase;
12import android.database.sqlite.SQLiteOpenHelper;
13import android.util.Log;
14
15import com.example.test65.mvp.model.Employee;
16import com.example.test65.mvp.model.EmployeeContract;
17
18import java.util.ArrayList;
19import java.util.Arrays;
20import java.util.List;
21
22import javax.inject.Inject;
23
24import static com.example.test65.mvp.model.EmployeeContract.Entry.*;
25import static com.example.test65.utilities.StringProcessor.specRefactoring;
26
27public class Storage extends SQLiteOpenHelper {
28 private static final String TAG = Storage.class.getSimpleName();
29
30 private static final String DROP_TABLE = "DROP TABLE IF EXISTS " + TABLE_NAME;
31
32 private static final String CREATE_TABLE =
33 "CREATE TABLE IF NOT EXISTS " + TABLE_NAME + " (" +
34 NAME + " STRING NOT NULL, " +
35 SURNAME + " STRING NOT NULL, " +
36 BIRTHDATE + " STRING, " +
37 AVATAR_URL + " STRING, " +
38 SPECIALTY_ID + " STRING NOT NULL, " +
39 SPECIALTY_NAME + " STRING NOT NULL" +
40 "); ";
41
42 @Inject
43 public Storage(Context context) {
44 super(context, TABLE_NAME, null, 1);
45 }
46
47 @Override
48 public void onCreate(SQLiteDatabase db) {
49 try {
50 db.execSQL(CREATE_TABLE);
51 } catch (SQLException e) {
52 Log.d(TAG, e.getMessage());
53 }
54 }
55
56 @Override
57 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
58 db.execSQL(DROP_TABLE);
59 onCreate(db);
60 }
61
62 public void addEmployee(Employee employee) {
63 SQLiteDatabase db = this.getWritableDatabase();
64
65 ContentValues values = new ContentValues();
66 values.put(NAME, employee.getName());
67 values.put(SURNAME, employee.getSurname());
68 values.put(BIRTHDATE, employee.getBirthdate());
69 values.put(AVATAR_URL, employee.getAvatarUrl());
70 String specNames = "";
71 for (String name : employee.getSpec_name()) {
72 specNames += name + ",";
73 }
74 values.put(SPECIALTY_NAME, specNames);
75 String specIDs = "";
76 for (String id : employee.getSpec_id()) {
77 specIDs += String.valueOf(id) + ",";
78 }
79 values.put(SPECIALTY_ID, specIDs);
80 try {
81 db.insert(TABLE_NAME, null, values);
82 } catch (SQLException e) {
83 Log.d(TAG, e.getMessage());
84 }
85 db.close();
86 }
87
88 public List<Employee> getEmployes(String selectionCode) {
89 List<Employee> employeeList = new ArrayList<>();
90 SQLiteDatabase db = getReadableDatabase();
91 Cursor cursor;
92 try {
93 switch (selectionCode) {
94 case "managers":
95 cursor = db.query(TABLE_NAME, null, "s_id LIKE ?", new String[]{"%102,"},
96 null, null, null);
97 break;
98 case "developers":
99 cursor = db.query(TABLE_NAME, null, "s_id LIKE ?", new String[]{"101,%"},
100 null, null, null);
101 break;
102 default:
103 cursor = db.query(TABLE_NAME, null, null, null,
104 null, null, null);
105 break;
106 }
107 if (cursor != null) {
108 if (cursor.getCount() > 0) {
109 if (cursor.moveToFirst()) {
110 do {
111 Employee employee = new Employee();
112 employee.setName(cursor.getString(cursor.getColumnIndex(NAME)));
113 employee.setSurname(cursor.getString(cursor.getColumnIndex(SURNAME)));
114 employee.setBirthdate(cursor.getString(cursor.getColumnIndex(BIRTHDATE)));
115 employee.setAvatarUrl(cursor.getString(cursor.getColumnIndex(AVATAR_URL)));
116 specRefactoring(cursor.getString(cursor.getColumnIndex(SPECIALTY_NAME)),
117 cursor.getString(cursor.getColumnIndex(SPECIALTY_ID)), employee);
118 employeeList.add(employee);
119 } while (cursor.moveToNext());
120 }
121 }
122 }
123 } catch (SQLException e) {
124 Log.d(TAG, "Can't fetch data from a database: " + e.getMessage());
125 }
126 return employeeList;
127 }
128}