All Downloads are FREE. Search and download functionalities are using the official Maven repository.

eu.eurekabpo.primefaces.model.DefaultPremiumScheduleModel Maven / Gradle / Ivy

Go to download

Eureka-BPO PremiumSchedule - Schedule Component for Primefaces with premium FullCalendar features: Vertical Resource View and Timeline View

The newest version!
package eu.eurekabpo.primefaces.model;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;

import org.primefaces.model.DefaultScheduleModel;
import org.primefaces.model.ScheduleEvent;

public class DefaultPremiumScheduleModel extends DefaultScheduleModel implements PremiumScheduleModel {
	private static final long serialVersionUID = -7164411821077003614L;

	private Map>> events;
	private boolean eventLimit = false;
	
	public DefaultPremiumScheduleModel() {
		events = new HashMap<>();
	}
	
	public DefaultPremiumScheduleModel(Map>> events) {
		this.events = events;
	}
	
	@Override
	public Set getKeys() {
		return events.keySet();
	}
	
	@Override
	public void addEvent(Object key, ScheduleEvent event) {
		event.setId(UUID.randomUUID().toString());
		List> lEvents = events.get(key);
		if (lEvents == null) {
			lEvents = events.put(key, new ArrayList>());
		}
		events.get(key).add(event);
	}

	@Override
	public boolean deleteEvent(ScheduleEvent event) {
		boolean result = false;
		for (List> lEvents : events.values()) {
			result = result | lEvents.remove(event);
		}
		return result;
	}
	
	@Override
	public List> getEvents() {
		List> result = new ArrayList>();
		for (List> lEvents : events.values()) {
			result.addAll(lEvents);
		}
		return result;
	}
	
	@Override
	public List> getEvents(Object key) {
		return events.get(key);
	}

	@Override
	public ScheduleEvent getEvent(String id) {
		for (List> lEvents : events.values()) {
			for (ScheduleEvent event : lEvents) {
				if (event.getId() != null && event.getId().equals(id)) {
					return event;
				}
			}
		}
		return null;
	}
	
	@Override
	public void updateEvent(ScheduleEvent event) {
		Object key = null;
		int index = -1;

		outer: for (Map.Entry>> entry : events.entrySet()) {
			for (int i = 0; i < entry.getValue().size(); i++) {
				if (entry.getValue() .get(i).getId().equals(event.getId())) {
					key = entry.getKey();
					index = i;

					break outer;
				}
			}
		}

		if (key != null && index >= 0) {
			events.get(key).set(index, event);
		}
	}
	
	@Override
	public int getEventCount() {
		int count = 0;
		for (List> lEvents : events.values()) {
			count += lEvents.size();
		}
		return count;
	}
	
	@Override
	public int getEventCount(Object key) {
		return events.get(key).size();
	}

	@Override
	public void clear() {
		events = new HashMap<>();
	}
	
	@Override
	public void clear(Object key) {
		events.put(key, new ArrayList<>());
	}

	@Override
	public boolean isEventLimit() {
		return eventLimit;
	}

	public void setEventLimit(boolean eventLimit) {
		this.eventLimit = eventLimit;
	}
}