PojoUtils.java 8.68 KB
package fi.codecrew.moya.rest;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import javax.xml.bind.annotation.XmlElement;

import fi.codecrew.moya.model.EventMap;
import fi.codecrew.moya.model.EventUser;
import fi.codecrew.moya.model.GroupMembership;
import fi.codecrew.moya.model.Place;
import fi.codecrew.moya.model.PrintedCard;
import fi.codecrew.moya.model.Product;
import fi.codecrew.moya.model.Reader;
import fi.codecrew.moya.model.ReaderEvent;
import fi.codecrew.moya.rest.pojo.map.v1.MapPojo;
import fi.codecrew.moya.rest.pojo.map.v1.MapRoot;
import fi.codecrew.moya.rest.pojo.map.v1.PlacePojo;
import fi.codecrew.moya.rest.pojo.map.v1.PlaceRoot;
import fi.codecrew.moya.rest.pojo.placemap.v1.PlacemapMapRootPojo;
import fi.codecrew.moya.rest.pojo.placemap.v1.ProductRestPojo;
import fi.codecrew.moya.rest.pojo.placemap.v1.SimplePlacePojo;
import fi.codecrew.moya.rest.pojo.placemap.v1.SimplePlacelistRoot;
import fi.codecrew.moya.rest.pojo.reader.v1.ReaderEventRestPojo;
import fi.codecrew.moya.rest.pojo.reader.v1.ReaderRestPojo;
import fi.codecrew.moya.rest.pojo.userinfo.v1.CardRoot;
import fi.codecrew.moya.rest.pojo.userinfo.v1.EventUserRestPojo;
import fi.codecrew.moya.rest.pojo.userinfo.v1.PrintedCardRestPojo;
import fi.codecrew.moya.rest.pojo.userinfo.v1.SimpleEventuserRoot;
import fi.codecrew.moya.rest.pojo.userinfo.v1.UserReservationPlacePojo;

public class PojoUtils {
	public static EventUserRestPojo initEventUserRestPojo(EventUser user)
	{
		EventUserRestPojo ret = new EventUserRestPojo();
		ret.setNick(user.getUser().getNick());
		ret.setLogin(user.getUser().getLogin());
		ret.setEventuserId(user.getId());
		ret.setUserId(user.getUser().getId());
		ret.setFirstname(user.getUser().getFirstnames());
		ret.setLastname(user.getUser().getLastname());

		return ret;
	}

	public static SimpleEventuserRoot parseEventusers(List<EventUser> users) {
		ArrayList<EventUserRestPojo> list = new ArrayList<>();
		for (EventUser u : users) {
			list.add(initEventUserRestPojo(u));
		}
		return new SimpleEventuserRoot(list);

	}

	public static PrintedCardRestPojo initPrintedCardRestPojo(PrintedCard card)
	{
		PrintedCardRestPojo ret = new PrintedCardRestPojo();
		ret.setEventuserId(card.getUser().getId());
		ret.setId(card.getId());

		if (card.getTemplate() != null)
			ret.setTemplate(card.getTemplate().getName());

		ret.setUsername(card.getUser().getNick());
		ret.setWholeName(card.getUser().getWholeName());
		ret.setState(card.getCardState().toString());
		if (card.getPrintTime() != null)
			ret.setPrintTime(card.getPrintTime().getTime());
		return ret;
	}

	public static CardRoot parsePrintedCards(List<PrintedCard> cards)
	{
		ArrayList<PrintedCardRestPojo> ret = new ArrayList<PrintedCardRestPojo>();
		for (PrintedCard c : cards) {
			ret.add(initPrintedCardRestPojo(c));
		}
		CardRoot root = new CardRoot();
		root.setCards(ret);
		return root;
	}

	public static PlacePojo initPlacePojo(Place place) {
		PlacePojo ret = new PlacePojo();
		if (place.getProduct() != null)
			ret.setProductId(place.getProduct().getId());
		if (place.getPlaceReserver() != null && place.getPlaceReserver().getPlaceGroup() != null && place.getPlaceReserver().getPlaceGroup().getCreator() != null)
			ret.setReserverId(place.getPlaceReserver().getPlaceGroup().getCreator().getId());

		ret.setId(place.getId());
		ret.setDescription(place.getDescription());
		ret.setName(place.getName());
		ret.setMapX(place.getMapX());
		ret.setMapY(place.getMapY());
		ret.setDetails(place.getDetails());
		ret.setCode(place.getCode());
		ret.setHeight(place.getHeight());
		ret.setWidth(place.getWidth());
		ret.setTaken(place.isTaken());
		ret.setBuyable(place.isBuyable());
		ret.setReleaseTime(place.getReleaseTime());
		ret.setDisabled(place.isDisabled());
		if (place.getMap() != null) {
			ret.setMapId(place.getMap().getId());
		}
		if (place.getPlaceReserver() != null && place.getPlaceReserver().getUser() != null) {
			ret.setEventuserId(place.getPlaceReserver().getUser().getId());
		}

		return ret;
	}

	public static PlaceRoot initPlaceRoot(EventMap map) {
		PlaceRoot ret = new PlaceRoot();
		ret.setMap(initMapPojo(map));
		ret.setPlaces(parsePlaces(map.getPlaces()));
		return ret;
	}

	private static List<PlacePojo> parsePlaces(List<Place> places) {
		ArrayList<PlacePojo> ret = new ArrayList<PlacePojo>();
		for (Place place : places) {
			ret.add(initPlacePojo(place));
		}
		return ret;
	}

