changeset 103:03dcbaf8b8cf

add download-as-csv button to browse repository.
author casties
date Thu, 03 Nov 2016 20:10:58 +0100
parents db3e1bbfde03
children 42d0169904f3
files src/main/java/de/mpiwg/itgroup/ismi/browse/AbstractEntityRepositoryBean.java src/main/java/de/mpiwg/itgroup/ismi/browse/EntityRepositoryBean.java src/main/webapp/browse/components/advancedResult.xhtml src/main/webapp/browse/components/showAllResult.xhtml
diffstat 4 files changed, 141 insertions(+), 58 deletions(-) [+]
line wrap: on
line diff
--- a/src/main/java/de/mpiwg/itgroup/ismi/browse/AbstractEntityRepositoryBean.java	Tue Nov 01 19:45:16 2016 +0100
+++ b/src/main/java/de/mpiwg/itgroup/ismi/browse/AbstractEntityRepositoryBean.java	Thu Nov 03 20:10:58 2016 +0100
@@ -83,26 +83,31 @@
 		this.updateDefinitions(getWrapper().getLWDefinitions());		
 	}
 	
-	private void updateAdvancedEntities() {
-		if (StringUtils.isNotEmpty(getObjectClass())) {
-			this.advancedPaginator.initCount();
-			int startRecord = this.advancedPaginator.getCurrentPage()
-					* this.advancedPaginator.getItemsPerPage();
-			if((this.advancedPaginator.getCurrentPage() + 1) == this.advancedPaginator.getNumberOfPages()){
-				int mod = this.entities.size() % advancedPaginator.getItemsPerPage();
-				if(mod == 0){
-					this.currentEntities = entities.subList(startRecord, startRecord + this.advancedPaginator.getItemsPerPage());
-				}else{
-					this.currentEntities = entities.subList(startRecord, startRecord + mod);	
-				}
-				
-			}else{
-				this.currentEntities = entities.subList(startRecord, startRecord + this.advancedPaginator.getItemsPerPage());	
-			}
-			
-		} else
-			this.currentEntities = new ArrayList<Entity>();
-	}
+    /**
+     * Put the current subset of this.entites in this.currentEntities.
+     */
+    private void updateAdvancedEntities() {
+        if (StringUtils.isNotEmpty(getObjectClass())) {
+            this.advancedPaginator.initCount();
+            int startRecord = this.advancedPaginator.getCurrentPage() * this.advancedPaginator.getItemsPerPage();
+            if ((this.advancedPaginator.getCurrentPage() + 1) == this.advancedPaginator.getNumberOfPages()) {
+                int mod = this.entities.size() % advancedPaginator.getItemsPerPage();
+                if (mod == 0) {
+                    this.currentEntities = entities.subList(startRecord,
+                            startRecord + this.advancedPaginator.getItemsPerPage());
+                } else {
+                    this.currentEntities = entities.subList(startRecord, startRecord + mod);
+                }
+
+            } else {
+                this.currentEntities = entities.subList(startRecord,
+                        startRecord + this.advancedPaginator.getItemsPerPage());
+            }
+        } else {
+            // empty object_class
+            this.currentEntities = new ArrayList<Entity>();
+        }
+    }
 	
 	public String advancedFirst() {
 		this.advancedPaginator.first();
--- a/src/main/java/de/mpiwg/itgroup/ismi/browse/EntityRepositoryBean.java	Tue Nov 01 19:45:16 2016 +0100
+++ b/src/main/java/de/mpiwg/itgroup/ismi/browse/EntityRepositoryBean.java	Thu Nov 03 20:10:58 2016 +0100
@@ -1,11 +1,17 @@
 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;
@@ -14,6 +20,7 @@
 import de.mpiwg.itgroup.ismi.util.guiComponents.DataPaginator;
 
 public class EntityRepositoryBean extends AbstractEntityRepositoryBean {
+    
 	private static final long serialVersionUID = -2380877853539157567L;
 	
 	private transient DataPaginator paginator = new DataPaginator();
@@ -29,30 +36,27 @@
 	}
 
 
-	private 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>());
-		}
-	}
+    private 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();
@@ -143,6 +147,71 @@
 			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;
--- a/src/main/webapp/browse/components/advancedResult.xhtml	Tue Nov 01 19:45:16 2016 +0100
+++ b/src/main/webapp/browse/components/advancedResult.xhtml	Thu Nov 03 20:10:58 2016 +0100
@@ -15,7 +15,7 @@
     <h:column 
     	rendered="#{!empty EntityRepository.entities}">
         <h:outputText value="go to page" styleClass="sumaryMsg"/>
-        <h:inputText value="#{EntityRepository.page}" />
+        <h:inputText value="#{EntityRepository.page}" size="4"/>
         <h:commandButton value="submit"
             actionListener="#{EntityRepository.actionGoToPageAdvancedResult}" />
         <h:outputText value=" #{EntityRepository.pageMsg}"
@@ -25,29 +25,29 @@
     
     <h:column rendered="#{!empty EntityRepository.entities}">
         <h:panelGroup>
