001    package data.stdforms.twotableformsheet;
002    
003    import sale.*;
004    
005    import data.*;
006    import data.stdforms.*;
007    
008    import users.*;
009    
010    /**
011     * MoveStrategy for a Catalog source and a StoringStock destination. <i>Abstract</i> as creating StockItems is
012     * application dependant.
013     *
014     * @see #createStockItem
015     *
016     * @author Steffen Zschaler
017     * @version 2.0 20/08/1999
018     * @since v2.0
019     */
020    public abstract class CSSStrategy extends MoveStrategy {
021    
022        /**
023         * Get the sub-process that will move items from the source to the destination.
024         *
025         * @param p the process into which the sub-process wil be embedded.
026         * @param sp the SalesPoint, if any, at which the FormSheet is being displayed.
027         * @param cSource the source Catalog.
028         * @param ssDest the destination StoringStock.
029         * @param db the DataBasket relative to which to perform the operation.
030         * @param ci the CatalogItem that is selected in the source.
031         * @param ttfs the FormSheet that triggers the process.
032         *
033         * @override Never
034         */
035        public Transition getMoveToDestProcess(SaleProcess p, SalesPoint sp, Catalog cSource, StoringStock ssDest,
036                DataBasket db, CatalogItem ci, TwoTableFormSheet ttfs) {
037            return new GateChangeTransition(getCheckMoveToDestGate(p, sp, cSource, ssDest, db, ci, ttfs));
038        }
039    
040        /**
041         * Get the first gate of the sub-process that will move items from the source to the destination.
042         *
043         * <p>This Gate will check whether the move is allowable, and if so, will trigger a Transition that
044         * performs it.</p>
045         *
046         * @param p the process into which the sub-process wil be embedded.
047         * @param sp the SalesPoint, if any, at which the FormSheet is being displayed.
048         * @param cSource the source Catalog.
049         * @param ssDest the destination StoringStock.
050         * @param db the DataBasket relative to which to perform the operation.
051         * @param ci the CatalogItem that is selected in the source.
052         * @param ttfs the FormSheet that triggers the process.
053         *
054         * @override Never Instead, override {@link #checkMoveToDest}, {@link #createStockItem} and/or
055         * {@link #moveToDest}.
056         */
057        protected Gate getCheckMoveToDestGate(SaleProcess p, final SalesPoint sp, final Catalog cSource,
058                final StoringStock ssDest, final DataBasket db, final CatalogItem ci,
059                final TwoTableFormSheet ttfs) {
060            return new Gate() {
061                public Transition getNextTransition(SaleProcess p, User u) throws InterruptedException {
062                    int nCheckReturn = checkMoveToDest(p, sp, cSource, ssDest, db, ci);
063    
064                    if (nCheckReturn == 0) {
065                        final StockItem si = createStockItem(p, sp, cSource, ssDest, db, ci);
066    
067                        if (si != null) {
068                            return new Transition() {
069                                public Gate perform(SaleProcess p, User u) {
070                                    moveToDest(p, sp, cSource, ssDest, db, si);
071    
072                                    return ttfs.getGate();
073                                }
074                            };
075                        }
076                    } else {
077                        error(p, nCheckReturn);
078                    }
079    
080                    return new GateChangeTransition(ttfs.getGate());
081                }
082            };
083        }
084    
085        /**
086         * Check whether the indicated move is allowable. If so, return 0, otherwise return a non-zero error value
087         * that can be passed on to {@link sale.stdforms.FormSheetStrategy#error}. You can assume that you are at a {@link Gate}.
088         *
089         * @param p the process into which the sub-process wil be embedded.
090         * @param sp the SalesPoint, if any, at which the FormSheet is being displayed.
091         * @param cSource the source Catalog.
092         * @param ssDest the destination StoringStock.
093         * @param db the DataBasket relative to which to perform the operation.
094         * @param ci the CatalogItem that is selected in the source.
095         *
096         * @override Sometimes The default implementation returns 0.
097         */
098        protected int checkMoveToDest(SaleProcess p, SalesPoint sp, Catalog cSource, StoringStock ssDest,
099                DataBasket db, CatalogItem ci) throws InterruptedException {
100            return 0;
101        }
102    
103        /**
104         * Create a fresh StockItem following the specifications given. You can assume that you are at a
105         * {@link Gate}.
106         *
107         * @param p the process into which the sub-process wil be embedded.
108         * @param sp the SalesPoint, if any, at which the FormSheet is being displayed.
109         * @param cSource the source Catalog.
110         * @param ssDest the destination StoringStock.
111         * @param db the DataBasket relative to which to perform the operation.
112         * @param ci the CatalogItem that is selected in the source.
113         *
114         * @override Always This method is application dependant.
115         */
116        protected abstract StockItem createStockItem(SaleProcess p, SalesPoint sp, Catalog cSource,
117                StoringStock ssDest, DataBasket db, CatalogItem ci) throws InterruptedException;
118    
119        /**
120         * Move the item as indicated into the destination Stock. You can assume that you are in a
121         * {@link Transition}.
122         *
123         * @param p the process into which the sub-process wil be embedded.
124         * @param sp the SalesPoint, if any, at which the FormSheet is being displayed.
125         * @param cSource the source Catalog.
126         * @param ssDest the destination StoringStock.
127         * @param db the DataBasket relative to which to perform the operation.
128         * @param si the StockItem to be moved into the destination.
129         *
130         * @override Sometimes
131         */
132        protected void moveToDest(SaleProcess p, SalesPoint sp, Catalog cSource, StoringStock ssDest,
133                DataBasket db, StockItem si) {
134            ssDest.add(si, db);
135        }
136    
137        /**
138         * Get the sub-process that will move items from the destination to the source.
139         *
140         * @param p the process into which the sub-process wil be embedded.
141         * @param sp the SalesPoint, if any, at which the FormSheet is being displayed.
142         * @param cSource the source Catalog.
143         * @param ssDest the destination StoringStock.
144         * @param db the DataBasket relative to which to perform the operation.
145         * @param si the StockItem that is selected in the destination.
146         * @param ttfs the FormSheet that triggers the process.
147         *
148         * @override Never
149         */
150        public Transition getMoveToSourceProcess(SaleProcess p, SalesPoint sp, Catalog cSource,
151                StoringStock ssDest, DataBasket db, StockItem si, TwoTableFormSheet ttfs) {
152            return new GateChangeTransition(getCheckMoveToSourceGate(p, sp, cSource, ssDest, db, si, ttfs));
153        }
154    
155        /**
156         * Get the first gate of the sub-process that will move items from the destination to the source.
157         *
158         * <p>This Gate will check whether the move is allowable, and if so, will trigger a Transition that
159         * performs it.</p>
160         *
161         * @param p the process into which the sub-process wil be embedded.
162         * @param sp the SalesPoint, if any, at which the FormSheet is being displayed.
163         * @param cSource the source Catalog.
164         * @param ssDest the destination StoringStock.
165         * @param db the DataBasket relative to which to perform the operation.
166         * @param si the StockItem that is selected in the destination.
167         * @param ttfs the FormSheet that triggers the process.
168         *
169         * @override Never Instead, override {@link #checkMoveToSource} and/or {@link #moveToSource}.
170         */
171        protected Gate getCheckMoveToSourceGate(SaleProcess p, final SalesPoint sp, final Catalog cSource,
172                final StoringStock ssDest, final DataBasket db, final StockItem si, final TwoTableFormSheet ttfs) {
173            return new Gate() {
174                public Transition getNextTransition(SaleProcess p, User u) throws InterruptedException {
175                    int nCheckReturn = checkMoveToSource(p, sp, cSource, ssDest, db, si);
176    
177                    if (nCheckReturn == 0) {
178                        return new Transition() {
179                            public Gate perform(SaleProcess p, User u) {
180                                moveToSource(p, sp, cSource, ssDest, db, si);
181    
182                                return ttfs.getGate();
183                            }
184                        };
185                    } else {
186                        error(p, nCheckReturn);
187    
188                        return new GateChangeTransition(ttfs.getGate());
189                    }
190                }
191            };
192        }
193    
194        /**
195         * Check whether the indicated move is allowable. If so, return 0, otherwise return a non-zero error value
196         * that can be passed on to {@link sale.stdforms.FormSheetStrategy#error}. You can assume that you are at a {@link Gate}.
197         *
198         * @param p the process into which the sub-process wil be embedded.
199         * @param sp the SalesPoint, if any, at which the FormSheet is being displayed.
200         * @param cSource the source Catalog.
201         * @param ssDest the destination StoringStock.
202         * @param db the DataBasket relative to which to perform the operation.
203         * @param si the StockItem that is selected in the destination.
204         *
205         * @override Sometimes The default implementation returns 0.
206         */
207        protected int checkMoveToSource(SaleProcess p, SalesPoint sp, Catalog cSource, StoringStock ssDest,
208                DataBasket db, StockItem si) throws InterruptedException {
209            return 0;
210        }
211    
212        /**
213         * Move the indicated item as indicated from the destination Stock. You can assume that you are in a
214         * {@link Transition}.
215         *
216         * @param p the process into which the sub-process wil be embedded.
217         * @param sp the SalesPoint, if any, at which the FormSheet is being displayed.
218         * @param cSource the source Catalog.
219         * @param ssDest the destination StoringStock.
220         * @param db the DataBasket relative to which to perform the operation.
221         * @param si the StockItem that is selected in the destination.
222         *
223         * @override Sometimes
224         */
225        protected void moveToSource(SaleProcess p, SalesPoint sp, Catalog cSource, StoringStock ssDest,
226                DataBasket db, StockItem si) {
227            try {
228                ssDest.remove(si, db);
229            }
230            catch (data.events.VetoException ve) {
231                error(p, REMOVE_VETO_EXCEPTION);
232            }
233        }
234    }