ActionLogMessageView.java 2.54 KB
package fi.codecrew.moya.web.cdiview.actionlog;

import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.List;

import javax.ejb.EJB;
import javax.enterprise.context.ConversationScoped;
import javax.enterprise.context.RequestScoped;
import javax.inject.Named;

import org.primefaces.event.SelectEvent;
import org.primefaces.model.tagcloud.DefaultTagCloudItem;
import org.primefaces.model.tagcloud.DefaultTagCloudModel;
import org.primefaces.model.tagcloud.TagCloudItem;
import org.primefaces.model.tagcloud.TagCloudModel;

import fi.codecrew.moya.beans.ActionLogBeanLocal;
import fi.codecrew.moya.enums.apps.ContentPermission;
import fi.codecrew.moya.model.ActionLogMessage;
import fi.codecrew.moya.model.ActionLogMessageTag;
import fi.codecrew.moya.web.cdiview.GenericCDIView;

@Named
@ConversationScoped
public class ActionLogMessageView extends GenericCDIView {
	private static final long serialVersionUID = 1L;

	private boolean updateEnabled = true;
	private TagCloudModel tagCloud = null;
	
	private List<ActionLogMessageTag> activeTags = null;

	@EJB
	private transient ActionLogBeanLocal actionLogBean;

	public void initView() {
		if(super.requirePermissions(ContentPermission.MANAGE_ACTIONLOG)) {
			this.beginConversation();
			
			refreshTagCloud();
			
			if(activeTags == null) {						
				activeTags = new ArrayList<>();
			}
		}
	}
	
	public void refreshTagCloud() {
		tagCloud = new DefaultTagCloudModel();
		
		for(ActionLogMessageTag almt : actionLogBean.getAllTags()) {
			tagCloud.addTag(new DefaultTagCloudItem(almt.getTag(), 1));
		}
	}
	
	public boolean getUpdateEnabled() {
		return updateEnabled;
	}

	public void setUpdateEnabled(boolean updateEnabled) {
		this.updateEnabled = updateEnabled;
	}

	public List<ActionLogMessage> getMessages() {
		return actionLogBean.getAllActionLogEventsByFilter(activeTags);
	}
	
	public TagCloudModel getTagCloud() {
		return tagCloud;
	}
	
	public void onTagSelect(SelectEvent event) {  
        TagCloudItem item = (TagCloudItem)event.getObject();
        
        ActionLogMessageTag almt = actionLogBean.getActionLogMessageTagByString(item.getLabel());
        if(!activeTags.contains(almt))
        	activeTags.add(almt); 
         
    } 
	
	public void selectFilterTag(String tag) {
		for(ActionLogMessageTag almt : this.activeTags) {
			if(tag.equals(almt.getTag())) {
				this.activeTags.remove(almt);
				break;
			}
		}
	}
	
	public List<ActionLogMessageTag> getActiveTags() {
		return this.activeTags;
	}
}