package com.ese.ils.beta.beans;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.model.SelectItem;
import org.icefaces.ace.component.chart.Axis;
import org.icefaces.ace.component.chart.AxisType;
import org.icefaces.ace.model.chart.CartesianSeries;
import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.service.ServiceContext;
import com.liferay.portlet.polls.DuplicateVoteException;
import com.liferay.portlet.polls.model.PollsChoice;
import com.liferay.portlet.polls.model.PollsQuestion;
import com.liferay.portlet.polls.model.PollsVote;
import com.liferay.portlet.polls.service.PollsChoiceLocalServiceUtil;
import com.liferay.portlet.polls.service.PollsQuestionLocalServiceUtil;
import com.liferay.portlet.polls.service.PollsQuestionServiceUtil;
import com.liferay.portlet.polls.service.PollsVoteLocalServiceUtil;
/**
* Implementiert den Umfrage-Modus, inklusive Ergebnis-Diagramm
* @author eduard walter
*
*/
@ManagedBean
@ViewScoped
public class VoteBackingBean implements Serializable {
private static final long serialVersionUID = 451009636017109953L;
@ManagedProperty(value = "#{voteNavigationBean}")
private transient VoteNavigationBean voteNavigationBean;
@ManagedProperty(value="#{navigationBackingBean}")
private transient NavigationBackingBean navigationBackingBean;
private String questionTitle;
private String questionText;
private String currentVoteId;
private List<SelectItem> voteItems;
private boolean disableVoteStatus = false;
private List<CartesianSeries> barData;
private boolean voteNotifier;
private long newestVoteId;
private int voteCount;
private String choiceA;
private String choiceB;
private String choiceC;
private String choiceD;
private int voteCountA = 14;
/**
* Navigationsmethode mit Ziel voteHome.xhtml
* Laedt verfuegbare Umfragen in eine Liste
* @param event
*/
public void moveToVoteHome(ActionEvent event){
fetchVoteItems();
navigationBackingBean.setHomeStatus(false);
voteNavigationBean.setVoteCreateStatus(false);
voteNavigationBean.setVoteShowStatus(false);
voteNavigationBean.setVoteResultsStatus(false);
voteNavigationBean.setVoteHomeStatus(true);
}
/**
* Laedt aktuelle Umfragen aus der DB
*/
public void fetchVoteItems(){
voteItems = new ArrayList<SelectItem>();
try {
for (PollsQuestion pQuestion : PollsQuestionLocalServiceUtil
.getPollsQuestions(0, PollsQuestionLocalServiceUtil
.getPollsQuestionsCount() - 1)) {
//user title parsed from xml!
voteItems.add(new SelectItem(pQuestion.getQuestionId(),""+pQuestion.getTitleCurrentValue()));
}
} catch (SystemException e) {
e.printStackTrace();
}
}
/**
* Wird aus der Benachrichtigung ueber neue Umfrage ausgeloest
* fuehrt direkt zur neuen Umfrage
*/
public void moveToNewVote(){
System.out.println(currentVoteId);
setCurrentVoteId(""+newestVoteId);
fetchVote();
navigationBackingBean.setHomeStatus(false);
voteNavigationBean.setVoteHomeStatus(false);
voteNavigationBean.setVoteResultsStatus(false);
voteNavigationBean.setVoteShowStatus(true);
}
/**
* Navigationsmethode mit Ziel voteShow.xhtml
* @param event
*/
public void moveToVoteShow(ActionEvent event){
fetchVote();
voteNavigationBean.setVoteHomeStatus(false);
voteNavigationBean.setVoteResultsStatus(false);
voteNavigationBean.setVoteShowStatus(true);
}
/**
* Laedt eine Umfrage zur Ansicht aus der DB
*/
public void fetchVote(){
try {
PollsQuestion pQuestion = PollsQuestionLocalServiceUtil.getPollsQuestion(Long.valueOf(currentVoteId));
questionTitle=pQuestion.getTitleCurrentValue();
questionText=pQuestion.getDescriptionCurrentValue();
List<PollsChoice> choiceList = pQuestion.getChoices();
choiceA=choiceList.get(0).getDescription();
choiceB=choiceList.get(1).getDescription();
choiceC=choiceList.get(2).getDescription();
choiceD=choiceList.get(3).getDescription();
} catch (NumberFormatException e) {
e.printStackTrace();
} catch (PortalException e) {
e.printStackTrace();
} catch (SystemException e) {
e.printStackTrace();
}
}
/**
* Navigationsmethode mit Ziel voteResults.xhtml
* Laedt aktuelle Umfrage und entsprechende Ergebnisse
* @param event
*/
public void moveToVoteResults(ActionEvent event){
fetchVote();
fetchResults();
voteNavigationBean.setVoteHomeStatus(false);
voteNavigationBean.setVoteShowStatus(false);
voteNavigationBean.setVoteResultsStatus(true);
}
/**
* Laedt Ergebnisse der aktuellen Umfrage aus der DB zur Ansicht im Diagramm
*/
public void fetchResults(){
try {
barData = new ArrayList<CartesianSeries>() {{
add(new CartesianSeries() {{
setType(CartesianType.BAR);
PollsQuestion pQuestion = PollsQuestionLocalServiceUtil.getPollsQuestion(Long.valueOf(currentVoteId));
add("A", PollsVoteLocalServiceUtil.getChoiceVotesCount(pQuestion.getChoices().get(0).getChoiceId()));
add("B", PollsVoteLocalServiceUtil.getChoiceVotesCount(pQuestion.getChoices().get(1).getChoiceId()));
add("C", PollsVoteLocalServiceUtil.getChoiceVotesCount(pQuestion.getChoices().get(2).getChoiceId()));
add("D", PollsVoteLocalServiceUtil.getChoiceVotesCount(pQuestion.getChoices().get(3).getChoiceId()));
}});
}};
setVoteCount(PollsVoteLocalServiceUtil.getQuestionVotesCount(Long.valueOf(currentVoteId)));
} catch (NumberFormatException e) {
e.printStackTrace();
} catch (PortalException e) {
e.printStackTrace();
} catch (SystemException e) {
e.printStackTrace();
}
}
private Axis barDemoDefaultAxis = new Axis() {{
setTickAngle(0);
}};
private Axis barDemoXAxis = new Axis() {{
setType(AxisType.CATEGORY);
}};
private Axis barDemoYAxis = new Axis() {{
setAutoscale(false);
setTickInterval("1");
}};
//verschiedene Abstimmungsmoeglichkeiten
public void voteA(ActionEvent event){
vote(0);
moveToVoteResults(event);
}
public void voteB(ActionEvent event){
vote(1);
moveToVoteResults(event);
}
public void voteC(ActionEvent event){
vote(2);
moveToVoteResults(event);
}
public void voteD(ActionEvent event){
vote(3);
moveToVoteResults(event);
}
/**
* Speichert eine Stimme fuer die gewaehlte Antwortmoeglichkeit ab
* @param i
*/
public void vote(int i){
try {
PollsVoteLocalServiceUtil.addVote(getCurrentUserId(), Long.valueOf(currentVoteId),
PollsQuestionLocalServiceUtil.getPollsQuestion(Long.valueOf(currentVoteId)).getChoices().get(i).getChoiceId(),
new ServiceContext());
} catch (NumberFormatException e) {
e.printStackTrace();
} catch (PortalException e) {
e.printStackTrace();
} catch (SystemException e) {
e.printStackTrace();
}
}
/**
* Hilfsmethode zur Ermittlung der aktuellen UserId
* @return long id
*/
private long getCurrentUserId() {
FacesContext fc = FacesContext.getCurrentInstance();
ExternalContext externalContext = fc.getExternalContext();
long id = Long.parseLong(externalContext.getUserPrincipal().getName());
return id;
}
public VoteNavigationBean getVoteNavigationBean() {
return voteNavigationBean;
}
public void setVoteNavigationBean(VoteNavigationBean voteNavigationBean) {
this.voteNavigationBean = voteNavigationBean;
}
public NavigationBackingBean getNavigationBackingBean() {
return navigationBackingBean;
}
public void setNavigationBackingBean(NavigationBackingBean navigationBackingBean) {
this.navigationBackingBean = navigationBackingBean;
}
public String getQuestionTitle() {
return questionTitle;
}
public void setQuestionTitle(String questionTitle) {
this.questionTitle = questionTitle;
}
public String getQuestionText() {
return questionText;
}
public void setQuestionText(String questionText) {
this.questionText = questionText;
}
public String getCurrentVoteId() {
return currentVoteId;
}
public void setCurrentVoteId(String currentVoteId) {
this.currentVoteId = currentVoteId;
}
public List<SelectItem> getVoteItems() {
return voteItems;
}
public void setVoteItems(List<SelectItem> voteItems) {
this.voteItems = voteItems;
}
public String getChoiceA() {
return choiceA;
}
public void setChoiceA(String choiceA) {
this.choiceA = choiceA;
}
public String getChoiceB() {
return choiceB;
}
public void setChoiceB(String choiceB) {
this.choiceB = choiceB;
}
public String getChoiceC() {
return choiceC;
}
public void setChoiceC(String choiceC) {
this.choiceC = choiceC;
}
public String getChoiceD() {
return choiceD;
}
public void setChoiceD(String choiceD) {
this.choiceD = choiceD;
}
public boolean isDisableVoteStatus() {
try {
if(currentVoteId!=null){
PollsVote pVote = PollsVoteLocalServiceUtil.addVote(getCurrentUserId(), Long.valueOf(currentVoteId),
PollsQuestionLocalServiceUtil.getPollsQuestion(Long.valueOf(currentVoteId)).getChoices().get(0).getChoiceId(),
new ServiceContext());
PollsVoteLocalServiceUtil.deletePollsVote(pVote.getVoteId());
return false;
} else {
return true;
}
} catch (DuplicateVoteException e) {
System.out.println("User hat bereits gevotet!");
return true;
} catch (NumberFormatException e) {
e.printStackTrace();
} catch (PortalException e) {
e.printStackTrace();
} catch (SystemException e) {
e.printStackTrace();
}
return false;
}
public void setDisableVoteStatus(boolean disableVoteStatus) {
this.disableVoteStatus = disableVoteStatus;
}
public int getVoteCountA() {
return voteCountA;
}
public void setVoteCountA(int voteCountA) {
this.voteCountA = voteCountA;
}
public List<CartesianSeries> getBarData() {
return barData;
}
public void setBarData(List<CartesianSeries> barData) {
this.barData = barData;
}
public Axis getBarDemoDefaultAxis() {
return barDemoDefaultAxis;
}
public void setBarDemoDefaultAxis(Axis barDemoDefaultAxis) {
this.barDemoDefaultAxis = barDemoDefaultAxis;
}
public Axis getBarDemoXAxis() {
return barDemoXAxis;
}
public void setBarDemoXAxis(Axis barDemoXAxis) {
this.barDemoXAxis = barDemoXAxis;
}
public Axis getBarDemoYAxis() {
return barDemoYAxis;
}
public void setBarDemoYAxis(Axis barDemoYAxis) {
this.barDemoYAxis = barDemoYAxis;
}
public boolean isVoteNotifier() {
try {
getCurrentUserId();
List<PollsQuestion> pqList = PollsQuestionLocalServiceUtil.getPollsQuestions(0,
PollsQuestionLocalServiceUtil.getPollsQuestionsCount() - 1);
PollsVote pVote = PollsVoteLocalServiceUtil.addVote(getCurrentUserId(),pqList.get(0).getQuestionId(),
PollsQuestionLocalServiceUtil.getPollsQuestion(pqList.get(0).getQuestionId()).getChoices().get(0).getChoiceId(),
new ServiceContext());
PollsVoteLocalServiceUtil.deletePollsVote(pVote.getVoteId());
setNewestVoteId(pqList.get(0).getQuestionId());
return true;
} catch(NullPointerException e){
return false;
} catch (DuplicateVoteException e){
System.out.println("user hat bereits gevotet");
return false;
} catch (SystemException e) {
e.printStackTrace();
} catch (NumberFormatException e) {
e.printStackTrace();
} catch (PortalException e) {
e.printStackTrace();
}
return voteNotifier;
}
public void setVoteNotifier(boolean voteNotifier) {
this.voteNotifier = voteNotifier;
}
public long getNewestVoteId() {
return newestVoteId;
}
public void setNewestVoteId(long newestVoteId) {
this.newestVoteId = newestVoteId;
}
public int getVoteCount() {
return voteCount;
}
public void setVoteCount(int voteCount) {
this.voteCount = voteCount;
}
}