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

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

import fi.codecrew.moya.model.EventMap;
import fi.codecrew.moya.model.EventUser;
import fi.codecrew.moya.model.Place;
import fi.codecrew.moya.model.PrintedCard;
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.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;

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