Latest Entries »

Thursday, November 21, 2013

Playframework 2.2.1 with Bootstrap 3.0 less css compilation error [solved]

Like many developers you wish to have newest version of each tool. In my case it was exactly the same. I want playframework 2.2.1 and newest bootstrap. But yet not everything was working very good and I've got compilation errors on less files. Meanwhile there are few links: link1, link2 but they were not working correctly and therefore they were to complicated. I've combined some links, used my instinct and found easy way to fix this problem :)

Renaming helper files except bootstrap.less

Yes, it is very easy to do.

 Then I changed all imports:



Finally I changed encoding in my notepad++ to UTF:


Then in my view I could write this:
<link rel="stylesheet" href="@routes.Assets.at("stylesheets/bootstrap/bootstrap.css")" />

Thursday, October 18, 2012

MSSQL ROUND and SUM problem with 0.01 Value

Introduction

Sometimes when you are dealing with sql rounding you can run into problem with 0.01 rest when you sum some values. This is very annoying and many companies implement their own algorithm to properly round values that they match sum. For example you got sum of two numbers: d1 = 20.1597, d2 = 8.8452, and sum of them should be 29.0049. Someone finally round this value to 29.00 using this algorithm: sum = round(d1 + d2). Simple as rolling dice. But when comes to SQL, another developer want to show each of value seperately and thus he or she want to cast this value as varchar. Oh yeah varchar. When you cast d1 and d2 from money to varchar you got strange behaviour:
CAST(@d1 as varchar(100)) gives output as 20.16
CAST(@d2 as varchar(100)) gives output as 8.85
Hence sum of this two values 20.16 + 8.85 gives 29.01. FACEPALM....

Solution of your nighmares with 0.01

Some of your collegues perhaps did solve this problem with very complicated alhorithms which can make headache when trying to figure out code but I'll focus on simplest way as possible. And I'll take advantage of SQL. No strange math.
First to do you need to get temp variables from @d1 and @d2 which are rounded and not-rounded values.
declare @d1_r money = ROUND(@d1,2)
DECLARE @d1_nr money = ROUND(@d1,2,1)


DECLARE @d2_r money = ROUND(@d2,2)
DECLARE @d2_nr money = ROUND(@d2,2,1)


Having this values we can do cartesian set and get values that match sum of 29.
DECLARE @tab table (val money)

INSERT INTO @tab
SELECT @d1
UNION
SELECT @d1_nr
UNION
SELECT @d1_r
UNION
SELECT @d2
UNION
SELECT @d2_nr
UNION
SELECT @d2_r

With this code above we can do cross join:
SELECT  t1.val
  , t2.val
FROM
 @tab t1
 CROSS JOIN @tab t2
WHERE
 t1.val + t2.val = @sum
Which will output:
20.16 8.84
20.15 8.85
8.85 20.15
8.84 20.16

When we need to get one value from this query we can simply write:
 DECLARE @t1_val money
 DECLARE @t2_val money
 
 /* Cartesian set where t1 + t2 = sum */
 SELECT TOP(1) @t1_val = t1.val, @t2_val = t2.val
  FROM
   @tab t1
   CROSS JOIN @tab t2
  WHERE
   t1.val + t2.val = @sum

IF (@variant = 1)
 return CAST(@t1_val as varchar(20))
else
 return CAST(@t2_val as varchar(20))


So having all code together I create function called fn_round_two_numbers:

