package com.ese.ils.beta.beans;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.model.SelectItem;
import com.ese.ils.beta.model.Module;
import com.ese.ils.beta.model.Question;
import com.ese.ils.beta.service.ModuleLocalServiceUtil;
import com.ese.ils.beta.service.QuestionLocalServiceUtil;
import com.icesoft.faces.context.effects.JavascriptContext;
import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.model.User;
import com.liferay.portal.service.UserServiceUtil;
/**
* dient zur Implementierung von Geschaeftslogik
* @author julien hofer
*
*/
@ManagedBean
@ViewScoped
public class DozentLogicBean implements Serializable {
private static final long serialVersionUID = -7931635152042080531L;
//Vaiables
long userId;
String moduleTitle;
String addInfo;
String answer;
List<SelectItem> moduleListOfDozent;
List<SelectItem> questionListOfModule;
//Injection
@ManagedProperty(value="#{questionModelBean}")
private transient QuestionModelBean questionModelBean;
@ManagedProperty(value="#{dozentMessageBean}")
private transient DozentMessageBean dozentMessageBean;
/**
* erzeugt eine neues Modul und speichert es in der Datenbank
* @param event
*/
public void createNewModule(ActionEvent event){
try {
Date d = new Date();
fetchUserId(event);
ModuleLocalServiceUtil.addModule((int) userId, moduleTitle, addInfo, d);
System.out.println("Neues Modul wurde angelegt!!!!!!!");
dozentMessageBean.showMessage(event, "Neues Modul wurde angelegt!");
} catch (Exception e) {
e.printStackTrace();
System.out.println("Das Hinzuf�gen eines Moduls hat nicht geklappt!");
}
}
/**
* loescht ein Modul mit bestimmter <code>moduleId</code>
* @param moduleId
*/
public void deleteModule(int moduleId) {
try {
ModuleLocalServiceUtil.deleteModule(moduleId);
System.out.println("Modul mit der ID :" + moduleId + " gel�scht");
} catch (PortalException e) {
System.out.println("Modul l�schen fehlgeschlagen PortletException!");
e.printStackTrace();
} catch (SystemException e) {
System.out.println("Modul l�schen fehlgeschlagen SystemException!");
e.printStackTrace();
}
}
/**
* holt alle Module eines Dozenten aus der Datenbank
* @param event
*/
public void fetchModulesOfDozent(ActionEvent event) {
fetchUserId(event);
moduleListOfDozent = new ArrayList<SelectItem>();
for (Module module : ModuleLocalServiceUtil.fetchModulesByLecturerUser(userId)) {
System.out.println("ModulId: " + module.getModuleId()
+ " Titel: " + module.getModuleTitle());
moduleListOfDozent.add(new SelectItem(module.getModuleId(),module.getModuleTitle()));
}
}
/**
* holt alle Fragen eines bestimmten Moduls anhand der <code>moduleId</code>
* @param event
* @param moduleId
*/
public void fetchQuestionsOfModule(ActionEvent event, int moduleId) {
System.out.println("Hole Fragen f�r ModulId: " + moduleId);
questionListOfModule = new ArrayList<SelectItem>();
for (Question question : QuestionLocalServiceUtil.fetchQuestionItems(moduleId)) {
questionListOfModule.add(new SelectItem(question.getQuestionId(),question.getQuestionText()));
}
}
/**
* beantwortet eine Frage
* @param event
*/
public void answerQuestion(ActionEvent event) {
try {
long questionId = questionModelBean.getQuestionId();
Question question = QuestionLocalServiceUtil.fetchQuestion(questionId);
question.setAnswer(answer);
QuestionLocalServiceUtil.updateQuestion(question);
dozentMessageBean.showMessage(event, "Frage wurde beantwortet!");
} catch (SystemException sysEx) {
sysEx.printStackTrace();
System.out.println("Beim Beantworten ist ein Fehler aufgetreten!");
}
}
/**
* holt die <code>userId</code> des aktuell angemeldeten Users
* @param event
*/
public void fetchUserId(ActionEvent event) {
String remoteUserId = (String)((Map)FacesContext.getCurrentInstance().getExternalContext().getRequestMap().get("javax.portlet.userinfo")).get("liferay.user.id");
try {
User user = UserServiceUtil.getUserById(Long.parseLong(remoteUserId));
String userName = user.getScreenName();
userId = user.getUserId();
System.out.println("===========================");
System.out.println("Eingeloggt als: " + userName);
System.out.println("User ID: " + userId);
System.out.println("===========================");
} catch (NumberFormatException e) {
e.printStackTrace();
} catch (PortalException e) {
e.printStackTrace();
} catch (SystemException e) {
e.printStackTrace();
}
}
//GETTER UND SETTER
public String getModuleTitle() {
return moduleTitle;
}
public void setModuleTitle(String moduleTitle) {
this.moduleTitle = moduleTitle;
}
public String getAddInfo() {
return addInfo;
}
public void setAddInfo(String addInfo) {
this.addInfo = addInfo;
}
public List<SelectItem> getModuleListOfDozent() {
return moduleListOfDozent;
}
public void setModuleListOfDozent(List<SelectItem> moduleListOfDozent) {
this.moduleListOfDozent = moduleListOfDozent;
}
public List<SelectItem> getQuestionListOfModule() {
return questionListOfModule;
}
public void setQuestionListOfModule(List<SelectItem> questionListOfModule) {
this.questionListOfModule = questionListOfModule;
}
public QuestionModelBean getQuestionModelBean() {
return questionModelBean;
}
public void setQuestionModelBean(QuestionModelBean questionModelBean) {
this.questionModelBean = questionModelBean;
}
public String getAnswer() {
return answer;
}
public void setAnswer(String answer) {
this.answer = answer;
}
public long getUserId() {
return userId;
}
public void setUserId(long userId) {
this.userId = userId;
}
public DozentMessageBean getDozentMessageBean() {
return dozentMessageBean;
}
public void setDozentMessageBean(DozentMessageBean dozentMessageBean) {
this.dozentMessageBean = dozentMessageBean;
}
}