Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
gmutuhu committed Jan 29, 2016
2 parents 0690f12 + c5313be commit db0a052
Show file tree
Hide file tree
Showing 9 changed files with 179 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package org.devgateway.importtool.model;

import com.fasterxml.jackson.annotation.JsonAutoDetect;

import org.springframework.hateoas.Identifiable;

import javax.persistence.*;

import java.io.Serializable;


Expand All @@ -19,12 +21,14 @@ public class Project implements Identifiable<Long>, Serializable {
@Column(name = "id", unique = true, nullable = false)
private Long id;

@Lob
@Column(name = "title")
private String title;

@Column(name = "status")
private String status;

@Lob
@Column(name = "notes")
private String notes;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.util.StringUtils;

// TODO: Sort methods, move classes to generic helpers for all processors if possible
// TODO: Clean up code, find opportunities to reuse methods (example update/insert)
Expand Down Expand Up @@ -246,6 +247,7 @@ private void updateProject(JsonBean project, InternalDocument source, List<Field
Field destinationField = mapping.getDestinationField();

switch (sourceField.getType()) {
case RECIPIENT_COUNTRY:
case LIST:
if (!destinationField.getFieldName().equals("type_of_assistance") && !destinationField.getFieldName().equals("financing_instrument")) {
Optional<FieldValueMapping> optValueMapping = valueMappings.stream().filter(n -> {
Expand All @@ -257,7 +259,7 @@ private void updateProject(JsonBean project, InternalDocument source, List<Field
project.set(destinationField.getFieldName(), getCodeFromList(source, optValueMapping.get()));
}
}
break;
break;
case MULTILANG_STRING:
project.set(destinationField.getFieldName(), getMapFromString(source, destinationField.getFieldName(), mapping));
break;
Expand Down Expand Up @@ -361,6 +363,7 @@ private JsonBean transformProject(InternalDocument source, List<FieldMapping> fi
Field destinationField = mapping.getDestinationField();

switch (sourceField.getType()) {
case RECIPIENT_COUNTRY:
case LIST:
if (!destinationField.getFieldName().equals("type_of_assistance") && !destinationField.getFieldName().equals("financing_instrument")) {
Optional<FieldValueMapping> optValueMapping = valueMappings.stream().filter(n -> {
Expand Down Expand Up @@ -433,7 +436,9 @@ private JsonBean getTransactions(InternalDocument source, List<FieldMapping> fie
String currencyCode = source.getStringFields().get("default-currency");
String currencyIdString = getCurrencyId(currencyCode);
int currencyId = Integer.parseInt(currencyIdString);

FieldValue recipientCountry = source.getRecepientCountryFields().get("recipient-country").stream().findFirst().get();
Double percentage = (StringUtils.isEmpty(recipientCountry.getPercentage())) ? 100.00 : Double.parseDouble(recipientCountry.getPercentage());

for (FieldMapping mapping : fieldMappings) {
Field sourceField = mapping.getSourceField();
Field destinationField = mapping.getDestinationField();
Expand All @@ -460,7 +465,7 @@ private JsonBean getTransactions(InternalDocument source, List<FieldMapping> fie
fundingDetail.set("adjustment_type", getAdjustmentType(destinationSubType));
fundingDetail.set("transaction_date", getTransactionDate(dateString));
fundingDetail.set("currency", currencyId);
fundingDetail.set("transaction_amount", getTransactionAmount(amount));
fundingDetail.set("transaction_amount", getTransactionAmount(amount,percentage));
fundingDetails.add(fundingDetail);
}
}
Expand Down Expand Up @@ -509,9 +514,9 @@ private String getCurrencyId(String currencyCode) {
return currencyValue.getCode();
}

private Object getTransactionAmount(String amount) {
private Object getTransactionAmount(String amount, Double percentage) {
Double amountValue = Double.parseDouble(amount);
return amountValue;
return (amountValue * percentage)/100;
}

private int getIdFromList(String fieldValue, String sourceField, List<FieldMapping> fieldMappings, List<FieldValueMapping> valueMappings, Boolean useCode) throws ValueMappingException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ public List<Field> getFilterFields() {
NodeList nodeList = doc.getElementsByTagName(field.getFieldName());
List<FieldValue> reducedPossibleValues = new ArrayList<FieldValue>();
switch (field.getType()) {
case RECIPIENT_COUNTRY:
case LIST:
if (nodeList.getLength() > 0) {
for (int i = 0; i < nodeList.getLength(); i++) {
Expand Down Expand Up @@ -347,6 +348,31 @@ private List<InternalDocument> extractDocuments(Document doc) throws Exception {
}
}
break;
case RECIPIENT_COUNTRY:
Field filtersField = filterFieldList.stream().filter(n -> {
return field.getFieldName().equals(n.getFieldName());
}).findFirst().get();

fieldNodeList = element.getElementsByTagName(field.getFieldName());
List<FieldValue> recipients = new ArrayList<FieldValue>();
for (int j = 0; j < fieldNodeList.getLength(); j++) {
Element fieldElement = (Element) fieldNodeList.item(j);
FieldValue recipient = new FieldValue();
String code = fieldElement.getAttribute("code");
boolean includeCountry = includedByFilter(filtersField.getFilters(), code);
if(includeCountry){
recipient.setCode(code);
Optional<FieldValue> fieldValue = field.getPossibleValues().stream().filter(f -> f.getCode().equals(code)).findFirst();
if(fieldValue.isPresent()){
recipient.setValue(fieldValue.get().getValue());
}
recipient.setPercentage(fieldElement.getAttribute("percentage"));
recipients.add(recipient);
}

}
document.addRecepientCountryFields(field.getFieldName(), recipients);
break;
case STRING:
String stringValue = "";
fieldNodeList = element.getElementsByTagName(field.getFieldName());
Expand All @@ -361,7 +387,6 @@ private List<InternalDocument> extractDocuments(Document doc) throws Exception {
document.addStringField(field.getFieldName(), stringValue);
break;
case ORGANIZATION:

fieldNodeList = element.getElementsByTagName(field.getFieldName());
if (fieldNodeList.getLength() > 0) {
for (int j = 0; j < fieldNodeList.getLength(); j++) {
Expand Down Expand Up @@ -415,7 +440,7 @@ private List<InternalDocument> extractDocuments(Document doc) throws Exception {
case TRANSACTION:
try {
NodeList nodes;
nodes = (NodeList) xPath.evaluate("transaction/transaction-type[@code='" + field.getSubType() + "']/parent::*", element, XPathConstants.NODESET);
nodes = (NodeList) xPath.evaluate("transaction/transaction-type[@code='" + field.getSubType() + "' or @code= '" + field.getSubTypeCode() + "']/parent::*", element, XPathConstants.NODESET);
for (int j = 0; j < nodes.getLength(); ++j) {
String reference = "";
String receivingOrganization = "";
Expand Down Expand Up @@ -577,8 +602,10 @@ protected void instantiateStaticFields() {
fieldList.add(policyMarker);
filterFieldList.add(policyMarker);

Field recipientCountry = new Field("Recipient Country", "recipient-country", FieldType.LIST, true);
Field recipientCountry = new Field("Recipient Country", "recipient-country", FieldType.RECIPIENT_COUNTRY, true);
recipientCountry.setPossibleValues(getCodeListValues("recipient-country"));
recipientCountry.setExclusive(true);
recipientCountry.setFilterRequired(true);
fieldList.add(recipientCountry);
filterFieldList.add(recipientCountry);

Expand Down Expand Up @@ -609,10 +636,12 @@ protected void instantiateStaticFields() {
// Transaction Fields
Field commitments = new Field("Commitments", "transaction", FieldType.TRANSACTION, true);
commitments.setSubType("C");
commitments.setSubTypeCode("2");
fieldList.add(commitments);

Field disbursements = new Field("Disbursements", "transaction", FieldType.TRANSACTION, true);
disbursements.setSubType("D");
disbursements.setSubTypeCode("3");
fieldList.add(disbursements);

// Organization Fields
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ public List<Field> getFilterFields() {
NodeList nodeList = doc.getElementsByTagName(field.getFieldName());
List<FieldValue> reducedPossibleValues = new ArrayList<FieldValue>();
switch (field.getType()) {
case RECIPIENT_COUNTRY:
case LIST:
if (nodeList.getLength() > 0) {
for (int i = 0; i < nodeList.getLength(); i++) {
Expand Down Expand Up @@ -345,6 +346,30 @@ private List<InternalDocument> extractDocuments(Document doc) throws Exception {
}
}
break;
case RECIPIENT_COUNTRY:
Field filtersField = filterFieldList.stream().filter(n -> {
return field.getFieldName().equals(n.getFieldName());
}).findFirst().get();

fieldNodeList = element.getElementsByTagName(field.getFieldName());
List<FieldValue> recipients = new ArrayList<FieldValue>();
for (int j = 0; j < fieldNodeList.getLength(); j++) {
Element fieldElement = (Element) fieldNodeList.item(j);
FieldValue recipient = new FieldValue();
String code = fieldElement.getAttribute("code");
boolean includeCountry = includedByFilter(filtersField.getFilters(), code);
if(includeCountry){
recipient.setCode(code);
Optional<FieldValue> fieldValue = field.getPossibleValues().stream().filter(f -> f.getCode().equals(code)).findFirst();
if(fieldValue.isPresent()){
recipient.setValue(fieldValue.get().getValue());
}
recipient.setPercentage(fieldElement.getAttribute("percentage"));
recipients.add(recipient);
}
}
document.addRecepientCountryFields(field.getFieldName(), recipients);
break;
case STRING:
String stringValue = "";
fieldNodeList = element.getElementsByTagName(field.getFieldName());
Expand Down Expand Up @@ -576,8 +601,10 @@ private void instantiateStaticFields() {
fieldList.add(policyMarker);
filterFieldList.add(policyMarker);

Field recipientCountry = new Field("Recipient Country", "recipient-country", FieldType.LIST, true);
Field recipientCountry = new Field("Recipient Country", "recipient-country", FieldType.RECIPIENT_COUNTRY, true);
recipientCountry.setPossibleValues(getCodeListValues("recipient-country"));
recipientCountry.setExclusive(true);
recipientCountry.setFilterRequired(true);
fieldList.add(recipientCountry);
filterFieldList.add(recipientCountry);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ public class Field {
private List<String> filters = new ArrayList<String>();
private List<Field> dependencies = new ArrayList<Field>();
private boolean isRequired;

private boolean isExclusive;

private boolean filterRequired;
// These types are silly but needed for now.
// Refactor to something more reasonable and generic (Make Field an
// interface and get types to be implementations of it)
Expand Down Expand Up @@ -171,4 +173,22 @@ public boolean isRequired() {
public void setRequired(boolean isRequired) {
this.isRequired = isRequired;
}


public boolean isExclusive() {
return isExclusive;
}

public void setExclusive(boolean isExclusive) {
this.isExclusive = isExclusive;
}

public boolean isFilterRequired() {
return filterRequired;
}

public void setFilterRequired(boolean filterRequired) {
this.filterRequired = filterRequired;
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package org.devgateway.importtool.services.processor.helper;

public enum FieldType {
BOOLEAN, STRING, INTEGER, LIST, MULTILANG_STRING, DATE, TRANSACTION, ORGANIZATION, CONTACT
BOOLEAN, STRING, INTEGER, LIST, RECIPIENT_COUNTRY , MULTILANG_STRING, DATE, TRANSACTION, ORGANIZATION, CONTACT
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,23 @@ public class FieldValue {
private int index;
private String code;
private String value;
private boolean selected = false;
private String percentage = "";
public String getCode() {
return code;
}
public boolean isSelected() {
return selected;
}
public void setSelected(boolean selected) {
this.selected = selected;
}
public String getPercentage() {
return percentage;
}
public void setPercentage(String percentage) {
this.percentage = percentage;
}
public void setCode(String code) {
this.code = code;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class InternalDocument {
Expand All @@ -12,8 +13,25 @@ public class InternalDocument {
private Map<String, Map<String, String>> organizationFields = new HashMap<String, Map<String, String>>();
private Map<String, Map<String, String>> transactionFields = new HashMap<String, Map<String, String>>();
private Map<String, Map<String, String>> contactFields = new HashMap<String, Map<String, String>>();
private Map<String, Date> dateFields = new HashMap<String, Date>();
private Map<String, Date> dateFields = new HashMap<String, Date>();
private Map<String, List<FieldValue>> recepientCountryFields = new HashMap<String, List<FieldValue>>();





public Map<String, List<FieldValue>> getRecepientCountryFields() {
return recepientCountryFields;
}

public void setRecepientCountryFields(
Map<String, List<FieldValue>> recepientCountryFields) {
this.recepientCountryFields = recepientCountryFields;
}




private boolean allowEdit = false;

public boolean isAllowEdit() {
Expand All @@ -40,6 +58,8 @@ public void addTransactionField(String fieldName, Map<String, String> transactio
getTransactionFields().put(fieldName, transaction);
}



public void addContactFields(String fieldName, Map<String, String> contact) {
getContactFields().put(fieldName, contact);
}
Expand Down Expand Up @@ -115,4 +135,10 @@ public void setContactFields(Map<String, Map<String, String>> contactFields) {
}




public void addRecepientCountryFields(String fieldName, List<FieldValue> value) {
this.getRecepientCountryFields().put(fieldName, value);
}

}
Loading

0 comments on commit db0a052

Please sign in to comment.