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<T> extends Object implements DataBasketEntry<T>, 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 T 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, T 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        @SuppressWarnings("unchecked")
103            public void rollback() {
104            if (!isHandled()) {
105                m_fHandled = true;
106    
107                m_dbiOwner.log(DataBasketImpl.ROLLBACK_ACTION, this);
108    
109                SelfManagingDBEDestination<T> smdbedDest = (SelfManagingDBEDestination<T>)getDestination();
110                if (smdbedDest != null) {
111                    smdbedDest.rollbackAdd(m_dbiOwner, this);
112                }
113    
114                SelfManagingDBESource<T> smdbesSource = (SelfManagingDBESource<T>)getSource();
115                if (smdbesSource != null) {
116                    smdbesSource.rollbackRemove(m_dbiOwner, this);
117                }
118            }
119        }
120    
121        /**
122         * Commit the operation. This is done by first calling {@link SelfManagingDBESource#commitRemove} in the
123         * source and then {@link SelfManagingDBEDestination#commitAdd} in the destination of the entry.
124         *
125         * @override Never
126         */
127        @SuppressWarnings("unchecked")
128            public void commit() {
129            if (!isHandled()) {
130                m_fHandled = true;
131    
132                m_dbiOwner.log(DataBasketImpl.COMMIT_ACTION, this);
133    
134                SelfManagingDBESource<T> smdbesSource = (SelfManagingDBESource<T>)getSource();
135                if (smdbesSource != null) {
136                    smdbesSource.commitRemove(m_dbiOwner, this);
137                }
138    
139                SelfManagingDBEDestination<T> smdbedDest = (SelfManagingDBEDestination<T>)getDestination();
140                if (smdbedDest != null) {
141                    smdbedDest.commitAdd(m_dbiOwner, this);
142                }
143            }
144        }
145    
146        /**
147         * Set the DataBasket owning this DataBasketEntry.
148         *
149         * <p>This method is public as an implementation detail and must not be called directly.</p>
150         *
151         * @override Never
152         */
153        public void setOwner(DataBasket dbOwner) {
154            m_dbiOwner = (DataBasketImpl)dbOwner;
155        }
156    
157        /**
158         * Return the DataBasket containing this DataBasketEntry. Initially <code>null</code>. Once
159         * {@link #setOwner} has been called, always returns the last value of the <code>dbOwner</code>
160         * parameter passed to <code>setOwner</code>.
161         *
162         * @return the DataBasket containing this DataBasketEntry.
163         */
164        public DataBasket getOwner() {
165            return m_dbiOwner;
166        }
167    
168        /**
169         * Get the entry's source.
170         *
171         * @override Never
172         */
173        public DataBasketEntrySource getSource() {
174            return m_dbesSource;
175        }
176    
177        /**
178         * Get the entry's destination.
179         *
180         * @override Never
181         */
182        public DataBasketEntryDestination getDestination() {
183            return m_dbedDest;
184        }
185    
186        /**
187         * Get the entry's value, i.e.&nbsp;the object that was moved by the operation.
188         *
189         * @override Never
190         */
191        public T getValue() {
192            return m_oValue;
193        }
194    
195        /**
196         * Get the entry's main key.
197         *
198         * @override Never
199         */
200        public String getMainKey() {
201            return m_sMainKey;
202        }
203    
204        /**
205         * Get the entry's secondary key.
206         *
207         * @override Never
208         */
209        public String getSecondaryKey() {
210            return m_sSecondaryKey;
211        }
212    
213        /**
214         * Return true, if the entry has been either completely commited or rolled back.
215         *
216         * @override Never
217         */
218        public boolean isHandled() {
219            return m_fHandled;
220        }
221    
222        // Loggable interface method
223        /**
224         * Return a LogEntry that describes this DataBasketEntry.
225         *
226         * @override Always The default implementation is generic and therefore usually not optimal for a subclass
227         * implementation.
228         */
229        public LogEntry getLogData() {
230            final String sMainKey = getMainKey();
231            final String sSecondaryKey = getSecondaryKey();
232            final String sSource = "" + getSource();
233            final String sDest = "" + getDestination();
234            final String sValue = "" + getValue();
235    
236            return new LogEntry() {
237    
238                    private static final long serialVersionUID = 338378085223507454L;
239    
240                            public String toString() {
241                    return "DataBasketEntry: \"" + sMainKey + "\"/\"" + sSecondaryKey + "\": " + "\"" + sValue +
242                            "\" (\"" + sSource + "\" -> \"" + sDest + "\")";
243                }
244            };
245        }
246    }