Saturday, July 26, 2014

JSF 1.2: Project Woodstock Application using JPA

Woodstock Dataprovider Entity Example
Here is another example of using Project Woodstock along with JPA in an Enterprise Application. The project requires the sample database included in NetBeans.

The project was updated using NetBeans 6.5.1.


The code for the project can be found on Bitbucket here: WoodstockJPAApplication

Friday, July 25, 2014

JSF 1.2: Project Woodstock Multiple Selection Table Example

Multiple Selection Table

This is another example of a Project Woodstock project that was converted from Project Rave and Sun Studio Creator 2. This example details a multiple selection table, and was originally created by Winston Prakash.

I have updated the project using NetBeans 6.5.1 and tested on GlassFish 2.1.1.

The updated project can be found on BitBucket here: MultipleSelectionTable

Thursday, July 24, 2014

JSF 1.2: Project Rave Single Selection Table

Single Selection Table
Here is another example Project Rave/Woodstock project originally written by Winston Prakash for Sun Studio Creator 2. It has been updated using NetBeans 6.5.1 and tested on Glassfish 2.1.1.

The project can be found on BitBucket here: SingleSelectionTable

JSF 1.2: Woodstock Collapsible Group Table Example

Collapsible Group Table Example
Here is another example of a Project Rave data table converted to Project Woodstock. Project Woodstock was a great idea and the implementation with Visual JSF was the right path to go with JSF development. It is a shame that the project was canceled by Sun. I met a lot of great people who worked on the projects, and are still friends today. The code for this project was originally developed by Winston Prakash at Sun.

The code was developed using NetBeans 6.5.1 and can be downloaded from BitBucket here: CollapsibleGroupTable


Monday, July 14, 2014

JSF 1.2: Project Woodstock Button Facet Table

I was going through some old code examples. I found one created with Sun Studio Creator. Yes, it was very old.

The original example was developed by Winston Prakash.

I did some updates to Project Woodstock from the original Project Rave, and came up with a pretty new example page.

The project can be downloaded here: ButtonHeaderTable

Note: You will need to use NetBeans 6.5.1, or 6.7.1 to run it.

Sunday, July 13, 2014

JSF 1.2: Visual Web Pack (Project Woodstock) Java Persistence API Example

Master-Detail Example
This is some example code that I have for a Visual Web Pack (VWP) project that demonstrates some complex data table examples.

I often fantasize about being able to get the band back together and make Woodstock 2.0. Here is an example of why. This was complex for JSF 1.2.

The code can be downloaded from: vwpjpaexamples

I would strongly recommend using NetBeans 6.5.1 to build and run the example project.

Thursday, July 10, 2014

A simple practical "pragmatic" Formatter for java.util.logging.Logger

I have quite a trove of code examples I have collected over the years. Here is another example of a Formatter used with the java.util.logging.Logger to generate a standard output.

PragmaticFormatter.java


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package com.bluelotussoftware.logging;
 
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.Formatter;
import java.util.logging.LogRecord;
 
/**
 * <p>
 * A formatter that generates a standard message as shown below.</p>
* <pre>
 * [17/10/2014 22:17:01.348 EDT] com.example.project.App INFO main This is informational message!
 * </pre>
*
 * @author John Yeary <jyeary@bluelotussoftware.com>
 * @version 1.0
 */
public class PragmaticFormatter extends Formatter {
 
    private static final String DATEFORMATPATTERN = "mm/dd/yyyy HH:mm:ss.SSS z";
    private SimpleDateFormat dateFormatter = new SimpleDateFormat(DATEFORMATPATTERN);
 
    /**
     * Default constructor.
     */
    public PragmaticFormatter() {
    }
 
    /**
     * {@inheritDoc}
     */
    public String format(final LogRecord record) {
        StringBuilder sb = new StringBuilder();
        sb.append("[").append(dateFormatter.format(new Date(record.getMillis()))).append("] ");
        sb.append(record.getSourceClassName()).append(" ");
        sb.append(record.getLevel().getName()).append(" ");
        sb.append(record.getSourceMethodName()).append(" ");
        sb.append(record.getMessage()).append("\n");
        return sb.toString();
    }
 
    /**
     * This returns the {@link SimpleDateFormat} used by the {@link Formatter}.
     *
     * @return {@link SimpleDateFormat} used by the {@link Formatter}.
     */
    public SimpleDateFormat getDateFormatter() {
        return dateFormatter;
    }
 
