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