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 CountingStock 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 CSDBStrategy 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 csSource the source CountingStock.
025         * @param dbDest the destination DataBasket.
026         * @param ci the CatalogItem that is selected in the source.
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 getMoveToDestProcess(SaleProcess p, SalesPoint sp, CountingStock csSource,
033                DataBasket dbDest, CatalogItem ci, int nCount, TwoTableFormSheet ttfs) {
034            return new GateChangeTransition(getCheckMoveToDestGate(p, sp, csSource, dbDest, ci, nCount, 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         * @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 csSource the source CountingStock.
046         * @param dbDest the destination DataBasket.
047         * @param ci the CatalogItem that is selected in the source.
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 #checkMoveToDest} and/or {@link #moveToDest}.
052         */
053        protected Gate getCheckMoveToDestGate(SaleProcess p, final SalesPoint sp, final CountingStock csSource,
054                final DataBasket dbDest, 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 = checkMoveToDest(p, sp, csSource, dbDest, ci, nCount);
058    
059                    if (nCheckResult == 0) {
060                        return new Transition() {
061                            public Gate perform(SaleProcess p, User u) {
062                                moveToDest(p, sp, csSource, dbDest, 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 csSource the source CountingStock.
083         * @param dbDest the destination DataBasket.
084         * @param ci the CatalogItem that is selected in the source.
085         * @param nCount the number of items to be moved.
086         *
087         * @override Sometimes The default implementation returns 0.
088         */
089        protected int checkMoveToDest(SaleProcess p, SalesPoint sp, CountingStock csSource, DataBasket dbDest,
090                CatalogItem ci, int nCount) throws InterruptedException {
091            return 0;
092        }
093    
094        /**
095         * Move the indicated number of items as indicated into the destination DataBasket. You can assume that you
096         * are 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 csSource the source CountingStock.
101         * @param dbDest the destination DataBasket.
102         * @param ci the CatalogItem that is selected in the source.
103         * @param nCount the number of items to be moved.
104         *
105         * @override Sometimes
106         */
107        protected void moveToDest(SaleProcess p, SalesPoint sp, CountingStock csSource, DataBasket dbDest,
108                CatalogItem ci, int nCount) {
109            try {
110                csSource.remove(ci.getName(), nCount, dbDest);
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 destination to the source.
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 csSource the source CountingStock.
126         * @param dbDest the destination DataBasket.
127         * @param dbe the DataBasketEntry that is selected in the destination.
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 getMoveToSourceProcess(SaleProcess p, SalesPoint sp, CountingStock csSource,
134                DataBasket dbDest, DataBasketEntry dbe, int nCount, TwoTableFormSheet ttfs) {
135            return new GateChangeTransition(getCheckMoveToSourceGate(p, sp, csSource, dbDest, dbe, nCount, ttfs));
136        }
137    
138        /**
139         * Get the first gate of the sub-process that will move items from the destination to the source.
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 csSource the source CountingStock.
147         * @param dbDest the destination DataBasket.
148         * @param dbe the DataBasketEntry that is selected in the destination.
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 #checkMoveToSource} and/or {@link #moveToSource}.
153         */
154        protected Gate getCheckMoveToSourceGate(SaleProcess p, final SalesPoint sp, final CountingStock csSource,
155                final DataBasket dbDest, 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 = checkMoveToSource(p, sp, csSource, dbDest, dbe, nCount);
160    
161                    if (nCheckResult == 0) {
162                        return new Transition() {
163                            public Gate perform(SaleProcess p, User u) {
164                                moveToSource(p, sp, csSource, dbDest, 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 csSource the source CountingStock.
185         * @param dbDest the destination DataBasket.
186         * @param dbe the DataBasketEntry that is selected in the destination.
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 yoy try to move more elements than are available
191         * in the DataBasketEntry.
192         */
193        protected int checkMoveToSource(SaleProcess p, SalesPoint sp, CountingStock csSource, DataBasket dbDest,
194                DataBasketEntry dbe, int nCount) throws InterruptedException {
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 from the source into the destination. You can assume that
205         * you are 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 csSource the source CountingStock.
210         * @param dbDest the destination DataBasket.
211         * @param dbe the DataBasketEntry that is selected in the destination.
212         * @param nCount the number of items to be moved.
213         *
214         * @override Sometimes
215         */
216        protected void moveToSource(SaleProcess p, SalesPoint sp, CountingStock csSource, DataBasket dbDest,
217                DataBasketEntry dbe, int nCount) {
218            csSource.add(dbe.getSecondaryKey(), nCount, dbDest);
219        }
220    }