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

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

import fi.codecrew.moya.model.*;
import fi.codecrew.moya.rest.pojo.appconfig.v1.ApplicationInstancePojo;
import fi.codecrew.moya.rest.pojo.appconfig.v1.EventPojo;
import fi.codecrew.moya.rest.pojo.appconfig.v1.EventRoot;
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;
import fi.codecrew.moya.rest.pojo.util.v1.ErrorRoot;

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.setDisabled(place.isDisabled());


		// I cannot change REST -api without making new version, so let's simulate this ReleaseTime -feature from reserveTime
		Calendar relTime = Calendar.getInstance();

		relTime.add(relTime.HOUR, 4);

		if (place.getReserveTime() != null)
			ret.setReleaseTime(relTime);
		else
			ret.setReleaseTime(null);


		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.getPrintedCard() != null) {
			if (event.getPrintedCard().getUser() != null) {
				ret.setEventUser(PojoUtils.initEventUserRestPojo(event.getPrintedCard().getUser()));
			}
		} else if (event.getUser() != null) {
			ret.setEventUser(PojoUtils.initEventUserRestPojo(event.getUser()));
		}

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

		if (event.getPrintedCard() != null) {
			ret.setPrintedCardId(event.getPrintedCard().getId());
		}
		if (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, boolean hasPermissionViewAllusers) {
		return parseSimplePlaces(places, user, hasPermissionViewAllusers, false);
	}

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

	}

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

		if (hasPermissionViewAllusers) {
			if (p.getPlaceReserver() != null) {
				if (p.getPlaceReserver().getUser() != null) {
					ret.setUserDescription(p.getPlaceReserver().getUser().getUser().getShortUserDescriptor());
				} else if (p.getPlaceReserver().getPlaceGroup() != null && p.getPlaceReserver().getPlaceGroup().getCreator() != null) {
					ret.setUserDescription(p.getPlaceReserver().getPlaceGroup().getCreator().getUser().getShortUserDescriptor());
				}
			}
		}

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

		if (onlyHilightPlaces) {

		}

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

	public static EventPojo parseEvent(LanEvent event) {
		ArrayList<String> urls = new ArrayList<>();

		for (LanEventDomain domain : event.getDomains()) {
			urls.add(domain.getDomain());
		}

		EventPojo pojo = new EventPojo();
		pojo.setName(event.getName());
		pojo.setLanEventId(event.getId());
		pojo.setUrls(urls);
		pojo.setStartTime(event.getStartTime());
		return pojo;
	}

	public static EventRoot parseEvents(List<LanEvent> events) {

		EventRoot root = new EventRoot();

		ArrayList<EventPojo> eventPojos = new ArrayList<>();
		for (LanEvent event : events) {
			eventPojos.add(parseEvent(event));
		}

		root.setEvents(eventPojos);

		return root;
	}

	public static ApplicationInstancePojo parseApplicationInstance(ApiApplicationInstance instance) {

		ApplicationInstancePojo pojo = new ApplicationInstancePojo();

		pojo.setName(instance.getName());
		pojo.setAuthname(instance.getAuthname());
		pojo.setSecretKey(instance.getSecretKey());

		return pojo;
	}

	public static ErrorRoot initErrorPojo(String errorMessage) {
		ErrorRoot errorRoot = new ErrorRoot();
		errorRoot.setError(errorMessage);
		return errorRoot;
	}


}