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 Catalog source and destination.
012     *
013     * @author Steffen Zschaler
014     * @version 2.0 20/08/1999
015     * @since v2.0
016     */
017    public class CCStrategy extends MoveStrategy {
018    
019        /**
020             * ID for serialization.
021             */
022            private static final long serialVersionUID = -2933078468538022404L;
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 cDest the destination Catalog.
031         * @param db the DataBasket relative to which to perform the operation.
032         * @param ci the CatalogItem that is selected in the source.
033         * @param ttfs the FormSheet that triggers the process.
034         *
035         * @override Never
036         */
037        public Transition getMoveToDestProcess(SaleProcess p, SalesPoint sp, Catalog cSource, Catalog cDest,
038                DataBasket db, CatalogItem ci, TwoTableFormSheet ttfs) {
039            return new GateChangeTransition(getCheckMoveToDestGate(p, sp, cSource, cDest, db, ci, 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         * @return {@link #getCheckMoveGate}.
049         *
050         * @param p the process into which the sub-process wil be embedded.
051         * @param sp the SalesPoint, if any, at which the FormSheet is being displayed.
052         * @param cSource the source Catalog.
053         * @param cDest the destination Catalog.
054         * @param db the DataBasket relative to which to perform the operation.
055         * @param ci the CatalogItem that is selected in the source.
056         * @param ttfs the FormSheet that triggers the process.
057         *
058         * @override Never Instead, override {@link #checkMove} and/or {@link #moveImpl}.
059         */
060        protected Gate getCheckMoveToDestGate(SaleProcess p, SalesPoint sp, Catalog cSource, Catalog cDest,
061                DataBasket db, CatalogItem ci, TwoTableFormSheet ttfs) {
062            return getCheckMoveGate(p, sp, cSource, cDest, db, ci, ttfs);
063        }
064    
065        /**
066         * Get the sub-process that will move items from the destination to the source.
067         *
068         * @param p the process into which the sub-process wil be embedded.
069         * @param sp the SalesPoint, if any, at which the FormSheet is being displayed.
070         * @param cSource the source Catalog.
071         * @param cDest the destination Catalog.
072         * @param db the DataBasket relative to which to perform the operation.
073         * @param ci the CatalogItem that is selected in the destination.
074         * @param ttfs the FormSheet that triggers the process.
075         *
076         * @override Never
077         */
078        public Transition getMoveToSourceProcess(SaleProcess p, SalesPoint sp, Catalog cSource, Catalog cDest,
079                DataBasket db, CatalogItem ci, TwoTableFormSheet ttfs) {
080            return new GateChangeTransition(getCheckMoveToSourceGate(p, sp, cSource, cDest, db, ci, ttfs));
081        }
082    
083        /**
084         * Get the first gate of the sub-process that will move items from the destination to the source.
085         *
086         * <p>This Gate will check whether the move is allowable, and if so, will trigger a Transition that
087         * performs it.</p>
088         *
089         * @return {@link #getCheckMoveGate}.
090         *
091         * @param p the process into which the sub-process wil be embedded.
092         * @param sp the SalesPoint, if any, at which the FormSheet is being displayed.
093         * @param cSource the source Catalog.
094         * @param cDest the destination Catalog.
095         * @param db the DataBasket relative to which to perform the operation.
096         * @param ci the CatalogItem that is selected in the destination.
097         * @param ttfs the FormSheet that triggers the process.
098         *
099         * @override Never Instead, override {@link #checkMove} and/or {@link #moveImpl}.
100         */
101        protected Gate getCheckMoveToSourceGate(SaleProcess p, SalesPoint sp, Catalog cSource, Catalog cDest,
102                DataBasket db, CatalogItem ci, TwoTableFormSheet ttfs) {
103            return getCheckMoveGate(p, sp, cDest, cSource, db, ci, ttfs);
104        }
105    
106        /**
107         * Get the first gate of a sub-process that will move items from one Catalog into another.
108         *
109         * <p>This Gate will check whether the move is allowable, and if so, will trigger a Transition that
110         * performs it.</p>
111         *
112         * @param p the process into which the sub-process wil be embedded.
113         * @param sp the SalesPoint, if any, at which the FormSheet is being displayed.
114         * @param cSource the source Catalog.
115         * @param cDest the destination Catalog.
116         * @param db the DataBasket relative to which to perform the operation.
117         * @param ci the CatalogItem that is to be moved from the source into the destination Catalog.
118         * @param ttfs the FormSheet that triggers the process.
119         *
120         * @override Never Instead, override {@link #checkMove} and/or {@link #moveImpl}.
121         */
122        protected Gate getCheckMoveGate(SaleProcess p, final SalesPoint sp, final Catalog cSource,
123                final Catalog cDest, final DataBasket db, final CatalogItem ci, final TwoTableFormSheet ttfs) {
124            return new Gate() {
125                            private static final long serialVersionUID = -7885510790768088268L;
126    
127                            public Transition getNextTransition(SaleProcess p, User u) throws InterruptedException {
128                    int nCheckReturn = checkMove(p, sp, cSource, cDest, db, ci);
129    
130                    if (nCheckReturn == 0) {
131                        return new Transition() {
132                                                    private static final long serialVersionUID = -5819789838546069359L;
133    
134                                                    public Gate perform(SaleProcess p, User u) {
135                                moveImpl(p, sp, cSource, cDest, db, ci);
136    
137                                return ttfs.getGate();
138                            }
139                        };
140                    } else {
141                        error(p, nCheckReturn);
142    
143                        return new GateChangeTransition(ttfs.getGate());
144                    }
145                }
146            };
147        }
148    
149        /**
150         * Move the indicated item from the source Catalog into the destination Catalog. You can assume that you are
151         * in a {@link Transition}.
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 cSource the source Catalog.
156         * @param cDest the destination Catalog.
157         * @param db the DataBasket relative to which to perform the operation.
158         * @param ci the CatalogItem that is to be moved.
159         *
160         * @override Sometimes
161         */
162        protected void moveImpl(SaleProcess p, SalesPoint sp, Catalog cSource, Catalog cDest, DataBasket db,
163                CatalogItem ci) {
164            try {
165                cSource.remove(ci, db);
166                cDest.add(ci, db);
167            }
168            catch (data.events.VetoException ve) {
169                error(p, REMOVE_VETO_EXCEPTION);
170            }
171            catch (DuplicateKeyException dke) {
172                error(p, DUPLICATE_KEY_EXCEPTION);
173            }
174            catch (DataBasketConflictException dbce) {
175                error(p, DATABASKET_CONFLICT_ERROR);
176            }
177        }
178    
179        /**
180         * Check whether the indicated move is allowable. If so, return 0, otherwise return a non-zero error value
181         * that can be passed on to {@link sale.stdforms.FormSheetStrategy#error}. You can assume that you are at a {@link Gate}.
182         *
183         * @param p the process into which the sub-process wil be embedded.
184         * @param sp the SalesPoint, if any, at which the FormSheet is being displayed.
185         * @param cSource the source Catalog.
186         * @param cDest the destination Catalog.
187         * @param db the DataBasket relative to which to perform the operation.
188         * @param ci the CatalogItem that is to be moved.
189         *
190         * @override Sometimes The default implementation returns 0.
191         */
192        protected int checkMove(SaleProcess p, SalesPoint sp, Catalog cSource, Catalog cDest, DataBasket db,
193                CatalogItem ci) throws InterruptedException {
194            return 0;
195        }
196    }