Skip to content

Commit

Permalink
Merge beda2ca into e9c1cb7
Browse files Browse the repository at this point in the history
  • Loading branch information
rkodev committed Jul 23, 2019
2 parents e9c1cb7 + beda2ca commit 1f63abe
Show file tree
Hide file tree
Showing 7 changed files with 289 additions and 62 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
VERSION_NAME=1.4.16-SNAPSHOT
VERSION_NAME=1.4.17-SNAPSHOT
VERSION_CODE=2
GROUP=org.smartregister
POM_SETTING_DESCRIPTION=OpenSRP Client Immunization
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ public class ServiceType {
private Long id;
private String type;
private String name;
private String serviceGroup;
private String serviceNameEntity;
private String serviceNameEntityId;
private String dateEntity;
Expand All @@ -22,13 +23,15 @@ public class ServiceType {
public ServiceType() {
}

public ServiceType(Long id, String type, String name, String serviceNameEntity, String
/*
public ServiceType(Long id, String type, String name, String serviceGroup, String serviceNameEntity, String
serviceNameEntityId, String dateEntity, String dateEntityId, String units, String
serviceLogic, String prerequisite, String preOffset, String expiryOffset, String
milestoneOffset, Long updatedAt) {
this.id = id;
this.type = type;
this.name = name;
this.serviceGroup = serviceGroup;
this.serviceNameEntity = serviceNameEntity;
this.serviceNameEntityId = serviceNameEntityId;
this.dateEntity = dateEntity;
Expand All @@ -41,6 +44,113 @@ public ServiceType(Long id, String type, String name, String serviceNameEntity,
this.milestoneOffset = milestoneOffset;
this.updatedAt = updatedAt;
}
*/

private ServiceType(Builder builder) {
this.id = builder.id;
this.type = builder.type;
this.name = builder.name;
this.serviceGroup = builder.serviceGroup;
this.serviceNameEntity = builder.serviceNameEntity;
this.serviceNameEntityId = builder.serviceNameEntityId;
this.dateEntity = builder.dateEntity;
this.dateEntityId = builder.dateEntityId;
this.units = builder.units;
this.serviceLogic = builder.serviceLogic;
this.prerequisite = builder.prerequisite;
this.preOffset = builder.preOffset;
this.expiryOffset = builder.expiryOffset;
this.milestoneOffset = builder.milestoneOffset;
this.updatedAt = builder.updatedAt;
}

public static class Builder {
private Long id;
private String type;
private String name;
private String serviceGroup;
private String serviceNameEntity;
private String serviceNameEntityId;
private String dateEntity;
private String dateEntityId;
private String units;
private String serviceLogic;
private String prerequisite;
private String preOffset;
private String expiryOffset;
private String milestoneOffset;
private Long updatedAt;

public Builder(Long id, String type, String name) {
this.id = id;
this.type = type;
this.name = name;
}

public Builder withServiceGroup(String serviceGroup) {
this.serviceGroup = serviceGroup;
return this;
}

public Builder withServiceNameEntity(String serviceNameEntity) {
this.serviceNameEntity = serviceNameEntity;
return this;
}

public Builder withServiceNameEntityId(String serviceNameEntityId) {
this.serviceNameEntityId = serviceNameEntityId;
return this;
}

public Builder withDateEntity(String dateEntity) {
this.dateEntity = dateEntity;
return this;
}

public Builder withDateEntityId(String dateEntityId) {
this.dateEntityId = dateEntityId;
return this;
}

public Builder withUnits(String units) {
this.units = units;
return this;
}

public Builder withServiceLogic(String serviceLogic) {
this.serviceLogic = serviceLogic;
return this;
}

public Builder withPrerequisite(String prerequisite) {
this.prerequisite = prerequisite;
return this;
}

public Builder withPreOffset(String preOffset) {
this.preOffset = preOffset;
return this;
}

public Builder withExpiryOffset(String expiryOffset) {
this.expiryOffset = expiryOffset;
return this;
}

public Builder withMilestoneOffset(String milestoneOffset) {
this.milestoneOffset = milestoneOffset;
return this;
}

public Builder withUpdatedAt(Long updatedAt) {
this.updatedAt = updatedAt;
return this;
}

public ServiceType build() {
return new ServiceType(this);
}
}

public Long getId() {
return id;
Expand All @@ -66,6 +176,14 @@ public void setName(String name) {
this.name = name;
}

public String getServiceGroup() {
return serviceGroup;
}

public void setServiceGroup(String serviceGroup) {
this.serviceGroup = serviceGroup;
}

public String getServiceNameEntity() {
return serviceNameEntity;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,14 @@
import java.util.Calendar;
import java.util.List;

import timber.log.Timber;

public class RecurringServiceTypeRepository extends BaseRepository {
public static final String TABLE_NAME = "recurring_service_types";
public static final String ID_COLUMN = "_id";
public static final String TYPE = "type";
public static final String NAME = "name";
public static final String SERVICE_GROUP = "service_group";
public static final String SERVICE_NAME_ENTITY = "service_name_entity";
public static final String SERVICE_NAME_ENTITY_ID = "service_name_entity_id";
public static final String DATE_ENTITY = "date_entity";
Expand All @@ -31,13 +34,16 @@ public class RecurringServiceTypeRepository extends BaseRepository {
public static final String EXPIRY_OFFSET = "expiry_offset";
public static final String MILESTONE_OFFSET = "milestone_offset";
public static final String UPDATED_AT_COLUMN = "updated_at";
public static final String[] TABLE_COLUMNS = {ID_COLUMN, TYPE, NAME, SERVICE_NAME_ENTITY, SERVICE_NAME_ENTITY_ID, DATE_ENTITY, DATE_ENTITY_ID, UNITS, SERVICE_LOGIC, PREREQUISITE, PRE_OFFSET, EXPIRY_OFFSET, MILESTONE_OFFSET, UPDATED_AT_COLUMN};
public static final String[] TABLE_COLUMNS = {ID_COLUMN, TYPE, NAME, SERVICE_GROUP, SERVICE_NAME_ENTITY, SERVICE_NAME_ENTITY_ID, DATE_ENTITY, DATE_ENTITY_ID, UNITS, SERVICE_LOGIC, PREREQUISITE, PRE_OFFSET, EXPIRY_OFFSET, MILESTONE_OFFSET, UPDATED_AT_COLUMN};
private static final String TAG = RecurringServiceTypeRepository.class.getCanonicalName();
private static final String CREATE_TABLE_SQL = "CREATE TABLE recurring_service_types (_id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,type VARCHAR NOT NULL, name VARCHAR NOT NULL,service_name_entity VARCHAR,service_name_entity_id VARCHAR, date_entity VARCHAR, date_entity_id VARCHAR, units VARCHAR,service_logic VARCHAR NULL,prerequisite VARCHAR NULL,pre_offset VARCHAR NULL,expiry_offset VARCHAR NULL, milestone_offset VARCHAR NULL, updated_at INTEGER NULL, UNIQUE(type, name) ON CONFLICT IGNORE)";
private static final String CREATE_TABLE_SQL = "CREATE TABLE recurring_service_types (_id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,type VARCHAR NOT NULL, name VARCHAR NOT NULL, service_group VARCHAR,service_name_entity VARCHAR,service_name_entity_id VARCHAR, date_entity VARCHAR, date_entity_id VARCHAR, units VARCHAR,service_logic VARCHAR NULL,prerequisite VARCHAR NULL,pre_offset VARCHAR NULL,expiry_offset VARCHAR NULL, milestone_offset VARCHAR NULL, updated_at INTEGER NULL, UNIQUE(type, name) ON CONFLICT IGNORE)";
private static final String TYPE_INDEX = "CREATE INDEX " + TABLE_NAME + "_" + TYPE + "_index ON " + TABLE_NAME + "(" + TYPE + " COLLATE NOCASE);";
private static final String NAME_INDEX = "CREATE INDEX " + TABLE_NAME + "_" + NAME + "_index ON " + TABLE_NAME + "(" + NAME + " COLLATE NOCASE);";
private static final String UPDATED_AT_INDEX = "CREATE INDEX " + TABLE_NAME + "_" + UPDATED_AT_COLUMN + "_index ON " + TABLE_NAME + "(" + UPDATED_AT_COLUMN + ");";


public static final String ADD_SERVICE_GROUP_COLUMN = "ALTER TABLE " + TABLE_NAME + " ADD " + SERVICE_GROUP + " VARCHAR;";

public RecurringServiceTypeRepository(Repository repository) {
super(repository);
}
Expand Down Expand Up @@ -77,10 +83,10 @@ public void add(ServiceType serviceType, SQLiteDatabase database_) {
} else {
String idSelection = ID_COLUMN + " = ?";
database.update(TABLE_NAME, createValuesFor(serviceType), idSelection,
new String[] {serviceType.getId().toString()});
new String[]{serviceType.getId().toString()});
}
} catch (Exception e) {
Log.e(TAG, e.getMessage(), e);
Timber.e(e);
}
}

Expand All @@ -93,14 +99,14 @@ public ServiceType find(Long caseId, SQLiteDatabase database_) {
database = getReadableDatabase();
}
cursor = database
.query(TABLE_NAME, TABLE_COLUMNS, ID_COLUMN + " = ?", new String[] {caseId.toString()}, null, null, null,
.query(TABLE_NAME, TABLE_COLUMNS, ID_COLUMN + " = ?", new String[]{caseId.toString()}, null, null, null,
null);
List<ServiceType> serviceTypes = readAllServiceTypes(cursor);
if (!serviceTypes.isEmpty()) {
serviceType = serviceTypes.get(0);
}
} catch (Exception e) {
Log.e(TAG, e.getMessage(), e);
Timber.e(e);
} finally {
if (cursor != null) {
cursor.close();
Expand All @@ -114,6 +120,7 @@ private ContentValues createValuesFor(ServiceType serviceType) {
values.put(ID_COLUMN, serviceType.getId());
values.put(TYPE, serviceType.getType() != null ? addHyphen(serviceType.getType()) : null);
values.put(NAME, serviceType.getName() != null ? addHyphen(serviceType.getName()) : null);
values.put(SERVICE_GROUP, serviceType.getServiceGroup());
values.put(SERVICE_NAME_ENTITY, serviceType.getServiceNameEntity());
values.put(SERVICE_NAME_ENTITY_ID, serviceType.getServiceNameEntityId());
values.put(DATE_ENTITY, serviceType.getDateEntity());
Expand Down Expand Up @@ -147,27 +154,30 @@ private List<ServiceType> readAllServiceTypes(Cursor cursor) {


serviceTypes.add(
new ServiceType(cursor.getLong(cursor.getColumnIndex(ID_COLUMN)),

new ServiceType.Builder(
cursor.getLong(cursor.getColumnIndex(ID_COLUMN)),
type,
name,
cursor.getString(cursor.getColumnIndex(SERVICE_NAME_ENTITY)),
cursor.getString(cursor.getColumnIndex(SERVICE_NAME_ENTITY_ID)),
cursor.getString(cursor.getColumnIndex(DATE_ENTITY)),
cursor.getString(cursor.getColumnIndex(DATE_ENTITY_ID)),
cursor.getString(cursor.getColumnIndex(UNITS)),
cursor.getString(cursor.getColumnIndex(SERVICE_LOGIC)),
cursor.getString(cursor.getColumnIndex(PREREQUISITE)),
cursor.getString(cursor.getColumnIndex(PRE_OFFSET)),
cursor.getString(cursor.getColumnIndex(EXPIRY_OFFSET)),
cursor.getString(cursor.getColumnIndex(MILESTONE_OFFSET)),
cursor.getLong(cursor.getColumnIndex(UPDATED_AT_COLUMN))
));
name
).withServiceGroup(cursor.getString(cursor.getColumnIndex(SERVICE_GROUP)))
.withServiceNameEntity(cursor.getString(cursor.getColumnIndex(SERVICE_NAME_ENTITY)))
.withServiceNameEntityId(cursor.getString(cursor.getColumnIndex(SERVICE_NAME_ENTITY_ID)))
.withDateEntity(cursor.getString(cursor.getColumnIndex(DATE_ENTITY)))
.withDateEntityId(cursor.getString(cursor.getColumnIndex(DATE_ENTITY_ID)))
.withUnits(cursor.getString(cursor.getColumnIndex(UNITS)))
.withServiceLogic(cursor.getString(cursor.getColumnIndex(SERVICE_LOGIC)))
.withPrerequisite(cursor.getString(cursor.getColumnIndex(PREREQUISITE)))
.withPreOffset(cursor.getString(cursor.getColumnIndex(PRE_OFFSET)))
.withExpiryOffset(cursor.getString(cursor.getColumnIndex(EXPIRY_OFFSET)))
.withMilestoneOffset(cursor.getString(cursor.getColumnIndex(MILESTONE_OFFSET)))
.withUpdatedAt(cursor.getLong(cursor.getColumnIndex(UPDATED_AT_COLUMN))).build()
);

cursor.moveToNext();
}
}
} catch (Exception e) {
Log.e(TAG, e.getMessage(), e);
Timber.e(e);
} finally {
if (cursor != null) {
cursor.close();
Expand Down Expand Up @@ -199,7 +209,7 @@ public List<ServiceType> findByType(String type) {
SQLiteDatabase database = getReadableDatabase();
Cursor cursor = database
.query(TABLE_NAME, TABLE_COLUMNS, TYPE + " = ? " + COLLATE_NOCASE + " ORDER BY " + UPDATED_AT_COLUMN,
new String[] {type}, null, null, null, null);
new String[]{type}, null, null, null, null);
return readAllServiceTypes(cursor);
}

Expand All @@ -214,7 +224,7 @@ public List<ServiceType> searchByName(String name_) {
SQLiteDatabase database = getReadableDatabase();
Cursor cursor = database
.query(TABLE_NAME, TABLE_COLUMNS, NAME + " LIKE ? " + COLLATE_NOCASE + " ORDER BY " + UPDATED_AT_COLUMN,
new String[] {"%" + name + "%"}, null, null, null, null);
new String[]{"%" + name + "%"}, null, null, null, null);
return readAllServiceTypes(cursor);
}

Expand All @@ -225,7 +235,7 @@ public ServiceType find(Long caseId) {
public void deleteServiceType(Long caseId) {
ServiceType serviceType = find(caseId, null);
if (serviceType != null) {
getWritableDatabase().delete(TABLE_NAME, ID_COLUMN + "= ?", new String[] {caseId.toString()});
getWritableDatabase().delete(TABLE_NAME, ID_COLUMN + "= ?", new String[]{caseId.toString()});
}
}

Expand All @@ -235,15 +245,32 @@ public List<ServiceType> fetchAll() {
return readAllServiceTypes(cursor);
}

/***
* Extract the list of service types that belong to a specific group
* @param group
* @return
*/
public List<String> fetchTypes(String group) {
SQLiteDatabase database = getReadableDatabase();
Cursor cursor = database.query(TABLE_NAME, new String[]{TYPE}, SERVICE_GROUP + " = ? ", new String[]{group}, TYPE, null, UPDATED_AT_COLUMN);
return extractCursorTypes(cursor);
}

/**
* Extracts all service types
*
* @return
*/
public List<String> fetchTypes() {
String sql = " SELECT " + TYPE + " FROM " + TABLE_NAME + " GROUP BY " + TYPE + " ORDER BY " + UPDATED_AT_COLUMN;
SQLiteDatabase database = getReadableDatabase();
Cursor cursor = database.rawQuery(sql, null);
return extractCursorTypes(cursor);
}

public List<String> extractCursorTypes(Cursor cursor) {
List<String> types = new ArrayList<>();

try {

if (cursor != null && cursor.getCount() > 0 && cursor.moveToFirst()) {
while (!cursor.isAfterLast()) {
String type = cursor.getString(cursor.getColumnIndex(TYPE));
Expand All @@ -253,11 +280,10 @@ public List<String> fetchTypes() {
types.add(type);

cursor.moveToNext();

}
}
} catch (Exception e) {
Log.e(TAG, e.getMessage(), e);
Timber.e(e);
} finally {
if (cursor != null) {
cursor.close();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import java.util.Arrays;
import java.util.List;

import timber.log.Timber;

/**
* Created by keyman on 17/05/2017.
*/
Expand All @@ -42,6 +44,7 @@ public static void populateRecurringServices(Context context, SQLiteDatabase dat
String type = JsonFormUtils.getString(jsonObject, "type");
String serviceLogic = JsonFormUtils.getString(jsonObject, "service_logic");
String units = JsonFormUtils.getString(jsonObject, "units");
String service_group = JsonFormUtils.getString(jsonObject, "service_group");
String serviceNameEntity = null;
String serviceNameEntityId = null;

Expand Down Expand Up @@ -122,6 +125,7 @@ public static void populateRecurringServices(Context context, SQLiteDatabase dat
serviceType.setId(id);
serviceType.setType(type);
serviceType.setName(name);
serviceType.setServiceGroup(service_group);
serviceType.setServiceNameEntity(serviceNameEntity);
serviceType.setServiceNameEntityId(serviceNameEntityId);
serviceType.setDateEntity(dateEntity);
Expand All @@ -143,7 +147,7 @@ public static void populateRecurringServices(Context context, SQLiteDatabase dat
}
}
} catch (JSONException e) {
Log.e(IMDatabaseUtils.class.getName(), e.getMessage(), e);
Timber.e(e);
}
}

Expand Down

0 comments on commit 1f63abe

Please sign in to comment.