Seam - Database Image to Screen Output


2008-02-01 Digg! icurtain Delcious icurtain Technorati icurtain


front end

a4j:mediaOutput id = "telephone" element = "img" mimeTye = "image/png"
createContent = "#{imagePainter.drawImage}"
value="#{object.instance.objectId}"

in this code the object id is handed to the drawImage funtion - this function then looks up the record and populates et which has a byteArray stored on it - it then writes this out to the screen using a file output writer

back end

@Name("imagePainter")

public class ImagePainter {


@In("midasEntityManager")
private EntityManager em;


public void drawImage (OutputStream output, Object emblemTemplateId) throws IOException{


Object et = em.find(Object.class, objectId);

ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(et.byteArrayData());

FileUtilities.write(byteArrayInputStream, output, 1024);

}

/* private List getEmblemTypeById(String queryString, String emblemTypeId) { List reportSet = new ArrayList(); Query emblemQuery = em.createNamedQuery(queryString); emblemQuery.setParameter("emblemTypeId", emblemTypeId); reportSet = (List)emblemQuery.getResultList(); return reportSet; }*/

}

public static void write(InputStream input, OutputStream output, int bufferSize) throws IOException {
byte[] buffer = new byte[bufferSize] ;
int readSize = -1 ;
while ( (readSize = input.read(buffer)) != -1) {
output.write(buffer, 0, readSize);
output.flush();
}
try {
input.close();
} finally {
output.close();
}
}