package com.xszymo.hibernate.configuration;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.io.Writer;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;

import com.xszymo.hibernate.interfaces.AnswersMessageService;
import com.xszymo.hibernate.interfaces.QuestionsMessageService;
import com.xszymo.hibernate.interfaces.UserService;
import com.xszymo.hibernate.tables.Answer;
import com.xszymo.hibernate.tables.Question;

@Configuration
public class LoadAtStart {
	public static final String PATH_QUESTIONS = "/home/xszymo/Desktop/Programming/text1.txt";
	public static final boolean readQuestionsAndAnswersFromTxt = true;

	@Resource(name = "answerMessageService")
	AnswersMessageService answer;

	@Resource(name = "questionMessageService")
	QuestionsMessageService question;

	@Autowired
	UserService userService;

	@PostConstruct
	public void halo() throws IOException {
		// String everything = "";
		if (readQuestionsAndAnswersFromTxt) {
			File file = new File(PATH_QUESTIONS);
			if (!(file.exists() && !file.isDirectory())) {
				Writer writer = new BufferedWriter(
						new OutputStreamWriter(new FileOutputStream(PATH_QUESTIONS), "utf-8"));
				writer.write("Test -.|.- test :) ");
				if (writer != null)
					writer.close();
			}

			try (BufferedReader br = new BufferedReader(new FileReader(PATH_QUESTIONS))) {
				StringBuilder sb = new StringBuilder();
				String line = br.readLine();
				while (line != null) {
					boolean a = false;
					String hey = "";
					String hey1 = "";
					char[] chars = line.toCharArray();
					for (int i = 0; i < chars.length; i++) {
						int k = i;
						if (chars[k++] == '-' && i < chars.length - 1)
							if (chars[k++] == '.')
								if (chars[k++] == '|')
									if (chars[k++] == '.')
										if (chars[k++] == '-') {
											a = true;
											i = k + 1;
										}
						if (!a)
							hey += chars[i];
						else
							hey1 += chars[i];
					}
					Question questuerinio = new Question(hey.substring(0, hey.length() - 1));
					Answer answerino = new Answer(hey1);
					answerino.setQuestion(questuerinio);
					questuerinio.getAnswers().add(answerino);
					question.persist(questuerinio);
					answer.persist(answerino);
					line = br.readLine();
				}

				sb.append(line);
				sb.append(System.lineSeparator());
				line = br.readLine();

				// everything = sb.toString();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}

@@ -34,24 +34,25 @@
 iguration;%0A%0A
+%0A
 import java.
@@ -212,32 +212,59 @@
 .io.FileReader;%0A
+import java.io.FileWriter;%0A
 import java.io.I
@@ -302,32 +302,60 @@
 utStreamWriter;%0A
+import java.io.PrintWriter;%0A
 import java.io.U
@@ -480,17 +480,99 @@
 source;%0A
+import com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException;
 %0A
-
 import o
@@ -687,17 +687,16 @@
 ration;%0A
-%0A
 import c
@@ -1079,17 +1079,16 @@
 ing/text
-1
 .txt%22;%0A%09
@@ -2365,24 +2365,86 @@
 %09%09%09%09%09%09%09%09%09%09%7D%0A
+%09%09%09%09%09%09if(i %3E= chars.length)%0A%09%09%09%09%09%09%09i = chars.length-1;%0A%09%09%09%09%09%09%0A
 %09%09%09%09%09%09if (!a
@@ -2583,19 +2583,17 @@
 length()
- - 
+-
 1));%0A%09%09%09
@@ -2612,16 +2612,68 @@
 werino =
+ null;%0A%09%09%09%09%09if(!hey1.equals(%22 %22)) %7B%0A%09%09%09%09%09answerino =
  new Ans
@@ -2768,24 +2768,31 @@
 answerino);%0A
+%09%09%09%09%09%7D%0A
 %09%09%09%09%09questio
@@ -2812,24 +2812,54 @@
 stuerinio);%0A
+%09%09%09%09%09if(!hey1.equals(%22 %22)) %7B%0A%09
 %09%09%09%09%09answer.
@@ -2874,24 +2874,40 @@
 answerino);%0A
+%09%09%09%09%09%7D%0A%09%09%09%09    %0A
 %09%09%09%09%09line = 

fix a few bugs in loadAtStart