/*
* Constellation - An open source and standard compliant SDI
* http://www.constellation-sdi.org
*
* Copyright 2014 Geomatys.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.constellation.swing;
import org.constellation.configuration.DataSourceType;
import org.constellation.generic.database.Automatic;
import org.constellation.generic.database.BDD;
import org.constellation.om2.OM2DatabaseCreator;
import javax.sql.DataSource;
import javax.swing.*;
import java.io.File;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.logging.Level;
/**
*
* @author guilhem
*/
public class JSosOM2EditPane extends JServiceEditionPane {
/**
* Creates new form JSosOM2EditPane
*/
public JSosOM2EditPane(final Automatic configuration) {
initComponents();
if (configuration != null) {
final BDD bdd = configuration.getBdd();
if (bdd != null) {
if (bdd.getClassName() != null) {
guiDriver.setText(bdd.getClassName());
}
if (bdd.getConnectURL() != null) {
guiDbURL.setText(bdd.getConnectURL());
}
if (bdd.getUser()!= null) {
guiUser.setText(bdd.getUser());
}
if (bdd.getPassword()!= null) {
guiPwd.setText(bdd.getPassword());
}
}
}
}
@Override
public Automatic getConfiguration() {
final String className = guiDriver.getText();
final String connectURL = guiDbURL.getText();
final String user = guiUser.getText();
final String password = new String(guiPwd.getPassword());
final BDD bdd = new BDD(className, connectURL, user, password);
final Automatic configuration = new Automatic("OM2", bdd);
return configuration;
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
guiDriver = new javax.swing.JTextField();
jLabel2 = new javax.swing.JLabel();
guiDbURL = new javax.swing.JTextField();
jLabel3 = new javax.swing.JLabel();
guiUser = new javax.swing.JTextField();
jLabel4 = new javax.swing.JLabel();
guiPwd = new javax.swing.JPasswordField();
dbCreateButton = new javax.swing.JButton();
jLabel5 = new javax.swing.JLabel();
guiPGDir = new javax.swing.JTextField();
java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("org/constellation/swing/Bundle"); // NOI18N
org.openide.awt.Mnemonics.setLocalizedText(jLabel1, bundle.getString("driverClass")); // NOI18N
guiDriver.setText("org.postgresql.Driver");
org.openide.awt.Mnemonics.setLocalizedText(jLabel2, bundle.getString("dbUrl")); // NOI18N
guiDbURL.setText("jdbc:postgresql://<host>:5432/<database name>");
org.openide.awt.Mnemonics.setLocalizedText(jLabel3, bundle.getString("dbUser")); // NOI18N
org.openide.awt.Mnemonics.setLocalizedText(jLabel4, bundle.getString("dbPwd")); // NOI18N
org.openide.awt.Mnemonics.setLocalizedText(dbCreateButton, org.openide.util.NbBundle.getMessage(JSosOM2EditPane.class, "installMDWDatabase")); // NOI18N
dbCreateButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
dbCreateButtonActionPerformed(evt);
}
});
org.openide.awt.Mnemonics.setLocalizedText(jLabel5, org.openide.util.NbBundle.getMessage(JSosOM2EditPane.class, "postgisDir")); // NOI18N
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGap(0, 0, Short.MAX_VALUE)
.addComponent(dbCreateButton))
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel1)
.addComponent(jLabel2)
.addComponent(jLabel3)
.addComponent(jLabel4)
.addComponent(jLabel5))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(guiDriver)
.addComponent(guiDbURL, javax.swing.GroupLayout.DEFAULT_SIZE, 398, Short.MAX_VALUE)
.addComponent(guiUser)
.addComponent(guiPwd)
.addComponent(guiPGDir))))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(guiDriver, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(guiDbURL, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel3)
.addComponent(guiUser, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel4)
.addComponent(guiPwd, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel5)
.addComponent(guiPGDir, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 37, Short.MAX_VALUE)
.addComponent(dbCreateButton)
.addContainerGap())
);
}// </editor-fold>//GEN-END:initComponents
private void dbCreateButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_dbCreateButtonActionPerformed
final String className = guiDriver.getText();
final String connectURL = guiDbURL.getText();
final String user = guiUser.getText();
final String password = new String(guiPwd.getPassword());
final String pgDir = guiPGDir.getText();
final BDD bdd = new BDD(className, connectURL, user, password);
if (className.isEmpty() ||
connectURL.isEmpty() ||
user.isEmpty() ||
password.isEmpty() ||
pgDir.isEmpty()) {
JOptionPane.showMessageDialog(null, LayerRowModel.BUNDLE.getString("missingParameter"),
LayerRowModel.BUNDLE.getString("missingParameterTitle"), JOptionPane.ERROR_MESSAGE);
return;
}
// look for PGDir
final File pgDirectory = new File(pgDir);
if (!pgDirectory.isDirectory()) {
JOptionPane.showMessageDialog(null, LayerRowModel.BUNDLE.getString("invalidPostgisDir"),
LayerRowModel.BUNDLE.getString("invalidPostgisDirTitle"), JOptionPane.ERROR_MESSAGE);
return;
}
// try to connect first
try {
final Connection c = bdd.getFreshConnection();
c.close();
} catch (SQLException ex) {
JOptionPane.showMessageDialog(null, LayerRowModel.BUNDLE.getString("invalidConnection"),
LayerRowModel.BUNDLE.getString("invalidConnectionTitle"), JOptionPane.ERROR_MESSAGE);
return;
}
// look for existing database
boolean exist;
try {
final DataSource source = bdd.getDataSource();
exist = OM2DatabaseCreator.structurePresent(source);
} catch (SQLException ex) {
exist = false;
}
// build the database if not exist
if (!exist) {
try {
final DataSource source = bdd.getDataSource();
OM2DatabaseCreator.createObservationDatabase(source, true, pgDirectory);
} catch (IOException | SQLException ex) {
LOGGER.log(Level.WARNING, "Unable to create the OM2 database", ex);
}
} else {
JOptionPane.showMessageDialog(null, LayerRowModel.BUNDLE.getString("alreadyExistingDatabase"),
LayerRowModel.BUNDLE.getString("alreadyExistingDatabaseTitle"), JOptionPane.ERROR_MESSAGE);
}
}//GEN-LAST:event_dbCreateButtonActionPerformed
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton dbCreateButton;
private javax.swing.JTextField guiDbURL;
private javax.swing.JTextField guiDriver;
private javax.swing.JTextField guiPGDir;
private javax.swing.JPasswordField guiPwd;
private javax.swing.JTextField guiUser;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
// End of variables declaration//GEN-END:variables
@Override
public DataSourceType getDatasourceType() {
return DataSourceType.OM2;
}
}