view src/main/java/de/mpiwg/itgroup/ismi/browse/EntityRepositoryBean.java @ 169:0b5d02012299 public_by_author

more work on publicByAuthor feature.
author Robert Casties <casties@mpiwg-berlin.mpg.de>
date Thu, 31 May 2018 20:26:10 +0200
parents 29bd63f749c6
children 631864bfec2e
line wrap: on
line source

package de.mpiwg.itgroup.ismi.browse;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.mpi.openmind.repository.bo.Entity;
import org.mpi.openmind.repository.utils.CsvEntityListWriter;

import de.mpiwg.itgroup.ismi.entry.beans.SessionBean;
import de.mpiwg.itgroup.ismi.event.beans.CopyEvent;
import de.mpiwg.itgroup.ismi.event.beans.StudyEvent;
import de.mpiwg.itgroup.ismi.event.beans.TransferEvent;
import de.mpiwg.itgroup.ismi.util.guiComponents.DataPaginator;

public class EntityRepositoryBean extends AbstractEntityRepositoryBean {
    
	private static final long serialVersionUID = -2380877853539157567L;
	
	protected transient DataPaginator paginator = new DataPaginator();
	
	public EntityRepositoryBean(){
		super();
	}
	
	@Override
	public void reset(){
		super.reset();
		this.paginator = new DataPaginator();
	}


    protected void updateEntities() {
        if (StringUtils.isNotEmpty(getObjectClass())) {
            this.paginator.initCount();
            int startRecord = this.paginator.getCurrentPage() * this.paginator.getItemsPerPage();

            if (this.paginator.getNumberOfPages() == 0) {
                this.setEntities(new ArrayList<Entity>());
            } else {
                int mod = getWrapper().getEntitiesCount(getObjectClass()) % paginator.getItemsPerPage();
                if ((paginator.getCurrentPage() + 1) == paginator.getNumberOfPages() && mod != 0) {
                    this.setEntities(
                            getWrapper().getEntityByDefSubList(getObjectClass(), startRecord, startRecord + mod));
                } else {
                    this.setEntities(getWrapper().getEntityByDefSubList(getObjectClass(), startRecord,
                            startRecord + paginator.getItemsPerPage()));
                }
            }
        } else {
            this.setEntities(new ArrayList<Entity>());
        }
    }

	public String first() {
		this.paginator.first();
		this.updateEntities();
		return GOTO_ENTITY_REPOSITORY;
	}

	public String last() {
		this.paginator.last();
		this.updateEntities();
		return GOTO_ENTITY_REPOSITORY;
	}

	public String fastForward() {
		this.paginator.fastForward();
		this.updateEntities();
		return GOTO_ENTITY_REPOSITORY;
	}

	public String fastRewind() {
		this.paginator.fastRewind();
		this.updateEntities();
		return GOTO_ENTITY_REPOSITORY;
	}

	public String previous() {
		this.paginator.previous();
		this.updateEntities();
		return GOTO_ENTITY_REPOSITORY;
	}

	public String next() {
		this.paginator.next();
		this.updateEntities();
		return GOTO_ENTITY_REPOSITORY;
	}

	public void listenerGoToPage(ActionEvent event) {
		try {
			this.setPageMsg("");
			Integer intPage = new Integer(this.getPage());
			if (intPage != null) {
				intPage--;
				this.paginator.goToPage(intPage);
				this.updateEntities();
				
			}
		} catch (Exception e) {
			this.setPageMsg("page is invalid!");
		}
	}

	public String actionShowAll() {
		
		this.setResultMode(MODE_ALL);
		this.setResultSummaryMsg("");
		this.setPage("");
		this.paginator.setCurrentPage(0);
		int entitiesCount = getWrapper().getEntitiesCount(getObjectClass());
		this.paginator.resetNumberOfPages(entitiesCount);
		this.updateEntities();
		
		setResultSummaryMsg(entitiesCount + " items were found!");
		
		return GOTO_ENTITY_REPOSITORY;
	}
	
	
	public String details() {
		Entity selectedEntity = (Entity) getRequestBean("entity");
		EntityDetailsBean bean = (EntityDetailsBean) getRequestBean(SESSION_BEAN_ENTITY_DETAILS);
		bean.setEntity(selectedEntity);
		
		return GOTO_ENTITY_DETAILS;
	}

	public String actionEdit() {
		Entity entity = (Entity) getRequestBean("entity");
		getSessionBean().editEntity(entity);
		//return SessionBean.PAGE_ENTRY;
		
		if(entity.getObjectClass().equals(StudyEvent.OC) ||
				entity.getObjectClass().equals(CopyEvent.OC) ||
				entity.getObjectClass().equals(TransferEvent.OC)){
			return SessionBean.PAGE_EVENT_FORM;			
		}else{
			return SessionBean.PAGE_ENTRY;
		}
	}
	
	/**
	 * Downloads all Entities of the selected class as CSV.
	 * 
     * To be used for resultMode == all.
     * 
	 * @throws IOException
	 */
	public void downloadAllCsv() throws IOException {
	    List<Entity> allEnts = getWrapper().getEntitiesByDef(getObjectClass());
	    sendAsCsv(allEnts);
	}
	
	/**
	 * Downloads all currently selected Entities as CSV.
	 * 
	 * To be used for resultMode == advanced.
	 * 
	 * @throws IOException
	 */
	public void downloadAdvancedCsv() throws IOException {
	    sendAsCsv(getEntities());
	}
	
	/**
     * Send the given Entities as CSV file to the client.
     *  
     * @throws IOException 
     */
    public void sendAsCsv(List<Entity> entList) throws IOException {
        // Get the FacesContext
        FacesContext facesContext = FacesContext.getCurrentInstance();
         
        // Get HTTP response
        HttpServletResponse response = (HttpServletResponse) facesContext.getExternalContext().getResponse();
         
        // Set response headers
        response.reset();   // Reset the response in the first place
        response.setCharacterEncoding("UTF-8");
        response.setHeader("Content-Type", "application/csv");  // Set the content type
        response.setHeader("Content-Disposition", "attachment; filename=entities.csv");        
         
        // Open response output Writer
        PrintWriter responseWriter = response.getWriter();
         
        // make sure all entities attributes are loaded
        for (Entity entity : entList) {
            if (entity.isLightweight()) {
                getWrapper().getEntityContent(entity);
            }
        }
        
        // write all entities to the response 
        CsvEntityListWriter.writeEntities(entList, responseWriter);
         
        // Make sure that everything is out
        responseWriter.flush();
        responseWriter.close();
         
        // JSF doc: 
        // Signal the JavaServer Faces implementation that the HTTP response for this request has already been generated 
        // (such as an HTTP redirect), and that the request processing lifecycle should be terminated
        // as soon as the current phase is completed.
        facesContext.responseComplete();      
    }   

	public DataPaginator getPaginator() {
		return paginator;
	}

	public void setPaginator(DataPaginator paginator) {
		this.paginator = paginator;
	}
}