catch up with branch development

Signed-off-by: Ralf Mollik <ramollik@compex-commerce.com>
diff --git a/org.eclipse.osbp.blob/src/org/eclipse/osbp/blob/service/BlobService.java b/org.eclipse.osbp.blob/src/org/eclipse/osbp/blob/service/BlobService.java
index 23df1f8..01fd2fa 100644
--- a/org.eclipse.osbp.blob/src/org/eclipse/osbp/blob/service/BlobService.java
+++ b/org.eclipse.osbp.blob/src/org/eclipse/osbp/blob/service/BlobService.java
@@ -47,9 +47,9 @@
 import com.vaadin.ui.Label;
 
 /**
- * Provides and persist blob data from and into a database via JPA.
- * For images different sizes are calculated and stored that can be retrieved
- * later to get better performance
+ * Provides and persist blob data from and into a database via JPA. For images
+ * different sizes are calculated and stored that can be retrieved later to get
+ * better performance
  * 
  * @author dominguez
  * 
@@ -68,6 +68,7 @@
 
 	private static final String DEFAULTBLOBFILENAME = "osbee.png";
 	private static final String DEFAULTBLOBMIMETYPE = "image/png";
+
 	/**
 	 * Inner class that extract the width and height values from the specified
 	 * resolution string and provides them.
@@ -100,7 +101,8 @@
 
 	public BlobService() {
 		if (DtoServiceAccess.getService(BlobMappingDto.class) instanceof IDTOServiceWithMutablePersistence<?>) {
-			dtoBlobMappingDtoService = (IDTOServiceWithMutablePersistence<BlobMappingDto>) DtoServiceAccess.getService(BlobMappingDto.class);
+			dtoBlobMappingDtoService = (IDTOServiceWithMutablePersistence<BlobMappingDto>) DtoServiceAccess
+					.getService(BlobMappingDto.class);
 		}
 		this.blobAPI = new BlobTypingAPI();
 	}
@@ -116,7 +118,7 @@
 	}
 
 	protected synchronized void notifyBlobUploadEvent(IBlobEvent event) {
-		for (IBlobUploadEventListener listener : listeners){
+		for (IBlobUploadEventListener listener : listeners) {
 			listener.blobUploaded(event);
 		}
 	}
@@ -142,16 +144,24 @@
 		return DatatypeConverter.parseBase64Binary(new String(data));
 	}
 
-	/* (non-Javadoc)
-	 * @see org.eclipse.osbp.ui.api.customfields.IBlobService#createBlobMappingBlobs(java.io.InputStream, int)
+	/*
+	 * (non-Javadoc)
+	 * 
+	 * @see
+	 * org.eclipse.osbp.ui.api.customfields.IBlobService#createBlobMappingBlobs(
+	 * java.io.InputStream, int)
 	 */
 	@Override
 	public List<Object> createBlobMappingBlobs(InputStream stream, int mimeType) throws IOException {
 		return createBlobMappingBlobs(stream, blobAPI.getMimeTypeContentTypeById(mimeType));
 	}
-	
-	/* (non-Javadoc)
-	 * @see org.eclipse.osbp.ui.api.customfields.IBlobService#createBlobMappingBlobs(java.io.InputStream, java.lang.String)
+
+	/*
+	 * (non-Javadoc)
+	 * 
+	 * @see
+	 * org.eclipse.osbp.ui.api.customfields.IBlobService#createBlobMappingBlobs(
+	 * java.io.InputStream, java.lang.String)
 	 */
 	@Override
 	public List<Object> createBlobMappingBlobs(InputStream stream, String mimeType) throws IOException {
@@ -160,17 +170,19 @@
 		// In case of a image blob
 		if (isImage(mimeType)) {
 			blobList = createImages(bytes, blobAPI, mimeType);
-		}
-		else {
+		} else {
 			// Otherwise
 			blobList = createBlobList(bytes);
 		}
 		return blobList;
 	}
 
