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