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