CREATE FUNCTION [dbo].[fn_round_two_numbers]
(
 @d1 money,
 @d2 money,
 @sum money,
 @variant int
)
RETURNS VARCHAR(100)
AS
BEGIN
 /* @d1 rounded and not rounded  */
 DECLARE @d1_r money = ROUND(@d1,2)
 DECLARE @d1_nr money = ROUND(@d1,2,1)
 
 /* @d2  rounded and not rounded */
 DECLARE @d2_r money = ROUND(@d2,2)
 DECLARE @d2_nr money = ROUND(@d2,2,1)
 
 /* Temp table for all values */
 DECLARE @tab table
 (val money)
 
 /* Inserting into table */
 INSERT INTO @tab
 SELECT @d1
 UNION
 SELECT @d1_nr
 UNION
 SELECT @d1_r
 UNION
 SELECT @d2
 UNION
 SELECT @d2_nr
 UNION
 SELECT @d2_r
 
 DECLARE @t1_val money
 DECLARE @t2_val money
 
 /* Cartesian set where t1 + t2 = sum */
 SELECT TOP(1) @t1_val = t1.val, @t2_val = t2.val
  FROM
   @tab t1
   CROSS JOIN @tab t2
  WHERE
   t1.val + t2.val = @sum

IF (@variant = 1)
 return CAST(@t1_val as varchar(20))
else
 return CAST(@t2_val as varchar(20))
 
RETURN '!@#AGGR SOMETHING GOES WRONG' 
END

Concusions

With this function you are safe as turtle inside shell. You can simply extend this function to using n-number rounding.

And this is all. Thanks for reading.

Friday, April 27, 2012

Styling input file only with CSS (no images)


Introduction


As far as I browse the Internet there was no jQuery plugin that allow us to beautify <input type="file" /> without graphic images. This is really bad because very often our inputs are styled with CSS3: gradients, shadows, corners etc.. and it can be dynamically changed without any problem. So I took things in my hands and I made small changes to very popular jQuery plugin File Style for jQuery to allow use only CSS.

How it works

This plugin has aimed to create a wrapper for<input type="file" /> the same as in File Style but without any images. Demo is included in zip archived and should look like this:




Source code

Here is zipped source code and demo page. Meanwhile I've got time I will host this at github or code.google.com. If you got problem with downloads please post comment or email me.

Wednesday, March 28, 2012

Tool that every developer should know

Introduction

Today I'll discuss about things that make our work effortless and bring to us tools that your IDE won't give you. I can bet that in your workshop you use Visual Studio, SQL Managment Studio or NetBeans, Eclipse etc... but there is a whole bunch of tools that they are so helpful and when you find them you cannot live without using it. I'll introduce my list of software that is really helpful for me and you can check some of them if you didn't heard about it. Of course all is freeware.


LINQPad

How many times did you spend when you want to check small chunk of your code and see what it print out? Or you want to visualize some of your Linq Queries? This tool did a real impact on my and how to test small stuff. From time when I started using LINQPad I realize that I don't need debugger so often and I run my code inline.



Expresso

I had always know that regular expression are powerful tool build in all programming languages but I hadn't opportunity to learn them from scratch. Even tutorials across web doesn't seem to be nice because there were whole of theory and I want test my expression in practise. With regex developer can do a lot of stuff and I used them but I was just copying rules from regular expression database websites for my purposes and I didn't use them on my own. Nevertheless that day come and I find Expresso. Great tool for visual building expression, testing them without any problems. Now when I come across many common tasks (text replacing from SQL queries, writing some logs, replacing Strings in my code, using Validation) I saw that I can do two thing. First learn some more and finally I solve my problems more in more sophisticated way. In every detail I recommend this program for everyone.




Notepad++

Yes. I know that everybody use it and are familiar with this program. But did you explore most of assistance that come with Notepad++ ?

  • With ALT + SHIFT + ARROWS you have Column Mode Selection,
  • With regular expression you can replace everything you need
  • You can jsbeautify/js.minify your javascripts with JsMin plugin
  • You can execute all commands with NppExec 
  • You can see json files hierarchically
  • More stuff is inside all of available plugins



Code compare

In this category you propably have your own best software. I had simple but very useful called WinMerge for a long time. Once I had more time I started to looking for software for comparing files and finally found one which is my top 1 in source comarision. Code Compare by Devart exceeded my expectations. This program show file diffrences like in modern comparision tools, it has Visual Studio plugin for inline IDE comparision and last very handy feautre: Integration with Explorer. You can choose two diffrent files selecting 'Select left', 'Select right' and then compare those files without any waste of time. This software has great support for merging files with '3 window comparision' feature.




