February 18, 2014

JSFUtils.java

I will keep updating this file

JSFUtils.java

Maven, EJB, JSF, Primefaces, JPA



How to build an Eclipse project contains:

EJB 3.1 and JSF 2.2 and Primefaces 4 and JPA 2.1

1. Run the following command to create a maven web project

mvn archetype:generate -DgroupId={project-packaging} -DartifactId={project-name} -DpackageName={package-name} -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false


2. Update the generated pom file as the following file:

pom.xml


<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.group.id</groupId>
    <artifactId>com.art.id</artifactId>
    <packaging>war</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>product name</name>
    <url>product url</url>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
 
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-api</artifactId>
            <version>6.0</version>
            <scope>provided</scope>
        </dependency>
 
        <dependency>
            <groupId>commons-lang</groupId>
            <artifactId>commons-lang</artifactId>
            <version>2.6</version>
        </dependency>
 
        <dependency>
            <groupId>org.eclipse.persistence</groupId>
            <artifactId>org.eclipse.persistence.jpa</artifactId>
            <version>2.5.1</version>
        </dependency>
 
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.28</version>
        </dependency>
 
        <dependency>
            <groupId>com.sun.faces</groupId>
            <artifactId>jsf-api</artifactId>
            <version>2.2.0</version>
        </dependency>
 
        <dependency>
            <groupId>com.sun.faces</groupId>
            <artifactId>jsf-impl</artifactId>
            <version>2.2.0</version>
        </dependency>
 
        <dependency>
            <groupId>com.sun.faces</groupId>
            <artifactId>jsf-impl</artifactId>
            <version>2.2.0</version>
        </dependency>
 
        <dependency>
            <groupId>org.primefaces</groupId>
            <artifactId>primefaces</artifactId>
            <version>4.0</version>
        </dependency>
 
    </dependencies>
 
    <build>
        <finalName>webapp-warfile</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <configuration>
                    <webResources>
                        <resource>
                            <directory>${basedir}/src/main/java</directory>
                            <targetPath>WEB-INF/classes</targetPath>
                            <includes>
                                <include>**/*.properties</include>
                                <include>**/*.xml</include>
                                <include>**/*.css</include>
                                <include>**/*.html</include>
                            </includes>
                        </resource>
                    </webResources>
                </configuration>
            </plugin>
 
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>
 
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-eclipse-plugin</artifactId>
                <version>2.9</version>
                <configuration>
                    <wtpversion>2.0</wtpversion>
                    <jeeversion>6.0</jeeversion>
                    <additionalProjectFacets>
                        <jst.jsf>2.2</jst.jsf>
                        <jst.java>1.7</jst.java>
                        <jpt.jpa>2.1</jpt.jpa>
                        <jst.web>3.1</jst.web>
                    </additionalProjectFacets>
                </configuration>
            </plugin>
 
        </plugins>
    </build>
</project>


3. Then run mvn eclipse:eclipse

Done!

February 4, 2014

التفقيط بلغة جافا Translating Numbers to Arabic Words



I have been assigned to a task about Translating Numbers to Arabic Words in Arabic called "التفقيط".

The assignment was to build a simple Java class that contains the business logic.

I have finished it and hope it will be useful for all.

The Code:

package tafqeet;

public class TafqeetMain {

    public TafqeetMain() {
    }

    final private static String[] units = { "Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight",
            "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen",
            "Nineteen" };
    final private static String[] tens = { "", "", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty",
            "Ninety" };

    private static String convert(Integer number) {
        if (number < 20) {
            return units[number];
        }
        if (number < 100) {
            return tens[number / 10] + ((number % 10 > 0) ? " " + convert(number % 10) : "");
        }
        if (number < 1000) {
            return units[number / 100] + " Hundred" + ((number % 100 > 0) ? " and " + convert(number % 100) : "");
        }
        if (number < 1000000) {
            return convert(number / 1000) + " Thousand " + ((number % 1000 > 0) ? " " + convert(number % 1000) : "");
        }
        return convert(number / 1000000) + " Million "
                + ((number % 1000000 > 0) ? " " + convert(number % 1000000) : "");
    }

    public static String convertNumberToEnglishWords(String number) throws NumberFormatException {
        Integer i = Integer.parseInt(number);
        return convert(i);

    }

