/* * 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.openide.util.NbBundle; import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.sql.Connection; import java.sql.SQLException; import java.util.ResourceBundle; //import org.mdweb.sql.DatabaseCreator; //import org.mdweb.sql.DatabaseUpdater; //import org.mdweb.sql.DefaultDatabaseUpdater; /** * * @author Guilhem Legal (Geomatys) */ public class JCswMdwEditPane extends JServiceEditionPane { /** * Creates new form JCswMdwEditPane */ public JCswMdwEditPane(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()); } } guiIndexPubCheck.setSelected(configuration.getIndexOnlyPublishedMetadata()); guiIndexInterCheck.setSelected(configuration.getIndexInternalRecordset()); if (configuration.getProfileValue() != null) { guiProfileCombo.setSelectedItem(configuration.getProfileValue()); } } } @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("mdweb", bdd); configuration.setIndexOnlyPublishedMetadata(guiIndexPubCheck.isSelected()); configuration.setIndexInternalRecordset(guiIndexInterCheck.isSelected()); configuration.setProfile((String)guiProfileCombo.getSelectedItem()); 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 JLabel(); guiDriver = new JTextField(); jLabel2 = new JLabel(); guiDbURL = new JTextField(); jLabel3 = new JLabel(); guiUser = new JTextField(); jLabel4 = new JLabel(); guiPwd = new JPasswordField(); jLabel5 = new JLabel(); guiIndexPubCheck = new JCheckBox(); jLabel6 = new JLabel(); guiIndexInterCheck = new JCheckBox(); jLabel7 = new JLabel(); guiProfileCombo = new JComboBox(); dbCreateButton = new JButton(); setMinimumSize(new Dimension(562, 278)); ResourceBundle bundle = ResourceBundle.getBundle("org/constellation/swing/Bundle"); // NOI18N jLabel1.setText(bundle.getString("driverClass")); // NOI18N guiDriver.setText("org.postgresql.Driver"); jLabel2.setText(bundle.getString("dbUrl")); // NOI18N guiDbURL.setText("jdbc:postgresql://<host>:5432/<database name>"); jLabel3.setText(bundle.getString("dbUser")); // NOI18N jLabel4.setText(bundle.getString("dbPwd")); // NOI18N jLabel5.setText(bundle.getString("indexOnlyPublish")); // NOI18N jLabel6.setText(bundle.getString("indexInternal")); // NOI18N jLabel7.setText(bundle.getString("profil")); // NOI18N guiProfileCombo.setModel(new DefaultComboBoxModel(new String[] { "discovery", "transactional" })); dbCreateButton.setText(NbBundle.getMessage(JCswMdwEditPane.class, "installMDWDatabase")); // NOI18N dbCreateButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { dbCreateButtonActionPerformed(evt); } }); GroupLayout layout = new GroupLayout(this); this.setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING) .addComponent(jLabel1) .addComponent(jLabel2) .addComponent(jLabel3) .addComponent(jLabel4) .addComponent(jLabel5) .addComponent(jLabel6) .addComponent(jLabel7)) .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING) .addComponent(guiDriver, GroupLayout.DEFAULT_SIZE, 315, Short.MAX_VALUE) .addComponent(guiDbURL) .addComponent(guiUser) .addComponent(guiPwd) .addGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING) .addComponent(guiIndexInterCheck) .addComponent(guiIndexPubCheck)) .addGap(0, 0, Short.MAX_VALUE)) .addComponent(guiProfileCombo, 0, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))) .addGroup(GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addGap(0, 0, Short.MAX_VALUE) .addComponent(dbCreateButton))) .addContainerGap()) ); layout.setVerticalGroup( layout.createParallelGroup(GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE) .addComponent(jLabel1) .addComponent(guiDriver, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)) .addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE) .addComponent(jLabel2) .addComponent(guiDbURL, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)) .addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE) .addComponent(jLabel3) .addComponent(guiUser, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)) .addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE) .addComponent(jLabel4) .addComponent(guiPwd, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)) .addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE) .addComponent(jLabel5) .addComponent(guiIndexPubCheck)) .addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE) .addComponent(jLabel6) .addComponent(guiIndexInterCheck)) .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(GroupLayout.Alignment.TRAILING) .addComponent(jLabel7) .addComponent(guiProfileCombo, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)) .addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(dbCreateButton) .addContainerGap()) ); }// </editor-fold>//GEN-END:initComponents private void dbCreateButtonActionPerformed(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 BDD bdd = new BDD(className, connectURL, user, password); if (className.isEmpty() || connectURL.isEmpty() || user.isEmpty() || password.isEmpty()) { JOptionPane.showMessageDialog(null, LayerRowModel.BUNDLE.getString("missingParameter"), LayerRowModel.BUNDLE.getString("missingParameterTitle"), 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 = true; // try { // final DatabaseUpdater updater = new DefaultDatabaseUpdater(bdd.getDataSource(), true); // updater.isToUpgradeDatabase(); // } catch (SQLException ex) { // exist = false; // } // // // build the database if not exist // if (!exist) { // try { // DatabaseCreator creator = new DatabaseCreator(bdd.getDataSource(), true); // creator.createMetadataDatabase(true); // } catch (SQLException ex) { // LOGGER.log(Level.WARNING, "Unable to create the mdweb 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 JButton dbCreateButton; private JTextField guiDbURL; private JTextField guiDriver; private JCheckBox guiIndexInterCheck; private JCheckBox guiIndexPubCheck; private JComboBox guiProfileCombo; private JPasswordField guiPwd; private JTextField guiUser; private JLabel jLabel1; private JLabel jLabel2; private JLabel jLabel3; private JLabel jLabel4; private JLabel jLabel5; private JLabel jLabel6; private JLabel jLabel7; // End of variables declaration//GEN-END:variables @Override public DataSourceType getDatasourceType() { return DataSourceType.MDWEB; } }