package com.sharomank;
import com.sharomank.config.ApplicationResourceFactory;
import com.sharomank.domain.*;
import com.sharomank.domain.base.BaseItemEntity;
import com.sharomank.service.ClientService;
import org.hibernate.search.jpa.FullTextEntityManager;
import org.hibernate.search.jpa.Search;
import javax.persistence.EntityManager;
import java.util.List;
import java.util.Random;
/**
* @author sharomank
* @since 25/01/13 23:33
*/
public class TestClient {
private static final Random RANDOM = new Random();
public static void main(String[] args) {
ApplicationResourceFactory factory = ApplicationResourceFactory.getSingleton();
ClientService service = factory.getClientService();
TestClient client = new TestClient();
client.run(service);
}
private void run(ClientService service) {
System.out.println("TestClient start");
if (service != null) {
service.createFullTextIndex();
service.saveTag(createTag("Java"));
service.saveTag(createTag(".NET"));
service.saveTag(createTag("DB"));
service.saveTag(createTag("XML"));
service.saveTag(createTag("HTML"));
printInfo(Tag.class, service.countTags(), service.findAllTags());
service.saveBadge(createBadge("Starter"));
service.saveBadge(createBadge("Brain"));
service.saveBadge(createBadge("Expert"));
printInfo(Badge.class, service.countBadges(), service.findAllBadges());
Account questionAccount = service.saveAccount(createAccount("Roman", "Kurbangaliyev", "Valiahmetovich"));
Account answerAccount = service.saveAccount(createAccount("Ivan", "Popov", null));
service.saveAccount(createAccount("Troll", null, null));
service.saveAccount(createAccount("Nerd", null, null));
service.saveAccount(createAccount("Dummy", null, null));
printInfo(Account.class, service.countAccounts(), service.findAllAccounts());
Question question = new Question();
question.setTitle("How I must using this code?");
question.setText("Hello, I download this project, but it don't run. What I must do?");
question.setAccount(questionAccount);
question = service.saveQuestion(question);
voting(service, question);
service.saveQuestion(question);
printInfo(Question.class, service.countQuestions(), service.findAllQuestions());
createComment(service, answerAccount, question,
"Hm, it's must easy. I answer on your question later...");
createComment(service, questionAccount, question,
"Ok, I'm waiting...");
createComment(service, answerAccount, question,
"I'm very busy, don't wait fast answer, sorry.");
Answer answer = new Answer();
answer.setText("Download & install & configuration Maven 3 after open console in project folder and execute 'mvn clean install'");
answer.setAccount(answerAccount);
answer.setQuestion(question);
answer = service.saveAnswer(answer);
voting(service, answer);
service.saveAnswer(answer);
printInfo(Answer.class, service.countAnswers(), service.findAllAnswers());
createComment(service, questionAccount, answer,
"Hm, it's all?...");
createComment(service, questionAccount, answer,
"I think you have JDK on your PC.");
createComment(service, questionAccount, answer,
"Ok, thanks");
printInfo(Comment.class, service.countComments(), service.findAllComments());
for (Comment comment : service.findAllComments()) {
voting(service, comment);
service.saveComment(comment);
}
System.out.println("find question...");
Question tmp = service.findQuestion(question.getId());
System.out.println("comments size: " + tmp.getComments().size());
System.out.println("answers size: " + tmp.getAnswers().size());
System.out.println("votes size: " + tmp.getVotes().size());
List res = service.fullTextSearch("download");
System.out.println("fullTextSearch size: " + res.size());
}
System.out.println("TestClient complete!");
}
private Tag createTag(String title) {
Tag t = new Tag();
t.setTitle(title);
t.setText("This is description for '" + title + "' in " + t.getClass().getSimpleName());
return t;
}
private Badge createBadge(String title) {
Badge b = new Badge();
b.setTitle(title);
b.setText("This is description for '" + title + "' in " + b.getClass().getSimpleName());
return b;
}
private Account createAccount(String name, String surname, String patronymic) {
Account ac = new Account();
ac.setLogin(name + "_" + surname);
ac.setName(name);
ac.setSurname(surname);
ac.setPatronymic(patronymic);
return ac;
}
private Comment createComment(ClientService service, Account account, Question question, String text) {
Comment c = new Comment();
c.setText(text);
c.setAccount(account);
service.addCommentToQuestion(question.getId(), c);
return c;
}
private Comment createComment(ClientService service, Account account, Answer answer, String text) {
Comment c = new Comment();
c.setText(text);
c.setAccount(account);
service.addCommentToAnswer(answer.getId(), c);
return c;
}
private void printInfo(Class clazz, Long count, Iterable<?> list) {
System.out.println(clazz.getSimpleName() + " count: " + count);
for (Object obj : list) {
System.out.println(clazz.getSimpleName() + " " + obj.toString());
}
}
private void voting(ClientService service, BaseItemEntity item) {
for (Account ac : service.findAllAccounts()) {
item.addVote(ac, RANDOM.nextBoolean());
}
}
}