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