package ch.openech.datagenerator;

import java.util.Collections;
import java.util.List;

import ch.openech.mj.db.model.Constants;
import ch.openech.mj.db.model.annotation.Int;
import ch.openech.mj.edit.Editor;
import ch.openech.mj.edit.EditorDialogAction;
import ch.openech.mj.edit.form.AbstractFormVisual;
import ch.openech.mj.edit.form.FormVisual;
import ch.openech.mj.edit.validation.ValidationMessage;
import ch.openech.mj.edit.value.Required;
import ch.openech.mj.swing.FrameManager;
import ch.openech.mj.toolkit.ClientToolkit;
import ch.openech.mj.util.StringUtils;
import ch.openech.xml.write.EchNamespaceContext;
import ch.openech.xml.write.WriterEch0020;


public class GeneratePersonAction extends EditorDialogAction {

	public GeneratePersonAction(EchNamespaceContext echNamespaceContext) {
		super(new GeneratePersonEditor(echNamespaceContext));
	}
	
	public static class GeneratePersonEditor extends Editor<GeneratePersonData> {

		private final EchNamespaceContext echNamespaceContext;
		private int count;
		private int saveProgress;
		
		public GeneratePersonEditor(EchNamespaceContext echNamespaceContext) {
			this.echNamespaceContext = echNamespaceContext;
		}

		@Override
		protected FormVisual<GeneratePersonData> createForm() {
			AbstractFormVisual<GeneratePersonData> form = new AbstractFormVisual<GeneratePersonData>(GeneratePersonData.class, null, true);
			form.line(GeneratePersonData.GENERATE_PERSON_DATA.numberOfPersons);
			return form;
		}

		@Override
		protected GeneratePersonData newInstance() {
			return new GeneratePersonData();
		}

		@Override
		protected void validate(GeneratePersonData object, List<ValidationMessage> resultList) {
			// nothing to validate
		}

		@Override
		protected boolean save(GeneratePersonData data) {
			String input = data.numberOfPersons;
			if (StringUtils.isBlank(input)) return true;
			
			count = Integer.parseInt(input);
			if (count > 0) {
				WriterEch0020 writerEch0020 = new WriterEch0020(echNamespaceContext);
				for (saveProgress = 0; saveProgress<count; saveProgress++) {
					DataGenerator.generatePerson(writerEch0020);
					progress(saveProgress, count);
				}
			} else {
				ClientToolkit.getToolkit().showError(null, input);
				return false;
			}
			return true;
		}

		@Override
		protected List<ValidationMessage> validate(GeneratePersonData data) {
			if (StringUtils.isBlank(data.numberOfPersons)) {
				ValidationMessage message = new ValidationMessage(GeneratePersonData.GENERATE_PERSON_DATA.numberOfPersons, "Eingabe erforderlich");
				return Collections.singletonList(message);
			} else {
				int i = Integer.parseInt(data.numberOfPersons);
				if (i < 1) {
					ValidationMessage message = new ValidationMessage(GeneratePersonData.GENERATE_PERSON_DATA.numberOfPersons, "Eingabe erforderlich");
					return Collections.singletonList(message);
				}
			}
			return Collections.emptyList();
		}
		
		
	}

	public static class GeneratePersonData {
		public static final GeneratePersonData GENERATE_PERSON_DATA = Constants.of(GeneratePersonData.class);

		@Int(4) @Required
		public String numberOfPersons;
	}

}

@@ -445,49 +445,8 @@
 ed;%0A
-import ch.openech.mj.swing.FrameManager;%0A
 impo
@@ -1656,24 +1656,97 @@
 lidate%0A%09%09%7D%0A%0A
+%09%09@Override%0A%09%09protected boolean isSaveSynchron() %7B%0A%09%09%09return false;%0A%09%09%7D%0A%0A
 %09%09@Override%0A

GeneratePersonAction bis jetzt die einzige Aktion, die wirklich asynchrones speichern benötigt