MapManageView.java 7.95 KB
package fi.codecrew.moya.web.cdiview.map;

import java.io.ByteArrayInputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.ejb.EJB;
import javax.enterprise.context.ConversationScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.inject.Named;

import org.primefaces.model.DefaultStreamedContent;
import org.primefaces.model.StreamedContent;
import org.primefaces.model.UploadedFile;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import fi.codecrew.moya.beans.EventBeanLocal;
import fi.codecrew.moya.beans.EventMapBeanLocal;
import fi.codecrew.moya.beans.PlaceBeanLocal;
import fi.codecrew.moya.beans.ProductBeanLocal;
import fi.codecrew.moya.beans.UserBeanLocal;
import fi.codecrew.moya.enums.apps.MapPermission;
import fi.codecrew.moya.model.EventMap;
import fi.codecrew.moya.model.LanEvent;
import fi.codecrew.moya.model.Place;
import fi.codecrew.moya.model.Product;
import fi.codecrew.moya.web.cdiview.GenericCDIView;

@Named
@ConversationScoped
public class MapManageView extends GenericCDIView {

	/**
	 * 
	 */
	private static final long serialVersionUID = -3276250982780044688L;
	private static final Logger logger = LoggerFactory.getLogger(MapManageView.class);
	@EJB
	private transient UserBeanLocal userBean;
	@EJB
	private transient EventBeanLocal eventBean;
	@EJB
	private transient EventMapBeanLocal eventmapBean;
	@EJB
	private transient PlaceBeanLocal placebean;

	@EJB
	private transient ProductBeanLocal productbean;

	private transient UploadedFile bgFile;

	private List<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;
	private Integer mapId;
	private List<Product> productlist;
	
	private StreamedContent streamedFile;

	private LanEvent copyEvent;

	public void initCreate() {
		if (super.requirePermissions(MapPermission.MANAGE_MAPS)) {
			super.beginConversation();
		}

	}

	public void initView() {
		if (super.requirePermissions(MapPermission.MANAGE_MAPS)) {
			map = eventmapBean.find(getMapId());
			setProductlist(productbean.getProducts());
			super.beginConversation();
			streamedFile = new DefaultStreamedContent(new ByteArrayInputStream(placebean.generatePlacesPdf(54, 17, 18, 15)),"application/pdf", "paikkakoodit");
		}
		
		

	}

	public String submitBg()
	{
		map.setMapData(bgFile.getContents());
		map = eventmapBean.saveMap(map);
		return null;
	}

	public List<EventMap> getMaps() {
		if (eventmaps == null) {
			eventmaps = 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 saveMap() {

		eventmapBean.saveMap(getMap());
		return null;
	}

	public String createMap() {

		map = eventmapBean.create(getMapname());

		return "edit";
	}

	public void mapClick(ActionEvent e) {
		FacesContext context = FacesContext.getCurrentInstance();
		String clientId = e.getComponent().getClientId(context);

		Map<String, String> requestParams = context.getExternalContext().getRequestParameterMap();

		startX = new Integer(requestParams.get(clientId + ".x")).intValue();
		startY = new Integer(requestParams.get(clientId + ".y")).intValue();
	}

	public String removeAllPlaces()
	{
		map = eventmapBean.clearPlaces(map);
		return null;
	}

	public String generatePlaces() {
		String[] tablenames = getNamebase().split(";");
		List<Place> mapplaces = map.getPlaces();

		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());

		return null;
	}

	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;
	}

	public void setMapId(Integer mapId) {
		this.mapId = mapId;
	}

	public Integer getMapId() {
		return mapId;
	}

	public void setProductlist(List<Product> productlist) {
		this.productlist = productlist;
	}

	public List<Product> getProductlist() {
		return productlist;
	}

	public LanEvent getCopyEvent() {
		return copyEvent;
	}

	public void setCopyEvent(LanEvent copyEvent) {
		this.copyEvent = copyEvent;
	}

	public UploadedFile getBgFile() {
		return bgFile;
	}

	public void setBgFile(UploadedFile bgFile) {
		this.bgFile = bgFile;
	}
	//StreamedContent   new DefaultStreamedContent(pdfstream)
	public StreamedContent getStreamedFile() {

		 //DefaultStreamedContent(new ByteArrayInputStream(placebean.generatePlacesPdf(54, 17, 10, 6)));
		
		return streamedFile;
	}
}