tapestry
http://tapestry.apache.org/tapestry3/doc/api/index.html
http://archive.apache.org/dist/jakarta/tapestry/Tapestry-3.0.2-bin.zip
Mock Table utility for CRUD operations
While learning a new technology we may need a database table to store the data. But creating a database, setting up jdbc connection and writing crud operation may be cumbersome. So I thought it would be good to have some mock utility to represent a table which can be used just like a database table. Here is what I came up with.
1
package
com.sivalabs.sample.util;
2
import
java.io.Serializable;
4
public
interface
Identifiable<K>
extends
Serializable
6
public
void
setId(K id);
01
package
com.sivalabs.sample.util;
03
import
java.util.Collection;
04
import
java.util.HashMap;
07
public
abstract
class
Table<PK
extends
Object, T
extends
Identifiable<PK>>
09
protected
final
Map<PK, T> table =
new
HashMap<PK, T>();
10
public
abstract
PK getNextId();
16
public
void
create(T obj)
18
if
(table.containsKey(obj.getId()))
20
throw
new
RuntimeException(
"PrimaryKey ["
+obj.getId()+
"] already exists"
);
22
obj.setId(getNextId());
23
table.put(obj.getId(), obj);
26
public
Collection<T> getAll()
28
return
table.values();
31
public
T getById(PK id)
36
public
void
update(T obj)
38
if
(!table.containsKey(obj.getId()))
40
throw
new
RuntimeException(
"PrimaryKey ["
+obj.getId()+
"] doesn't exists"
);
42
table.put(obj.getId(), obj);
45
public
void
delete(T obj)
50
public
void
delete(PK id)
52
if
(!table.containsKey(id))
54
throw
new
RuntimeException(
"PrimaryKey ["
+id+
"] doesn't exists"
);
Let us create a pojo Message.java.
01
package
com.sivalabs.sample;
03
import
java.util.Date;
04
import
com.sivalabs.sample.util.Identifiable;
06
public
class
Message
implements
Identifiable<Integer>
08
private
static
final
long
serialVersionUID = 1L;
12
private
String postedBy;
13
private
Date postedDate =
new
Date();
18
public
Message(Integer id, String text, String postedBy, Date postedDate)
22
this
.postedBy = postedBy;
23
this
.postedDate = postedDate;
26
public
Integer getId()
30
public
void
setId(Integer id)
Now let us create a mock table for storing Messages. The Message table needs to extend Table and provide what is the type of primary key and what type of objects MessageTable is going to contain using generics <Integer, Message>.
01
package
com.sivalabs.sample.util;
02
import
java.util.concurrent.atomic.AtomicInteger;
03
import
com.sivalabs.sample.Message;
05
public
class
MessageTable
extends
Table<Integer, Message>
07
private
static
final
AtomicInteger ATOMIC_INTEGER =
new
AtomicInteger(
0
);
09
public
Integer getNextId()
11
return
ATOMIC_INTEGER.incrementAndGet();
Now let us create a MessageService which holds an instance of MessageTable and expose the CRUD operations to clients.
01
package
com.sivalabs.sample;
03
import
java.util.Collection;
04
import
java.util.Date;
05
import
com.sivalabs.sample.util.MessageTable;
08
public
class
MessageService
10
private
static
final
MessageTable MESSAGE_TABLE =
new
MessageTable();
13
MESSAGE_TABLE.create(
new
Message(
1
,
"Message1 Text"
,
"Siva"
,
new
Date()));
14
MESSAGE_TABLE.create(
new
Message(
2
,
"Message2 Text"
,
"Prasad"
,
new
Date()));
15
MESSAGE_TABLE.create(
new
Message(
3
,
"Message3 Text"
,
"Prasad"
,
new
Date()));
16
MESSAGE_TABLE.create(
new
Message(
4
,
"Message4 Text"
,
"Siva"
,
new
Date()));
19
public
Collection<Message> getMessages()
21
return
MESSAGE_TABLE.getAll();
24
public
Message getMessage(Integer id)
26
return
MESSAGE_TABLE.getById(id);
29
public
void
saveMessage(Message message)
31
MESSAGE_TABLE.create(message);
34
public
void
updateMessage(Message message)
36
MESSAGE_TABLE.update(message);
39
public
void
deleteMessage(Integer id)
41
MESSAGE_TABLE.delete(id);
Now if you want to create a mock table for another pojo User.java it is simple.
01
package
com.sivalabs.sample.util;
02
import
java.util.concurrent.atomic.AtomicInteger;
03
import
com.sivalabs.sample.User;
05
public
class
UserTable
extends
Table<Integer, User>
07
private
static
final
AtomicInteger ATOMIC_INTEGER =
new
AtomicInteger(
0
);
09
public
Integer getNextId()
11
return
ATOMIC_INTEGER.incrementAndGet();
If the primary key is always an auto incremented integer value we can move getNextId() method to Table.java. Then creating mock table becomes even more simpler.
1
package
com.sivalabs.sample.util;
2
import
com.sivalabs.sample.User;
4
public
class
UserTable
extends
Table<Integer, User>