001: /**
002: * Copyright (C) 2006, 2007 David Bulmore, Software Sensation Inc.
003: * All Rights Reserved.
004: *
005: * This file is part of JPersist.
006: *
007: * JPersist is free software; you can redistribute it and/or modify it under
008: * the terms of the GNU General Public License (Version 2) as published by
009: * the Free Software Foundation.
010: *
011: * JPersist is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with JPersist; if not, write to the Free Software Foundation,
018: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
019: */package jpersist.example;
020:
021: import java.util.Collection;
022: import java.util.Vector;
023: import java.util.logging.Level;
024: import jpersist.DatabaseManager;
025: import jpersist.Entity;
026: import jpersist.PersistentObject;
027: import jpersist.annotations.UpdateNullValues;
028:
029: public class SimpleExample {
030: public SimpleExample(DatabaseManager dbm) {
031: try {
032: // Clean out contacts
033: dbm.executeUpdate("delete from contacts");
034:
035: // Inserting contact with associations
036: Contact contact = new Contact("deisenhower", "mypasswd5",
037: "Dwight", "Eisenhower", "United States",
038: "deisenhower@unitedstates.gov");
039:
040: contact.getSupport().add(
041: new Support("Request", "New", "no phone",
042: "deisenhower@unitedstates.gov",
043: "Can I have my bust on a dollar, please."));
044: contact.getSupport().add(
045: new Support("Response", "Pending", "no phone",
046: "deisenhower@unitedstates.gov",
047: "Yes, but you may have to share it."));
048: contact.getSupport().add(
049: new Support("Request", "New", "no phone",
050: "deisenhower@unitedstates.gov",
051: "Share it with who?"));
052:
053: contact.getOrders().add(
054: new Order("Dwight D. Eisenhower Dollar",
055: new Integer(100), new Double(1.00),
056: "unverified"));
057: contact.getOrders().add(
058: new Order("Susan B. Anthony Dollar", new Integer(
059: 100), new Double(1.00), "unverified"));
060:
061: // Saving within an automatic transaction (covers all relationships)
062: contact.save(dbm);
063:
064: // Load based on information contained in classes
065: contact = dbm.loadObject(Contact.class,
066: "where :contactId = 'deisenhower'");
067: System.out.println("\ncontactId = "
068: + contact.getContactId());
069:
070: // Load a collection of objects from the database
071: Collection<Contact> c = dbm.loadObjects(
072: new Vector<Contact>(), Contact.class);
073:
074: for (Contact contact2 : c)
075: System.out.println("contactId = "
076: + contact2.getContactId());
077: } catch (Exception e) {
078: e.printStackTrace();
079: }
080: }
081:
082: public static void main(String[] args) {
083: DatabaseManager dbm = null;
084:
085: try {
086: try {
087: DatabaseManager.setLogLevel(Level.OFF);
088:
089: dbm = DatabaseManager
090: .getXmlDefinedDatabaseManager("jpersist");
091:
092: new SimpleExample(dbm);
093: } finally {
094: // also closes any open jpersist.Database
095: dbm.close();
096: }
097: } catch (Exception e) {
098: e.printStackTrace();
099: }
100: }
101:
102: @UpdateNullValues
103: public static class Contact extends PersistentObject {
104: private String contactId, password, firstName, lastName,
105: companyName, email;
106: private Vector<Support> support = new Vector<Support>();
107: private Vector<Order> orders = new Vector<Order>();
108:
109: public Contact() {
110: }
111:
112: public Contact(String contactId) {
113: this .contactId = contactId;
114: }
115:
116: public Contact(String contactId, String password,
117: String firstName, String lastName, String companyName,
118: String email) {
119: this .contactId = contactId;
120: this .password = password;
121: this .firstName = firstName;
122: this .lastName = lastName;
123: this .companyName = companyName;
124: this .email = email;
125: }
126:
127: public String getContactId() {
128: return contactId;
129: }
130:
131: public void setContactId(String id) {
132: contactId = id;
133: }
134:
135: public String getPassword() {
136: return password;
137: }
138:
139: public void setPassword(String passwd) {
140: password = passwd;
141: }
142:
143: public String getFirstName() {
144: return firstName;
145: }
146:
147: public void setFirstName(String fName) {
148: firstName = fName;
149: }
150:
151: public String getLastName() {
152: return lastName;
153: }
154:
155: public void setLastName(String lName) {
156: lastName = lName;
157: }
158:
159: public String getCompanyName() {
160: return companyName;
161: }
162:
163: public void setCompanyName(String name) {
164: companyName = name;
165: }
166:
167: public String getEmail() {
168: return email;
169: }
170:
171: public void setEmail(String email) {
172: this .email = email;
173: }
174:
175: // Associations
176: public Vector<Support> getDbAssociation(Support c) {
177: return support;
178: }
179:
180: public void setDbAssociation(Support c, Vector<Support> s) {
181: support = s;
182: }
183:
184: public Vector<Order> getDbAssociation(Order c) {
185: return orders;
186: }
187:
188: public void setDbAssociation(Order c, Vector<Order> o) {
189: orders = o;
190: }
191:
192: // association convenience (is optional)
193: public Vector<Support> getSupport() {
194: return support;
195: }
196:
197: public void setSupport(Vector<Support> support) {
198: this .support = support;
199: }
200:
201: public Vector<Order> getOrders() {
202: return orders;
203: }
204:
205: public void setOrders(Vector<Order> orders) {
206: this .orders = orders;
207: }
208:
209: public String toString() {
210: String returnString = contactId + ", " + firstName + ", "
211: + lastName + ", " + companyName + ", " + email
212: + "\n";
213:
214: if (support != null)
215: for (Support s : support)
216: returnString += s + "\n";
217:
218: if (orders != null)
219: for (Order o : orders)
220: returnString += o + "\n";
221:
222: return returnString;
223: }
224: }
225:
226: public static class Order extends Entity // can optionally extend Entity for esthetics
227: {
228: private Long orderId;
229: private Integer quantity;
230: private Double price;
231: private String contactId, product, status;
232:
233: public Order() {
234: }
235:
236: public Order(String product, Integer quantity, Double price,
237: String status) {
238: this .product = product;
239: this .quantity = quantity;
240: this .price = price;
241: this .status = status;
242: }
243:
244: public Order(String contactId, String product,
245: Integer quantity, Double price, String status) {
246: this .contactId = contactId;
247: this .product = product;
248: this .quantity = quantity;
249: this .price = price;
250: this .status = status;
251: }
252:
253: public Long getOrderId() {
254: return orderId;
255: }
256:
257: public void setOrderId(Long orderId) {
258: this .orderId = orderId;
259: }
260:
261: public String getContactId() {
262: return contactId;
263: }
264:
265: public void setContactId(String contactId) {
266: this .contactId = contactId;
267: }
268:
269: public String getProduct() {
270: return product;
271: }
272:
273: public void setProduct(String product) {
274: this .product = product;
275: }
276:
277: public Integer getQuantity() {
278: return quantity;
279: }
280:
281: public void setQuantity(Integer quantity) {
282: this .quantity = quantity;
283: }
284:
285: public Double getPrice() {
286: return price;
287: }
288:
289: public void setPrice(Double price) {
290: this .price = price;
291: }
292:
293: public String getStatus() {
294: return status;
295: }
296:
297: public void setStatus(String status) {
298: this .status = status;
299: }
300:
301: public String toString() {
302: return orderId + ", " + contactId + ", " + quantity + ", "
303: + price + ", " + product + ", " + status;
304: }
305: }
306:
307: public static class Support extends PersistentObject {
308: private Long supportId;
309: private String contactId, code, status, phone, email, request;
310:
311: public Support() {
312: }
313:
314: public Support(String code, String status, String phone,
315: String email, String request) {
316: this .code = code;
317: this .status = status;
318: this .phone = phone;
319: this .email = email;
320: this .request = request;
321: }
322:
323: public Support(String contactId, String code, String status,
324: String phone, String email, String request) {
325: this .contactId = contactId;
326: this .code = code;
327: this .status = status;
328: this .phone = phone;
329: this .email = email;
330: this .request = request;
331: }
332:
333: public Long getSupportId() {
334: return supportId;
335: }
336:
337: public void setSupportId(Long id) {
338: supportId = id;
339: }
340:
341: public String getContactId() {
342: return contactId;
343: }
344:
345: public void setContactId(String id) {
346: contactId = id;
347: }
348:
349: public String getCode() {
350: return code;
351: }
352:
353: public void setCode(String code) {
354: this .code = code;
355: }
356:
357: public String getStatus() {
358: return status;
359: }
360:
361: public void setStatus(String status) {
362: this .status = status;
363: }
364:
365: public String getPhone() {
366: return phone;
367: }
368:
369: public void setPhone(String phone) {
370: this .phone = phone;
371: }
372:
373: public String getEmail() {
374: return email;
375: }
376:
377: public void setEmail(String email) {
378: this .email = email;
379: }
380:
381: public String getRequest() {
382: return request;
383: }
384:
385: public void setRequest(String request) {
386: this .request = request;
387: }
388:
389: public String toString() {
390: return supportId + ", " + contactId + ", " + code + ","
391: + status + ", " + phone + ", " + email + ", "
392: + request;
393: }
394: }
395: }
|