BarcodeBean.java 6.05 KB
package fi.codecrew.moya.beans;

import java.io.IOException;
import java.io.InputStream;

import javax.ejb.EJB;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;

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

import fi.codecrew.moya.facade.PlaceFacade;
import fi.codecrew.moya.facade.PrintedCardFacade;
import fi.codecrew.moya.model.EventUser;
import fi.codecrew.moya.model.Place;
import fi.codecrew.moya.model.PrintedCard;
import fi.codecrew.moya.model.Product;
import fi.codecrew.moya.utilities.BarcodeUtils;

/**
 * Session Bean implementation class BarcodeBean
 */
@Stateless
@LocalBean
public class BarcodeBean implements BarcodeBeanLocal {

	private static final String PRINTED_CARD_PREFIX = "277"; //2M
	private static final String EVENTUSER_PREFIX = "279"; //2O
	private static final String PLACE_PREFIX = "289"; //2Y
	//private static final String NEXT_PREFIX = "265";			//2A

	private static final Logger logger = LoggerFactory.getLogger(BarcodeBean.class);

	@EJB
	PrintedCardFacade printedCardFacade;

	@EJB
	PlaceFacade placeFacade;

	@EJB
	UserBeanLocal userBean;
	
	@EJB
	ProductBeanLocal productBean;
	

	/**
	 * Default constructor.
	 */
	public BarcodeBean() {
		// TODO Auto-generated constructor stub
	}

	public InputStream getUserBarcode(EventUser user) throws IOException {
		StringBuilder sb = new StringBuilder();
		sb.append(EVENTUSER_PREFIX);
		String idStr = user.getId().toString();

		for (int i = 12 - idStr.length() - sb.length(); i > 0; --i) {
			sb.append("0");
		}
		sb.append(idStr);
		String barcode = sb.toString();
		logger.debug("Geneating barcode for user {} : {}", user, barcode);

		return BarcodeUtils.getBarcodeEAN(barcode);
	}

	public InputStream getCardBarcode(PrintedCard printedCard) throws IOException {
		StringBuilder sb = new StringBuilder();
		sb.append(PRINTED_CARD_PREFIX);
		String idStr = printedCard.getId().toString();

		for (int i = 12 - idStr.length() - sb.length(); i > 0; --i) {
			sb.append("0");
		}
		sb.append(idStr);
		String barcode = sb.toString();
		logger.debug("Geneating barcode for card {} : {}", printedCard, barcode);

		return BarcodeUtils.getBarcodeEAN(barcode);
	}

	public String getPlaceHexcode(Place place) {
		StringBuilder sb = new StringBuilder();
		sb.append(PLACE_PREFIX);
		String idStr = place.getId().toString();

		for (int i = 8 - idStr.length() - sb.length(); i > 0; --i) {
			sb.append("0");
		}

		sb.append(idStr);
		String barcode = sb.toString();

		String hexcode = Long.toHexString(Long.parseLong(barcode));

		String checksum = Integer.toHexString(hexcode.hashCode());
		checksum = checksum.substring(checksum.length() - 3);
		hexcode += checksum;

		logger.debug("Geneating hexcode for place {} : {}", place.getId(), hexcode);

		return hexcode;
	}

	public String getVrAuthCodeForCard(PrintedCard printedCard) {
		StringBuilder sb = new StringBuilder();
		sb.append(PRINTED_CARD_PREFIX);
		if (printedCard == null)
			return "";
		String idStr = printedCard.getId().toString();

		for (int i = 8 - idStr.length() - sb.length(); i > 0; --i) {
			sb.append("0");
		}
		sb.append(idStr);
		String code = sb.toString();

		String hexcode = Integer.toHexString(Integer.parseInt(code));
		String checksum = Integer.toHexString(hexcode.hashCode());
		checksum = checksum.substring(checksum.length() - 3);
		hexcode += checksum;
		//hexcode = Integer.toString(hexcode.hashCode());

		logger.debug("Geneating VrAuthcode for card {} : {}", printedCard.getId(), hexcode);

		return hexcode;
	}

	public String checkVrAuthCode(String code) {
		String checksumNew = code.substring(code.length() - 3);
		code = code.substring(0, (code.length() - 3));
		String checksumOld = Integer.toHexString(code.hashCode());
		checksumOld = checksumOld.substring(checksumOld.length() - 3);
		if (checksumNew.equals(checksumOld)) {
			int decimal = Integer.decode(code);
			code = Integer.toString(decimal);
			String prefix = code.substring(0, 2);
			int id = Integer.parseInt(code.substring(3, code.length() - 1));
			if (prefix.equals(PRINTED_CARD_PREFIX) && id != 0) {
				PrintedCard printedCard = printedCardFacade.find(id);
				if (printedCard != null)
					return printedCard.getUser().getNick();
				else
					return "Invalid";
			}
		} else {
			return "Invalid";
		}
		return "Invalid";
	}

	@Override
	public Place getPlaceFromHexcode(String hexcode) {

		String barcode = "" + Integer.parseInt(hexcode, 16);

		try {
			if (barcode.startsWith(PLACE_PREFIX)) {
				int id = Integer.parseInt(barcode.substring(3, barcode.length() - 1));
				Place place = placeFacade.find(id);

				return place;
			}
		} catch (NumberFormatException x) {
		}

		return null;
	}

	public PrintedCard getPrintedCard(String barcode) {
		if (barcode == null || barcode.isEmpty())
			return null;

		// it's our special front barcode
		try {
			if (barcode.startsWith(PRINTED_CARD_PREFIX)) {
				int id = Integer.parseInt(barcode.substring(3, barcode.length() - 1));
				PrintedCard card = printedCardFacade.find(id);

				if (card != null)
					return card;
			}
		} catch (NumberFormatException x) {
		}

		return printedCardFacade.findByBarcode(barcode);
	}

	public EventUser getUser(String barcode) {
		if (barcode == null || barcode.isEmpty())
			return null;

		// trim zeros off from barcode
		String stripped = barcode;
		while (stripped.length() > 0 && stripped.charAt(0) == '0') {
			stripped = stripped.substring(1);
		}

		if (stripped.length() > 0)
			barcode = stripped;

		try {
			// it's our special front barcode
			if (barcode.startsWith(EVENTUSER_PREFIX)) {
				int id = Integer.parseInt(barcode.substring(3, barcode.length() - 1));
				logger.debug("finding user with barcode {} and id {}", barcode, id);
				EventUser user = userBean.findByEventUserId(id);

				return user;
			}
		} catch (NumberFormatException x) {
		}

		return null;
	}

	@Override
	public Product getProduct(String barcode) {
		return productBean.findByBarcode(barcode);
	}

	@Override
	public Place getPlaceFromBarcode(String barcode) {

		// TODO: tarttee hakea se paikkakoodi, ja sen avulla paikka. Toivon että se on ny tämä eikä placecodehäsmäkkä, mikä se oikeasti on. Vittu lisää refactorointia
	}

}