ea753340f2dd4442cd271832f7f870b7f6125992
[RRRRHHHH_Code] / ruralHousesDB / src / dataAccess / DB4oServerManager.java
1 package dataAccess;
2
3 import java.awt.BorderLayout;
4 import java.awt.FlowLayout;
5 import java.awt.event.ActionEvent;
6 import java.awt.event.ActionListener;
7 import java.io.File;
8
9 import javax.swing.JButton;
10 import javax.swing.JDialog;
11 import javax.swing.JPanel;
12 import javax.swing.JTextArea;
13 import javax.swing.border.EmptyBorder;
14
15 import com.db4o.ObjectServer;
16 import com.db4o.cs.Db4oClientServer;
17 import com.db4o.cs.config.ServerConfiguration;
18
19 import configuration.ConfigXML;
20 import domain.Account;
21 import domain.Booking;
22 import domain.Offer;
23 import domain.Owner;
24 import domain.RuralHouse;
25
26 public class DB4oServerManager extends JDialog {
27
28         /**
29          * 
30          */
31         private static final long serialVersionUID = 1L;
32         private final JPanel contentPanel = new JPanel();
33         JTextArea textArea;
34         ObjectServer server;
35         private ServerConfiguration configurationCS;
36
37         /**
38          * Launch the application.
39          */
40         public static void main(String[] args) {
41                 try {
42                         DB4oServerManager  dialog = new DB4oServerManager ();
43                         dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
44                         dialog.setVisible(true);
45                 } catch (Exception e) {
46                         e.printStackTrace();
47                 }
48         }
49
50         /**
51          * Create the dialog.
52          */
53         public DB4oServerManager () {
54                 setTitle("DB4oManagerServer: running the database server");
55                 setBounds(100, 100, 486, 180);
56                 getContentPane().setLayout(new BorderLayout());
57                 contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
58                 getContentPane().add(contentPanel, BorderLayout.CENTER);
59                 contentPanel.setLayout(new BorderLayout(0, 0));
60                 {
61                         textArea = new JTextArea();
62                         contentPanel.add(textArea);
63                 }
64                 {
65                         JPanel buttonPane = new JPanel();
66                         buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
67                         getContentPane().add(buttonPane, BorderLayout.SOUTH);
68                         {
69                                 JButton okButton = new JButton("OK");
70                                 okButton.addActionListener(new ActionListener() {
71                                         public void actionPerformed(ActionEvent e) {
72                                                 textArea.append("\n\n\nClosing the database... ");
73                                             try {
74                                                         server.close();
75                                                 } catch (Exception e1) {
76                                                 }
77                                                 System.exit(1);
78                                         }
79                                 });
80                                 okButton.setActionCommand("OK");
81                                 buttonPane.add(okButton);
82                                 getRootPane().setDefaultButton(okButton);
83                         }
84                         {
85                                 JButton cancelButton = new JButton("Cancel");
86                                 cancelButton.setActionCommand("Cancel");
87                                 buttonPane.add(cancelButton);
88                         }
89                 }
90                 
91                 ConfigXML c=ConfigXML.getInstance();
92                 
93                 
94                 try{
95
96                         if (c.getDataBaseOpenMode().equals("initialize")) new File(c.getDb4oFilename()).delete();
97                         
98                         configurationCS = Db4oClientServer.newServerConfiguration();
99                         configurationCS.common().activationDepth(c.getActivationDepth());
100                         configurationCS.common().updateDepth(c.getUpdateDepth());
101                         configurationCS.common().objectClass(Owner.class).cascadeOnDelete(true);
102                         configurationCS.common().objectClass(Booking.class).cascadeOnUpdate(true);
103                         configurationCS.common().objectClass(RuralHouse.class)
104                                         .cascadeOnUpdate(true);
105                         configurationCS.common().objectClass(Account.class).cascadeOnUpdate(true);
106                         configurationCS.common().objectClass(Offer.class).cascadeOnUpdate(true);
107                         configurationCS.common().objectClass(Account.class).cascadeOnUpdate(true);
108
109                         server = Db4oClientServer.openServer(configurationCS,
110                                                                                                  c.getDb4oFilename(),c.getDatabasePort());
111                         
112                         textArea.append("\nConnection to the database '"+c.getDb4oFilename()+"' opened in port "+c.getDatabasePort());
113         
114                     server.grantAccess(c.getUser(),c.getPassword());
115
116                     textArea.append("\nAccess granted to: "+c.getUser());
117
118
119
120                         textArea.append("\nPress button to exit this database server... ");
121                         
122                 } catch (Exception e) {
123                         textArea.append("Something has happened in DB4oManagerServer: "+e.toString());
124
125                 }
126         }
127 }
128