スキップしてメイン コンテンツに移動

投稿

Calculate DENSE_RANK for MySQL

In this post, I will show you SQL query for calculateing dense rank for MySQL . You know Oracle and SQL Server have the function called DENSE_RANK, which is for returning rank of the value. Unfortunately MySQL doesn't have the built-in function. We can calculate dense rank by nested select for same table with count function. However this strategy is toooo slow because calculating rank by counting number of rows above the target row for every rows. Anyway here is the query for calculating dense rank for MySQL. SELECT id, @rnk:=IF(@preval score, @rnk, @row + 1) AS dns_rnk, @row:= @row+1 AS rnk, @preval:=score as score FROM table # be careful for NULL handling. # if all the values of score column are null, then dns_rank will zero. # please set proper initial value for @preval based on your data. JOIN (SELECT @rnk := 0, @preval :=null, @row := 0) r ORDER BY score DESC The result should be something like this. id dns_rnk rnk score 1 1 1 100 4 2 2 81 ...

Symfony 1.4 Accessing Context

Symfony 1.4 is already a legacy framework. But I think a lot of web system still running on it. In this post, I will show you some quick tips for accessing infromation by using sfContext . sfContext provides almost everything you want access. But you should not use context everywhere too much because sfContext is a kind of global objeect and if a lot of codes depends on context, your code will be hard to be tested or maintained, I think. Basic You can access sfContext by following code. sfContext::getInstance(); In sfFilter, you can access sfContext by the following code. $this->getContext(); What Kind of Fields You can access? Routing name $context->getRouting()->getCurrentRouteName(); User $context->getUser(); Request $context->getRequest(); Response $context->getResponse(); Controller $context->getController(); Logger sfContext::getInstance()->getLogger(); Advanced Use Check if http request is secure (e.g. https request). $co...

Java: Capture Web Page

Recently I was investigating how to capture web page as image. I recently realized that there is a nicer nice component for navigating web page in Java FX. javafx.scene.web.WebView javafx.embed.swing.JFXPanel I referred this stackoverflow post and wrote a Java class for capturing web page and save it as image. Please note that you should include jfxrt.jar, which was in JDK1.7.0_XX/jre/lib, should be added to your classpath. Code Here is an actual code. Please feel free to use it... I am very happy if you give me some feedback for this code. package com.dukesoftware.javafx; import static com.dukesoftware.utils.io.IOUtils.getExtension; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.util.Map; import java.util.Set; import java.util.concurrent.Executors; import java.util.concurrent.LinkedBlockingQueue; import javafx.application.Platform; import javafx.beans.value.ChangeListener; import javafx.beans.value.ObservableValue;...

ActionScript 3.0: Capture Web Page Image

In my recent post, I was trying to find out easy way to capture we page image. Today, I will show you my small code tip for capturing web page and saving it as image by ActionScript 3.0. WebPageCapture class This is the main class for capturing web page and save it as image. A few points I should comment... In captureAndSave method, I have used timer because even after Complete event is triggered, somehow sometimes web page is not rendered properly... (might be depends on web site or lazy javascript loading.) requstQueue field is for making sure the request is processed one by one after the former image capturing request is done. You can avoid this if you create HTMLLoader instance for each request. Looks default JPEGEncoder is quite slow.... I have googled and found the following great article. If you are interested in performance, see this excellent post . I was amazed actually :D If you are interested in asynchronous encoding, see this marvelous post . I was impressed :D...

Create Web Page Thumbnail by Java or C#

I have googled how to create web page thumbnail. Hope this post helps anyone who are trying to create web page image. Java Lunch a native browser from Desktop class and capture active window by Robot class I think this isn't smart way however it should work. Please read this . The disadvantage of this method is you cannot achieve it on off screen. Using SWT Browser Please read this . The biggest disadvantage is again you cannot do it on off screen. Pure Java Solution If you are seeking pure java solution, maybe Cobra or Css Box will help you. Unfortunately Cobra is not updated recently, and Css Box is still very new on the other hand. I hope I can post example of code on this blog in near future.... Using QT-Jambi I'm not familiar with Qt library but this post explains how to create web page image by Qt Jambi , which is java wrapper of Qt Library. The post provides code example, too!! C# Using System.Windows.Forms.WebBrowser this article in www.codeproj...

Java: How to Manipulate Image as Double Array (Read and Write)

Read Image as Double Array You might think "We can use Raster#getPixel method if we would like to grab pixels, can't we?". Yes that's true. The key problem is the low performance because getPixel method internally call SampleModel and convert raw value to proper pixel value every method call. So we should call Raster#getPixels method and grab all pixels one method call. I have done similar stuffs in C# and posted on this blog. On the other hand in Java, this is a bit messy because there are a lot of related classes - Raster, ImageIO, Reader, Writer, etc ;). The simplest way I did is: read image as BufferedImage by ImageIO call BufferedImage#getRaster method call Raster#getPixels method Here is an exmple code. public static TemporalImage readImagePixelDoubleArray(InputStream is, int bitPerPixel) throws IOException{ BufferedImage image = ImageIO.read(is); Raster raster = image.getRaster(); int x = raster.getMinX(); int y = raster....

Java: How to Load Classes at Runtime from Jar or Class Files

I wrote java program for loading classes at runtime from jar file or class files Hope this post will help somebody... package com.dukesoftware.utils.reflect; import java.io.File; import java.io.FileFilter; import java.io.IOException; import java.net.URI; import java.net.URL; import java.net.URLClassLoader; import java.util.ArrayList; import java.util.Enumeration; import java.util.List; import java.util.jar.JarEntry; import java.util.jar.JarFile; public class DynamicClassLoader { public static List<Class<?>> readAllClassesFormJarFile(File jarFilePath) throws IOException{ List<Class<?>> classes = new ArrayList<>(); URL[] urls = { new URL("jar:" + jarFilePath.toURI().toURL() + "!/") }; URLClassLoader loader = URLClassLoader.newInstance(urls); for(Enumeration<JarEntry> en = new JarFile(jarFilePath).entries(); en.hasMoreElements();){ final String name = en.nextElement().getName(...