Skip to content

Commit

Permalink
Merge pull request #50 from dhis2/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
ifoche authored Jul 21, 2017
2 parents 38cc276 + 7c1b9e1 commit 718e4a7
Show file tree
Hide file tree
Showing 94 changed files with 2,420 additions and 161 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,6 @@ build

#Windows
*.db

# Last commit file
app/src/main/res/raw/lastcommit.txt
18 changes: 17 additions & 1 deletion api/build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
apply plugin: 'com.android.library'
apply plugin: 'com.neenbedankt.android-apt'
apply plugin: 'jacoco-android'

android {
compileSdkVersion 25
Expand All @@ -17,8 +18,20 @@ android {
targetCompatibility JavaVersion.VERSION_1_7
}

packagingOptions {
exclude 'META-INF/LICENSE'
exclude 'META-INF/NOTICE'
}

lintOptions {
disable 'RtlSymmetry', 'RtlHardcoded'
disable 'RtlSymmetry', 'RtlHardcoded', 'RestrictedApi'
abortOnError false
}

buildTypes {
debug {
testCoverageEnabled true
}
}
}

Expand Down Expand Up @@ -47,4 +60,7 @@ dependencies {

// Other
compile 'joda-time:joda-time:2.9.2'

// Java test dependencies
testCompile "junit:junit:4.12"
}
1 change: 1 addition & 0 deletions api/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package="org.hisp.dhis.android.dashboard.api">

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

<application android:allowBackup="true" />

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@

package org.hisp.dhis.android.dashboard.api.controllers;

import static org.hisp.dhis.android.dashboard.api.models.BaseIdentifiableObject.merge;
import static org.hisp.dhis.android.dashboard.api.models.BaseIdentifiableObject.toListIds;
import static org.hisp.dhis.android.dashboard.api.models.BaseIdentifiableObject.toMap;
import static org.hisp.dhis.android.dashboard.api.utils.NetworkUtils.findLocationHeader;
import static org.hisp.dhis.android.dashboard.api.utils.NetworkUtils.handleApiException;
import static org.hisp.dhis.android.dashboard.api.utils.NetworkUtils.unwrapResponse;

import android.net.Uri;

import com.raizlabs.android.dbflow.sql.builder.Condition;
Expand Down Expand Up @@ -61,13 +68,6 @@
import retrofit.client.Header;
import retrofit.client.Response;

import static org.hisp.dhis.android.dashboard.api.models.BaseIdentifiableObject.merge;
import static org.hisp.dhis.android.dashboard.api.models.BaseIdentifiableObject.toListIds;
import static org.hisp.dhis.android.dashboard.api.models.BaseIdentifiableObject.toMap;
import static org.hisp.dhis.android.dashboard.api.utils.NetworkUtils.findLocationHeader;
import static org.hisp.dhis.android.dashboard.api.utils.NetworkUtils.handleApiException;
import static org.hisp.dhis.android.dashboard.api.utils.NetworkUtils.unwrapResponse;

final class DashboardController {
final DhisApi mDhisApi;

Expand Down Expand Up @@ -96,6 +96,11 @@ private static List<Dashboard> queryDashboards() {
.queryList();
}

public static List<DashboardElement> queryAllDashboardElement() {
return new Select().from(DashboardElement.class)
.queryList();
}

private static List<DashboardItem> queryDashboardItems(Dashboard dashboard) {
Where<DashboardItem> where = new Select().from(DashboardItem.class)
.where(Condition.column(DashboardItem$Table
Expand Down Expand Up @@ -164,7 +169,8 @@ private List<Dashboard> updateDashboards(DateTime lastUpdated) throws APIExcepti
"]");

if (lastUpdated != null) {
QUERY_MAP_FULL.put("filter", "lastUpdated:gt:" + lastUpdated.toString());
QUERY_MAP_FULL.put("filter",
"lastUpdated:gt:" + lastUpdated.toLocalDateTime().toString());
}

// List of dashboards with UUIDs (without content). This list is used
Expand All @@ -183,8 +189,11 @@ private List<Dashboard> updateDashboards(DateTime lastUpdated) throws APIExcepti
continue;
}

int i=0;
for (DashboardItem item : dashboard.getDashboardItems()) {
item.setDashboard(dashboard);
item.setOrderPosition(i);
i++;
}
}
}
Expand Down Expand Up @@ -213,7 +222,7 @@ private List<DashboardItem> updateDashboardItems(List<Dashboard> dashboards, Dat
QUERY_MAP_BASIC.put("fields", "id,created,lastUpdated,shape");

if (lastUpdated != null) {
QUERY_MAP_BASIC.put("filter", "lastUpdated:gt:" + lastUpdated.toString());
QUERY_MAP_BASIC.put("filter", "lastUpdated:gt:" + lastUpdated.toLocalDateTime().toString());
}

// List of actual dashboard items.
Expand Down Expand Up @@ -628,6 +637,8 @@ private List<DashboardItemContent> updateApiResources(DateTime lastUpdated) thro
DashboardItemContent.TYPE_REPORTS, lastUpdated));
dashboardItemContent.addAll(updateApiResourceByType(
DashboardItemContent.TYPE_RESOURCES, lastUpdated));
dashboardItemContent.addAll(updateApiResourceByType(
DashboardItemContent.TYPE_MESSAGES, lastUpdated));
return dashboardItemContent;
}

