package com.aperture_software.glados_wiki.support;
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.springframework.web.servlet.mvc.support.RedirectAttributesModelMap;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
/**
* Created by jhyun on 13. 12. 20.
*/
@Component
public class FlashAlerts {
public static final String FLASHMAP_KEY = "FlashAlerts";
public static class FlashAlert implements Cloneable, Serializable {
private static final long serialVersionUID = 2838212699849255844L;
private Object type;
private Object mesg;
public FlashAlert(Object type, Object mesg) {
this.type = type;
this.mesg = mesg;
}
public Object getType() {
return type;
}
public void setType(Object type) {
this.type = type;
}
public Object getMesg() {
return mesg;
}
public void setMesg(Object mesg) {
this.mesg = mesg;
}
@Override
public int hashCode() {
return HashCodeBuilder.reflectionHashCode(this);
}
@Override
public boolean equals(Object obj) {
return EqualsBuilder.reflectionEquals(this, obj);
}
@Override
public Object clone() throws CloneNotSupportedException {
return super.clone();
}
@Override
public String toString() {
return ToStringBuilder.reflectionToString(this);
}
}
public void add(RedirectAttributes redirectAttributes, FlashAlert flashAlert) {
if (!redirectAttributes.containsAttribute(FLASHMAP_KEY)) {
redirectAttributes.addFlashAttribute(FLASHMAP_KEY, new ArrayList<FlashAlert>());
}
//
List<FlashAlert> flashAlerts = (List<FlashAlert>) redirectAttributes.getFlashAttributes().get(FLASHMAP_KEY);
flashAlerts.add(flashAlert);
redirectAttributes.addFlashAttribute(FLASHMAP_KEY, flashAlerts);
}
}