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

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import oracle.jdbc.driver.OracleTypes;
import oracle.sql.BLOB;

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

/**
 * SNS logbook
 * 
 * @author <NAME>
 * @author <NAME>
 */
public class SNSLogbook implements ILogbook
{
    final private int MAX_TEXT_SIZE;
    private static final String DEFAULT_BADGE_NUMBER = "999992"; //$NON-NLS-1$
    final private RDBUtil rdb;
    final private String logbook;
    final String badge_number;

    /** Constructor
     *  @param rdb RDB connection
     *  @param user User (for which we'll try to get the badge number)
     *  @param logbook SNS logbook to use
     *  @throws Exception on error
     */
    public SNSLogbook(final RDBUtil rdb, final String user,
            final String logbook) throws Exception
    {
        this.rdb = rdb;
        this.logbook = logbook;
        badge_number = getBadgeNumber(user);
        /* The maximum allowed size of logbook text entry */
        MAX_TEXT_SIZE = getContentLength();
    }

    /** Create the Elog entry 
     * 
     *  @param String title   title of the elog entry
     *  @param String text    user entered text for the elog entry
     *  @param String imageName  name of the image to attach or null if no image
     *  @throws Exception
     *  @throws SQLException
     */
    @SuppressWarnings("nls")
    public void createEntry(String title, String text, String imageName)
            throws Exception
    {
       /** If the input imageName is null - meaning there's no image to attach
        *  and the text is not too large for an elog entry, create an average elog 
        *  entry.
        */

        if (imageName == null && text!=null && text.length()<MAX_TEXT_SIZE)
        {
            final String mysql = "call logbook.logbook_pkg.insert_logbook_entry"
                    + "(?, ?, ?, ?, ?)";
            final String category = "";
            // create an sql call for the elog
            final CallableStatement statement = rdb.getConnection().prepareCall(mysql);
            try
            {
                statement.setString(1, badge_number);
                statement.setString(2, logbook);
                statement.setString(3, title);
                statement.setString(4, category);
                statement.setString(5, text);
                statement.executeQuery();
            }
            finally
            {
                statement.close();
            }
            return;
        }
        
        // If text is made into an attachment due to size restraints, replace the
        // text that is now an attachment with text to explain why there is an
        // attachment
   final String info = "Input text exceeds " + MAX_TEXT_SIZE + " characters, see attachment.";
 
        // Get the Entry ID from the RDB for the attachment/image.
        final int entry_id =  getEntryID(title, info);

        // If an image was input add the image to the elog. 
        if(imageName != null)
        {   
           addFileToElog(imageName, "I", entry_id);
        }
        // Copy the oversized text entry to a text file and create and elog entry with
        //  this text file as an attachment.
        if(text!=null && text.length()>=MAX_TEXT_SIZE)
        {
           // Get a name for the created text file
           final File tmp_file = File.createTempFile("Logbook", ".txt");
           final String fname = tmp_file.getAbsolutePath();
           
           // Store the oversized text entry in the text file
           setContents(tmp_file, text);
           // Add the text attachment to the elog
           addFileToElog(fname, "A", entry_id);
           tmp_file.deleteOnExit();
        }
    }

    /** There will be multiple attachments, so ask the RDB for an entry id to
     *  be used to keep all the attachments for this elog entry together.
     * 
     *  @param String title   title of the elog entry
     *  @param String text    explanatory text for the elog entry
     *  @param String imageName  name of the image to attach or null if no image
     *  @throws Exception
     *  @throws SQLException
     */

    private int getEntryID(String title, String text) throws Exception
    {
      // Initiate the multi-file sql and retrieve the entry_id
       final String mysql = "call logbook.logbook_pkg.insert_logbook_entry"
          + "(?, ?, ?, ?, ?, ?)";
       final CallableStatement statement = rdb.getConnection().prepareCall(mysql);
       try
       {
           statement.setString(1, badge_number);
           statement.setString(2, logbook);
           statement.setString(3, title);
           statement.setString(4, "");
           statement.setString(5, text);
           statement.registerOutParameter(6, OracleTypes.NUMBER);
           statement.executeQuery();
           final int entry_id = Integer.parseInt(statement.getString(6));
           return entry_id;
       }
       finally
       {
          statement.close();
       }
    }
    
