ObjectDB for Java/JDO Demo package directory.pc
|
|
|
The directory.pc package contains the four persistence capable classes
of this demo, as well as the JDO descriptor file, package.jdo, which defines the persistence capable classes:
directory/pc/package.jdo
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE jdo SYSTEM "http://java.sun.com/dtd/jdo_1_0.dtd">
<jdo>
<package name="directory.pc" >
<class name="Element" />
<class name="Item" persistence-capable-superclass="Element" />
<class name="Book" persistence-capable-superclass="Item" />
<class name="Category" persistence-capable-superclass="Element" />
</package>
</jdo>
|
Here is the implementation of the four persistence capable classes:
directory/pc/Element.java
// ObjectDB for Java Demo - JDO Directory - A Brief JDO Tutorial
// Copyright (C) 2001-2003, ObjectDB Software. All rights reserved.
package directory.pc;
import javax.jdo.*;
/**
* The Element abstract class is a super class of both Item and Category.
*/
public abstract class Element implements InstanceCallbacks {
// Persistent Fields:
private String name;
private Category parent;
// Constructors:
protected Element() {
}
protected Element(String name) {
this.name = name;
}
// JDO Callbacks:
public void jdoPreDelete() {
if (parent != null)
parent.getElements().remove(this);
}
public void jdoPostLoad() {}
public void jdoPreStore() {}
public void jdoPreClear() {}
// Accessor Methods:
public String getName() {
return name;
}
public Category getParent() {
return parent;
}
// Mutator Methods:
public void setName(String name) {
this.name = name;
}
public void setParent(Category parent) {
this.parent = parent;
parent.getElements().add(this);
}
// String Representation:
public String toString() {
return (name != null) ? name : "<new " + getClass().getName() + ">";
}
}
|
directory/pc/Category.java
// ObjectDB for Java Demo - JDO Directory - A Brief JDO Tutorial
// Copyright (C) 2001-2003, ObjectDB Software. All rights reserved.
package directory.pc;
import java.util.ArrayList;
import javax.jdo.*;
/**
* The Category class represents a category in the directory.
*/
public class Category extends Element {
// Persistent Fields:
private ArrayList elements = new ArrayList();
// Constructors:
public Category() {}
public Category(String name) {
super(name);
}
// JDO Callbacks:
public void jdoPreDelete() {
JDOHelper.getPersistenceManager(this).deletePersistentAll(elements);
super.jdoPreDelete();
}
public void jdoPostLoad() {}
public void jdoPreStore() {}
public void jdoPreClear() {}
// Accessor Methods:
public ArrayList getElements() {
return elements;
}
// Mutator Methods:
public void add(Element element) {
element.setParent(this);
}
}
|
directory/pc/Item.java
// ObjectDB for Java Demo - JDO Directory - A Brief JDO Tutorial
// Copyright (C) 2001-2003, ObjectDB Software. All rights reserved.
package directory.pc;
/**
* The Item class represents a simple link item in the directory.
*/
public class Item extends Element {
// Persistent Fields:
private String link;
// Constructors:
public Item() {}
public Item(String name, String link) {
super(name);
this.link = link;
}
// Accessor Methods:
public String getLink() {
return link;
}
}
|
directory/pc/Book.java
// ObjectDB for Java Demo - JDO Directory - A Brief JDO Tutorial
// Copyright (C) 2001-2003, ObjectDB Software. All rights reserved.
package directory.pc;
import java.util.*;
/**
* The Book class represents a book item in the directory.
*/
public class Book extends Item {
// Persistent Fields:
private String isbn;
private String authors;
private String publisher;
private Date date;
private int pages;
// Constructors:
public Book() {}
public Book(String name, int pages, String link, String authors,
String isbn, String publisher, int year, int month) {
super(name, link);
this.pages = pages;
this.isbn = isbn;
this.authors = authors;
this.publisher = publisher;
Calendar calendar = Calendar.getInstance();
calendar.clear();
calendar.set(year, month - 1, 1);
this.date = calendar.getTime();
}
// Accessor Methods:
public int getPages() {
return pages;
}
public String getIsbn() {
return isbn;
}
public String getAuthors() {
return authors;
}
public String getPublisher() {
return publisher;
}
public Date getDate() {
return date;
}
public int getYear() {
if (date == null)
return 0;
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return calendar.get(Calendar.YEAR);
}
// String Representation:
public String toString() {
if (getName() == null)
return super.toString();
else
return getAuthors() + ", " + getName() + ", " +
getPublisher() + " " + getYear();
}
}
|
|