EntryWrapper.java 1.92 KB
package fi.codecrew.moya.web.cdiview.voting;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.faces.model.ListDataModel;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import fi.codecrew.moya.beans.VotingBeanLocal;
import fi.codecrew.moya.model.CompoEntry;
import fi.codecrew.moya.model.Vote;

public class EntryWrapper implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = -8261328741517788115L;
	private Integer votetotal;
	private final CompoEntry entry;

	private Integer vote = 0;
	private static final Logger logger = LoggerFactory.getLogger(EntryWrapper.class);

	public EntryWrapper(CompoEntry entry, Vote voteEntity, boolean calcSummary) {
		this.entry = entry;
		if (voteEntity != null)
		{
			vote = voteEntity.getScore();
		}
		if (calcSummary) {
			votetotal = 0;
			for (Vote v : entry.getVotes()) {
				votetotal += v.getScore();
			}
		}
	}

	public static ListDataModel<EntryWrapper> init(List<CompoEntry> compoEntries, VotingBeanLocal votbean, boolean calcSummary) {
		ArrayList<EntryWrapper> ret = new ArrayList<EntryWrapper>();
		for (CompoEntry entry : compoEntries)
		{
			if (entry.getSort() != null && entry.getSort() > 0)
			{
				Vote voteEntity = votbean.saveVote(entry, null);
				EntryWrapper wrap = new EntryWrapper(entry, voteEntity, calcSummary);
				ret.add(wrap);

			}
		}
		return new ListDataModel<EntryWrapper>(ret);
	}

	public CompoEntry getEntry() {
		return entry;
	}

	public Integer getVote() {
		return vote;
	}

	public void setVote(Integer vot) {
		if (vot == null) {
			this.vote = 0;
		} else {
			this.vote = vot;
		}
	}

	public Integer getVotetotal()
	{
		return votetotal;
	}

	public Integer getRatingVote()
	{
		return vote + 3;
	}

	public void setRatingVote(Integer vot)
	{
		if (vot < 1 || vot > 5)
		{
			vot = 3;
		}
		this.vote = vot - 3;
		logger.info("setting ratevote {} to realvote {}", vot, vote);

	}
}