package faq.server;
import java.util.ArrayList;
import java.util.concurrent.ConcurrentMap;
import org.mapdb.DB;
import faq.shared.Category;
import faq.shared.CategoryComponent;
import faq.shared.CategoryIterator;
import faq.shared.Question;
/**Classe che gestisce la memorizzazione delle domande
*
* Gestisce la memorizzazione di una nuova domanda all'interno dello stesso database
* che memorizza le categorie.
* Permette di aggiungere o eliminare domande relative a una determinata categoria.
*
*
*/
public class QuestionHandlerImpl
{
\t // Database
\t private DB db;
\t // Lista radici alberi categorie
\t private ConcurrentMap<String, Category> categoryMap;
\t // oggetto per la gestione delle categorie
\t private CategoryHandlerImpl categoryHandler;
\t
\t public void clear(){
\t \t categoryMap.clear();
\t \t categoryHandler.clear();
\t }
\t public QuestionHandlerImpl(DB db, CategoryHandlerImpl categoryHandler)
\t {
\t \t this.db = db;
\t \t this.categoryMap = this.db.getTreeMap("category");
\t \t this.categoryHandler = categoryHandler;
\t \t
\t }
\t
\t public void addQuestion(Category category, Question question)
\t {
\t \t Category root = this.categoryMap.get("root");
\t \t Category categoria = categoryHandler.getCategory(category.getName(), root);
\t \t categoria.add(question);
\t \t question.setCategory(categoria);
\t \t categoryMap.replace("root", categoryMap.get("root"), root);
\t \t this.db.commit();
\t }
\t
\t public void removeQuestion(Category category, Question question)
\t {
\t \t Category root = this.categoryMap.get("root");
\t \t Category categoria = categoryHandler.getCategory(category.getName(), root);
\t \t categoria.remove(question);
\t \t categoryMap.replace("root", categoryMap.get("root"), root);
\t \t this.db.commit();
\t }
\t
\t
\t // Tutte le domande relative a una categoria
\t public ArrayList<Question> getQuestions(Category category)
\t {
\t \t Category root = this.categoryMap.get("root");
\t \t Category categoria = categoryHandler.getCategory(category.getName(), root);
\t \t ArrayList<Question> res = new ArrayList<Question>();
\t \t CategoryIterator sfogliaCategoria = (CategoryIterator) categoria.createIterator();
\t \t while(sfogliaCategoria.hasNext())
\t \t {
\t \t \t CategoryComponent componente = (CategoryComponent) sfogliaCategoria.next();
\t \t \t if(componente instanceof Question)
\t \t \t {
\t \t \t \t res.add((Question)componente);
\t \t \t }
\t \t \t else
\t \t \t {
\t \t \t \t res.addAll(getQuestions((Category) componente));
\t \t \t }
\t \t }
\t \t res.trimToSize();
\t \t return res;
\t }
\t
\t public ArrayList<Question> getAllQuestions()
\t {
\t \t Category root = this.categoryMap.get("root");
\t \t return getQuestions(root);
\t }
}
/**
*
*/
package faq.server;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.concurrent.ConcurrentMap;
import org.mapdb.DB;
import faq.shared.Answer;
import faq.shared.Category;
import faq.shared.Question;
/**Classe che gestisce la memorizzazione delle risposte
*
* Gestisce la memorizzazione di una nuova risposta all'interno dello stesso database
* che memorizza le domande e le categorie.
* Permette di aggiungere o eliminare risposte relative a una determinata domanda.
* \t
*
*/
public class AnswerHandlerImpl
{
\t // Database
\t private DB db;
\t // Lista radici alberi categorie
\t private ConcurrentMap<String, Category> categoryMap;
\t
\t public void clear() {
\t \t categoryMap.clear();
\t \t
\t }
\t
\t public AnswerHandlerImpl(DB db)
\t {
\t \t this.db = db;
\t \t this.categoryMap = this.db.getTreeMap("category");
\t } \t
\t public void addAnswer(Question question, Answer answer)
\t {
\t \t Category root = this.categoryMap.get("root");
\t \t question.addAnswer(answer);
\t \t categoryMap.replace("root", categoryMap.get("root"), root);
\t \t this.db.commit();
\t }
\t
\t public void removeAnswer(Question question, Answer answer)
\t {
\t \t Category root = this.categoryMap.get("root");
\t \t question.removeAnswer(answer);
\t \t categoryMap.replace("root", categoryMap.get("root"), root);
\t \t this.db.commit();
\t }
\t
\t // Tutte le risposte relative a una domanda
\t public ArrayList<Answer> getAnswers(Question question)
\t {
\t \t Category root = this.categoryMap.get("root");
\t \t ArrayList<Answer> res = new ArrayList<Answer>();
\t \t Iterator sfogliaDomanda = question.createIterator();
\t \t while(sfogliaDomanda.hasNext())
\t \t {
\t \t \t res.add((Answer) sfogliaDomanda.next()); \t \t \t
\t \t }
\t \t res.trimToSize();
\t \t return res;
\t }
}
/**
*
*/
package faq.server;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.concurrent.ConcurrentMap;
import org.mapdb.DB;
import faq.shared.Answer;
import faq.shared.Category;
import faq.shared.Judgment;
/**Classe che gestisce la memorizzazione dei giudizi
*
* Gestisce la memorizzazione di un nuovo giudizio all'interno dello stesso database
* che memorizza le risposte, le domande e le categorie.
* Permette di aggiungere giudizi relative a una determinata risposta.
*
*
*/
public class JudgmentHandlerImpl
{
\t // Database
\t private DB db;
\t // Lista radici alberi categorie
\t private ConcurrentMap<String, Category> categoryMap;
\t
\t
\t public void clear(){
\t \t categoryMap.clear();
\t }
\t
\t public JudgmentHandlerImpl(DB db)
\t {
\t \t this.db = db;
\t \t this.categoryMap = this.db.getTreeMap("category");
\t } \t
\t public void addJudgment(Answer answer, Judgment judgment)
\t {
\t \t Category root = this.categoryMap.get("root");
\t \t answer.addJudgment(judgment);;
\t \t categoryMap.replace("root", categoryMap.get("root"), root);
\t \t this.db.commit();
\t }
\t
\t // Tutte i giudizi relative a una risposta
\t public ArrayList<Judgment> getJudgments(Answer answer)
\t {
\t \t //Category root = this.categoryMap.get("root");
\t \t ArrayList<Judgment> res = new ArrayList<Judgment>();
\t \t Iterator sfogliaRisposta = answer.createIterator();
\t \t while(sfogliaRisposta.hasNext())
\t \t {
\t \t \t res.add((Judgment) sfogliaRisposta.next()); \t \t \t
\t \t }
\t \t res.trimToSize();
\t \t return res;
\t }
}
/*
* Gestisce le operazioni sugli users
*/
public class LoginServiceImpl {
\t
\t // Database
\t private DB db;
\t // Lista utenti
\t private ConcurrentMap<String, User> userMap;
\t
\t public LoginServiceImpl(DB db){
\t \t this.db = db;
\t \t this.userMap = this.db.getTreeMap("user");
\t \t
\t \t if(userMap.get("admin") == null){ // l'admin deve essere sempre presente nel db
\t \t \t addAdmin();
\t \t }
\t }
\t
\t // Agigunge un utente al db, se gia' presente lancia eccezione
\t public User addUser(String username, String password, String email, String name, String surname, String sex, String birthDate, String birthPlace, String address) throws DuplicatedUserNameException {
\t \t if(userMap.get(username) != null){
\t \t \t throw new DuplicatedUserNameException(username);
\t \t }
\t \t else{
\t \t \t User user = new NormalUser(username,password,email,name,surname,sex,birthDate,birthPlace,address);
\t \t \t
\t \t \t this.userMap.put(username, user);
\t \t \t this.db.commit();
\t \t \t return user;
\t \t }
\t }
\t
\t // Metodo che azzera la collezione utenti
\t public void clear(){
\t \t userMap.clear();
\t }
\t
\t public void remove(String userName){
\t \t userMap.remove(userName);
\t }
\t
\t
\t // Aggiunge l'utente admin
\t public void addAdmin(){
\t \t User admin = new AdminUser("admin", "admin", "[email protected]", null, null, null, null, null, null);
\t \t userMap.put("admin", admin);
\t \t this.db.commit();
\t }
\t // Controlla username e password, se l'utente non e' presente o la password e' sbagliata lancia eccezioni
\t public User login(String userName, String password) throws WrongPasswordException, UserNotFoundException {
\t \t User user = this.userMap.get(userName);
\t \t if(user != null){
\t \t \t if(!user.getPassword().equals(password)){
\t \t \t \t throw new WrongPasswordException();
\t \t \t }
\t \t }
\t \t else{
\t \t \t throw new UserNotFoundException();
\t \t }
\t \t return user;
\t }
\t
\t // Tutti gli utenti (funzionalità admin)
\t \t public ArrayList<User> getAllUsers(){
\t \t \t ArrayList<User> res = new ArrayList<User>(userMap.values());
\t \t \t return res;
\t \t }
\t \t
\t \t // Tutti gli utenti non giudici o admin (funzionalità admin)
\t \t public ArrayList<User> getRegistered(){
\t \t \t ArrayList<User> users = new ArrayList<User>(userMap.values());
\t \t \t ArrayList<User> res = new ArrayList<User>();
\t \t \t for(User u : users){
\t \t \t \t if(u instanceof NormalUser){
\t \t \t \t \t res.add(u);
\t \t \t \t }
\t \t \t }
\t \t \t res.trimToSize();
\t \t \t return res;
\t \t }
\t \t
\t \t // Nomina un giudice sostituendo l'istanza di utente normale
\t \t // con quella di utente giudice, associandola allo stesso nome
\t \t public ArrayList<User> nominateJudge(String userName){
\t \t \t NormalUser normalToJudge = (NormalUser) userMap.get(userName);
\t \t \t //judge.setRoleJudge();
\t \t \t User judge = new JudgeUser(normalToJudge);
\t \t \t userMap.replace(userName, normalToJudge, judge);
\t \t \t this.db.commit();
\t \t \t return getRegistered();
\t \t }
\t \t
\t \t // Tutti gli utenti giudici (funzionalità admin)
\t \t public ArrayList<User> getJudges(){
\t \t \t ArrayList<User> users = new ArrayList<User>(userMap.values());
\t \t \t ArrayList<User> res = new ArrayList<User>();
\t \t \t for(User u : users){
\t \t \t \t if(u instanceof JudgeUser){
\t \t \t \t \t res.add(u);
\t \t \t \t }
\t \t \t }
\t \t \t res.trimToSize();
\t \t \t return res;
\t \t }
}
package faq.shared;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import com.google.gwt.i18n.shared.DateTimeFormat;
/**Classe per la gestione delle domande
*
*/
public class Question extends CategoryComponent implements Serializable
{
\t /**
\t *
\t */
\t private static final long serialVersionUID = 1L;
\t private String question;
\t private RegisteredUser user;
\t private Date date;
\t private ArrayList<Answer> answers = new ArrayList<>();
\t private Category category;
\t
\t public Category getCategory() {
\t \t return category;
\t }
\t public void setCategory(Category category) {
\t \t this.category = category;
\t }
\t public Question(){}
\t
\t public Question(String question, RegisteredUser user)
\t {
\t \t this.question=question;
\t \t this.user=user;
\t \t this.date= new Date();
\t }
\t
\t public String getQuestion()
\t {
\t \t return question;
\t }
\t
\t public User getUser()
\t {
\t \t return user;
\t }
\t
\t public Date getDate()
\t {
\t \t return date;
\t }
\t
\t public String getDay()
\t {
\t \t return DateTimeFormat.getFormat("yyyy-MM-dd").format(this.date);
\t \t //DateTimeFormat fmt = DateTimeFormat.getFormat("EEEE, MMMM dd, yyyy");
\t //return fmt.format(this.date);
\t
\t }
\t
\t public String getTime()
\t { \t
\t \t return DateTimeFormat.getFormat("hh:mm").format(this.date);
\t }
\t
\t public void addAnswer(Answer answer)
\t {
\t \t answers.add(answer);
\t }
\t
\t public void removeAnswer(Answer answer)
\t {
\t \t answers.remove(answer);
\t }
\t
\t public Answer getAnswer(int i)
\t {
\t \t return answers.get(i);
\t }
\t
\t public Iterator<Object> createIterator()
\t {
\t \t return new AnswerIterator<>(answers);
\t }
\t
\t @Override
\t public int compareTo(Object o) {
\t \t return getDate().compareTo(((Question) o).getDate());
\t }
}
package faq.shared;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import com.google.gwt.i18n.shared.DateTimeFormat;
/**Classe che gestisce le risposte
*
*
*/
public class Answer implements Collection, Comparable<Object>, Serializable
{
\t /**
\t *
\t */
\t private static final long serialVersionUID = 1L;
\t private String answer;
\t private RegisteredUser user;
\t private Date date;
\t private ArrayList<Judgment> judgments = new ArrayList<>();
\t
\t public Answer(){}
\t
\t public Answer(String answer, RegisteredUser user)
\t {
\t \t this.answer=answer;
\t \t this.user=user;
\t \t this.date= new Date();
\t }
\t
\t public String getAnswer()
\t {
\t \t return answer;
\t }
\t
\t public User getUser()
\t {
\t \t return user;
\t }
\t
\t public Date getDate()
\t {
\t \t return date;
\t }
\t
\t public String getDay()
\t {
\t \t return DateTimeFormat.getFormat("yyyy-MM-dd").format(this.date);
\t \t //DateTimeFormat fmt = DateTimeFormat.getFormat("EEEE, MMMM dd, yyyy");
\t //return fmt.format(this.date);
\t
\t }
\t
\t public String getTime()
\t { \t
\t \t return DateTimeFormat.getFormat("hh:mm").format(this.date);
\t }
\t
\t public void addJudgment(Judgment judgment)
\t {
\t \t judgments.add(judgment);
\t }
\t
\t public Judgment getJudgment(int i)
\t {
\t \t return judgments.get(i);
\t }
\t
\t public double getEverageJudgment()
\t {
\t \t int total = 0;
\t \t for(Judgment j : judgments)
\t \t {
\t \t \t total = total + j.getJudgment();
\t \t }
\t \t if(judgments.size() == 0)
\t \t {
\t \t \t return 0;
\t \t }
\t \t else
\t \t {
\t \t \t return total/judgments.size();
\t \t }
\t }
\t @Override
\t public Iterator<Object> createIterator() {
\t \t
\t \t return new JudgmentIterator<>(judgments);
\t }
\t @Override
\t public int compareTo(Object o) {
\t \t
\t \t if (this.judgments.size() == 0)
\t \t {
\t \t \t return getDate().compareTo(((Answer) o).getDate());
\t \t }
\t \t else
\t \t {
\t \t \t if(this.getEverageJudgment() == ((Answer) o).getEverageJudgment())
\t \t \t {
\t \t \t \t return getDate().compareTo(((Answer) o).getDate());
\t \t \t }
\t \t \t else if(this.getEverageJudgment() > ((Answer) o).getEverageJudgment())
\t \t \t {
\t \t \t \t return 1;
\t \t \t }
\t \t \t else
\t \t \t {
\t \t \t \t return -1;
\t \t \t }
\t \t }
\t \t
\t }
}
이 내가 형 Category
의 category
내 DB 입력 Question
의 question
을 넣을 수있는 방법, 내 코드? 나는 할 수 없다 .. 제발 도와주세요. thx u
"야후 응답"으로 gwt로 google 응용 프로그램을 구현해야하며 패턴 합성, 반복기, 전략 및 사용자를위한 싱글 톤으로 클래스를 관리해야합니다.
이제 데이터베이스를 채우고 싶지만 문자열은 없지만 객체를 사용합니다 .. 데이터베이스 (mapdb)를 카테고리, 질문 및 답변으로 채우려면 어떻게해야합니까? Category
을 String
에 입력해야합니까?
package faq.server;
import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import org.mapdb.DB;
import org.mapdb.DBMaker;
import faq.client.FAQService;
import faq.shared.Answer;
import faq.shared.Category;
import faq.shared.DuplicatedUserNameException;
import faq.shared.Judgment;
import faq.shared.Question;
import faq.shared.User;
import faq.shared.UserNotFoundException;
import faq.shared.WrongPasswordException;
public class FAQServiceImpl implements FAQService {
LoginServiceImpl loginService;
AnswerHandlerImpl answerHandler;
CategoryHandlerImpl categoryHandler;
JudgmentHandlerImpl judgmentHandler;
QuestionHandlerImpl questionHandler;
public FAQServiceImpl() {
DB db = DBMaker.newFileDB(new File("FAQ-DB.txt")).closeOnJvmShutdown().make();
loginService = new LoginServiceImpl(db);
answerHandler = new AnswerHandlerImpl(db);
categoryHandler = new CategoryHandlerImpl(db);
judgmentHandler = new JudgmentHandlerImpl(db);
questionHandler = new QuestionHandlerImpl(db, categoryHandler);
}
// DEBUG svuotare il db
public void restartDb() {
loginService.clear();
answerHandler.clear();
categoryHandler.clear();
judgmentHandler.clear();
questionHandler.clear();
loginService.addAdmin();
categoryHandler.addRoot();
}
// DEBUG popolare il db
public void populate() {
try {
addUser("Sergio" , "m" , "[email protected]" , null , null , null , null , null , null);
addUser("Valentina" , "f" , "[email protected]" , null , null , null , null , null , null);
addUser("massimo" , "massimo" , "[email protected]" , null , null , null , null , null , null);
} catch (DuplicatedUserNameException e) {
e.printStackTrace();
}
addCategory("Scienze");
addSubCategory(null , "Ecologia");
addSubCategory(null , "Programmazione");
addSubCategory(null , "JavaScript");
addSubCategory(null , "Java");
addSubCategory(null , "PHP");
}
public User addUser(String username, String password, String email, String name, String surname, String sex,
String birthDate, String birthPlace, String address) throws DuplicatedUserNameException {
return loginService.addUser(username , password , email , name , surname , sex , birthDate , birthPlace ,
address);
}
public User login(String userName, String password) throws WrongPasswordException, UserNotFoundException {
return loginService.login(userName , password);
}
public ArrayList<User> getRegistered() {
return loginService.getRegistered();
}
public ArrayList<User> getAllUsers() {
return loginService.getAllUsers();
}
public ArrayList<User> nominateJudge(String userName){
return loginService.nominateJudge(userName);
}
public ArrayList<User> getJudges() {
return loginService.getJudges();
}
public ArrayList<Question> addQuestion(Category category, Question question) {
questionHandler.addQuestion(category, question);
return getQuestions(category);
}
@Override
public ArrayList<Question> getQuestions(Category category) {
return getQuestions(category);
}
@Override
public ArrayList<Question> removeQuestion(Category category, Question question, User user) {
questionHandler.removeQuestion(category, question);
return getQuestions(category);
}
@Override
public Question addAnswer(Answer answer, Question question) {
answerHandler.addAnswer(question, answer);
return question;
}
public ArrayList<Answer> getAnswers(Question question) {
return answerHandler.getAnswers(question);
}
@Override
public Question removeAnswer(Question question, User user, Answer answer) {
answerHandler.removeAnswer(question, answer);
return question;
}
@Override
public Category renameCategory(Category category, String name) {
categoryHandler.renameCategory(category, name);
return getAllCategory();
}
@Override
public Category addCategory(String categoryName) {
categoryHandler.addCategory(categoryName);
return getAllCategory();
}
@Override
public Category addSubCategory(Category category, String name) {
categoryHandler.addSubCategory(category, name);
return getAllCategory();
}
@Override
public Question getQuestions(Category category, String question) {
return getQuestions(category, question);
}
@Override
public Answer addJudgment(Answer answer, Judgment judgment) {
judgmentHandler.addJudgment(answer, judgment);
return answer ;
}
@Override
public Category getAllCategory() {
return categoryHandler.getAllCategory();
}
@Override
public ArrayList<Question> getAllQuestions() {
return questionHandler.getAllQuestions();
}
@Override
public Category getCategory(String categoryName) {
return categoryHandler.getCategory(categoryName, null);
}
}
나는 데이터베이스 .. 들으을 채우기 위해이 유
채우기 방법이 DB, chec categoryHandlerImpl의 구현. 객체가 있지만 String 인 경우 객체의 고유 한 값을 사용하여 하나의 문자열로 addCategory() 메서드를 호출합니다. 이게 제대로 작동하지 않으면 디버그해야합니다. – cralfaro
"할 수 없습니다"는 매우 유용한 오류 설명이 아닙니다. 정확히 무엇이 잘못 되었습니까? 컴파일 오류입니까 아니면 런타임에 있습니까? 전체 오류 메시지를 표시하십시오. 런타임 오류의 경우 전체 스택 추적. – vanje
addSubCategory (Cetegory category, String name) 그래서 poulate 메소드에서 "Category"객체를 가진 하위 카테고리를 어떻게 추가 할 수 있습니까? 이제 "null"을 넣었으나 잘못되었습니다. @cralfaro –