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 StoringStock source and destination.
012     *
013     * @author Steffen Zschaler
014     * @version 2.0 20/08/1999
015     * @since v2.0
016     */
017    public class SSSSStrategy extends MoveStrategy {
018    
019        /**
020         * Get the sub-process that will move items from the source to the destination.
021         *
022         * @param p the process into which the sub-process wil be embedded.
023         * @param sp the SalesPoint, if any, at which the FormSheet is being displayed.
024         * @param ssSource the source StoringStock.
025         * @param ssDest the destination StoringStock.
026         * @param db the DataBasket relative to which to perform the operation.
027         * @param si the StockItem that is selected in the source.
028         * @param ttfs the FormSheet that triggers the process.
029         *
030         * @override Never
031         */
032        public Transition getMoveToDestProcess(SaleProcess p, SalesPoint sp, StoringStock ssSource,
033                StoringStock ssDest, DataBasket db, StockItem si, TwoTableFormSheet ttfs) {
034            return new GateChangeTransition(getCheckMoveToDestGate(p, sp, ssSource, ssDest, db, si, ttfs));
035        }
036    
037        /**
038         * Get the first gate of the sub-process that will move items from the source to the destination.
039         *
040         * <p>This Gate will check whether the move is allowable, and if so, will trigger a Transition that
041         * performs it.</p>
042         *
043         * @return {@link #getCheckMoveGate}.
044         *
045         * @param p the process into which the sub-process wil be embedded.
046         * @param sp the SalesPoint, if any, at which the FormSheet is being displayed.
047         * @param ssSource the source StoringStock.
048         * @param ssDest the destination StoringStock.
049         * @param db the DataBasket relative to which to perform the operation.
050         * @param si the StockItem that is selected in the source.
051         * @param ttfs the FormSheet that triggers the process.
052         *
053         * @override Never Instead, override {@link #checkMove} and/or {@link #moveImpl}.
054         */
055        protected Gate getCheckMoveToDestGate(SaleProcess p, SalesPoint sp, StoringStock ssSource,
056                StoringStock ssDest, DataBasket db, StockItem si, TwoTableFormSheet ttfs) {
057            return getCheckMoveGate(p, sp, ssSource, ssDest, db, si, ttfs);
058        }
059    
060        /**
061         * Get the sub-process that will move items from the destination to the source.
062         *
063         * @param p the process into which the sub-process wil be embedded.
064         * @param sp the SalesPoint, if any, at which the FormSheet is being displayed.
065         * @param ssSource the source StoringStock.
066         * @param ssDest the destination StoringStock.
067         * @param db the DataBasket relative to which to perform the operation.
068         * @param si the StockItem that is selected in the destination.
069         * @param ttfs the FormSheet that triggers the process.
070         *
071         * @override Never
072         */
073        public Transition getMoveToSourceProcess(SaleProcess p, SalesPoint sp, StoringStock ssSource,
074                StoringStock ssDest, DataBasket db, StockItem si, TwoTableFormSheet ttfs) {
075            return new GateChangeTransition(getCheckMoveToSourceGate(p, sp, ssSource, ssDest, db, si, ttfs));
076        }
077    
078        /**
079         * Get the first gate of the sub-process that will move items from the destination to the source.
080         *
081         * <p>This Gate will check whether the move is allowable, and if so, will trigger a Transition that
082         * performs it.</p>
083         *
084         * @return {@link #getCheckMoveGate}.
085         *
086         * @param p the process into which the sub-process wil be embedded.
087         * @param sp the SalesPoint, if any, at which the FormSheet is being displayed.
088         * @param ssSource the source StoringStock.
089         * @param ssDest the destination StoringStock.
090         * @param db the DataBasket relative to which to perform the operation.
091         * @param si the StockItem that is selected in the destination.
092         * @param ttfs the FormSheet that triggers the process.
093         *
094         * @override Never Instead, override {@link #checkMove} and/or {@link #moveImpl}.
095         */
096        protected Gate getCheckMoveToSourceGate(SaleProcess p, SalesPoint sp, StoringStock ssSource,
097                StoringStock ssDest, DataBasket db, StockItem si, TwoTableFormSheet ttfs) {
098            return getCheckMoveGate(p, sp, ssDest, ssSource, db, si, ttfs);
099        }
100    
101        /**
102         * Get the first gate of a sub-process that will move items from one Stock into another.
103         *
104         * <p>This Gate will check whether the move is allowable, and if so, will trigger a Transition that
105         * performs it.</p>
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 ssSource the source StoringStock.
110         * @param ssDest the destination StoringStock.
111         * @param db the DataBasket relative to which to perform the operation.
112         * @param si the StockItem which is to be moved from the source into the destination Stock.
113         * @param ttfs the FormSheet that triggers the process.
114         *
115         * @override Never Instead, override {@link #checkMove} and/or {@link #moveImpl}.
116         */
117        protected Gate getCheckMoveGate(SaleProcess p, final SalesPoint sp, final StoringStock ssSource,
118                final StoringStock ssDest, final DataBasket db, final StockItem si, final TwoTableFormSheet ttfs) {
119            return new Gate() {
120                public Transition getNextTransition(SaleProcess p, User u) throws InterruptedException {
121                    int nCheckReturn = checkMove(p, sp, ssSource, ssDest, db, si);
122    
123                    if (nCheckReturn == 0) {
124                        return new Transition() {
125                            public Gate perform(SaleProcess p, User u) {
126                                moveImpl(p, sp, ssSource, ssDest, db, si);
127    
128                                return ttfs.getGate();
129                            }
130                        };
131                    } else {
132                        error(p, nCheckReturn);
133    
134                        return new GateChangeTransition(ttfs.getGate());
135                    }
136                }
137            };
138        }
139    
140        /**
141         * Check whether the indicated move is allowable. If so, return 0, otherwise return a non-zero error value
142         * that can be passed on to {@link sale.stdforms.FormSheetStrategy#error}. You can assume that you are at a {@link Gate}.
143         *
144         * @param p the process into which the sub-process wil be embedded.
145         * @param sp the SalesPoint, if any, at which the FormSheet is being displayed.
146         * @param ssSource the source StoringStock.
147         * @param ssDest the destination StoringStock.
148         * @param db the DataBasket relative to which to perform the operation.
149         * @param si the StockItem which is to be moved.
150         *
151         * @override Sometimes The default implementation returns 0.
152         */
153        protected int checkMove(SaleProcess p, SalesPoint sp, StoringStock ssSource, StoringStock ssDest,
154                DataBasket db, StockItem si) throws InterruptedException {
155            return 0;
156        }
157    
158        /**
159         * Move the indicated item from the source StoringStock into the destination StoringStock. You
160         * can assume that you are in a {@link Transition}.
161         *
162         * @param p the process into which the sub-process wil be embedded.
163         * @param sp the SalesPoint, if any, at which the FormSheet is being displayed.
164         * @param ssSource the source StoringStock.
165         * @param ssDest the destination StoringStock.
166         * @param db the DataBasket relative to which to perform the operation.
167         * @param si the StockItem which is to be moved.
168         *
169         * @override Sometimes
170         */
171        protected void moveImpl(SaleProcess p, SalesPoint sp, StoringStock ssSource, StoringStock ssDest,
172                DataBasket db, StockItem si) {
173            try {
174                ssDest.add(ssSource.remove(si, db), db);
175            }
176            catch (data.events.VetoException ve) {
177                error(p, REMOVE_VETO_EXCEPTION);
178            }
179        }
180    }