MapManageView.java 5.68 KB
package fi.insomnia.bortal.view;

import java.util.ArrayList;
import java.util.List;

import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.model.ListDataModel;

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

import fi.insomnia.bortal.beans.EventBeanLocal;
import fi.insomnia.bortal.beans.EventMapBeanLocal;
import fi.insomnia.bortal.beans.PermissionDeniedException;
import fi.insomnia.bortal.beans.PlaceBeanLocal;
import fi.insomnia.bortal.beans.UserBeanLocal;
import fi.insomnia.bortal.model.EventMap;
import fi.insomnia.bortal.model.Place;
import fi.insomnia.bortal.model.Product;

@ManagedBean(name = "mapManageView")
@SessionScoped
public class MapManageView extends GenericView {

	private static final Logger logger = LoggerFactory.getLogger(MapManageView.class);
	@EJB
	private UserBeanLocal userBean;
	@EJB
	private EventBeanLocal eventBean;
	@EJB
	private EventMapBeanLocal eventmapBean;
	@EJB
	private PlaceBeanLocal placebean;

	private ListDataModel<EventMap> eventmaps;
	private EventMap map;
	private String mapname;

	private String buyableLike;

	private boolean tablesHorizontal = false;
	private boolean oneRowTable = false;
	private int placesInRow = 1;
	private int width;
	private int height;
	private int startX;
	private int startY;

	private int tableCount = 1;
	private int tableXdiff;
	private int tableYdiff;

	private Product mapproduct;
	private String namebase;

	public ListDataModel<EventMap> getMaps() {
		eventmaps = new ListDataModel<EventMap>(eventBean.getCurrentEvent().getEventMaps());
		return eventmaps;
	}

	public String lockBuyable() {

		int count = placebean.setBuyable(map, buyableLike, false);
		this.addFaceMessage("mapManage.lockedPlaces", count);
		return null;
	}

	public String releaseBuyable() {
		int count = placebean.setBuyable(map, buyableLike, true);
		this.addFaceMessage("mapManage.releasedPlaces", count);

		return null;
	}

	public String editMap() {

		setMap(eventmaps.getRowData());
		return "edit";
	}

	public String saveMap() {

		eventmapBean.saveMap(getMap());
		return "edit";
	}

	public String createMap() {
		EventMap map;
		try {
			map = eventmapBean.create(getMapname());
			setMap(map);
		} catch (PermissionDeniedException e) {
			logger.info("Permission denied", e);
		}
		return "edit";
	}

	public void generatePlaces() {
		String[] tablenames = getNamebase().split(";");
		List<Place> mapplaces = map.getPlaces();
		logger.debug("places in map before {}", mapplaces.size());
		if (mapplaces == null || mapplaces.isEmpty()) {
			mapplaces = new ArrayList<Place>();
			map.setPlaces(mapplaces);
		}
		for (int tableI = 0; tableI < tableCount; ++tableI) {
			int tableXStart = tableI * tableXdiff;
			int tableYStart = tableI * tableYdiff;
			int rows = isOneRowTable() ? 1 : 2;

			for (int rowI = 0; rowI < rows; ++rowI) {
				int rowXStart = tableXStart + (tablesHorizontal ? 0 : width * rowI);
				int rowYStart = tableYStart + (tablesHorizontal ? height * rowI : 0);
				logger.debug("row start {} {}", rowXStart, rowYStart);

				for (int placeI = 1; placeI <= placesInRow; ++placeI) {

					Place place = new Place(map);
					place.setHeight(Math.abs(height));
					place.setWidth(Math.abs(width));
					int xpos = startX + rowXStart + (tablesHorizontal ? placeI * width : 0);
					int ypos = startY + rowYStart + (tablesHorizontal ? 0 : placeI * height);
					logger.debug("Creating map in {} {}", xpos, ypos);
					place.setMapX(xpos);
					place.setMapY(ypos);
					place.setProduct(mapproduct);
					place.setName(tablenames[tableI] + (placeI + placesInRow * rowI));
					mapplaces.add(place);
				}
			}
		}
		logger.debug("places in map after {}", mapplaces.size());

		map = eventmapBean.saveMap(map);
		logger.debug("places in map merge {}", map.getPlaces().size());

	}

	public void setMapname(String mapname) {
		this.mapname = mapname;
	}

	public String getMapname() {
		return mapname;
	}

	public void setMap(EventMap map) {
		this.map = map;
	}

	public EventMap getMap() {
		return map;
	}

	public int getWidth() {
		return width;
	}

	public void setWidth(int width) {
		this.width = width;
	}

	public int getHeight() {
		return height;
	}

	public void setHeight(int height) {
		this.height = height;
	}

	public int getStartX() {
		return startX;
	}

	public void setStartX(int startX) {
		this.startX = startX;
	}

	public int getStartY() {
		return startY;
	}

	public void setStartY(int startY) {
		this.startY = startY;
	}

	public Product getMapproduct() {
		return mapproduct;
	}

	public void setMapproduct(Product mapproduct) {
		this.mapproduct = mapproduct;
	}

	public String getNamebase() {
		return namebase;
	}

	public int getTableCount() {
		return tableCount;
	}

	public void setTableCount(int tableCount) {
		this.tableCount = tableCount;
	}

	public int getTableXdiff() {
		return tableXdiff;
	}

	public void setTableXdiff(int tableXdiff) {
		this.tableXdiff = tableXdiff;
	}

	public int getTableYdiff() {
		return tableYdiff;
	}

	public void setTableYdiff(int tableYdiff) {
		this.tableYdiff = tableYdiff;
	}

	public void setTablesHorizontal(boolean tablesHorizontal) {
		this.tablesHorizontal = tablesHorizontal;
	}

	public boolean isTablesHorizontal() {
		return tablesHorizontal;
	}

	public void setPlacesInRow(int placesInRow) {
		this.placesInRow = placesInRow;
	}

	public int getPlacesInRow() {
		return placesInRow;
	}

	public void setOneRowTable(boolean oneRowTable) {
		this.oneRowTable = oneRowTable;
	}

	public boolean isOneRowTable() {
		return oneRowTable;
	}

	public void setNamebase(String namebase) {
		this.namebase = namebase;
	}

	public void setBuyableLike(String buyableLike) {
		this.buyableLike = buyableLike;
	}

	public String getBuyableLike() {
		return buyableLike;
	}
}