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             * ID for serialization.
021             */
022            private static final long serialVersionUID = 7900120823255498325L;
023    
024            /**
025         * Get the sub-process that will move items from the destination to the source.
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 dbSource the source DataBasket.
030         * @param csDest the destination CountingStock.
031         * @param ci the CatalogItem that is selected in the destination.
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 getMoveToSourceProcess(SaleProcess p, SalesPoint sp, DataBasket dbSource,
038                CountingStock csDest, CatalogItem ci, int nCount, TwoTableFormSheet ttfs) {
039            return new GateChangeTransition(getCheckMoveToSourceGate(p, sp, dbSource, csDest, ci, nCount, ttfs));
040        }
041    
042        /**
043         * Get the first gate of the sub-process that will move items from the destination to the source.
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 dbSource the source DataBasket.
051         * @param csDest the destination CountingStock.
052         * @param ci the CatalogItem that is selected in the destination.
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 #checkMoveToSource} and/or {@link #moveToSource}.
057         */
058        protected Gate getCheckMoveToSourceGate(SaleProcess p, final SalesPoint sp, final DataBasket dbSource,
059                final CountingStock csDest, final CatalogItem ci, final int nCount, final TwoTableFormSheet ttfs) {
060            return new Gate() {
061                            private static final long serialVersionUID = 7439345899588451423L;
062    
063                            public Transition getNextTransition(SaleProcess p, User u) throws InterruptedException {
064                    int nCheckResult = checkMoveToSource(p, sp, dbSource, csDest, ci, nCount);
065    
066                    if (nCheckResult == 0) {
067                        return new Transition() {
068                                                    private static final long serialVersionUID = 3979383014212140149L;
069    
070                                                    public Gate perform(SaleProcess p, User u) {
071                                moveToSource(p, sp, dbSource, csDest, 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 dbSource the source DataBasket.
092         * @param csDest the destination CountingStock.
093         * @param ci the CatalogItem that is selected in the destination.
094         * @param nCount the number of items to be moved.
095         *
096         * @override Sometimes The default implementation returns 0.
097         */
098        protected int checkMoveToSource(SaleProcess p, SalesPoint sp, DataBasket dbSource, CountingStock csDest,
099                CatalogItem ci, int nCount) throws InterruptedException {
100            return 0;
101        }
102    
103        /**
104         * Move the indicated number of items as indicated from the destination Stock. You can assume that you are
105         * 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 dbSource the source DataBasket.
110         * @param csDest the destination CountingStock.
111         * @param ci the CatalogItem that is selected in the destination.
112         * @param nCount the number of items to be moved.
113         *
114         * @override Sometimes
115         */
116        protected void moveToSource(SaleProcess p, SalesPoint sp, DataBasket dbSource, CountingStock csDest,
117                CatalogItem ci, int nCount) {
118            try {
119                csDest.remove(ci.getName(), nCount, dbSource);
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 source to the destination.
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 dbSource the source DataBasket.
135         * @param csDest the destination CountingStock.
136         * @param dbe the DataBasketEntry that is selected in the source.
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 getMoveToDestProcess(SaleProcess p, SalesPoint sp, DataBasket dbSource,
143                CountingStock csDest, DataBasketEntry dbe, int nCount, TwoTableFormSheet ttfs) {
144            return new GateChangeTransition(getCheckMoveToDestGate(p, sp, dbSource, csDest, dbe, nCount, ttfs));
145        }
146    
147        /**
148         * Get the first gate of the sub-process that will move items from the source to the destination.
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 dbSource the source DataBasket.
156         * @param csDest the destination CountingStock.
157         * @param dbe the DataBasketEntry that is selected in the source.
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 #checkMoveToDest} and/or {@link #moveToDest}.
162         */
163        protected Gate getCheckMoveToDestGate(SaleProcess p, final SalesPoint sp, final DataBasket dbSource,
164                final CountingStock csDest, final DataBasketEntry dbe, final int nCount,
165                final TwoTableFormSheet ttfs) {
166            return new Gate() {
167                            private static final long serialVersionUID = -1712957808511344461L;
168    
169                            public Transition getNextTransition(SaleProcess p, User u) throws InterruptedException {
170                    int nCheckResult = checkMoveToDest(p, sp, dbSource, csDest, dbe, nCount);
171    
172                    if (nCheckResult == 0) {
173                        return new Transition() {
174                                                    private static final long serialVersionUID = -6069941064381773629L;
175    
176                                                    public Gate perform(SaleProcess p, User u) {
177                                moveToDest(p, sp, dbSource, csDest, 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 dbSource the source DataBasket.
198         * @param csDest the destination CountingStock.
199         * @param dbe the DataBasketEntry that is selected in the source.
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 there are not enough elements in the
204         * DataBasketEntry.
205         */
206        protected int checkMoveToDest(SaleProcess p, SalesPoint sp, DataBasket dbSource, CountingStock csDest,
207                DataBasketEntry dbe, int nCount) {
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 into the destination Stock. You can assume that you are
218         * 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 dbSource the source DataBasket.
223         * @param csDest the destination CountingStock.
224         * @param dbe the DataBasketEntry that is selected in the source.
225         * @param nCount the number of items to be moved.
226         *
227         * @override Sometimes
228         */
229        protected void moveToDest(SaleProcess p, SalesPoint sp, DataBasket dbSource, CountingStock csDest,
230                DataBasketEntry dbe, int nCount) {
231            csDest.add(dbe.getSecondaryKey(), nCount, dbSource);
232        }
233    }