PowerShell and PowerGUI


When I realized that all tasks in work doesn't fire google and I've got all in my head I sadly found I did not learn anything new. In meantime I've got task for analyze logs made by my application to check whether some code is bugged. Finally I despite cmd.exe and love unix bash! Quick serching over web and I found. I can learn one more script language which is strongly connected with .NET and automates routine taks and thus bring some coding entertainment. I've started with some simple scripts and now I get one important thought. Everything you thought is difficult or impossible you can do with PowerShell scripts!. After some scripts in Notepad++ I was searching for powerfull "IDE" for PowerShell. God hear my prays and I found PowerGUI. Again I found amazing tool which is one of secret weapon.




SQL Search

While you are implementing your enormous project and you use stored procedures, sql functions and other sql objects? One day you look inside stored procedures and whisper to yourselve "Where the hell that procedure is? I can't find it!". So am I. Working in one company my friend gave mi a link to SQL Search plugin for Managment Studio. That was awesome shot! I found tool for finding stored procedure by name. Moreover I could find even search inside body of sql objects?!?! No more headache when I had to modify some sql statements! Gloria !!




Open DBDiff

As far as SQL database cannot be versioned by Source Control System a'la Subversion I had nightmares when came to moving procedure changes between testing and production environment. Previously I had chance to synchronize database using Visual Studio Ultimate which had great SQL Schema/Data Comparision tool but when I moved to new company I was on my own. I've been saving all modified sql procedures to file system but when I simply forgot some of procedures? Customer will be calling and I will get headache fixing all bugs? Of course no. I could use Open DBDiff and check what did changed between test and production and I can generate script for this! What a relief ...





ClipX

This program is dedicated for everyone (including me) who had a chance to copy/paste, copy/paste lot of code, copy/paste log information, copy/paste, copy/paste, copy/paste lot of code, copy/paste, copy/paste, copy/paste another code, copy/paste another code, copy/paste log information and simple CTRL + C and CTRL + V is not enought. With ClipX you have clipboard with 25 or 50 items, you can search across clipboard pastes and you can pate each item you wish copying it just once! This is really handy tool when you work with many projects or between documents when you need to copy/paste, copy/paste, copy/paste some of your previous code.




soapUI

Did you have occasion debugging SOAP WebServices? Did you get nuts when you get insane when you receive WSDL file and ask yourseve: Why it's not working?. Maybe you was looking for some bugs across sent XML reqest or responses? For me each question is answered YES. I had problems with SOAP when was working with some WebServices. Earlier when I didn't know about soapUI I was receiving all traffic via Fiddler but it was like torture. Finding one request beyond all network traffic... This software is perfectly written because you have all you want and all you need in one.



luke


Long time ago when I had task to improve e-commerce searching engine. Meanwhile SQL Server 2008 doesn't support my native full text search I found Lucene.NET engine. Everything would be good except one thing. Testing my new improvement was total nightmare. I couldn't just write piece of code, run debugger and check if proper search query was build and then repeat this approach. So I ask uncle google for help and he suggested me luke (Lucene index browser). From that moment I could see structure of Documents, execute queries against full text index and in result improve my software better and faster.



If you read this article and found useful or you got software to share please leave a comment.

Monday, March 12, 2012

JavaServer Faces website navigation map with Primefaces


Introduction


Hello. In this article I will show you how to create a simple website navigation map like those in asp.net (web.sitemap) which contains reference to your pages. Composite Component of navigation path will be localized on many languages and will be build on PrimeFaces BreadCrumb component. Final result will look like this:

Using PrimeFaces

