001    package data.ooimpl;
002    
003    import data.*;
004    
005    /**
006     * Pure Java implementation of the {@link StockItem} interface. See the documentation for
007     * {@link StockItem} for a description of the semantics of this class.
008     *
009     * @author Steffen Zschaler
010     * @version 2.0 19/08/1999
011     * @since v2.0
012     */
013    public class StockItemImpl extends AbstractNameable implements StockItem, DataBasketKeys {
014    
015        /**
016         * The {@link Stock} that contains this {@link StockItem}.
017         *
018         * @serial
019         */
020        private StockImpl m_stiOwner;
021    
022        /**
023         * Create a new StockItemImpl.
024         *
025         * @param sName the name of the new item.
026         */
027        public StockItemImpl(String sName) {
028            super(sName);
029        }
030    
031        /**
032         * Get the Stock that contains this StockItem.
033         *
034         * @override Never
035         */
036        public Stock getStock() {
037            return m_stiOwner;
038        }
039    
040        /**
041         * Get the CatalogItem that is associated with this StockItem.
042         *
043         * <p>If the {@link StockItem} has a {@link #getStock Stock}, the associated {@link CatalogItem} is the
044         * CatalogItem of the same name that is found in the Stock's {@link Stock#getCatalog associated Catalog}.
045         * Otherwise, it is <code>null</code>.
046         *
047         * @param db the DataBasket used to determine visibility.
048         *
049         * @override Never
050         */
051        public CatalogItem getAssociatedItem(DataBasket db) {
052            if (getStock() != null) {
053                if (getStock().getCatalog(db) != null) {
054                    try {
055                        return getStock().getCatalog(db).get(getName(), db, false);
056                    }
057                    catch (data.events.VetoException ve) {}
058                }
059            }
060    
061            return null;
062        }
063    
064        /**
065         * Set the Stock that contains this StockItem.
066         *
067         * @override Never
068         */
069        protected void setStock(StockImpl sti) {
070            m_stiOwner = sti;
071            attach(sti); // as NameContext
072        }
073    
074        /**
075         * Clone this StockItem.
076         *
077         * @override Always
078         */
079        public Object clone() {
080            return new StockItemImpl(getName());
081        }
082    
083        /**
084         * Get a shallow clone of this item.
085         *
086         * <p>For a normal item, shallow and deep clones are identical, which is why the default implementation
087         * returns <code>((StockItemImpl) clone())</code>. However, when making a shallow clone of a Stock, the
088         * individual StockItems will not be cloned.</p>
089         *
090         * @override Sometimes  The default implementation returns <code>(StockItemImpl) clone()</code>.
091         */
092        public StockItemImpl getShallowClone() {
093            return (StockItemImpl)clone();
094        }
095    
096        /**
097         * Check whether this StockItem equals the given object.
098         *
099         * @override Sometimes The default implementation returns <code>(this == o)</code>.
100         */
101        public boolean equals(Object o) {
102            return (this == o);
103        }
104    
105        /**
106         * Compare this StockItem to the given object.
107         *
108         * @override Sometimes The default implementation will assume <code>o</code> to be a StockItem and will
109         * compare the names. Stocks, however, will always be greater than StockItems.
110         *
111         * @exception ClassCastException if the given object cannot be converted into a {@link StockItem}.
112         */
113        public int compareTo(Object o) {
114            if ((o instanceof Stock) && (!(this instanceof Stock))) {
115                return -1;
116            }
117            return getName().compareTo(((StockItem)o).getName());
118        }
119    
120        /**
121         * Internal communication method needed for referential integrity in StoringStocks.
122         *
123         * <p><strong>Attention</strong>: This method must not be called directly.</p>
124         *
125         * @override Never
126         */
127        void internalSetName(String sNewName) {
128            // we need to trick the NameContext, because, normally, Stocks will not allow their members to change their
129            // name.
130            NameContext nc = detachNC();
131    
132            try {
133                setName(sNewName, null);
134            }
135            catch (NameContextException nce) {}
136    
137            attach(nc);
138        }
139    
140        /**
141         * Return a String representation of the item.
142         *
143         * @override Sometimes
144         */
145        public String toString() {
146            return "StockItem \"" + getName() + "\"";
147        }
148    
149        /**
150         * Helper method used to maintain StockImpl - CatalogImpl links in nested Stocks/Catalogs. For internal use only.
151         *
152         * @param db the DataBasket that protecting this activity.
153         * @param nAction the action that occurred. Can be either {@link #COMMIT_ACTION}, {@link #ROLLBACK_ACTION},
154         * {@link #STARTEDIT_ACTION}.
155         */
156        void relinkCatalog(DataBasket db, int nAction) {}
157    
158        static final int COMMIT_ACTION = 1;
159        static final int ROLLBACK_ACTION = 2;
160        static final int STARTEDIT_ACTION = 3;
161    }