	private static MapPojo initMapPojo(EventMap map) {
		MapPojo ret = new MapPojo();
		ret.setId(map.getId());
		ret.setName(map.getName());
		ret.setActive(map.isActive());
		return ret;
	}

	public static MapRoot parseMaps(List<EventMap> eventMaps) {
		List<MapPojo> ret = new ArrayList<>();
		for (EventMap m : eventMaps) {
			ret.add(initMapPojo(m));
		}

		return new MapRoot(ret);
	}

	public static ReaderEventRestPojo initReaderEventRestPojo(ReaderEvent event) {
		ReaderEventRestPojo ret = new ReaderEventRestPojo();

		if (event != null && event.getPrintedCard() != null) {
			if (event.getPrintedCard().getUser() != null) {
				ret.setEventUser(PojoUtils.initEventUserRestPojo(event.getPrintedCard().getUser()));
			}
		} else if (event != null && event.getUser() != null) {
			ret.setEventUser(PojoUtils.initEventUserRestPojo(event.getUser()));
		}

		ret.setReaderEventId(event.getId());
		ret.setReaderEventTime(event.getUpdatetime());
		ret.setReaderId(event.getReader().getId());

		if (event != null && event.getPrintedCard() != null) {
			ret.setPrintedCardId(event.getPrintedCard().getId());
		}
		if (event != null && event.getPrintedCard() != null) {
			ret.setPrintedCardState(event.getPrintedCard().getCardState().name());
		}
		return ret;
	}

	public static List<ReaderEventRestPojo> parseReaderEvents(List<ReaderEvent> readers) {

		List<ReaderEventRestPojo> ret = new ArrayList<>();
		for (ReaderEvent re : readers) {
			ret.add(initReaderEventRestPojo(re));
		}
		return ret;
	}

	public static ReaderRestPojo initReaderRestPojo(Reader reader)
	{
		ReaderRestPojo ret = new ReaderRestPojo();
		ret.setReaderId(reader.getId());
		ret.setIdentification(reader.getIdentification());
		ret.setDescription(reader.getDescription());

		String rt = null;
		if (reader.getType() != null) {
			rt = reader.getType().name();
		}
		ret.setReaderType(rt);
		return ret;
	}

	public static List<ReaderRestPojo> parseReaders(List<Reader> readers) {
		ArrayList<ReaderRestPojo> ret = new ArrayList<>();
		for (Reader r : readers) {
			ret.add(initReaderRestPojo(r));
		}
		return ret;
	}

	public static List<ProductRestPojo> parseProducts(List<Product> prods) {
		ArrayList<ProductRestPojo> ret = new ArrayList<ProductRestPojo>();
		for (Product p : prods) {
			ret.add(initProductRestPojo(p));
		}
		return ret;
	}

	public static ProductRestPojo initProductRestPojo(Product product)
	{
		ProductRestPojo ret = new ProductRestPojo();
		ret.setId(product.getId());
		ret.setName(product.getName());
		ret.setDescription(product.getDescription());
		ret.setPrice(product.getPrice().setScale(2, BigDecimal.ROUND_HALF_UP).toString());

		return ret;

	}

	public static SimplePlacelistRoot parseSimplePlaces(List<Place> places, EventUser user)
	{
		SimplePlacelistRoot ret = new SimplePlacelistRoot();
		ArrayList<SimplePlacePojo> placeList = new ArrayList<SimplePlacePojo>();
		ret.setPlaces(placeList);
		for (Place p : places) {
			placeList.add(initSimplePlacePojo(p, user));
		}
		return ret;

	}

	private static SimplePlacePojo initSimplePlacePojo(Place p, EventUser user) {
		SimplePlacePojo ret = new SimplePlacePojo();
		ret.setId(p.getId());
		ret.setName(p.getName());
		String state = null;

		switch (p.getState(user))
		{
		case DISABLED:
			state = "D";
			break;
		case FREE:
			state = "F";
			break;
		case LOCKED:
			state = "L";
			break;
		case MY_PLACE:
			state = "P";
			break;
		case RESERVED:
			state = "R";
			break;
		case TEMP_RESERVED_FORME:
			state = "T";
			break;
		default:
			break;
		}
		ret.setState(state);

		ret.setX(p.getMapX());
		ret.setY(p.getMapY());
		ret.setWidth(p.getWidth());
		ret.setHeight(p.getHeight());
		return ret;
	}

	public static PlacemapMapRootPojo.MapPojo initPlacemapMapPojo(EventMap map) {
		PlacemapMapRootPojo.MapPojo ret = new PlacemapMapRootPojo.MapPojo();
		ret.setId(map.getId());
		ret.setName(map.getName());
		ret.setWidth(map.getWidth());
		ret.setHeight(map.getHeight());
		return ret;
	}

	public static UserReservationPlacePojo initUserReservationPlace(GroupMembership g) {
		UserReservationPlacePojo ur = new UserReservationPlacePojo();
		ur.setPlacegiven(g.getEnteredEvent() != null);

		Place place = g.getPlaceReservation();
		if (place != null) {
			ur.setPlaceid(place.getId());
			ur.setPlacename(place.getName());
			ur.setProductId(place.getProduct().getId());
			ur.setProductName(place.getProduct().getName());
		}
		return ur;
	}
}