    public static String convertNumberToArabicWords(String number) throws NumberFormatException {

        // check if the input string is number or not
        Double.parseDouble(number);

        // check if its floating point number or not
        if (number.contains(".")) { // yes
            // the number
            String theNumber = number.substring(0, number.indexOf('.'));
            // the floating point number
            String theFloat = number.substring(number.indexOf('.') + 1);
            // check how many digits in the number 1:x 2:xx 3:xxx 4:xxxx 5:xxxxx
            // 6:xxxxxx
            switch (theNumber.length()) {
            case 1:
                return convertOneDigits(theNumber) + " فاصلة " + convertTwoDigits(theFloat);
            case 2:
                return convertTwoDigits(theNumber) + " فاصلة " + convertTwoDigits(theFloat);
            case 3:
                return convertThreeDigits(theNumber) + " فاصلة " + convertTwoDigits(theFloat);
            case 4:
                return convertFourDigits(theNumber) + " فاصلة " + convertTwoDigits(theFloat);
            case 5:
                return convertFiveDigits(theNumber) + " فاصلة " + convertTwoDigits(theFloat);
            case 6:
                return convertSixDigits(theNumber) + " فاصلة " + convertTwoDigits(theFloat);
            default:
                return "";
            }
        }

        else {
            switch (number.length()) {
            case 1:
                return convertOneDigits(number);
            case 2:
                return convertTwoDigits(number);
            case 3:
                return convertThreeDigits(number);
            case 4:
                return convertFourDigits(number);
            case 5:
                return convertFiveDigits(number);
            case 6:
                return convertSixDigits(number);
            default:
                return "";
            }

        }
    }

    // -------------------------------------------

    private static String convertOneDigits(String oneDigit) {
        switch (Integer.parseInt(oneDigit)) {
        case 1:
            return "واحد";
        case 2:
            return "إثنان";
        case 3:
            return "ثلاثه";
        case 4:
            return "أربعه";
        case 5:
            return "خمسه";
        case 6:
            return "سته";
        case 7:
            return "سبعه";
        case 8:
            return "ثمانيه";
        case 9:
            return "تسعه";
        default:
            return "";
        }
    }

    private static String convertTwoDigits(String twoDigits) {
        String returnAlpha = "00";
        // check if the first digit is 0 like 0x
        if (twoDigits.charAt(0) == '0' && twoDigits.charAt(1) != '0') { // yes
            // convert two digits to one
            return convertOneDigits(String.valueOf(twoDigits.charAt(1)));
        } else { // no
            // check the first digit 1x 2x 3x 4x 5x 6x 7x 8x 9x
            switch (getIntVal(twoDigits.charAt(0))) {
            case 1: { // 1x
                if (getIntVal(twoDigits.charAt(1)) == 1) {
                    return "إحدى عشر";
                }
                if (getIntVal(twoDigits.charAt(1)) == 2) {
                    return "إثنى عشر";
                } else {
                    return convertOneDigits(String.valueOf(twoDigits.charAt(1))) + " " + "عشر";
                }
            }
            case 2: // 2x x:not 0
                returnAlpha = "عشرون";
                break;
            case 3: // 3x x:not 0
                returnAlpha = "ثلاثون";
                break;
            case 4: // 4x x:not 0
                returnAlpha = "أريعون";
                break;
            case 5: // 5x x:not 0
                returnAlpha = "خمسون";
                break;
            case 6: // 6x x:not 0
                returnAlpha = "ستون";
                break;
            case 7: // 7x x:not 0
                returnAlpha = "سبعون";
                break;
            case 8: // 8x x:not 0
                returnAlpha = "ثمانون";
                break;
            case 9: // 9x x:not 0
                returnAlpha = "تسعون";
                break;
            default:
                returnAlpha = "";
                break;
            }
        }

        // 20 - 99
        // x0 x:not 0,1
        if (convertOneDigits(String.valueOf(twoDigits.charAt(1))).length() == 0) {
            return returnAlpha;
        } else { // xx x:not 0
            return convertOneDigits(String.valueOf(twoDigits.charAt(1))) + " و " + returnAlpha;
        }
    }

    private static String convertThreeDigits(String threeDigits) {

        // check the first digit x00
        switch (getIntVal(threeDigits.charAt(0))) {

        case 1: { // 100 - 199
            if (getIntVal(threeDigits.charAt(1)) == 0) { // 10x
                if (getIntVal(threeDigits.charAt(2)) == 0) { // 100
                    return "مائه";
                } else { // 10x x: is not 0
                    return "مائه" + " و " + convertOneDigits(String.valueOf(threeDigits.charAt(2)));
                }
            } else {// 1xx x: is not 0
                return "مائه" + " و " + convertTwoDigits(threeDigits.substring(1, 3));
            }
        }
        case 2: { // 200 - 299
            if (getIntVal(threeDigits.charAt(1)) == 0) { // 20x
                if (getIntVal(threeDigits.charAt(2)) == 0) { // 200
                    return "مائتين";
                } else { // 20x x:not 0
                    return "مائتين" + " و " + convertOneDigits(String.valueOf(threeDigits.charAt(2)));
                }
            } else { // 2xx x:not 0
                return "مائتين" + " و " + convertTwoDigits(threeDigits.substring(1, 3));
            }
        }
        case 3:
        case 4:
        case 5:
        case 6:
        case 7:
        case 8:
        case 9: { // 300 - 999
            if (getIntVal(threeDigits.charAt(1)) == 0) { // x0x x:not 0
                if (getIntVal(threeDigits.charAt(2)) == 0) { // x00 x:not 0
                    return convertOneDigits(String.valueOf(threeDigits.charAt(1) + "مائه"));
                } else { // x0x x:not 0
                    return convertOneDigits(String.valueOf(threeDigits.charAt(0))) + "مائه" + " و "
                            + convertOneDigits(String.valueOf(threeDigits.charAt(2)));
                }
            } else { // xxx x:not 0
                return convertOneDigits(String.valueOf(threeDigits.charAt(0))) + "مائه" + " و "
                        + convertTwoDigits(threeDigits.substring(1, 3));
            }
        }

        case 0: { // 000 - 099
            if (threeDigits.charAt(1) == '0') { // 00x
                if (threeDigits.charAt(2) == '0') { // 000
                    return "";
                } else { // 00x x:not 0
                    return convertOneDigits(String.valueOf(threeDigits.charAt(2)));
                }
            } else { // 0xx x:not 0
                return convertTwoDigits(threeDigits.substring(1, 3));
            }
        }
        default:
            return "";
        }
    }

