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