package com.akjava.lib.common.form;

import java.util.List;

import com.akjava.lib.common.functions.LabelAndValueDto;
import com.akjava.lib.common.functions.SplitLineFunction;
import com.google.common.base.Function;


public class FormDataDto {
	
	//TODO convert list
	
	public static FormData csvToFormData(String singleLine){
		return new CsvToFormDataFunction().apply(singleLine);
	}
	
	public static class CsvToFormDataFunction implements Function<String, FormData>{
		private boolean optionWithNumber=true;
		public boolean isOptionWithNumber() {
			return optionWithNumber;
		}
		public void setOptionWithNumber(boolean optionWithNumber) {
			this.optionWithNumber = optionWithNumber;
		}
		@Override
		public FormData apply(String value) {
			FormData data=new FormData();
			List<String> csvs=new SplitLineFunction(true, true).apply(value);
			
			if(csvs.size()>0){
				data.setName(csvs.get(0));//String
			}
			if(csvs.size()>1){
				data.setKey(csvs.get(1));//String
			}
			
			if(csvs.size()>2){
				int type=0;
				String v=csvs.get(2).toLowerCase();
				if(v.equals("text_short")){
					type=0;
				}else if(v.equals("text_long")){
					type=1;
				}else if(v.equals("id")){
					type=2;
				}else if(v.equals("check")){
					type=3;
				}else if(v.equals("select_single")){
					type=4;
				}else if(v.equals("select_multi")){
					type=5;
				}else if(v.equals("create_date")){
					type=6;
				}else if(v.equals("create_user")){
					type=7;
				}
				data.setType(type);
			}
			
			if(csvs.size()>3){
				String optionText=csvs.get(3);
				if(optionWithNumber){
					data.setOptionValues(LabelAndValueDto.lineToLabelAndValuesWithNumber(optionText));
				}else{
					data.setOptionValues(LabelAndValueDto.lineToLabelAndValues(optionText));
				}
				
			}
			
			if(csvs.size()>4){
				data.setDefaultValue(csvs.get(4));
			}
			
			if(csvs.size()>5){
				String isAuto=csvs.get(5);
				if(isAuto.equals("true")){
					data.setCreateAuto(true);
				}
			}
			
			//TODO parse validator
			
			//
			
			if(csvs.size()>7){
				data.setPlaceHolder(csvs.get(7));
			}
			
			if(csvs.size()>8){
				data.setComment(csvs.get(8));
			}
			
			return data;
		}
		
	}
}

@@ -49,20 +49,103 @@
 va.util.
-List
+HashMap;%0D%0Aimport java.util.LinkedHashMap;%0D%0Aimport java.util.List;%0D%0Aimport java.util.Map
 ;%0D%0A%0D%0Aimp
@@ -299,16 +299,56 @@
 ction;%0D%0A
+import com.google.common.base.Objects;%0D%0A
 %0D%0A%0D%0Apubl
@@ -458,16 +458,16 @@
 Line)%7B%0D%0A
-
 %09%09return
@@ -519,16 +519,1031 @@
 );%0D%0A%09%7D%0D%0A
+%09public static Map%3CString,String%3E formDataToMap(FormData data)%7B%0D%0A%09%09Map%3CString,String%3E hashMap=new LinkedHashMap%3CString, String%3E();%0D%0A%09%09hashMap.put(%22name%22, data.getName());%0D%0A%09%09hashMap.put(%22key%22, data.getKey());%0D%0A%09%09hashMap.put(%22type%22, FormData.getTypeLabel(data.getType()));%0D%0A%09%09hashMap.put(%22optionValues%22, LabelAndValueDto.labelAndValueToString(data.getOptionValues()));%0D%0A%09%09hashMap.put(%22defaultValue%22, data.getDefaultValue());%0D%0A%09%09hashMap.put(%22createAuto%22, %22%22+data.isCreateAuto());%0D%0A%09%09//TODO add validators%0D%0A%09%09hashMap.put(%22placeHolder%22, data.getPlaceHolder());%0D%0A%09%09hashMap.put(%22comment%22, data.getComment());%0D%0A%09%09/**%0D%0A%09%09 * %09return Objects.toStringHelper(this).add(%22name%22, name).add(%22key%22,key).add(%22type%22, getTypeLabel(type)+%22%5B%22+type+%22%5D%22)%0D%0A%09%09%09.add(%22optionValues%22,LabelAndValueDto.labelAndValueToString(optionValues))%0D%0A%09%09%09.add(%22defaultValue%22, defaultValue).add(%22createAuto%22,%22%22+createAuto)%0D%0A%09%09%09//TODO add validators%0D%0A%09%09%09.add(%22placeHolder%22, placeHolder).add(%22comment%22, comment)%0D%0A%09%09%09.toString();%0D%0A%09%09 */%0D%0A%09%09return hashMap;%0D%0A%09%7D%0D%0A
 %09%0D%0A%09publ

add formDataToMap