    private static String convertFourDigits(String fourDigits) {
        // xxxx
        switch (getIntVal(fourDigits.charAt(0))) {

        case 1: { // 1000 - 1999
            if (getIntVal(fourDigits.charAt(1)) == 0) { // 10xx x:not 0
                if (getIntVal(fourDigits.charAt(2)) == 0) { // 100x x:not 0
                    if (getIntVal(fourDigits.charAt(3)) == 0) { // 1000
                        return "ألف";
                    } else { // 100x x:not 0
                        return "ألف" + " و " + convertOneDigits(String.valueOf(fourDigits.charAt(3)));
                    }
                } else { // 10xx x:not 0
                    return "ألف" + " و " + convertTwoDigits(fourDigits.substring(2, 3));
                }
            } else { // 1xxx x:not 0
                return "ألف" + " و " + convertThreeDigits(fourDigits.substring(1, 4));
            }
        }
        case 2: { // 2000 - 2999
            if (getIntVal(fourDigits.charAt(1)) == 0) { // 20xx
                if (getIntVal(fourDigits.charAt(2)) == 0) { // 200x
                    if (getIntVal(fourDigits.charAt(3)) == 0) { // 2000
                        return "ألفين";
                    } else { // 200x x:not 0
                        return "ألفين" + " و " + convertOneDigits(String.valueOf(fourDigits.charAt(3)));
                    }
                } else { // 20xx x:not 0
                    return "ألفين" + " و " + convertTwoDigits(fourDigits.substring(2, 3));
                }
            } else { // 2xxx x:not 0
                return "ألفين" + " و " + convertThreeDigits(fourDigits.substring(1, 4));
            }
        }
        case 3:
        case 4:
        case 5:
        case 6:
        case 7:
        case 8:
        case 9: { // 3000 - 9999
            if (getIntVal(fourDigits.charAt(1)) == 0) { // x0xx x:not 0
                if (getIntVal(fourDigits.charAt(2)) == 0) { // x00x x:not 0
                    if (getIntVal(fourDigits.charAt(3)) == 0) { // x000 x:not 0
                        return convertOneDigits(String.valueOf(fourDigits.charAt(0))) + " ألاف";
                    } else { // x00x x:not 0
                        return convertOneDigits(String.valueOf(fourDigits.charAt(0))) + " ألاف" + " و "
                                + convertOneDigits(String.valueOf(fourDigits.charAt(3)));
                    }
                } else { // x0xx x:not 0
                    return convertOneDigits(String.valueOf(fourDigits.charAt(0))) + " ألاف" + " و "
                            + convertTwoDigits(fourDigits.substring(2, 3));
                }
            } else { // xxxx x:not 0
                return convertOneDigits(String.valueOf(fourDigits.charAt(0))) + " ألاف" + " و "
                        + convertThreeDigits(fourDigits.substring(1, 4));
            }
        }

        default:
            return "";
        }
    }

    private static String convertFiveDigits(String fiveDigits) {
        if (convertThreeDigits(fiveDigits.substring(2, 5)).length() == 0) { // xx000
                                                                            // x:not
                                                                            // 0
            return convertTwoDigits(fiveDigits.substring(0, 2)) + " ألف ";
        } else { // xxxxx x:not 0
            return convertTwoDigits(fiveDigits.substring(0, 2)) + " ألفا " + " و "
                    + convertThreeDigits(fiveDigits.substring(2, 5));
        }
    }

    private static String convertSixDigits(String sixDigits) {

        if (convertThreeDigits(sixDigits.substring(2, 5)).length() == 0) { // xxx000
                                                                           // x:not
                                                                           // 0
            return convertThreeDigits(sixDigits.substring(0, 3)) + " ألف ";
        } else { // xxxxxx x:not 0
            return convertThreeDigits(sixDigits.substring(0, 3)) + " ألفا " + " و "
                    + convertThreeDigits(sixDigits.substring(3, 6));
        }
    }

    private static int getIntVal(char c) {
        return Integer.parseInt(String.valueOf(c));
    }

    // ----------------------------------------------------------

    public static void main(String[] args) {
        System.out.println(TafqeetMain.convertNumberToEnglishWords("123456"));

        System.out.println(TafqeetMain.convertNumberToArabicWords("123412.11"));
    }
}