* To change this template, choose Tools | Templates * and open the template in the editor. */
package com.perficient.rms.bean.managedbean;
import com.icesoft.faces.component.ext.RowSelectorEvent; import com.icesoft.faces.context.ByteArrayResource; import com.icesoft.faces.context.Resource;
import com.perficient.rms.domain.ContractTemplate; import com.perficient.rms.service.ContractTemplateService; import com.perficient.rms.service.impl.ServiceFactory; import com.perficient.rms.util.DateConverter; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.Serializable; import java.util.Date; import java.util.List; import java.util.TimeZone;
import javax.faces.context.ExternalContext; import javax.faces.context.FacesContext; import javax.faces.convert.DateTimeConverter; import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel; /** *
* @author paul.fang */
public class ContractTemplateDownloadBean extends PortletSessionManager {
private String[] columnNames = {\"Template\\"Comment\
private ContractTemplateService templateService;
private DateTimeConverter converter;
private String attribute_row;
private String attribute_name;
private String download_link;
private Resource download_link_resource;
private String fileName;
public String getFileName() {
return fileName; }
public void setFileName(String fileName) { this.fileName = fileName; }
public String getDownload_link() { return download_link; }
public void setDownload_link(String download_link) { this.download_link = download_link; }
public Resource getDownload_link_resource() { return download_link_resource; }
public void setDownload_link_resource(Resource download_link_resource) { this.download_link_resource = download_link_resource; }
public ContractTemplateDownloadBean() throws IOException { templateService = ServiceFactory.getContractTemplateService();
converter = new DateConverter(); converter.setPattern(\"yyyy-MM-dd\");
converter.setTimeZone(TimeZone.getDefault()); }
public String[] getColumnNames() { return columnNames; }
public DataModel getDataModel() { List templateList;
String template_name = (String) this.getApplicationAttribute(attribute_name); if (template_name == null) {
templateList = templateService.getAllTemplate(); } else {
templateList = templateService.getTemplateByName(template_name); }
return new ListDataModel(templateList); }
public void rowSelection(RowSelectorEvent selectorEvent) throws IOException { int rowIndex = selectorEvent.getRow(); DataModel dataModel = getDataModel(); dataModel.setRowIndex(rowIndex);
ContractTemplate template = (ContractTemplate) dataModel.getRowData(); download_link=template.getTemplatePath(); fileName=template.getTemplateName()+\".doc\"; FacesContext fc=FacesContext.getCurrentInstance(); ExternalContext ec=fc.getExternalContext(); if(download_link==null) {
System.out.print(\"No data\"); } else {
download_link_resource=new MyResource(ec, fileName, download_link); }
this.setApplicationAttribute(attribute_row, template); this.renderSessionGroup(); }
public DateTimeConverter getConverter() { return converter; }
public void setAttribute_name(String attribute_name) { this.attribute_name = attribute_name; }
public void setAttribute_row(String attribute_row) { this.attribute_row = attribute_row; }
public String getAttribute_name() { return attribute_name; }
public String getAttribute_row() { return attribute_row; }
class MyResource implements Resource, Serializable{ private String resourceName; private InputStream inputStream; private final Date lastModified; private ExternalContext extContext; private String path;
public MyResource(ExternalContext ec, String resourceName, String path) { this.extContext = ec;
this.resourceName = resourceName; this.lastModified = new Date(); this.path = path;
}
public InputStream open() throws IOException { if (inputStream == null) {
InputStream stream = extContext.getResourceAsStream(path); byte[] byteArray = toByteArray(stream);
inputStream = new ByteArrayInputStream(byteArray); }
return inputStream; }
public byte[] toByteArray(InputStream input) {
ByteArrayOutputStream output=new ByteArrayOutputStream(); byte[]buf=new byte[4096]; int len=0; try {
while((len=input.read(buf))>-1) output.write(buf,0,len); }
catch(Exception e) { }
return output.toByteArray(); }
public String calculateDigest() { return resourceName; }
public Date lastModified() { return lastModified; }
public void withOptions(Options arg0) throws IOException { } } }
因篇幅问题不能全部显示,请点此查看更多更全内容