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             * ID for serialization.
021             */
022            private static final long serialVersionUID = -5836134122991358625L;
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 csSource the source CountingStock.
030         * @param dbDest the destination DataBasket.
031         * @param ci the CatalogItem that is selected in the source.
032         * @param nCount the number of items to be moved.
033         * @param ttfs the FormSheet that triggers the process.
034         *
035         * @override Never
036         */
037        public Transition getMoveToDestProcess(SaleProcess p, SalesPoint sp, CountingStock csSource,
038                DataBasket dbDest, CatalogItem ci, int nCount, TwoTableFormSheet ttfs) {
039            return new GateChangeTransition(getCheckMoveToDestGate(p, sp, csSource, dbDest, ci, nCount, ttfs));
040        }
041    
042        /**
043         * Get the first gate of the sub-process that will move items from the source to the destination.
044         *
045         * <p>This Gate will check whether the move is allowable, and if so, will trigger a Transition that
046         * performs it.</p>
047         *
048         * @param p the process into which the sub-process wil be embedded.
049         * @param sp the SalesPoint, if any, at which the FormSheet is being displayed.
050         * @param csSource the source CountingStock.
051         * @param dbDest the destination DataBasket.
052         * @param ci the CatalogItem that is selected in the source.
053         * @param nCount the number of items to be moved.
054         * @param ttfs the FormSheet that triggers the process.
055         *
056         * @override Never Instead, override {@link #checkMoveToDest} and/or {@link #moveToDest}.
057         */
058        protected Gate getCheckMoveToDestGate(SaleProcess p, final SalesPoint sp, final CountingStock csSource,
059                final DataBasket dbDest, final CatalogItem ci, final int nCount, final TwoTableFormSheet ttfs) {
060            return new Gate() {
061                            private static final long serialVersionUID = -2134366798542598368L;
062    
063                            public Transition getNextTransition(SaleProcess p, User u) throws InterruptedException {
064                    int nCheckResult = checkMoveToDest(p, sp, csSource, dbDest, ci, nCount);
065    
066                    if (nCheckResult == 0) {
067                        return new Transition() {
068                                                    private static final long serialVersionUID = -7395678485373711942L;
069    
070                                                    public Gate perform(SaleProcess p, User u) {
071                                moveToDest(p, sp, csSource, dbDest, ci, nCount);
072    
073                                return ttfs.getGate();
074                            }
075                        };
076                    } else {
077                        error(p, nCheckResult);
078    
079                        return new GateChangeTransition(ttfs.getGate());
080                    }
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 csSource the source CountingStock.
092         * @param dbDest the destination DataBasket.
093         * @param ci the CatalogItem that is selected in the source.
094         * @param nCount the number of items to be moved.
095         *
096         * @override Sometimes The default implementation returns 0.
097         */
098        protected int checkMoveToDest(SaleProcess p, SalesPoint sp, CountingStock csSource, DataBasket dbDest,
099                CatalogItem ci, int nCount) throws InterruptedException {
100            return 0;
101        }
102    
103        /**
104         * Move the indicated number of items as indicated into the destination DataBasket. You can assume that you
105         * are in a {@link Transition}.
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 csSource the source CountingStock.
110         * @param dbDest the destination DataBasket.
111         * @param ci the CatalogItem that is selected in the source.
112         * @param nCount the number of items to be moved.
113         *
114         * @override Sometimes
115         */
116        protected void moveToDest(SaleProcess p, SalesPoint sp, CountingStock csSource, DataBasket dbDest,
117                CatalogItem ci, int nCount) {
118            try {
119                csSource.remove(ci.getName(), nCount, dbDest);
120            }
121            catch (NotEnoughElementsException neee) {
122                error(p, NOT_ENOUGH_ELEMENTS_ERROR);
123            }
124            catch (data.events.VetoException ve) {
125                error(p, REMOVE_VETO_EXCEPTION);
126            }
127        }
128    
129        /**
130         * Get the sub-process that will move items from the destination to the source.
131         *
132         * @param p the process into which the sub-process wil be embedded.
133         * @param sp the SalesPoint, if any, at which the FormSheet is being displayed.
134         * @param csSource the source CountingStock.
135         * @param dbDest the destination DataBasket.
136         * @param dbe the DataBasketEntry that is selected in the destination.
137         * @param nCount the number of items to be moved.
138         * @param ttfs the FormSheet that triggers the process.
139         *
140         * @override Never
141         */
142        public Transition getMoveToSourceProcess(SaleProcess p, SalesPoint sp, CountingStock csSource,
143                DataBasket dbDest, DataBasketEntry dbe, int nCount, TwoTableFormSheet ttfs) {
144            return new GateChangeTransition(getCheckMoveToSourceGate(p, sp, csSource, dbDest, dbe, nCount, ttfs));
145        }
146    
147        /**
148         * Get the first gate of the sub-process that will move items from the destination to the source.
149         *
150         * <p>This Gate will check whether the move is allowable, and if so, will trigger a Transition that
151         * performs it.</p>
152         *
153         * @param p the process into which the sub-process wil be embedded.
154         * @param sp the SalesPoint, if any, at which the FormSheet is being displayed.
155         * @param csSource the source CountingStock.
156         * @param dbDest the destination DataBasket.
157         * @param dbe the DataBasketEntry that is selected in the destination.
158         * @param nCount the number of items to be moved.
159         * @param ttfs the FormSheet that triggers the process.
160         *
161         * @override Never Instead, override {@link #checkMoveToSource} and/or {@link #moveToSource}.
162         */
163        protected Gate getCheckMoveToSourceGate(SaleProcess p, final SalesPoint sp, final CountingStock csSource,
164                final DataBasket dbDest, final DataBasketEntry dbe, final int nCount,
165                final TwoTableFormSheet ttfs) {
166            return new Gate() {
167                            private static final long serialVersionUID = -8944846922425129109L;
168    
169                            public Transition getNextTransition(SaleProcess p, User u) throws InterruptedException {
170                    int nCheckResult = checkMoveToSource(p, sp, csSource, dbDest, dbe, nCount);
171    
172                    if (nCheckResult == 0) {
173                        return new Transition() {
174                                                    private static final long serialVersionUID = -8212355807438048733L;
175    
176                                                    public Gate perform(SaleProcess p, User u) {
177                                moveToSource(p, sp, csSource, dbDest, dbe, nCount);
178    
179                                return ttfs.getGate();
180                            }
181                        };
182                    } else {
183                        error(p, nCheckResult);
184    
185                        return new GateChangeTransition(ttfs.getGate());
186                    }
187                }
188            };
189        }
190    
191        /**
192         * Check whether the indicated move is allowable. If so, return 0, otherwise return a non-zero error value
193         * that can be passed on to {@link sale.stdforms.FormSheetStrategy#error}. You can assume that you are at a {@link Gate}.
194         *
195         * @param p the process into which the sub-process wil be embedded.
196         * @param sp the SalesPoint, if any, at which the FormSheet is being displayed.
197         * @param csSource the source CountingStock.
198         * @param dbDest the destination DataBasket.
199         * @param dbe the DataBasketEntry that is selected in the destination.
200         * @param nCount the number of items to be moved.
201         *
202         * @override Sometimes The default implementation returns
203         * {@link ProcessErrorCodes#NOT_ENOUGH_ELEMENTS_ERROR} if yoy try to move more elements than are available
204         * in the DataBasketEntry.
205         */
206        protected int checkMoveToSource(SaleProcess p, SalesPoint sp, CountingStock csSource, DataBasket dbDest,
207                DataBasketEntry dbe, int nCount) throws InterruptedException {
208    
209            if (nCount <= ((Number)dbe.getValue()).intValue()) {
210                return 0;
211            } else {
212                return NOT_ENOUGH_ELEMENTS_ERROR;
213            }
214        }
215    
216        /**
217         * Move the indicated number of items as indicated from the source into the destination. You can assume that
218         * you are in a {@link Transition}.
219         *
220         * @param p the process into which the sub-process wil be embedded.
221         * @param sp the SalesPoint, if any, at which the FormSheet is being displayed.
222         * @param csSource the source CountingStock.
223         * @param dbDest the destination DataBasket.
224         * @param dbe the DataBasketEntry that is selected in the destination.
225         * @param nCount the number of items to be moved.
226         *
227         * @override Sometimes
228         */
229        protected void moveToSource(SaleProcess p, SalesPoint sp, CountingStock csSource, DataBasket dbDest,
230                DataBasketEntry dbe, int nCount) {
231            csSource.add(dbe.getSecondaryKey(), nCount, dbDest);
232        }
233    }