<commit_before>package org.csstudio.logbook.sns;

import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;

import org.csstudio.logbook.ILogbook;
import org.csstudio.logbook.ILogbookFactory;
import org.csstudio.platform.utility.rdb.RDBUtil;

/** Implementation of the ILogbookFactory.
 *  Plugin.xml declares this class as the logbook factory extension point.
 *  @author <NAME>
 *  @author <NAME>
 */
public class SNSLogbookFactory implements ILogbookFactory
{
    /** {@inheritDoc} */
    @SuppressWarnings("nls")
    public String[] getLogbooks() throws Exception
    {
        final ArrayList<String> names = new ArrayList<String>();
        final RDBUtil rdb = RDBUtil.connect(Preferences.getURL(),
              Preferences.getLogListUser(), Preferences.getLogListPassword());
        final Statement statement = rdb.getConnection().createStatement();
        try
        {
            final ResultSet result = statement.executeQuery(
                    "SELECT oper_grp_nm FROM oper.oper_grp " +
	                "WHERE elog_ind='Y' ORDER BY oper_grp_nm");
            while (result.next())
                names.add(result.getString(1));
        }
        finally
        {
            statement.close();
            rdb.close();
        }
        // Convert into plain array
        final String ret_val[] = new String[names.size()];
        return names.toArray(ret_val);
    }
    
    /** {@inheritDoc} */
    public String getDefaultLogbook()
    {
        return Preferences.getDefaultLogbook();
    }

    /** {@inheritDoc} */
    public ILogbook connect(final String logbook, final String user, final String password)
            throws Exception
    {
        return connect(Preferences.getURL(), logbook, user, password);
    }

    /** Connect to logbook.
     *  <p>
     *  This call is <u>only for unit tests</u>.
     *  Application code should use the
     *   <code>org.csstudio.logbook.sns.LogbookFactory</code>
     *  and not directly call the <code>SNSLogbookFactory</code>.
     *  
     *  @param url RDB URL
     *  @param logbook Logbook name
     *  @param user   RDB user
     *  @param password RDB password
     *  @return ILogbook
     *  @throws Exception
     */
    @SuppressWarnings("nls")
    public ILogbook connect(final String url, final String logbook,
            final String user, final String password) throws Exception
    {
        // Connect to the SNS RDB
        if (user.length() <= 0)
            throw new Exception("Empty user name");
        if (password.length() <= 0)
            throw new Exception("Empty password");
        if (url == null)
            throw new Exception("Missing logbook URL");
        if (logbook == null)
            throw new Exception("Missing logbook name");
        final RDBUtil rdb = RDBUtil.connect(url, user, password);
        return new SNSLogbook(rdb, user, logbook);
    }

   @Override
   public String[] getLoogbooks() throws Exception
   {
      // TODO Auto-generated method stub
      return null;
   }
}<commit_msg>new API for connect<commit_after>package org.csstudio.logbook.sns;

import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;

import org.csstudio.logbook.ILogbook;
import org.csstudio.logbook.ILogbookFactory;
import org.csstudio.platform.utility.rdb.RDBUtil;

/** Implementation of the ILogbookFactory.
 *  Plugin.xml declares this class as the logbook factory extension point.
 *  @author <NAME>
 *  @author <NAME>
 */
public class SNSLogbookFactory implements ILogbookFactory
{
    /** {@inheritDoc} */
    @SuppressWarnings("nls")
    public String[] getLogbooks() throws Exception
    {
        final ArrayList<String> names = new ArrayList<String>();
        final RDBUtil rdb = RDBUtil.connect(Preferences.getURL(),
                Preferences.getLogListUser(), Preferences.getLogListPassword(),
                false);
        final Statement statement = rdb.getConnection().createStatement();
        try
        {
            final ResultSet result = statement.executeQuery(
                    "SELECT oper_grp_nm FROM oper.oper_grp " +
	                "WHERE elog_ind='Y' ORDER BY oper_grp_nm");
            while (result.next())
                names.add(result.getString(1));
        }
        finally
        {
            statement.close();
            rdb.close();
        }
        // Convert into plain array
        final String ret_val[] = new String[names.size()];
        return names.toArray(ret_val);
    }
    
    /** {@inheritDoc} */
    public String getDefaultLogbook()
    {
        return Preferences.getDefaultLogbook();
    }

    /** {@inheritDoc} */
    public ILogbook connect(final String logbook, final String user, final String password)
            throws Exception
    {
        return connect(Preferences.getURL(), logbook, user, password);
    }

    /** Connect to logbook.
     *  <p>
     *  This call is <u>only for unit tests</u>.
     *  Application code should use the
     *   <code>org.csstudio.logbook.sns.LogbookFactory</code>
     *  and not directly call the <code>SNSLogbookFactory</code>.
     *  
     *  @param url RDB URL
     *  @param logbook Logbook name
     *  @param user   RDB user
     *  @param password RDB password
     *  @return ILogbook
     *  @throws Exception
     */
    @SuppressWarnings("nls")
    public ILogbook connect(final String url, final String logbook,
            final String user, final String password) throws Exception
    {
        // Connect to the SNS RDB
        if (user.length() <= 0)
            throw new Exception("Empty user name");
        if (password.length() <= 0)
            throw new Exception("Empty password");
        if (url == null)
            throw new Exception("Missing logbook URL");
        if (logbook == null)
            throw new Exception("Missing logbook name");
        final RDBUtil rdb = RDBUtil.connect(url, user, password, false);
        return new SNSLogbook(rdb, user, logbook);
    }
}
{'fraction_non_alphanumeric': 0.10458715596330276, 'fraction_numerical': 0.001000834028356964, 'mean_word_length': 2.332962757087271, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2410329', 'n_tokens_mistral': 1707, 'n_tokens_neox': 1684, 'n_words': 461}