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