Friday, April 17, 2009

Screen Graber Deamon

This was a hack to grab the questions from a paid online quiz. Idea here is, this program will run as a deamon in the background and take the screen shot every 10 second and save it into a jpeg file on temp directory.

public void captureFullScreen() { try { Robot robot = new Robot(); Rectangle area = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()); File target = new File(saveLocation, imageName + "_" + System.currentTimeMillis() + "." + imageType); saveImageToFile(robot.createScreenCapture(area), target); } catch (AWTException e) { System.err.println("Exception while capturing screen. " + e); } } /** * Saves the specified RenderedImage to the specified File. * * @param renderedImage the image to write to file. * @param target the file to write the image to. */ private void saveImageToFile(RenderedImage renderedImage, File target) { try { ImageIO.write(renderedImage, imageType, target); } catch (IOException e) { System.err.println(e); } }
and now the Deamon
public static void main(String args[]) throws InterruptedException { while(true) { new ScreenGraber().captureFullScreen(); Thread.sleep(10000); // will sleep for 10 seconds } }

Wednesday, April 8, 2009

JPA OneToMany using FetchType.EAGER

Scenario: Want to insert multiple address to basket header with a unique basketId.

BasketHeader.java @OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL) @JoinColumn(name = "basketId", nullable = false) public Set getBasketBillTos() { return basketBillTos; }
By specifying @JoinColumn basketId is set as the foreign key column name for the relationship. Otherwise a default name will be given for the foreign key column. If you are mapping to an existing database schema you will typically require setting the @JoinColumn, otherwise the defaults are probably fine. Implementation on action
Set basketBillToSet = new HashSet(); basketBillToSet.add(basketBillTo); basketHeader.setBasketBillTos(basketBillToSet);

Wednesday, April 1, 2009

Struts 2 OGNL variable evaluation

< s: set name="startIndex" value="%{#parameters['start'][0]}"/ > < s: property value="@java.lang.Integer@parseInt(#startIndex)+4"/ >
Here the first statement grab the query string value using parameter object and assign it as a string, the '%{}' syntax is used for this purpose. The second statement convert the string to int and add the two integers.
IMPORTANT: for the parseInt to work you should enable the constant in struts.xml < constant name="struts.ognl.allowStaticMethodAccess" value="true" / >