private List<InterpretationComment> updateInterpretationComments( List<Interpretation> interpretations) { List<InterpretationComment> interpretationComments = new ArrayList<>(); if (interpretations != null && !interpretations.isEmpty()) { for (Interpretation interpretation : interpretations) { interpretationComments.addAll(interpretation.getComments()); } } return interpretationComments; }
private List<User> updateInterpretationUsers( List<Interpretation> interpretations, List<InterpretationComment> comments) { Map<String, User> users = new HashMap<>(); UserAccount currentUserAccount = mUserAccountService.getCurrentUserAccount(); User currentUser = mUserStore.queryByUid(currentUserAccount.getUId()); if (currentUser == null) { currentUser = mUserAccountService.toUser(currentUserAccount); } users.put(currentUser.getUId(), currentUser); if (interpretations != null && !interpretations.isEmpty()) { for (Interpretation interpretation : interpretations) { User user = interpretation.getUser(); if (users.containsKey(user.getUId())) { user = users.get(user.getUId()); interpretation.setUser(user); } else { users.put(user.getUId(), user); } } } if (comments != null && !comments.isEmpty()) { for (InterpretationComment comment : comments) { User user = comment.getUser(); if (users.containsKey(user.getUId())) { user = users.get(user.getUId()); comment.setUser(user); } else { users.put(user.getUId(), user); } } } return new ArrayList<>(users.values()); }
public void postInterpretation(Interpretation interpretation) throws ApiException { try { Response response; switch (interpretation.getType()) { /* case Interpretation.TYPE_CHART: { response = mDhisApi.postChartInterpretation( interpretation.getChart().getUId(), new TypedString(interpretation.getText())); break; } case Interpretation.TYPE_MAP: { response = mDhisApi.postMapInterpretation( interpretation.getMap().getUId(), new TypedString(interpretation.getText())); break; } case Interpretation.TYPE_REPORT_TABLE: { response = mDhisApi.postReportTableInterpretation( interpretation.getReportTable().getUId(), new TypedString(interpretation.getText())); break; } default: throw new IllegalArgumentException("Unsupported interpretation type"); */ } /* Header header = NetworkUtils.findLocationHeader(response.getHeaders()); String interpretationUid = Uri.parse(header .getValue()).getLastPathSegment(); */ // interpretation.setUId(interpretationUid); // interpretation.setAction(Action.SYNCED); mInterpretationStore.save(interpretation); updateInterpretationTimeStamp(interpretation); } catch (ApiException apiException) { // ApiExceptionHandler.handleApiException(apiException, interpretation, mInterpretationStore); } }
private List<DbOperation> createOperations( List<Interpretation> oldModels, List<Interpretation> newModels) { List<DbOperation> ops = new ArrayList<>(); Map<String, Interpretation> newModelsMap = modelUtils.toMap(newModels); Map<String, Interpretation> oldModelsMap = modelUtils.toMap(oldModels); for (String oldModelKey : oldModelsMap.keySet()) { Interpretation newModel = newModelsMap.get(oldModelKey); Interpretation oldModel = oldModelsMap.get(oldModelKey); if (newModel == null) { ops.add(DbOperation.with(mInterpretationStore).delete(oldModel)); continue; } if (newModel.getLastUpdated().isAfter(oldModel.getLastUpdated())) { newModel.setId(oldModel.getId()); ops.add(DbOperation.with(mInterpretationStore).update(newModel)); } newModelsMap.remove(oldModelKey); } for (String newModelKey : newModelsMap.keySet()) { Interpretation item = newModelsMap.get(newModelKey); // we also have to insert interpretation elements here ops.add(DbOperation.with(mInterpretationStore).insert(item)); List<InterpretationElement> elements = new ArrayList<>(); // = mInterpretationService.getInterpretationElements(item); for (InterpretationElement element : elements) { ops.add(DbOperation.with(mInterpretationElementStore).insert(element)); } } return ops; }
private List<Interpretation> updateInterpretations(DateTime lastUpdated) { final Map<String, String> QUERY_MAP_BASIC = new HashMap<>(); final Map<String, String> QUERY_MAP_FULL = new HashMap<>(); final String BASE = "id,created,lastUpdated,name,displayName,access"; QUERY_MAP_BASIC.put("fields", "id"); QUERY_MAP_FULL.put( "fields", BASE + ",text,type," + "chart" + "[" + BASE + "]," + "map" + "[" + BASE + "]," + "reportTable" + "[" + BASE + "]," + "user" + "[" + BASE + "]," + "dataSet" + "[" + BASE + "]," + "period" + "[" + BASE + "]," + "organisationUnit" + "[" + BASE + "]," + "comments" + "[" + BASE + ",user,text" + "]"); if (lastUpdated != null) { QUERY_MAP_FULL.put("filter", "lastUpdated:gt:" + lastUpdated.toString()); } /* List<Interpretation> actualInterpretations = NetworkUtils.unwrapResponse(mDhisApi .getInterpretations(QUERY_MAP_BASIC), "interpretations"); List<Interpretation> updatedInterpretations = NetworkUtils.unwrapResponse(mDhisApi .getInterpretations(QUERY_MAP_FULL), "interpretations"); */ List<Interpretation> updatedInterpretations = new ArrayList<>(); if (updatedInterpretations != null && !updatedInterpretations.isEmpty()) { for (Interpretation interpretation : updatedInterpretations) { // build relationship with comments if (interpretation.getComments() != null && !interpretation.getComments().isEmpty()) { for (InterpretationComment comment : interpretation.getComments()) { comment.setInterpretation(interpretation); } } // we need to set mime type and interpretation to each element switch (interpretation.getType()) { case Interpretation.TYPE_CHART: { interpretation.getChart().setType(InterpretationElement.TYPE_CHART); interpretation.getChart().setInterpretation(interpretation); break; } case Interpretation.TYPE_MAP: { interpretation.getMap().setType(InterpretationElement.TYPE_MAP); interpretation.getMap().setInterpretation(interpretation); break; } case Interpretation.TYPE_REPORT_TABLE: { interpretation.getReportTable().setType(InterpretationElement.TYPE_REPORT_TABLE); interpretation.getReportTable().setInterpretation(interpretation); break; } case Interpretation.TYPE_DATA_SET_REPORT: { interpretation.getDataSet().setType(InterpretationElement.TYPE_DATA_SET); interpretation.getPeriod().setType(InterpretationElement.TYPE_PERIOD); interpretation .getOrganisationUnit() .setType(InterpretationElement.TYPE_ORGANISATION_UNIT); interpretation.getDataSet().setInterpretation(interpretation); interpretation.getPeriod().setInterpretation(interpretation); interpretation.getOrganisationUnit().setInterpretation(interpretation); break; } } } } List<Interpretation> persistedInterpretations = null; // mInterpretationStore.filter(Action.TO_POST); if (persistedInterpretations != null && !persistedInterpretations.isEmpty()) { for (Interpretation interpretation : persistedInterpretations) { List<InterpretationElement> elements = mInterpretationElementStore.list(interpretation); // mInterpretationService.setInterpretationElements(interpretation, elements); List<InterpretationComment> comments = null; // mInterpretationCommentStore.queryByInterpretation(interpretation, Action.TO_POST); interpretation.setComments(comments); } } // return merge(actualInterpretations, updatedInterpretations, persistedInterpretations); return null; }