Expand Down Expand Up @@ -682,6 +693,8 @@ private List<DashboardItemContent> getApiResourceByType(String type, Map<String,
return unwrapResponse(mDhisApi.getReports(queryParams), "reports");
case DashboardItemContent.TYPE_RESOURCES:
return unwrapResponse(mDhisApi.getResources(queryParams), "documents");
case DashboardItemContent.TYPE_MESSAGES:
return unwrapResponse(mDhisApi.getResources(queryParams), "documents");
default:
throw new IllegalArgumentException("Unsupported DashboardItemContent type");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@

package org.hisp.dhis.android.dashboard.api.controllers;

import static org.hisp.dhis.android.dashboard.api.utils.Preconditions.isNull;

import android.content.Context;

import com.raizlabs.android.dbflow.config.FlowManager;
Expand All @@ -42,14 +44,18 @@
import org.hisp.dhis.android.dashboard.api.persistence.preferences.DateTimeManager;
import org.hisp.dhis.android.dashboard.api.persistence.preferences.LastUpdatedManager;

import static org.hisp.dhis.android.dashboard.api.utils.Preconditions.isNull;
import org.hisp.dhis.android.dashboard.api.persistence.preferences.SettingsManager;

public class DhisController {
private static DhisController mDhisController;
private Session mSession;
private DhisApi mDhisApi;
private Context mContext;

public enum ImageNetworkPolicy {NO_CACHE, CACHE}

private DhisController(Context context) {
mContext = context;
FlowManager.init(context);
LastUpdatedManager.init(context);
DateTimeManager.init(context);
Expand All @@ -73,6 +79,18 @@ public static DhisController getInstance() {
return mDhisController;
}

public static String buildImageUrl(String resource, String id, Context context) {
String widthUserPreference = SettingsManager.getInstance(context).getPreference(
(SettingsManager.CHART_WIDTH), SettingsManager.MINIMUM_WIDTH);
String heightUserPreference = SettingsManager.getInstance(context).getPreference(
(SettingsManager.CHART_HEIGHT), SettingsManager.MINIMUM_HEIGHT);
return getInstance().getServerUrl().newBuilder()
.addPathSegment("api").addPathSegment(resource).addPathSegment(id).addPathSegment(
"data.png")
.addQueryParameter("width", widthUserPreference).addQueryParameter("height", heightUserPreference)
.toString();
}

public UserAccount logInUser(HttpUrl serverUrl, Credentials credentials) throws APIException {
return signInUser(serverUrl, credentials);
}
Expand All @@ -90,7 +108,7 @@ public void logOutUser() throws APIException {

private UserAccount signInUser(HttpUrl serverUrl, Credentials credentials) throws APIException {
DhisApi dhisApi = RepoManager
.createService(serverUrl, credentials);
.createService(serverUrl, credentials, mContext);
UserAccount user = (new UserController(dhisApi)
.logInUser(serverUrl, credentials));

Expand Down Expand Up @@ -124,7 +142,7 @@ private void readSession() {
mDhisApi = RepoManager.createService(
mSession.getServerUrl(),
mSession.getCredentials()
);
, mContext);
}
}

Expand All @@ -147,4 +165,11 @@ public void syncDashboards() throws APIException {
public void syncInterpretations() throws APIException {
(new InterpretationController(mDhisApi)).syncInterpretations();
}

public void pullDashboardImages(ImageNetworkPolicy imageNetworkPolicy,Context context) {
(new PullImageController(context)).pullDashboardImages(imageNetworkPolicy);
}
public void pullInterpretationImages(ImageNetworkPolicy imageNetworkPolicy,Context context) {
(new PullImageController(context)).pullInterpretationImages(imageNetworkPolicy);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -541,7 +541,7 @@ private static List<DbOperation> createOperations(List<Interpretation> oldModels
continue;
}

if (newModel.getLastUpdated().isAfter(oldModel.getLastUpdated())) {
if (DateTime.parse(newModel.getLastUpdated()).isAfter(DateTime.parse(oldModel.getLastUpdated()))) {
newModel.setId(oldModel.getId());
ops.add(DbOperation.update(newModel));
}
Expand Down Expand Up @@ -573,6 +573,11 @@ private static List<Interpretation> queryInterpretations() {
.queryList();
}

public static List<InterpretationElement> queryAllInterpretationElements() {
return new Select().from(InterpretationElement.class)
.queryList();
}

private static List<User> queryInterpretationUsers() {
return new Select().from(User.class).queryList();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package org.hisp.dhis.android.dashboard.api.controllers;

import android.content.Context;

import com.squareup.picasso.MemoryPolicy;
import com.squareup.picasso.NetworkPolicy;

import org.hisp.dhis.android.dashboard.api.models.DashboardElement;
import org.hisp.dhis.android.dashboard.api.models.DashboardItemContent;
import org.hisp.dhis.android.dashboard.api.models.Interpretation;
import org.hisp.dhis.android.dashboard.api.models.InterpretationElement;
import org.hisp.dhis.android.dashboard.api.network.APIException;
import org.hisp.dhis.android.dashboard.api.utils.PicassoProvider;

import java.util.ArrayList;
import java.util.List;


final class PullImageController {

static Context mContext;

public PullImageController(Context context) {
mContext = context;
}

public void pullDashboardImages(DhisController.ImageNetworkPolicy imageNetworkPolicy) throws APIException {
List<String> requestList = new ArrayList<>();
requestList = downloadDashboardImages(requestList);
downloadImages(imageNetworkPolicy, requestList, mContext);
}

public void pullInterpretationImages(DhisController.ImageNetworkPolicy imageNetworkPolicy) throws APIException {
List<String> requestList = new ArrayList<>();
requestList = downloadInterpretationImages(requestList);
downloadImages(imageNetworkPolicy, requestList, mContext);
}

public static List<String> downloadInterpretationImages(List<String> requestList) {
for (InterpretationElement interpretationElement : InterpretationController
.queryAllInterpretationElements()) {
if (interpretationElement == null || interpretationElement.getType() == null) {
continue;
}
if (Interpretation.TYPE_CHART.equals(interpretationElement.getType())) {
requestList.add(
DhisController.buildImageUrl("charts", interpretationElement.getUId(),
mContext));
} else if (Interpretation.TYPE_MAP.equals(interpretationElement.getType())) {
requestList.add(DhisController.buildImageUrl("maps", interpretationElement.getUId(),
mContext));
}
}
return requestList;
}

public static List<String> downloadDashboardImages(List<String> requestList) {
for (DashboardElement element : DashboardController.queryAllDashboardElement()) {
if (element.getDashboardItem() == null
|| element.getDashboardItem().getType() == null) {
continue;
}

switch (element.getDashboardItem().getType()) {
case DashboardItemContent.TYPE_CHART: {
requestList.add(
DhisController.buildImageUrl("charts", element.getUId(), mContext));
break;
}
case DashboardItemContent.TYPE_EVENT_CHART: {
requestList.add(DhisController.buildImageUrl("eventCharts", element.getUId(),
mContext));
break;
}
case DashboardItemContent.TYPE_MAP: {
requestList.add(
DhisController.buildImageUrl("maps", element.getUId(), mContext));
break;
}
}
}
return requestList;
}

private static void downloadImages(DhisController.ImageNetworkPolicy imageNetworkPolicy,
final List<String> requestUrlList, final Context context) {

for (int i = 0; i < requestUrlList.size(); i++) {
final String request = requestUrlList.get(i);

if (imageNetworkPolicy == DhisController.ImageNetworkPolicy.NO_CACHE) {
PicassoProvider.getInstance(context, false)
.load(request).networkPolicy(NetworkPolicy.NO_CACHE)
.memoryPolicy(MemoryPolicy.NO_CACHE).fetch();
} else {
PicassoProvider.getInstance(context, false)
.load(request).fetch();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public final class Access {
*
* @return new Access object.
*/
static Access provideDefaultAccess() {
public static Access provideDefaultAccess() {
Access access = new Access();
access.setManage(true);
access.setExternalize(true);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package org.hisp.dhis.android.dashboard.api.models;

import com.raizlabs.android.dbflow.annotation.Column;
import com.raizlabs.android.dbflow.annotation.PrimaryKey;
import com.raizlabs.android.dbflow.annotation.Table;
import com.raizlabs.android.dbflow.structure.BaseModel;

import org.hisp.dhis.android.dashboard.api.models.meta.DbDhis;

@Table(databaseName = DbDhis.NAME)
public class AttributeDimension extends BaseModel {

@Column(name = "id")
@PrimaryKey(autoincrement = true)
long id;

UIDObject attribute;

public UIDObject getAttribute() {
return attribute;
}

public void setAttribute(UIDObject attribute) {
this.attribute = attribute;
}
}
Loading

0 comments on commit 718e4a7

Please sign in to comment.