Configuring PrimeFaces is as easy as riding a bike so I ommit this topic.
I'll create JSF 2.0 Composite Component with BreadCrumb inside and point it's dynamic model to ManagedBean which will use our newly created CC component.

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:cc="http://java.sun.com/jsf/composite"
      xmlns:p="http://primefaces.org/ui">

  <!-- INTERFACE -->
  <cc:interface>
      <cc:attribute name="model" required="true" />
      <cc:attribute name="styleClass" required="false" />
      <cc:attribute name="style" required="false" />
  </cc:interface>

  <!-- IMPLEMENTATION -->
  <cc:implementation>
      <p:breadCrumb styleClass="#{cc.attrs.styleClass}" style="#{cc.attrs.style}" model="#{cc.attrs.model}">
        </p:breadCrumb>
  </cc:implementation>
</html>

Implementing code for receive pages in project


Now it's time for ManagedBean holding all data. Most significant piece of code from class below is localized in public getWebsites(). Firstly I load localized messages and load filenames from root path "/". Next step is checking whether filename exists in localized messages:

package com.blogspot.rpiesnikowski.root;

import java.io.File;
import java.lang.String;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Locale;
import java.util.ResourceBundle;
import javax.inject.Named;
import javax.enterprise.context.RequestScoped;
import javax.faces.component.UIForm;
import javax.faces.component.UIViewRoot;
import javax.faces.component.html.HtmlOutputText;
import javax.faces.context.FacesContext;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.io.FileUtils;
import org.primefaces.component.menuitem.MenuItem;
import org.primefaces.model.DefaultMenuModel;
import org.primefaces.model.MenuModel;

/**
 *
 * @author rpiesnikowski
 */
@Named(value = "template")
@RequestScoped
public class TemplateBean {
    private MenuModel websites;

    /**
     * Method for dynamically create navigation for all pages inside
     * root directory.
     * @return
     */
    public MenuModel getWebsites() {
  // Receiving i18n bundle messages 
        String bundleName = "locales.messages";
        String Name = "";
        Locale locale = FacesContext.getCurrentInstance().getViewRoot().getLocale();
        ResourceBundle bundle = ResourceBundle.getBundle(bundleName, locale);
        websites = new DefaultMenuModel();
        HttpServletRequest req =  (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
        // Getting all files for path and then using Apache Commons 
  String rootPath = req.getServletContext().getRealPath("/");
        Collection<File> files = FileUtils.listFiles(new File(rootPath),new String[]{"xhtml"}, false);
        websites.addMenuItem(createMenuItem("#", "#"));
        for (File f: files){
            Name = f.getName().replace(".xhtml", "");
   // Checking if localized messages has key 
            if(bundle.containsKey(Name))
                websites.addMenuItem(createMenuItem(bundle.getString(Name), f.getName().replace(".xhtml", ".jsf")));
        }
        return websites;
    }

    public void setWebsites(MenuModel websites) {
        this.websites = websites;
    }

    protected MenuItem createMenuItem(String Name, String Url) {
        MenuItem item = new MenuItem();
        item.setValue(Name);
        item.setUrl(Url);
        return item;
    }
 
    public TemplateBean() {
    }
}

Now it's time to use component somewhere in Facelets Template:

<sc:NavigationPath model="${template.websites}" style="width: 99%;" />

Localizing NavigationPath Last but not least thing is to add localization files and modify your faces-config.xml. Put this between application node:

<application>
 <locale-config>
  <default-locale>en-US</default-locale>
  <supported-locale>pl</supported-locale>
  <supported-locale>fr</supported-locale>
  <supported-locale>es</supported-locale>
 </locale-config>
 <resource-bundle>
  <base-name>locales.messages</base-name>
  <var>msgs</var>
 </resource-bundle>
</application>

And it's time for localized message:

Calendar=Calendard a'la France
AjaxEngine=AjaxEngine a'la France
index=Index a'la France
DummyPage=DummyPage a'la France
BreadCrumb=BreadCrumb a'a France
asdf=asdf a'la France
ClientIdExample=ClientId a'la France

Additional tip

I've found really nice trick when I put my all empty localized messages into project. Try right click over messages.properties and choose Open. You this nice GUI form for filling localized strings.