    /**
     * Sets the {@link SimpleDateFormat} used by the {@link Formatter}
     *
     * @param dateFormatter The {@link SimpleDateFormat} to be set.
     */
    public void setDateFormatter(final SimpleDateFormat dateFormatter) {
        this.dateFormatter = dateFormatter;
    }
}
The next question is how do you use it? Easy enough... here is an example for you.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package com.example.project;
 
import com.bluelotussoftware.logging.PragmaticFormatter;
import java.util.logging.ConsoleHandler;
import java.util.logging.Level;
import java.util.logging.LogRecord;
import java.util.logging.Logger;
 
public class App {
 
    private static final Logger LOGGER = Logger.getLogger(App.class.getName());
 
    public App() {
    }
 
    public static void main(String[] args) {
        ConsoleHandler handler = new ConsoleHandler();
        handler.setFormatter(new PragmaticFormatter());
        LOGGER.addHandler(handler);
        LOGGER.log(new LogRecord(Level.INFO, "This is informational message!"));
    }
}
The code for the project is located here: pragmatic-logging-formatter

JSF 2.x Tip of the Day: Encoding Text for XML

I have a simple method to encode text to display inside an XML page, or to use inside other XML/JS for example SyntaxHighlighter.

XMLEncode


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public static String XMLEncode(final String text) {
    StringBuilder result = new StringBuilder();
 
    for (int i = 0; i < text.length(); i++) {
        char value = text.charAt(i);
        if (!((value >= 'a' && value <= 'z')
                || (value >= 'A' && value <= 'Z')
                || (value >= '0' && value <= '9'))) {
            MessageFormat.format("&#{0};", (int) value);
            result.append(MessageFormat.format("&#{0};", (int) value));
        } else {
            result.append(value);
        }
    }
    return result.toString();
}

Creating a BLOB Image Table

I am going through some old code while I wait for my Windows VM to update. I came across some code to create an image BLOB table on MySQL. I thought I would publish it before deleting it from my system. It might be helpful to someone.

CreateImagesTable.java


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package com.bluelotussoftware.database.utils;
 
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
 
/**
 * Creates an Image BLOB table on the target database.
 *
 * @author John Yeary
 * @version 1.0
 */
public class CreateImagesTable {
 
    // The URL connection string to the MySQL Database
    // This can be modified to attach to another database
    // The user and password need to be modified accordingly
    private final String URL = "jdbc:mysql://localhost/images?user=XXXX&password=YYYYYY";
 
    // SQL string to create images table
    private final String SQL = "CREATE TABLE IMAGES (ID INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT,"
            + "FILENAME VARCHAR(255) NOT NULL, "
            + "BINARYIMAGEDATA MEDIUMBLOB NOT NULL);";
 
    // This needs to be changed if you use another database driver
    private final String driver = "com.mysql.jdbc.Driver";
 
    private Connection con = null;
    private Statement stmt = null;
 
    /**
     * Creates a new instance of CreateImagesTable
     */
    public CreateImagesTable() {
    }
 
    /**
     * Creates an Image table for BLOB images on the target database.
     */
    public void create() {
        try {
            Class.forName(driver);
            con = DriverManager.getConnection(URL);
            stmt = con.createStatement();
            stmt.execute(SQL);
        } catch (ClassNotFoundException e) {
            System.err.println(e.getMessage());
        } catch (SQLException e) {
            System.err.println(e);
        } finally {
            try {
                if (con != null) {
                    con.close();
                }
            } catch (SQLException ignored) {
            }
        }
    }
 
    public static void main(String args[]) {
        CreateImagesTable table = new CreateImagesTable();
        table.create();
    }
 
}

cat: How do I list the contents of a text file?

I was asked by a new developer how you would cat the contents of a file in Java. I thought for a second and here is what I came up with. I thought I would just share it. Note: This is my 30s answer, and probably could be cleaned up.

Cat.java


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package com.bluelotussoftware.utilities;
 
import java.io.*;
import java.text.MessageFormat;
 
public class Cat {
 
    public static void cat(File named) {
        String line;
 
        try (RandomAccessFile input = new RandomAccessFile(named, "r")) {
            while ((line = input.readLine()) != null) {
                System.out.println(line);
            }
        } catch (FileNotFoundException e) {
            System.out.println(MessageFormat.format("File: {0} not found.", named.getName()));
        } catch (IOException e) {
            System.out.println(e.getMessage());
        } finally {
        }
    }
 
    public static void main(String[] args) {
        Cat.cat(new File(args[0]));
    }
}

Popular Posts