1 package ejava.examples.txagent.bo;
2
3 import java.io.ByteArrayInputStream;
4
5 import java.io.ByteArrayOutputStream;
6 import java.io.IOException;
7 import java.io.ObjectInputStream;
8 import java.io.ObjectOutputStream;
9 import java.io.Serializable;
10 import java.util.Collections;
11 import java.util.HashSet;
12 import java.util.Set;
13
14 import ejava.examples.txhotel.bo.Reservation;
15
16 @SuppressWarnings("serial")
17 public class Booking implements Serializable {
18 private long id;
19 private long version;
20 private String confirmation;
21 private Set<String> hotelConfirmations = new HashSet<String>();
22 private Set<Reservation> hotelReservations = new HashSet<Reservation>();
23
24 public Booking() {}
25 public Booking(
26 long id, long version, String confirmation) {
27 setId(id);
28 setVersion(version);
29 setConfirmation(confirmation);
30 }
31
32 public long getId() {
33 return id;
34 }
35 private void setId(long id) {
36 this.id = id;
37 }
38 public String getConfirmation() {
39 return confirmation;
40 }
41 public void setConfirmation(String confirmation) {
42 this.confirmation = confirmation;
43 }
44 public long getVersion() {
45 return version;
46 }
47 public void setVersion(long version) {
48 this.version = version;
49 }
50 public Set<String> getHotelConfirmations() {
51 return Collections.unmodifiableSet(hotelConfirmations);
52 }
53 @SuppressWarnings("unused")
54 private void setHotelConfirmations(Set<String> hotelConfirmations) {
55 this.hotelConfirmations = hotelConfirmations;
56 }
57 public Set<Reservation> getHotelReservations() {
58 return Collections.unmodifiableSet(hotelReservations);
59 }
60 @SuppressWarnings("unused")
61 private void setHotelReservations(Set<Reservation> hotelReservations) {
62 this.hotelReservations = hotelReservations;
63 }
64 public void addHotelReservation(Reservation reservation) {
65 if (!hotelConfirmations.contains(reservation.getConfirmation())) {
66 this.hotelConfirmations.add(reservation.getConfirmation());
67 this.hotelReservations.add(reservation);
68 }
69 }
70
71 @SuppressWarnings("unused")
72 private byte[] getHotelConfirmationsAsBytes() throws IOException {
73 ByteArrayOutputStream bos = new ByteArrayOutputStream();
74 ObjectOutputStream oos = new ObjectOutputStream(bos);
75 oos.writeObject(hotelConfirmations);
76 return bos.toByteArray();
77 }
78 @SuppressWarnings({ "unchecked", "unused" })
79 private void setHotelConfirmationsAsBytes(byte data[])
80 throws IOException, ClassNotFoundException {
81 ByteArrayInputStream bis = new ByteArrayInputStream(data);
82 ObjectInputStream ois = new ObjectInputStream(bis);
83 hotelConfirmations = (Set<String>)ois.readObject();
84 }
85
86
87 @Override
88 public boolean equals(Object obj) {
89 if (this==obj) return true;
90 if (obj!= null && obj instanceof Booking) {
91 return confirmation.equals(((Booking)obj).confirmation);
92 }
93 return false;
94 }
95
96 @Override
97 public int hashCode() {
98 return confirmation.hashCode();
99 }
100
101 public String toString() {
102 StringBuilder text = new StringBuilder();
103 text.append("id=" + id);
104 text.append(", version=" + version);
105 text.append(", conf#" + confirmation);
106 text.append(", hotel confirmations=" + hotelConfirmations);
107 text.append(", hotel reservations=" + hotelReservations);
108 return text.toString();
109 }
110 }