-            <h:commandButton image="/resources/css/xp/css-images//arrow-first.gif"
+            <h:commandButton image="/resources/css/xp/css-images/arrow-first.gif"
             style="border:none;" title="First Page" 
             action="#{EntityRepository.advancedFirst}"/> 
             
-            <h:commandButton image="/resources/css/xp/css-images//arrow-fr.gif"
+            <h:commandButton image="/resources/css/xp/css-images/arrow-fr.gif"
             style="border:none;" title="Fast Backwards" 
             action="#{EntityRepository.advancedFastRewind}"/> 
             
-            <h:commandButton image="/resources/css/xp/css-images//arrow-previous.gif"
+            <h:commandButton image="/resources/css/xp/css-images/arrow-previous.gif"
             style="border:none;" title="Previous Page" 
             action="#{EntityRepository.advancedPrevious}"/>     
             
             <h:outputText value="#{EntityRepository.advancedPaginator.recordStatus}" styleClass="sumaryMsg" /> 
             
-            <h:commandButton image="/resources/css/xp/css-images//arrow-next.gif"
+            <h:commandButton image="/resources/css/xp/css-images/arrow-next.gif"
             style="border:none;" title="Next Page" 
             action="#{EntityRepository.advancedNext}"/>
             
-            <h:commandButton image="/resources/css/xp/css-images//arrow-ff.gif"
+            <h:commandButton image="/resources/css/xp/css-images/arrow-ff.gif"
             style="border:none;" title="Fast Forward" 
             action="#{EntityRepository.advancedFastForward}"/>      
             
-            <h:commandButton image="/resources/css/xp/css-images//arrow-last.gif"
+            <h:commandButton image="/resources/css/xp/css-images/arrow-last.gif"
             style="border:none;" title="Last Page" 
             action="#{EntityRepository.advancedLast}"/>      
         </h:panelGroup>
@@ -72,34 +72,39 @@
     
     <h:column rendered="#{!empty EntityRepository.entities}">
         <h:panelGroup>
-            <h:commandButton image="/resources/css/xp/css-images//arrow-first.gif"
+            <h:commandButton image="/resources/css/xp/css-images/arrow-first.gif"
             style="border:none;" title="First Page" 
             action="#{EntityRepository.advancedFirst}"/> 
             
-            <h:commandButton image="/resources/css/xp/css-images//arrow-fr.gif"
+            <h:commandButton image="/resources/css/xp/css-images/arrow-fr.gif"
             style="border:none;" title="Fast Backwards" 
             action="#{EntityRepository.advancedFastRewind}"/> 
             
-            <h:commandButton image="/resources/css/xp/css-images//arrow-previous.gif"
+            <h:commandButton image="/resources/css/xp/css-images/arrow-previous.gif"
             style="border:none;" title="Previous Page" 
             action="#{EntityRepository.advancedPrevious}"/>     
             
             <h:outputText value="#{EntityRepository.advancedPaginator.recordStatus}" styleClass="sumaryMsg" />
             
-            <h:commandButton image="/resources/css/xp/css-images//arrow-next.gif"
+            <h:commandButton image="/resources/css/xp/css-images/arrow-next.gif"
             style="border:none;" title="Next Page" 
             action="#{EntityRepository.advancedNext}"/>
             
-            <h:commandButton image="/resources/css/xp/css-images//arrow-ff.gif"
+            <h:commandButton image="/resources/css/xp/css-images/arrow-ff.gif"
             style="border:none;" title="Fast Forward" 
             action="#{EntityRepository.advancedFastForward}"/>      
             
-            <h:commandButton image="/resources/css/xp/css-images//arrow-last.gif"
+            <h:commandButton image="/resources/css/xp/css-images/arrow-last.gif"
             style="border:none;" title="Last Page" 
             action="#{EntityRepository.advancedLast}"/>      
         </h:panelGroup>
     </h:column>      
 	
+    <h:column rendered="#{!empty EntityRepository.entities}">
+        <h:commandButton value="download all as CSV"
+            actionListener="#{EntityRepository.downloadAdvancedCsv}" />
+    </h:column>
+
 	</ui:composition>
 </body>
 </html>	
\ No newline at end of file
--- a/src/main/webapp/browse/components/showAllResult.xhtml	Tue Nov 01 19:45:16 2016 +0100
+++ b/src/main/webapp/browse/components/showAllResult.xhtml	Thu Nov 03 20:10:58 2016 +0100
@@ -14,7 +14,7 @@
         
     <h:column rendered="#{!empty EntityRepository.entities}">
         <h:outputText value="go to page" styleClass="sumaryMsg"/>
-        <h:inputText value="#{EntityRepository.page}" />
+        <h:inputText value="#{EntityRepository.page}" size="4"/>
         <h:commandButton value="submit"
             actionListener="#{EntityRepository.listenerGoToPage}" />
         <h:outputText value=" #{EntityRepository.pageMsg}"
@@ -83,6 +83,10 @@
         </h:panelGroup>
     </h:column>
 
+    <h:column rendered="#{!empty EntityRepository.entities}">
+        <h:commandButton value="download all as CSV"
+            actionListener="#{EntityRepository.downloadAllCsv}" />
+    </h:column>
 	
 	</ui:composition>
 </body>