001    import sale.*;
002    import data.*;
003    import data.ooimpl.*;
004    import data.stdforms.*;
005    import data.swing.*;
006    import data.events.*;
007    import users.*;
008    
009    import java.util.*;
010    
011    import javax.swing.*;
012    
013    
014    /**
015     * Prozess zur Bestandsanzeige und zum Editieren des Bestandes.
016     */
017    public class SeeVideoStockProcess extends SaleProcess
018    {
019    
020      //// attributes ////////////////////////////////////////////////////////////
021    
022      // Gates
023      protected UIGate selectionGate;
024    
025    
026      //// constructor ///////////////////////////////////////////////////////////
027    
028      /**
029       * Erzeugt ein neues Objekt der Klasse <CODE>SeeVideoStockProcess</CODE>.
030       */
031      public SeeVideoStockProcess()
032      {
033        super ("SeeVideoProcess");
034      }
035    
036      //// protected methods /////////////////////////////////////////////////////
037    
038      /**
039       * Baut die Oberfläche für den Verleihvorgang auf.
040       */
041      protected void setupMachine()
042      {
043        // Auswahl-Gate anlegen
044        selectionGate = new UIGate(null, null);
045    
046        // Der Datenkorb des Prozesses
047        final DataBasket db = getBasket();  
048    
049        // Video-Katalog holen
050        final Catalog videoCatalog = Shop.getTheShop().getCatalog("Video-Catalog");
051    
052        // Der Bestand der Videos
053        final CountingStockImpl videoStock = (CountingStockImpl)Shop.getTheShop().
054                                                getStock("Video-Countingstock");
055    
056        // darzustellendes Formsheet erstellen
057        final SingleTableFormSheet stfs =
058          SingleTableFormSheet.create("See videos in stock and edit Process",
059                                      videoStock,
060                                           selectionGate,
061                                           db,
062                                           true,
063                                           new EditableVideoStockTED(videoStock, db)
064                                          );
065       
066    
067        // FormSheetContentCreator am Auswahl-Gate anmelden    
068        stfs.addContentCreator(
069          new FormSheetContentCreator()
070          {
071            protected void createFormSheetContent(FormSheet fs)
072            {
073                   // alle vorhandenen Buttons entfernen
074                   fs.removeAllButtons();
075    
076              // neuen "New"-Button einbauen
077                   fs.addButton ("New", 100, 
078                     new sale.Action()
079                     {
080                       public void doAction (SaleProcess p, SalesPoint sp)
081                  {
082                    // Anlegen eines neuen Videos
083                    // Eingabefelder erzeugen
084                         JTextField jTextField1 = new JTextField();
085                         JTextField jTextField2 = new JTextField();
086                         JTextField jTextField3 = new JTextField();
087                         JTextField jTextField4 = new JTextField();
088            
089                    // neues JPanel anlegen und vertikales Boxlayout setzen
090                         JPanel jTextPanel = new JPanel();
091                         jTextPanel.setLayout(new BoxLayout(jTextPanel,
092                                             BoxLayout.Y_AXIS));
093    
094                    // Label und Paßwortfelder einfuegen
095                    jTextPanel.add(new JLabel("Name"));
096                         jTextPanel.add(jTextField1);
097                         jTextPanel.add(new JLabel("Buy (in Pf)"));
098                         jTextPanel.add(jTextField2);
099                         jTextPanel.add(new JLabel("Sell (in Pf)"));
100                         jTextPanel.add(jTextField3);
101                         jTextPanel.add(new JLabel("Amount"));
102                         jTextPanel.add(jTextField4);
103                         // Dialog darstellen
104                    JOptionPane.showMessageDialog (null,
105                                                             jTextPanel,
106                                                                  "New Video-Cassette",
107                                                                       JOptionPane.QUESTION_MESSAGE);
108      
109                    // Name des Videos
110                         String name = jTextField1.getText();
111    
112                    // Verkaufspreis
113                    int buy = 0;
114                         try {
115                           buy = (new Integer(jTextField2.getText())).intValue();
116                         }
117                         catch (NumberFormatException ne) {}
118    
119                    // Einkaufspreis
120                    int sell = 0;
121                         try {
122                           sell = (new Integer(jTextField3.getText())).intValue();
123                         }
124                         catch (NumberFormatException ne) {}
125     
126                    // Anzahl
127                         int amount = 0;
128                         try {
129                           amount = (new Integer(jTextField4.getText())).intValue();
130                         }
131                         catch (NumberFormatException ne) {}
132    
133                    // ueberpruefen, ob Eingabe korrekt
134                    if (sell > 0 && buy > 0 && amount > 0 && !name.equals("")) 
135                    {
136                           // in Katalog uebernehmen
137                           try {
138                             videoCatalog.add(new VideoCassette(name, new QuoteValue
139                               (new IntegerValue (buy), new IntegerValue (sell))), db);
140     
141                        // in Bestand uebernehmen
142                             videoStock.add(name, amount, db);
143                      }
144    
145                      // Element existiert bereits
146                      catch (DuplicateKeyException dke) {
147                             JOptionPane.showMessageDialog(null, "Element exists");
148                           }
149                    }
150    
151                    // Eingabe fehlerhaft
152                    else 
153                    {
154                           JOptionPane.showMessageDialog(null,
155                              "The given input was not correct!");
156                         }
157                  }              
158                     }
159                   );
160    
161              // neuen "Remove"-Button einbauen
162                   fs.addButton ("Remove", 101, 
163                     new sale.Action()
164                     {
165                       public void doAction (SaleProcess p, SalesPoint sp)
166                       {
167                         // markierte Zeile holen
168                    Object record = stfs.getSelectedRecord();
169                    if (record != null) 
170                    {
171                           VideoCassette videoCassette = (VideoCassette)
172                             ((data.swing.CountingStockTableModel.Record) record).getDescriptor();
173    
174                      // Anzahl der entliehenen Videos bestimmen
175                           int rented = 0;
176                           if (VideoMachine.getAllCustomer() != null)
177                      {
178                             Iterator i = VideoMachine.getAllCustomer().iterator();
179                             while (i.hasNext()) 
180                               rented = rented +
181                                 ((Customer)i.next()).getStoringStock().countItems(
182                                   videoCassette.getName(), null);
183                           }
184                          
185                      // ueberpruefen, ob gewaehltes Video ausgeliehen
186                           if (rented <= 0) 
187                           {
188                             // loeschen des gewaehlten Eintrags
189                             try {
190                               videoCatalog.remove(videoCassette, db);
191                        }     
192                             catch (VetoException ve) {
193                               ve.printStackTrace();
194                               JOptionPane.showMessageDialog(null,
195                            "The selected item can't be removed!");
196                             }
197                           }
198    
199                      // ausgeliehen Videos vorhanden
200                      else 
201                      {
202                             JOptionPane.showMessageDialog(null,
203                               "There are still rented videos!");
204                      }
205                         }
206                       }
207                     }
208                   );
209    
210              // neuen "Ok"-Button einbauen
211                   fs.addButton ("Ok", 102, 
212                     new sale.Action()
213                     {
214                       public void doAction (SaleProcess p, SalesPoint sp)
215                       {
216                         // Transition zum Commit-Gate als naechste Transition setzen
217                         selectionGate.setNextTransition(
218                           GateChangeTransition.CHANGE_TO_COMMIT_GATE);
219                       }
220                     }
221                   );
222    
223              // neuen "Cancel"-Button einbauen
224                   fs.addButton ("Cancel", 103, 
225                     new sale.Action()
226                     {
227                       public void doAction (SaleProcess p, SalesPoint sp)
228                       {
229                         // Transition zum Rollback-Gate als naechste Transition setzen
230                         selectionGate.setNextTransition(
231                           GateChangeTransition.CHANGE_TO_ROLLBACK_GATE);
232                       }
233                     }
234                   );
235    
236            }
237          }
238        );
239      }
240    
241    
242      //// public methods ////////////////////////////////////////////////////////
243    
244      /**
245       * Gibt das Startgate des Prozesses zurück.
246       */
247      public Gate getInitialGate()
248      {
249        // Automat aufbauen
250        setupMachine();
251     
252        // ...und Auswahl-Gate als Startgate zurueckgeben
253        return selectionGate;
254      }
255    
256      /**
257       * Übergibt das Log-Gate. Hier das Stop-Gate, da beim Beenden des
258       * Prozesses kein Log-Eintrag geschrieben werden soll.
259       */
260      public Gate getLogGate()
261      {
262        return getStopGate();
263      }
264    
265    }