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