001    package data.ooimpl;
002    
003    import data.*;
004    
005    import log.*;
006    
007    import java.io.Serializable;
008    
009    /**
010     * Basic simple implementation of the DataBasketEntry interface.
011     * DataBasketEntries may be coalesced, meaning that two separate operations may
012     * be expressed by one DataBasketEntry, if this is possible. Applications that
013     * inspect DataBasketEntries directly should not make assumptions about whether
014     * or not they have been coalesced.
015     *
016     * @author Steffen Zschaler
017     * @version 14/06/1999
018     * @since v2.0
019     */
020    public class DataBasketEntryImpl extends Object implements DataBasketEntry, Loggable, Serializable {
021    
022        /**
023             * ID for serialization.
024             */
025            private static final long serialVersionUID = -7555549735745558670L;
026    
027            /**
028         * The source of the operation.
029         *
030         * @serial
031         */
032        protected DataBasketEntrySource m_dbesSource;
033    
034        /**
035         * The destination of the operation.
036         *
037         * @serial
038         */
039        protected DataBasketEntryDestination m_dbedDest;
040    
041        /**
042         * The object moved by the operation.
043         *
044         * @serial
045         */
046        protected Object m_oValue;
047    
048        /**
049         * The entry's main key.
050         *
051         * @serial
052         */
053        protected String m_sMainKey;
054    
055        /**
056         * The entry's secondary key.
057         *
058         * @serial
059         */
060        protected String m_sSecondaryKey;
061    
062        /**
063         * true if the entry was commited or rolled back completely.
064         *
065         * @serial
066         */
067        protected boolean m_fHandled = false;
068    
069        /**
070         * The DataBasket owning this entry..
071         *
072         * @serial
073         */
074        protected DataBasketImpl m_dbiOwner;
075    
076        /**
077         * Create a new DataBasketEntryImpl.
078         *
079         * @param sMainKey the entry's main key.
080         * @param sSecondaryKey the entry's secondary key.
081         * @param dbesSource the operation's source. Assumed to be a {@link SelfManagingDBESource}.
082         * @param dbedDest the operation's destination.  Assumed to be a {@link SelfManagingDBEDestination}.
083         * @param oValue the object moved by the operation.
084         */
085        public DataBasketEntryImpl(String sMainKey, String sSecondaryKey, DataBasketEntrySource dbesSource,
086                DataBasketEntryDestination dbedDest, Object oValue) {
087            super();
088    
089            m_sMainKey = sMainKey;
090            m_sSecondaryKey = sSecondaryKey;
091            m_dbesSource = dbesSource;
092            m_dbedDest = dbedDest;
093            m_oValue = oValue;
094        }
095    
096        /**
097         * Rollback the operation. This is done by first calling {@link SelfManagingDBEDestination#rollbackAdd} in
098         * the destination and then {@link SelfManagingDBESource#rollbackRemove} in the source of the entry.
099         *
100         * @override Never
101         */
102        public void rollback() {
103            if (!isHandled()) {
104                m_fHandled = true;
105    
106                m_dbiOwner.log(DataBasketImpl.ROLLBACK_ACTION, this);
107    
108                SelfManagingDBEDestination smdbedDest = (SelfManagingDBEDestination)getDestination();
109                if (smdbedDest != null) {
110                    smdbedDest.rollbackAdd(m_dbiOwner, this);
111                }
112    
113                SelfManagingDBESource smdbesSource = (SelfManagingDBESource)getSource();
114                if (smdbesSource != null) {
115                    smdbesSource.rollbackRemove(m_dbiOwner, this);
116                }
117            }
118        }
119    
120        /**
121         * Commit the operation. This is done by first calling {@link SelfManagingDBESource#commitRemove} in the
122         * source and then {@link SelfManagingDBEDestination#commitAdd} in the destination of the entry.
123         *
124         * @override Never
125         */
126        public void commit() {
127            if (!isHandled()) {
128                m_fHandled = true;
129    
130                m_dbiOwner.log(DataBasketImpl.COMMIT_ACTION, this);
131    
132                SelfManagingDBESource smdbesSource = (SelfManagingDBESource)getSource();
133                if (smdbesSource != null) {
134                    smdbesSource.commitRemove(m_dbiOwner, this);
135                }
136    
137                SelfManagingDBEDestination smdbedDest = (SelfManagingDBEDestination)getDestination();
138                if (smdbedDest != null) {
139                    smdbedDest.commitAdd(m_dbiOwner, this);
140                }
141            }
142        }
143    
144        /**
145         * Set the DataBasket owning this DataBasketEntry.
146         *
147         * <p>This method is public as an implementation detail and must not be called directly.</p>
148         *
149         * @override Never
150         */
151        public void setOwner(DataBasket dbOwner) {
152            m_dbiOwner = (DataBasketImpl)dbOwner;
153        }
154    
155        /**
156         * Return the DataBasket containing this DataBasketEntry. Initially <code>null</code>. Once
157         * {@link #setOwner} has been called, always returns the last value of the <code>dbOwner</code>
158         * parameter passed to <code>setOwner</code>.
159         *
160         * @return the DataBasket containing this DataBasketEntry.
161         */
162        public DataBasket getOwner() {
163            return m_dbiOwner;
164        }
165    
166        /**
167         * Get the entry's source.
168         *
169         * @override Never
170         */
171        public DataBasketEntrySource getSource() {
172            return m_dbesSource;
173        }
174    
175        /**
176         * Get the entry's destination.
177         *
178         * @override Never
179         */
180        public DataBasketEntryDestination getDestination() {
181            return m_dbedDest;
182        }
183    
184        /**
185         * Get the entry's value, i.e.&nbsp;the object that was moved by the operation.
186         *
187         * @override Never
188         */
189        public Object getValue() {
190            return m_oValue;
191        }
192    
193        /**
194         * Get the entry's main key.
195         *
196         * @override Never
197         */
198        public String getMainKey() {
199            return m_sMainKey;
200        }
201    
202        /**
203         * Get the entry's secondary key.
204         *
205         * @override Never
206         */
207        public String getSecondaryKey() {
208            return m_sSecondaryKey;
209        }
210    
211        /**
212         * Return true, if the entry has been either completely commited or rolled back.
213         *
214         * @override Never
215         */
216        public boolean isHandled() {
217            return m_fHandled;
218        }
219    
220        // Loggable interface method
221        /**
222         * Return a LogEntry that describes this DataBasketEntry.
223         *
224         * @override Always The default implementation is generic and therefore usually not optimal for a subclass
225         * implementation.
226         */
227        public LogEntry getLogData() {
228            final String sMainKey = getMainKey();
229            final String sSecondaryKey = getSecondaryKey();
230            final String sSource = "" + getSource();
231            final String sDest = "" + getDestination();
232            final String sValue = "" + getValue();
233    
234            return new LogEntry() {
235    
236                    private static final long serialVersionUID = 338378085223507454L;
237    
238                            public String toString() {
239                    return "DataBasketEntry: \"" + sMainKey + "\"/\"" + sSecondaryKey + "\": " + "\"" + sValue +
240                            "\" (\"" + sSource + "\" -> \"" + sDest + "\")";
241                }
242            };
243        }
244    }