Posts Tagged Java
RSA & RDA – Logical to Physical Data Model Transformation
Posted by Redi Gokaj in Database, Java, JPA on June 4, 2010
Rational Software Architect Logical to Physical Data Model Transform from Redi Gokaj on Vimeo.
The purpose of this video is to explain a scenario which shows the way how to create a database project using an object oriented approach. Rational Software Architect and Rational Data Architect (Infosphere Data Architect) are used for this tutorial. Using RDA and RSA you may watch this simple tutorial about creating a logical model. After that, converting it to a physical model. The third step will be creating the database, and after that creating the entities mapped with the database. With a sample web application, these entities are used and tested.
Sorting JPA Collections
Posted by Redi Gokaj in Java, JPA on May 22, 2010
Assume that your database you have a “News” table and a “Comment” table. In your Java entities you have mapped the News and the Comment table as two classes. Different users post comments to your News and you need to display these comments and the web page were the News is displayed. So, the first operation to be done is to sort them according to the post date. You don’t need to write a query for it. Just take the comment collection of that specific news and use the @OrderBy annotation in order to sort.
@JoinTable(name=”NEWSCOMMENT”,joinColumns=@JoinColumn(name=”NEWSID”),inverseJoinColumns=@JoinColumn(name=”COMMENTID”))
@ManyToMany(fetch=FetchType.EAGER)
@OrderBy(“POSTDATE ASC”)
private List<Comment> commentCollection;
News news = … //Here you get your news object
List<Comment> comments = news.getCommentCollection(); //By this operation you accomplish the ordering of the comments shown to the news
In @OrderBy(“POSTDATE ASC”) the POSTDATE is an attribute of the table of Comments.
Proportional image resizing in Java
Posted by Redi Gokaj in Java, Software developing on April 8, 2010
In order to resize images in Java a simple code is used. In fact, depending on the quality of the photos you want to resize there can be different resize codes and libraries. But a common used is the one I’ll show in this post. Together with the resize code I’ll also give the code which makes the proportional resize. Proportional resizing is a “must” in order not to loose the shape of the image. Lest’s start with the resize code which is shown below:
private static BufferedImage resizeImage(BufferedImage image, int width, int height)
{
BufferedImage resizedImage = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
Graphics2D g = resizedImage.createGraphics();
g.drawImage(image, 0, 0, width, height, null);
g.dispose();
return resizedImage;
}
This is enough in order to resize the image, but before calling this function we need to set the proper width and height according to the proportional resize we will make.
//imageFile is any file taken from the buffer or a disk path. It should be of type java.io.File.
//thumbnailWidth & thumbnailHeight should be defined as values in order to have the new image's sizes.
BufferedImage originalImage = ImageIO.read(imageFile);
int originalWidth = originalImage.getWidth();
int originalHeight = originalImage.getHeight();
float widthRatio = originalWidth/thumbnailWidth;
float heightRatio = originalHeight/thumbnailHeight;
BufferedImage resizeImageJpg = null;
if (widthRatio > heightRatio)
{
float newThumbnailHeight = (float)originalHeight/(float)originalWidth * thumbnailWidth;
resizeImageJpg = resizeImage(originalImage, thumbnailWidth, (int) newThumbnailHeight);
}
else
{
float newThumbnailWidth = (float)originalWidth/(float)originalHeight * thumbnailHeight;
resizeImageJpg = resizeImage(originalImage, (int) newThumbnailWidth, thumbnailHeight);
}
ImageIO.write(resizeImageJpg, "jpg", new File("filename..."));
Recent Comments