You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@openjpa.apache.org by pp...@apache.org on 2010/05/26 23:43:55 UTC
svn commit: r948601 - in
/openjpa/trunk/openjpa-examples/openbooks/src/main/java/openbook: client/
domain/ server/ tools/converter/ util/
Author: ppoddar
Date: Wed May 26 21:43:54 2010
New Revision: 948601
URL: http://svn.apache.org/viewvc?rev=948601&view=rev
Log:
Add comment anchor for more directed code browsing
Modified:
openjpa/trunk/openjpa-examples/openbooks/src/main/java/openbook/client/BuyBookPage.java
openjpa/trunk/openjpa-examples/openbooks/src/main/java/openbook/domain/Book.java
openjpa/trunk/openjpa-examples/openbooks/src/main/java/openbook/domain/LineItem.java
openjpa/trunk/openjpa-examples/openbooks/src/main/java/openbook/domain/PurchaseOrder.java
openjpa/trunk/openjpa-examples/openbooks/src/main/java/openbook/domain/ShoppingCart.java
openjpa/trunk/openjpa-examples/openbooks/src/main/java/openbook/server/OpenBookServiceImpl.java
openjpa/trunk/openjpa-examples/openbooks/src/main/java/openbook/tools/converter/HTMLTokenRenderer.java
openjpa/trunk/openjpa-examples/openbooks/src/main/java/openbook/tools/converter/ParseTokenListener.java
openjpa/trunk/openjpa-examples/openbooks/src/main/java/openbook/tools/converter/PlainTokenRenderer.java
openjpa/trunk/openjpa-examples/openbooks/src/main/java/openbook/util/JSPUtility.java
Modified: openjpa/trunk/openjpa-examples/openbooks/src/main/java/openbook/client/BuyBookPage.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-examples/openbooks/src/main/java/openbook/client/BuyBookPage.java?rev=948601&r1=948600&r2=948601&view=diff
==============================================================================
--- openjpa/trunk/openjpa-examples/openbooks/src/main/java/openbook/client/BuyBookPage.java (original)
+++ openjpa/trunk/openjpa-examples/openbooks/src/main/java/openbook/client/BuyBookPage.java Wed May 26 21:43:54 2010
@@ -182,7 +182,7 @@ public final class BuyBookPage extends J
JButton searchButton = new JButton("Search", Images.START);
searchButton.setHorizontalTextPosition(SwingConstants.LEADING);
ShowCodeAction showCode = Demo.getInstance().new ShowCodeAction();
- showCode.setPage("Derived identity", "openbook/domain/LineItem.java.html#example.compound-derived-identity");
+ showCode.setPage("Dynamic Query", "openbook/server/OpenBookServiceImpl.java.html#buildQuery");
JButton viewCodeButton = new JButton(showCode);
JPanel buttonPanel = new JPanel();
Modified: openjpa/trunk/openjpa-examples/openbooks/src/main/java/openbook/domain/Book.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-examples/openbooks/src/main/java/openbook/domain/Book.java?rev=948601&r1=948600&r2=948601&view=diff
==============================================================================
--- openjpa/trunk/openjpa-examples/openbooks/src/main/java/openbook/domain/Book.java (original)
+++ openjpa/trunk/openjpa-examples/openbooks/src/main/java/openbook/domain/Book.java Wed May 26 21:43:54 2010
@@ -29,6 +29,7 @@ import javax.persistence.OneToOne;
import javax.persistence.Version;
/**
+ * <A name="class"/>
* An immutable persistent entity represents a Book.
* <br>
* The mutable properties of the book such as number of items in stock etc.
@@ -62,6 +63,11 @@ public class Book implements Serializabl
orphanRemoval=true)
private Inventory inventory;
+ /**
+ * <A name="authors">
+ * A many-to-many <em>eager</em> relation.
+ * By default, many-to-many relations are lazily fetched.
+ */
@ManyToMany(fetch=FetchType.EAGER)
private List<Author> authors;
Modified: openjpa/trunk/openjpa-examples/openbooks/src/main/java/openbook/domain/LineItem.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-examples/openbooks/src/main/java/openbook/domain/LineItem.java?rev=948601&r1=948600&r2=948601&view=diff
==============================================================================
--- openjpa/trunk/openjpa-examples/openbooks/src/main/java/openbook/domain/LineItem.java (original)
+++ openjpa/trunk/openjpa-examples/openbooks/src/main/java/openbook/domain/LineItem.java Wed May 26 21:43:54 2010
@@ -39,7 +39,7 @@ import javax.persistence.OrderColumn;
@IdClass(LineItem.LineItemId.class)
public class LineItem implements Serializable {
/**
- * <A name="example.compound-derived-identity">
+ * <A name="order">
* An example of a compound derived identity.
*/
@Id
Modified: openjpa/trunk/openjpa-examples/openbooks/src/main/java/openbook/domain/PurchaseOrder.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-examples/openbooks/src/main/java/openbook/domain/PurchaseOrder.java?rev=948601&r1=948600&r2=948601&view=diff
==============================================================================
--- openjpa/trunk/openjpa-examples/openbooks/src/main/java/openbook/domain/PurchaseOrder.java (original)
+++ openjpa/trunk/openjpa-examples/openbooks/src/main/java/openbook/domain/PurchaseOrder.java Wed May 26 21:43:54 2010
@@ -65,6 +65,10 @@ public class PurchaseOrder implements Se
*/
public enum Status {PENDING, DELIVERED};
+ /**
+ * <A name="id">
+ * Persistent Identity is generated by the provider.
+ */
@Id
@GeneratedValue
private long id;
@@ -72,6 +76,15 @@ public class PurchaseOrder implements Se
@OneToOne(optional=false)
private Customer customer;
+ /**
+ * <A name="items">
+ * A composite relation.
+ * All persistence operation on this order are cascaded to its line items.
+ * Moreover, a line item ceases to exist even in the database if it is no
+ * more referred to by a order. This is known as <em>orphan delete</em>
+ * and can be termed as <em>persistent garbage collection</em>.
+ *
+ */
@OneToMany(mappedBy="order", fetch=FetchType.EAGER, cascade=CascadeType.ALL, orphanRemoval=true)
private List<LineItem> items;
@@ -98,6 +111,7 @@ public class PurchaseOrder implements Se
}
/**
+ * <A name="init"/>
* Construct a new order by transferring the content of the given {@linkplain ShoppingCart}.
*
* @param cart a non-null, non-empty Shopping cart
@@ -141,7 +155,12 @@ public class PurchaseOrder implements Se
return status;
}
+ public boolean isDelivered() {
+ return Status.DELIVERED.equals(status);
+ }
+
/**
+ * <A name="setDelivered"/>
* Sets the status of this Purchase Order as delivered.
* Setting an order status as delivered nullifies the association to Line Items.
* Nullifying this association has the important side-effect of Line Item records
Modified: openjpa/trunk/openjpa-examples/openbooks/src/main/java/openbook/domain/ShoppingCart.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-examples/openbooks/src/main/java/openbook/domain/ShoppingCart.java?rev=948601&r1=948600&r2=948601&view=diff
==============================================================================
--- openjpa/trunk/openjpa-examples/openbooks/src/main/java/openbook/domain/ShoppingCart.java (original)
+++ openjpa/trunk/openjpa-examples/openbooks/src/main/java/openbook/domain/ShoppingCart.java Wed May 26 21:43:54 2010
@@ -21,6 +21,7 @@ import java.util.HashMap;
import java.util.Map;
/**
+ * <A name="non-persistent"/>
* A non-persistent entity holds the content of a shopping session for a {@linkplain Customer}.
* Used to create a persistent PurchaseOrder.
* Books can be added or removed from this cart.
@@ -30,7 +31,15 @@ import java.util.Map;
*/
@SuppressWarnings("serial")
public class ShoppingCart implements Serializable {
+ /**
+ * The owner of this cart.
+ */
private Customer customer;
+
+ /**
+ * <A name="items"/>
+ * The items in the cart and their respective quantity.
+ */
private Map<Book, Integer> items;
/**
Modified: openjpa/trunk/openjpa-examples/openbooks/src/main/java/openbook/server/OpenBookServiceImpl.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-examples/openbooks/src/main/java/openbook/server/OpenBookServiceImpl.java?rev=948601&r1=948600&r2=948601&view=diff
==============================================================================
--- openjpa/trunk/openjpa-examples/openbooks/src/main/java/openbook/server/OpenBookServiceImpl.java (original)
+++ openjpa/trunk/openjpa-examples/openbooks/src/main/java/openbook/server/OpenBookServiceImpl.java Wed May 26 21:43:54 2010
@@ -158,10 +158,13 @@ class OpenBookServiceImpl extends Persis
}
/**
- * Provide a name and email to login a Customer.
+ * <A name="login">
+ * Provide a name to login a Customer.
* If such a customer exists, return it. Otherwise creates a new one.
- * @param name
- * @return
+ *
+ * @param name name of an existing or a new Customer
+ *
+ * @return a Customer
*/
public Customer login(String name) {
EntityManager em = begin();
@@ -206,12 +209,20 @@ class OpenBookServiceImpl extends Persis
return result;
}
+ /**
+ * <A name="getQuery">
+ * Gets the string representation of a Criteria Query.
+ * The string form of a Criteria Query is not specified in JPA specification.
+ * But OpenJPA produces a readable form that is quite <em>similar</em> to
+ * equivalent JPQL.
+ */
public String getQuery(String title, Double minPrice, Double maxPrice, String author) {
CriteriaQuery<Book> q = buildQuery(title, minPrice, maxPrice, author);
return q.toString();
}
/**
+ * <A name="buildQuery">
* Creates a Query based on the values of the user input form.
* The user may or may not have filled a value for each form field
* and accordingly the query will be different.<br>
@@ -222,21 +233,31 @@ class OpenBookServiceImpl extends Persis
* introduced in JPA version 2.0.
* <br>
*
- *
* @return a typed query
*/
- private CriteriaQuery<Book> buildQuery(String title,
- Double minPrice, Double maxPrice,
- String author) {
+ private CriteriaQuery<Book> buildQuery(String title, Double minPrice, Double maxPrice, String author) {
+ // builder generates the Criteria Query as well as all the expressions
CriteriaBuilder cb = getUnit().getCriteriaBuilder();
+ // The query declares what type of result it will produce
CriteriaQuery<Book> q = cb.createQuery(Book.class);
+ // Which type will be searched
Root<Book> book = q.from(Book.class);
+ // of course, the projection term must match the result type declared earlier
+ q.select(book);
+
+ // Builds the predicates conditionally for the filled-in input fields
List<Predicate> predicates = new ArrayList<Predicate>();
- if (title != null && title.trim().length() > 0) {
+ if (!isEmpty(title)) {
Predicate matchTitle = cb.like(book.get(Book_.title), title);
predicates.add(matchTitle);
}
+ if (!isEmpty(author)) {
+ Predicate matchAuthor = cb.like(book.join(Book_.authors).get(Author_.name), "%"+author+"%");
+ predicates.add(matchAuthor);
+ }
+ // for price fields, also the comparison operation changes based on whether
+ // minimum or maximum price or both have been filled.
if (minPrice != null && maxPrice != null) {
Predicate matchPrice = cb.between(book.get(Book_.price), minPrice, maxPrice);
predicates.add(matchPrice);
@@ -247,26 +268,30 @@ class OpenBookServiceImpl extends Persis
Predicate matchPrice = cb.le(book.get(Book_.price), maxPrice);
predicates.add(matchPrice);
}
- if (author != null && author.trim().length() > 0) {
- Predicate matchAuthor = cb.like(book.join(Book_.authors).get(Author_.name), "%"+author+"%");
- predicates.add(matchAuthor);
- }
-
- q.select(book);
+ // Sets the evaluation criteria
if (!predicates.isEmpty())
q.where(predicates.toArray(new Predicate[predicates.size()]));
return q;
}
+
+ boolean isEmpty(String s) {
+ return s == null || s.trim().isEmpty();
+ }
/**
- * Deliver pending orders.
- * Queries for pending PurchaseOrders and attempts to deliver each in a separate
- * transaction. Some of the transactions may fail because of concurrent modification
- * on the inventory by the supplier.
+ * <A name="deliver"/>
+ * Delivers the given order, if it is pending.
+ * Delivery of an order amounts to decrementing inventory for each line item
+ * and eventually nullify the line items to demonstrate orphan delete feature.
+ * <br>
+ * The transactions may fail because of either insufficient inventory or
+ * concurrent modification of the same inventory by {@link #supply(Book, int) the supplier}.
*/
public PurchaseOrder deliver(PurchaseOrder o) {
+ if (o.isDelivered())
+ return o;
EntityManager em = begin();
o = em.merge(o);
for (LineItem item : o.getItems()) {
@@ -301,6 +326,7 @@ class OpenBookServiceImpl extends Persis
}
/**
+ * <A name="placeOrder"/>
* Creates a new {@linkplain PurchaseOrder} from the content of the given {@linkplain ShoppingCart}.
* The content of the cart is cleared as a result.
* <br>
Modified: openjpa/trunk/openjpa-examples/openbooks/src/main/java/openbook/tools/converter/HTMLTokenRenderer.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-examples/openbooks/src/main/java/openbook/tools/converter/HTMLTokenRenderer.java?rev=948601&r1=948600&r2=948601&view=diff
==============================================================================
--- openjpa/trunk/openjpa-examples/openbooks/src/main/java/openbook/tools/converter/HTMLTokenRenderer.java (original)
+++ openjpa/trunk/openjpa-examples/openbooks/src/main/java/openbook/tools/converter/HTMLTokenRenderer.java Wed May 26 21:43:54 2010
@@ -91,7 +91,7 @@ public class HTMLTokenRenderer implement
* Gets a end-of-line string: a HTML <br> tag followed by carriage return and line feed.
*/
public String endLine(int line) {
- return addLineBreak ? HTML_BR_TAG : HTML_BR_TAG + NEW_LINE;
+ return addLineBreak ? HTML_BR_TAG + NEW_LINE : NEW_LINE;
}
/**
Modified: openjpa/trunk/openjpa-examples/openbooks/src/main/java/openbook/tools/converter/ParseTokenListener.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-examples/openbooks/src/main/java/openbook/tools/converter/ParseTokenListener.java?rev=948601&r1=948600&r2=948601&view=diff
==============================================================================
--- openjpa/trunk/openjpa-examples/openbooks/src/main/java/openbook/tools/converter/ParseTokenListener.java (original)
+++ openjpa/trunk/openjpa-examples/openbooks/src/main/java/openbook/tools/converter/ParseTokenListener.java Wed May 26 21:43:54 2010
@@ -105,13 +105,10 @@ public class ParseTokenListener extends
public void consumeHiddenToken(Token token) {
if (this.backtracking > 0 && currentLine != 0) return;
int type = token.getType();
- if (type == JavaParser.COMMENT) {
-// LineBreakIterator lines = new LineBreakIterator(token.getText());
+ if (type == JavaParser.COMMENT || type == JavaParser.LINE_COMMENT) {
StringTokenizer linebreaker = new StringTokenizer(token.getText(), "\r\n", false);
int i = 0;
-// for (String line : lines) {
while (linebreaker.hasMoreTokens()) {
-// Token dummy = new CommonToken(JavaParser.COMMENT, line);
Token dummy = new CommonToken(JavaParser.COMMENT, linebreaker.nextToken());
changeLine(token.getLine() + i);
_stream.print(_renderer.render(decision, dummy));
Modified: openjpa/trunk/openjpa-examples/openbooks/src/main/java/openbook/tools/converter/PlainTokenRenderer.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-examples/openbooks/src/main/java/openbook/tools/converter/PlainTokenRenderer.java?rev=948601&r1=948600&r2=948601&view=diff
==============================================================================
--- openjpa/trunk/openjpa-examples/openbooks/src/main/java/openbook/tools/converter/PlainTokenRenderer.java (original)
+++ openjpa/trunk/openjpa-examples/openbooks/src/main/java/openbook/tools/converter/PlainTokenRenderer.java Wed May 26 21:43:54 2010
@@ -28,8 +28,8 @@ import org.antlr.runtime.Token;
*/
public class PlainTokenRenderer implements TokenRenderer {
private static final String EMPTY = "";
- private boolean showLineNumber;
- private String lineNumberFormat = "%04d :";
+ private boolean showLineNumber = true;
+ private String lineNumberFormat = "%04d";
public String endLine(int line) {
return EMPTY;
Modified: openjpa/trunk/openjpa-examples/openbooks/src/main/java/openbook/util/JSPUtility.java
URL: http://svn.apache.org/viewvc/openjpa/trunk/openjpa-examples/openbooks/src/main/java/openbook/util/JSPUtility.java?rev=948601&r1=948600&r2=948601&view=diff
==============================================================================
--- openjpa/trunk/openjpa-examples/openbooks/src/main/java/openbook/util/JSPUtility.java (original)
+++ openjpa/trunk/openjpa-examples/openbooks/src/main/java/openbook/util/JSPUtility.java Wed May 26 21:43:54 2010
@@ -33,11 +33,12 @@ import java.util.Date;
*
*/
public class JSPUtility {
+
/**
* Converts the given number in currency format.
*/
public static final DecimalFormat currencyFormatter = new DecimalFormat("###.##");
- public static final DateFormat dateFormatter = new SimpleDateFormat("MMM dd yyyy HH:mm");
+ public static final DateFormat dateFormatter = new SimpleDateFormat("MMM dd, HH:mm");
public static String format(Number price) {
return currencyFormatter.format(price);
@@ -60,15 +61,6 @@ public class JSPUtility {
}
/**
- * Background color of table rows.
- */
- public static String EVEN_ROW_COLOR = "#E3E4FA";
- public static String ODD_ROW_COLOR = "#E0FFFF";
- public static String getRowStyle(int row) {
- return "background-color:" + ((row % 2 == 0) ? EVEN_ROW_COLOR : ODD_ROW_COLOR);
- }
-
- /**
* Encodes parameter key-values in a URL.
*
* @param page the base page
@@ -104,4 +96,9 @@ public class JSPUtility {
return s;
}
}
+
+ public static final String SRC_ROOT = "generated-html";
+ public static String getURL(String className, String anchor) {
+ return SRC_ROOT + "/" + className.replace('.', '/') + (anchor != null ? "#"+anchor : "");
+ }
}