package org.openforis.collect.earth.core.model;

import java.util.HashMap;
import java.util.Map;

import org.openforis.collect.model.CollectRecord;

/**
 * 
 * @author S. Ricci
 *
 */
public class PlacemarkLoadResult {

	private Map<String, PlacemarkInputFieldInfo> inputFieldInfoByParameterName;
	private boolean success;
	private String message;
	private boolean activelySaved;
	private boolean skipFilled;
	private String currentStep;
	
	private transient CollectRecord collectRecord;
	
	public PlacemarkLoadResult() {
		this.success = false;
		this.inputFieldInfoByParameterName = new HashMap<String, PlacemarkInputFieldInfo>();
	}
	
	public void setFieldErrorMessage(String parameterName, String errorMessage) {
		PlacemarkInputFieldInfo placemarkInfo = getPlacemarkInfo(parameterName);
		placemarkInfo.setErrorMessage(errorMessage);
	}

	public PlacemarkInputFieldInfo getPlacemarkInfo(String parameterName) {
		PlacemarkInputFieldInfo placemarkInputFieldInfo = inputFieldInfoByParameterName.get(parameterName);
		if (placemarkInputFieldInfo == null) {
			placemarkInputFieldInfo = new PlacemarkInputFieldInfo();
		}
		return placemarkInputFieldInfo;
	}
	
	private void updateCalculatedFields() {
		PlacemarkInputFieldInfo activelySavedFieldInfo = inputFieldInfoByParameterName.get("collect_boolean_actively_saved");
		activelySaved = activelySavedFieldInfo != null && Boolean.TRUE.toString().equals(activelySavedFieldInfo.getValue());
	}

	/**
	 * Calculated field based on inputFieldInfoByParameterName content
	 */
	public boolean isActivelySaved() {
		return activelySaved;
	}
	
	public boolean isSuccess() {
		return success;
	}
	
	public void setSuccess(boolean success) {
		this.success = success;
	}
	
	public String getMessage() {
		return message;
	}
	
	public void setMessage(String message) {
		this.message = message;
	}
	
	public Map<String, PlacemarkInputFieldInfo> getInputFieldInfoByParameterName() {
		return inputFieldInfoByParameterName;
	}
	
	public void setInputFieldInfoByParameterName(
			Map<String, PlacemarkInputFieldInfo> inputFieldInfoByParameterName) {
		this.inputFieldInfoByParameterName = inputFieldInfoByParameterName;
		updateCalculatedFields();
	}
	
	public CollectRecord getCollectRecord() {
		return collectRecord;
	}
	
	public void setCollectRecord(CollectRecord collectRecord) {
		this.collectRecord = collectRecord;
	}

	public boolean isSkipFilled() {
		return skipFilled;
	}
	
	public void setSkipFilled(boolean skipFilled) {
		this.skipFilled = skipFilled;
	}
	
	public String getCurrentStep() {
		return currentStep;
	}
	
	public void setCurrentStep(String currentStep) {
		this.currentStep = currentStep;
	}

	@Override
	public String toString() {
		return "PlacemarkLoadResult [inputFieldInfoByParameterName="
				+ inputFieldInfoByParameterName + ", success=" + success
				+ ", message=" + message + ", activelySaved=" + activelySaved
				+ ", currentStep=" + currentStep + "]";
	}
	
}

@@ -89,16 +89,44 @@
 til.Map;
+%0Aimport java.util.Map.Entry;
 %0A%0Aimport
@@ -397,24 +397,52 @@
 ivelySaved;%0A
+%09private boolean validData;%0A
 %09private boo
@@ -1492,18 +1492,391 @@
 ue());%0A%09
-%7D%0A
+%09validData = calculateContainsValidData();%0A%09%7D%0A%0A%09private boolean calculateContainsValidData() %7B%0A%09%09for (Entry%3CString, PlacemarkInputFieldInfo%3E entry : inputFieldInfoByParameterName.entrySet()) %7B%0A%09%09%09PlacemarkInputFieldInfo info = entry.getValue();%0A%09%09%09if (info.isInError()) %7B%0A%09%09%09%09return false;%0A%09%09%09%7D%0A%09%09%7D%0A%09%09return true;%0A%09%7D%0A%09%0A%09public boolean isValidData() %7B%0A%09%09return validData;%0A%09%7D%0A%09
 %0A%09/**%0A%09 

Fix the "fly to next placemark" issue