001: /*-
002: * See the file LICENSE for redistribution information.
003: *
004: * Copyright (c) 2002,2008 Oracle. All rights reserved.
005: *
006: * $Id: Supplier.java,v 1.15.2.2 2008/01/07 15:14:02 cwl Exp $
007: */
008:
009: package collections.ship.marshal;
010:
011: import java.io.Serializable;
012:
013: import com.sleepycat.bind.tuple.TupleInput;
014: import com.sleepycat.bind.tuple.TupleOutput;
015:
016: /**
017: * A Supplier represents the combined key/data pair for a supplier entity.
018: *
019: * <p> In this sample, Supplier is bound to the stored key/data entry by
020: * implementing the MarshalledEntity interface, which is called by {@link
021: * SampleViews.MarshalledEntityBinding}. </p>
022: *
023: * <p> The binding is "tricky" in that it uses this class for both the stored
024: * data entry and the combined entity object. To do this, the key field(s) are
025: * transient and are set by the binding after the data object has been
026: * deserialized. This avoids the use of a SupplierData class completely. </p>
027: *
028: * <p> Since this class is used directly for data storage, it must be
029: * Serializable. </p>
030: *
031: * @author Mark Hayes
032: */
033: public class Supplier implements Serializable, MarshalledEntity {
034:
035: static final String CITY_KEY = "city";
036:
037: private transient String number;
038: private String name;
039: private int status;
040: private String city;
041:
042: public Supplier(String number, String name, int status, String city) {
043:
044: this .number = number;
045: this .name = name;
046: this .status = status;
047: this .city = city;
048: }
049:
050: /**
051: * Set the transient key fields after deserializing. This method is only
052: * called by data bindings.
053: */
054: void setKey(String number) {
055:
056: this .number = number;
057: }
058:
059: public final String getNumber() {
060:
061: return number;
062: }
063:
064: public final String getName() {
065:
066: return name;
067: }
068:
069: public final int getStatus() {
070:
071: return status;
072: }
073:
074: public final String getCity() {
075:
076: return city;
077: }
078:
079: public String toString() {
080:
081: return "[Supplier: number=" + number + " name=" + name
082: + " status=" + status + " city=" + city + ']';
083: }
084:
085: // --- MarshalledEntity implementation ---
086:
087: Supplier() {
088:
089: // A no-argument constructor is necessary only to allow the binding to
090: // instantiate objects of this class.
091: }
092:
093: public void unmarshalPrimaryKey(TupleInput keyInput) {
094:
095: this .number = keyInput.readString();
096: }
097:
098: public void marshalPrimaryKey(TupleOutput keyOutput) {
099:
100: keyOutput.writeString(this .number);
101: }
102:
103: public boolean marshalSecondaryKey(String keyName,
104: TupleOutput keyOutput) {
105:
106: if (keyName.equals(CITY_KEY)) {
107: if (this .city != null) {
108: keyOutput.writeString(this .city);
109: return true;
110: } else {
111: return false;
112: }
113: } else {
114: throw new UnsupportedOperationException(keyName);
115: }
116: }
117: }
|