-	
-	/* (non-Javadoc)
-	 * @see org.eclipse.osbp.ui.api.customfields.IBlobService#createBlobMapping(java.io.InputStream, java.lang.String, java.lang.String)
+	/*
+	 * (non-Javadoc)
+	 * 
+	 * @see
+	 * org.eclipse.osbp.ui.api.customfields.IBlobService#createBlobMapping(java.
+	 * io.InputStream, java.lang.String, java.lang.String)
 	 */
 	@Override
 	public String createBlobMapping(InputStream stream, String fileName, String mimeType) {
@@ -179,8 +191,8 @@
 		blobMapping.setMimeTypeId(blobAPI.getMimeTypeContentTypeId(mimeType));
 		try {
 			List<Object> blobList = createBlobMappingBlobs(stream, mimeType);
-			for(Object obj:blobList) {
-				blobMapping.addToBlobsRef((BlobDto)obj);
+			for (Object obj : blobList) {
+				blobMapping.addToBlobsRef((BlobDto) obj);
 			}
 			dtoBlobMappingDtoService.update(blobMapping);
 			IBlobEvent event = new BlobEvent(this, true, blobMapping.getId(), "", fileName);
@@ -192,7 +204,7 @@
 		}
 	}
 
-/**
+	/**
 	 * Creates for the specified image blob data by {@code byte[]} the base64
 	 * encoded image representation and a resized copy for each of the
 	 * predefined resolutions and send them back as a blob list ({@code List<
@@ -205,22 +217,14 @@
 	 * @throws IOException
 	 * 
 	 */
-	private List<Object> createImages(byte[] bytes, BlobTypingAPI blobAPI,
-			String mimeType) throws IOException {
+	private List<Object> createImages(byte[] bytes, BlobTypingAPI blobAPI, String mimeType) throws IOException {
 		List<Object> blobList = new ArrayList<>();
 		for (int i = 0; i < blobAPI.getNormalizer().size(); i++) {
-			ImageBlobResolution imgBlobRes = new ImageBlobResolution(
-					blobAPI.getNormalizerResolutionByListIndex(i));
-			if (imgBlobRes.height == 0) {
-				blobList.add(createImageBlob(bytes,
-						blobAPI.getNormalizerResolutionIdByListIndex(i)));
-			} else {
-				BufferedImage img = resizeImage(bytes, imgBlobRes.width,
-						imgBlobRes.height);
-				byte[] imageBytes = imageToByteArray(img, mimeType);
-				blobList.add(createImageBlob(imageBytes,
-						blobAPI.getNormalizerResolutionIdByListIndex(i)));
-			}
+			ImageBlobResolution imgBlobRes = new ImageBlobResolution(blobAPI.getNormalizerResolutionByListIndex(i));
+			// In case of resolution '0' no scaling will be done in 'getImage'
+			BufferedImage img = getImage(bytes, imgBlobRes.width, imgBlobRes.height);
+			byte[] imageBytes = imageToByteArray(img, mimeType);
+			blobList.add(createImageBlob(imageBytes, blobAPI.getNormalizerResolutionIdByListIndex(i)));
 		}
 		return blobList;
 	}
@@ -233,8 +237,7 @@
 	 * @return a converted {@link BufferedImage} to a byte array.
 	 * @throws IOException
 	 */
-	private byte[] imageToByteArray(BufferedImage image, String mimeType)
-			throws IOException {
+	private byte[] imageToByteArray(BufferedImage image, String mimeType) throws IOException {
 		ByteArrayOutputStream bos = new ByteArrayOutputStream();
 		String type = getFormatNameFromMimeType(mimeType);
 		ImageIO.write(image, type, bos);
@@ -242,8 +245,8 @@
 		return bos.toByteArray();
 	}
 
-	/**1
-	 * Extracts the format name from the mime type definition.
+	/**
+	 * 1 Extracts the format name from the mime type definition.
 	 * 
 	 * @param mimeType
 	 * @return format name of the mime type definition
@@ -254,7 +257,7 @@
 	}
 
 	/**
-	 * Resizes an image into the specified new width and height and convert them
+	 * Resizes an image into the specified new width and height if they are not '0' and convert them
 	 * from the incoming byte array to a {@link BufferedImage}.
 	 * 
 	 * @param bytes
@@ -264,15 +267,22 @@
 	 *         new width and height
 	 * @throws IOException
 	 */
-	private BufferedImage resizeImage(byte[] bytes, int newWidth, int newHeight)
-			throws IOException {
+	private BufferedImage getImage(byte[] bytes, int newWidth, int newHeight) throws IOException {
 		ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
 		Image image = ImageIO.read(bis);
 		bis.close();
-		image = image.getScaledInstance(newWidth, newHeight,
-				Image.SCALE_AREA_AVERAGING);
-		BufferedImage buffered = new BufferedImage(image.getWidth(null),
-				image.getHeight(null), BufferedImage.TYPE_INT_RGB);
+		/*
+		 * On resolution '0' the newWidth and newHeight is also '0', because no
+		 * scaling is required. But on png files its displaying behaviour on a
+		 * BIRT report differs from the behaviour on a dialog view. Creating a
+		 * BufferedImage with defined width and height avoids this different
+		 * displaying behaviour.
+		 */
+		if (newWidth > 0 && newHeight > 0) {
+			image = image.getScaledInstance(newWidth, newHeight, Image.SCALE_AREA_AVERAGING);
+		}
+		BufferedImage buffered = new BufferedImage(image.getWidth(null), image.getHeight(null),
+				BufferedImage.TYPE_INT_RGB);
 		buffered.getGraphics().drawImage(image, 0, 0, null);
 		return buffered;
 	}
@@ -319,7 +329,7 @@
 	 *         {@code resolutionId} or {@code null} if no blob found
 	 */
 	private BlobDto getBlobById(String blobMappingId, int resolutionId) {
-		if(blobMappingId != null && resolutionId >= 0) {
+		if (blobMappingId != null && resolutionId >= 0) {
 			BlobMappingDto blobMapping = dtoBlobMappingDtoService.get(blobMappingId);
 			if (blobMapping != null) {
 				return getBlobByResolution(resolutionId, blobMapping);
@@ -330,7 +340,7 @@
 
 	private BlobDto getBlobByResolution(int resolutionId, BlobMappingDto blobMapping) {
 		for (BlobDto blob : blobMapping.getBlobsRef()) {
-			if (isImage(blobMapping.getMimeTypeId())){
+			if (isImage(blobMapping.getMimeTypeId())) {
 				if (blob.getResolutionId() == resolutionId) {
 					return blob;
 				}
@@ -375,7 +385,7 @@
 	 */
 	@Override
 	public boolean isWord(int mimeTypeId) {
-		return mimeTypeId == IBlobTyping.WORD_DOC_MIME_TYPE_ID||mimeTypeId == IBlobTyping.WORD_DOCX_MIME_TYPE_ID;
+		return mimeTypeId == IBlobTyping.WORD_DOC_MIME_TYPE_ID || mimeTypeId == IBlobTyping.WORD_DOCX_MIME_TYPE_ID;
 	}
 
 	/**
@@ -387,7 +397,7 @@
 	 */
 	@Override
 	public boolean isExcel(int mimeTypeId) {
-		return mimeTypeId == IBlobTyping.EXCEL_XLS_MIME_TYPE_ID||mimeTypeId == IBlobTyping.EXCEL_XLSX_MIME_TYPE_ID;
+		return mimeTypeId == IBlobTyping.EXCEL_XLS_MIME_TYPE_ID || mimeTypeId == IBlobTyping.EXCEL_XLSX_MIME_TYPE_ID;
 	}
 
 	/**
@@ -400,6 +410,7 @@
 	public boolean isImage(String mimeType) {
 		return mimeType.startsWith(IBlobTyping.IMAGE_MIME_TYPE_PREFIX);
 	}
+
 	/**
 	 * Provides the base 64 encoded string representation of a specific blob in
 	 * a specific resolution.
@@ -413,7 +424,7 @@
 
 	private String getBase64ValueFromBlob(BlobDto blob) {
 		assert blob != null : "blobDto must not be null";
-		
+
 		if (isImage(blob.getBlobMapping().getMimeTypeId())) {
 			return getImageHTMLWithBase64EndodedData(blob.getData(), "",
 					blobAPI.getMimeTypeContentTypeById(blob.getBlobMapping().getMimeTypeId()));
@@ -430,8 +441,7 @@
 					blob.getBlobMapping().getFileName(),
 					blobAPI.getMimeTypeContentTypeById(blob.getBlobMapping().getMimeTypeId()));
 		} else {
-			return  getImageHTMLWithBase64EndodedData(getDefaultImageBase64String(),
-					blob.getBlobMapping().getFileName(),
+			return getImageHTMLWithBase64EndodedData(getDefaultImageBase64String(), blob.getBlobMapping().getFileName(),
 					blobAPI.getMimeTypeContentTypeById(blob.getBlobMapping().getMimeTypeId()));
 		}
 	}
@@ -479,7 +489,7 @@
 	private String getImageHTMLWithBase64EndodedData(String base64Str, String fileName, String mimeType) {
 		return "data:" + mimeType + ";base64," + base64Str;
 	}
-	
+
 	/**
 	 * Provides the base64 encoded string representation of the default
 	 * placeholder image (Compex-Logo)
@@ -527,8 +537,8 @@
 	@Override
 	public String getImage(String uuid, int resolutionId) {
 		BlobDto blob = getBlobById(uuid, resolutionId);
-		if(blob != null) {
-			return "<img src=\""+ getBase64ValueFromBlob(getBlobById(uuid, resolutionId)) + "\" alt=\"Encoded image in Base64\">";
+		if (blob != null) {
+			return "<img src=\"" + getBase64ValueFromBlob(blob) + "\" alt=\"Encoded image in Base64\">";
 		}
 		return null;
 	}
@@ -536,22 +546,22 @@
 	@Override
 	public String getBirtImage(String uuid, int resolutionId) {
 		BlobDto blob = getBlobById(uuid, resolutionId);
-		if(blob != null) {
-			return getBase64ValueFromBlob(getBlobById(uuid, resolutionId));
+		if (blob != null) {
+			return getBase64ValueFromBlob(blob);
 		}
 		return null;
 	}
-	
+
 	@Override
 	public StreamResource getResource(String uuid, int resolutionId) {
 		BlobDto blob = getBlobById(uuid, resolutionId);
-		if(blob != null) {
+		if (blob != null) {
 			return new StreamResource(new StreamSource() {
 				private static final long serialVersionUID = 1L;
-	
+
 				@Override
 				public InputStream getStream() {
-					if(isImage(blob.getBlobMapping().getMimeTypeId())) {
+					if (isImage(blob.getBlobMapping().getMimeTypeId())) {
 						return new ByteArrayInputStream(decodeBase64(blob.getData()));
 					} else {
 						return new ByteArrayInputStream(blob.getData());
@@ -566,11 +576,11 @@
 	public int getNormalizerResolutionIdByName(String displayResolution) {
 		return blobAPI.getNormalizerResolutionIdByName(displayResolution);
 	}
-	
+
 	@Override
 	public BufferedImage getBufferedImage(String uuid, int resolutionId) {
 		BlobDto blob = getBlobById(uuid, resolutionId);
-		if(blob != null) {
+		if (blob != null) {
 			InputStream in = new ByteArrayInputStream(decodeBase64(blob.getData()));
 			try {
 				return ImageIO.read(in);
@@ -584,7 +594,7 @@
 	@Override
 	public byte[] getByteArrayImage(String uuid, int resolutionId) {
 		BlobDto blob = getBlobById(uuid, resolutionId);
-		if(blob != null) {
+		if (blob != null) {
 			return decodeBase64(blob.getData());
 		}
 		return null;