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