001: package com.completex.objective.persistency.examples.ex003.app;
002:
003: import com.completex.objective.components.persistency.Persistency;
004: import com.completex.objective.components.persistency.OdalPersistencyException;
005: import com.completex.objective.components.persistency.Query;
006: import com.completex.objective.components.persistency.type.TracingArrayListCollectionFactory;
007: import com.completex.objective.components.persistency.type.TracingArrayList;
008: import com.completex.objective.persistency.examples.ex003.domain.Contact;
009: import com.completex.objective.persistency.examples.ex003.domain.Customer;
010: import com.completex.objective.persistency.examples.ex003.domain.CustomerContactRel;
011:
012: import java.util.List;
013: import java.util.Collection;
014:
015: /**
016: * @author Gennady Krizhevsky
017: */
018: public class CustomerDAO {
019:
020: private Persistency persistency;
021:
022: public CustomerDAO(Persistency persistency) {
023: this .persistency = persistency;
024: }
025:
026: public void setPersistency(Persistency persistency) {
027: this .persistency = persistency;
028: }
029:
030: public void insertCustomer(final Customer customer)
031: throws CustomerException {
032: try {
033: persistency.insert(customer);
034: } catch (OdalPersistencyException e) {
035: throw new CustomerException(e);
036: }
037: }
038:
039: public void updateCustomer(final Customer customer)
040: throws CustomerException {
041: try {
042: persistency.update(customer);
043: } catch (OdalPersistencyException e) {
044: throw new CustomerException(e);
045: }
046: }
047:
048: public void updateCustomerDetails(final Customer customer)
049: throws CustomerException {
050: try {
051: List rels = customer.getContactRels();
052: persistency.update(rels);
053: } catch (OdalPersistencyException e) {
054: throw new CustomerException(e);
055: }
056: }
057:
058: public Customer loadCustomer(final Long customerId)
059: throws CustomerException {
060: try {
061: return (Customer) persistency
062: .load(new Customer(customerId));
063: } catch (OdalPersistencyException e) {
064: throw new CustomerException(e);
065: }
066: }
067:
068: public Customer loadCustomerDetails(final Customer customer)
069: throws CustomerException {
070: try {
071: CustomerContactRel rel = new CustomerContactRel();
072: rel.setCustomerId(customer.getCustomerId());
073: Query query = persistency.getQueryFactory()
074: .newQueryByExample(rel);
075: query
076: .setMultipleResultFactory(TracingArrayListCollectionFactory
077: .factory());
078: TracingArrayList contactRels = (TracingArrayList) persistency
079: .select(query);
080: contactRels.setTrace();
081: customer.setContactRels(contactRels);
082: return customer;
083: } catch (OdalPersistencyException e) {
084: throw new CustomerException(e);
085: }
086: }
087:
088: public void insertContact(final Contact contact)
089: throws CustomerException {
090: try {
091: persistency.insert(contact);
092: } catch (OdalPersistencyException e) {
093: throw new CustomerException(e);
094: }
095: }
096:
097: public void updateContact(final Contact contact)
098: throws CustomerException {
099: try {
100: persistency.update(contact);
101: } catch (OdalPersistencyException e) {
102: throw new CustomerException(e);
103: }
104: }
105:
106: public Contact loadContact(final Long contactId)
107: throws CustomerException {
108: try {
109: return (Contact) persistency.load(new Contact(contactId));
110: } catch (OdalPersistencyException e) {
111: throw new CustomerException(e);
112: }
113: }
114:
115: public Contact[] loadCustomerContacts(final Long customerId)
116: throws CustomerException {
117: try {
118: Contact contactQuery = new Contact(customerId);
119: contactQuery.setCustomerId(customerId);
120: Collection collection = persistency.select(contactQuery);
121: return (Contact[]) collection
122: .toArray(new Contact[collection.size()]);
123: } catch (OdalPersistencyException e) {
124: throw new CustomerException(e);
125: }
126: }
127:
128: public void insertCustomerContactRel(final CustomerContactRel rel)
129: throws CustomerException {
130: try {
131: persistency.insert(rel);
132: } catch (OdalPersistencyException e) {
133: throw new CustomerException(e);
134: }
135: }
136:
137: }
|