    /** Determine the type of attachment, based on file extension, and add it
     *  to the elog entry with the entry_id.
     * 
     *  @param String title   title of the elog entry<commit_msg>clobbered text when called with short text and image<commit_after>package org.csstudio.logbook.sns;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import oracle.jdbc.driver.OracleTypes;
import oracle.sql.BLOB;

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

/**
 * SNS logbook
 * 
 * @author <NAME>
 * @author <NAME>
 */
public class SNSLogbook implements ILogbook
{
    final private int MAX_TEXT_SIZE;
    private static final String DEFAULT_BADGE_NUMBER = "999992"; //$NON-NLS-1$
    final private RDBUtil rdb;
    final private String logbook;
    final String badge_number;

    /** Constructor
     *  @param rdb RDB connection
     *  @param user User (for which we'll try to get the badge number)
     *  @param logbook SNS logbook to use
     *  @throws Exception on error
     */
    public SNSLogbook(final RDBUtil rdb, final String user,
            final String logbook) throws Exception
    {
        this.rdb = rdb;
        this.logbook = logbook;
        badge_number = getBadgeNumber(user);
        /* The maximum allowed size of logbook text entry */
        MAX_TEXT_SIZE = getContentLength();
    }

    /** Create the Elog entry 
     * 
     *  @param String title   title of the elog entry
     *  @param String text    user entered text for the elog entry
     *  @param String imageName  name of the image to attach or null if no image
     *  @throws Exception
     *  @throws SQLException
     */
    @SuppressWarnings("nls")
    public void createEntry(String title, String text, String imageName)
            throws Exception
    {
        final int entry_id; // Entry ID from RDB
        
        if (text.length() < MAX_TEXT_SIZE)
        {
            // Short text goes into entry
            entry_id = createBasicEntry(title, text);
        }
        else
        {
            // If text is made into an attachment due to size restraints, replace the
            // text that is now an attachment with text to explain why there is an
            // attachment
            final String info = "Input text exceeds " + MAX_TEXT_SIZE +
                                " characters, see attachment.";
            entry_id = createBasicEntry(title, info);

            // Get a name for the created text file
            final File tmp_file = File.createTempFile("Logbook", ".txt");
            final String fname = tmp_file.getAbsolutePath();
            
            // Store the oversized text entry in the text file
            setContents(tmp_file, text);
            // Add the text attachment to the elog
            addFileToElog(fname, "A", entry_id);
            tmp_file.deleteOnExit();
        }

        // If an image was input add the image to the elog. 
        if (imageName != null)
           addFileToElog(imageName, "I", entry_id);
    }

    /** Create basic ELog entry with title and text, obtaining entry ID
     *  which would allow addition of attachments.
     * 
     *  @param String title   title of the elog entry
     *  @param String text    text for the elog entry
     *  @throws Exception on error
     */
    private int createBasicEntry(final String title, final String text) throws Exception
    {
      // Initiate the multi-file sql and retrieve the entry_id
       final String mysql = "call logbook.logbook_pkg.insert_logbook_entry"
          + "(?, ?, ?, ?, ?, ?)";
       final CallableStatement statement = rdb.getConnection().prepareCall(mysql);
       try
       {
           statement.setString(1, badge_number);
           statement.setString(2, logbook);
           statement.setString(3, title);
           statement.setString(4, "");
           statement.setString(5, text);
           statement.registerOutParameter(6, OracleTypes.NUMBER);
           statement.executeQuery();
           final int entry_id = Integer.parseInt(statement.getString(6));
           return entry_id;
       }
       finally
       {
          statement.close();
       }
    }
    
    /** Determine the type of attachment, based on file extension, and add it
     *  to the elog entry with the entry_id.
     * 
     *  @param String title   title of the elog entry
{'fraction_non_alphanumeric': 0.08380821379445863, 'fraction_numerical': 0.0032422872863037927, 'mean_word_length': 2.1749844042420463, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4446191', 'n_tokens_mistral': 2849, 'n_tokens_neox': 2787, 'n_words': 921}