<commit_before>/*
 * Copyright (c) 2010 Brookhaven National Laboratory
 * Copyright (c) 2010 Helmholtz-Zentrum Berlin für Materialien und Energie GmbH
 * Subject to license terms and conditions.
 */
package edu.msu.nscl.olog;

import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.ws.rs.core.Response;
import org.apache.ibatis.exceptions.PersistenceException;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

/**
 * JDBC query to create one log.
 *
 * @author <NAME> taken from <NAME> <<EMAIL>>
 */
public class CreateLogQuery {

    private static SqlSessionFactory ssf = MyBatisSession.getSessionFactory();

    private CreateLogQuery() {
    }

    /**
     * Creates a log and its logbooks/tags in the database.
     *
     * @param log XmlLog object
     * @throws CFException wrapping an SQLException
     */
    public static XmlLog createLog(XmlLog log) throws CFException, UnsupportedEncodingException, NoSuchAlgorithmException {
        SqlSession ss = ssf.openSession();

        try {

            HashMap<String, Object> hm = new HashMap<String, Object>();

            hm.put("source", log.getSource());
            hm.put("owner", log.getOwner());
            hm.put("level", log.getLevel());
            //hm.put("subject", log.getSubject());
            hm.put("description", log.getDescription());

            ss.insert("mappings.LogMapping.createLog", hm);
            int logId = (Integer) ss.selectOne("mappings.LogMapping.lastId");

            if (logId > 0) {

                // Fetch the logbook/tag ids
                Map<String, Integer> pids = FindLogbookIdsQuery.getLogbookIdMap(log);

                // Insert logbook/tags
                // Fail if there isn't at least one logbook
                if (log.getXmlLogbooks().isEmpty()) {
                    throw new CFException(Response.Status.INTERNAL_SERVER_ERROR,
                            "There is no logbook associated with log: " + log.getId());
                }
                if (log.getXmlLogbooks().size() > 0 || log.getXmlTags().size() > 0) {
                    for (XmlLogbook logbook : log.getXmlLogbooks()) {
                        if (pids.get(logbook.getName()) == null) {
                            throw new CFException(Response.Status.NOT_FOUND,
                                    "Logbook '" + logbook.getName() + "' does not exist");
                        }

                        hm.clear();
                        hm.put("logid", logId);
                        hm.put("logbookid", FindLogbookIdsQuery.getLogbookId(logbook.getName()));
                        hm.put("state", null);
                        ss.insert("mappings.LogMapping.logsLogbooksEntry", hm);

                    }
                    for (XmlTag tag : log.getXmlTags()) {
                        if (pids.get(tag.getName()) == null) {
                            throw new CFException(Response.Status.NOT_FOUND,
                                    "Tag '" + tag.getName() + "' does not exist");
                        }
                        hm.clear();
                        hm.put("logid", logId);
                        hm.put("logbookid", FindLogbookIdsQuery.getLogbookId(tag.getName()));
                        hm.put("state", null);
                        ss.insert("mappings.LogMapping.logsLogbooksEntry", hm);
                    }
                }
                if (log.getXmlProperties().size() > 0) {
                    for (XmlProperty property : log.getXmlProperties()) {
                        if (property.getName().isEmpty() || property.getName() == null) {
                            throw new CFException(Response.Status.NOT_FOUND,
                                    "Property name (key) can not be null ");
                        }
                        hm.clear();
                        hm.put("lid", logId);
                        hm.put("name", property.getName());
                        ss.insert("mappings.PropertyMapping.addProperty", hm);

                        int propId = (Integer) ss.selectOne("mappings.PropertyMapping.lastId");
                        if (propId > 0) {
                            hm.clear();
                            hm.put("pid", propId);
                            Map<String, String> attributes = property.getAttributes();
                            Iterator attribute = attributes.entrySet().iterator();
                            while (attribute.hasNext()) {
                                Map.Entry e = (Map.Entry) attribute.next();
                                hm.put("name", e.getKey());
                                hm.put("value", e.getValue());
                                ss.insert("mappings.PropertyMapping.addPropertyAttribute", hm);
                            }
                        } else {
                            throw new CFException(Response.Status.NOT_FOUND, "Property attributes could not be created");
                        }

                    }
                }
                
                ss.commit();

                // Get log object directly from db so all information is filled in and we can update the md5
                log = FindLogsQuery.findLogByIdNoMD5((long) logId);

                hm.clear();

                hm.put("md5entry", getmd5Entry((long) logId, log));
                hm.put("md5recent", getmd5Recent((long) logId));
                hm.put("id", (long) logId);

                ss.update("mappings.LogMapping.updateMD5", hm);
            } else {
                throw new CFException(Response.Status.NOT_FOUND,
                        "Log could not be created");
            }

            ss.commit();
        } catch (PersistenceException e) {<commit_msg>Fix migration of properties from an old log to a new log<commit_after>/*
 * Copyright (c) 2010 Brookhaven National Laboratory
 * Copyright (c) 2010 Helmholtz-Zentrum Berlin für Materialien und Energie GmbH
 * Subject to license terms and conditions.
 */
package edu.msu.nscl.olog;

import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.ws.rs.core.Response;
import org.apache.ibatis.exceptions.PersistenceException;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

/**
 * JDBC query to create one log.
 *
 * @author <NAME> taken from <NAME> <<EMAIL>>
 */
public class CreateLogQuery {

    private static SqlSessionFactory ssf = MyBatisSession.getSessionFactory();

    private CreateLogQuery() {
    }

    /**
     * Creates a log and its logbooks/tags in the database.
     *
     * @param log XmlLog object
     * @throws CFException wrapping an SQLException
     */
    public static XmlLog createLog(XmlLog log) throws CFException, UnsupportedEncodingException, NoSuchAlgorithmException {
        SqlSession ss = ssf.openSession();

        try {

            HashMap<String, Object> hm = new HashMap<String, Object>();

            hm.put("source", log.getSource());
            hm.put("owner", log.getOwner());
            hm.put("level", log.getLevel());
            //hm.put("subject", log.getSubject());
            hm.put("description", log.getDescription());

            ss.insert("mappings.LogMapping.createLog", hm);
            int logId = (Integer) ss.selectOne("mappings.LogMapping.lastId");

            if (logId > 0) {

                // Fetch the logbook/tag ids
                Map<String, Integer> pids = FindLogbookIdsQuery.getLogbookIdMap(log);

                // Insert logbook/tags
                // Fail if there isn't at least one logbook
                if (log.getXmlLogbooks().isEmpty()) {
                    throw new CFException(Response.Status.INTERNAL_SERVER_ERROR,
                            "There is no logbook associated with log: " + log.getId());
                }
                if (log.getXmlLogbooks().size() > 0 || log.getXmlTags().size() > 0) {
                    for (XmlLogbook logbook : log.getXmlLogbooks()) {
                        if (pids.get(logbook.getName()) == null) {
                            throw new CFException(Response.Status.NOT_FOUND,
                                    "Logbook '" + logbook.getName() + "' does not exist");
                        }

                        hm.clear();
                        hm.put("logid", logId);
                        hm.put("logbookid", FindLogbookIdsQuery.getLogbookId(logbook.getName()));
                        hm.put("state", null);
                        ss.insert("mappings.LogMapping.logsLogbooksEntry", hm);

                    }
                    for (XmlTag tag : log.getXmlTags()) {
                        if (pids.get(tag.getName()) == null) {
                            throw new CFException(Response.Status.NOT_FOUND,
                                    "Tag '" + tag.getName() + "' does not exist");
                        }
                        hm.clear();
                        hm.put("logid", logId);
                        hm.put("logbookid", FindLogbookIdsQuery.getLogbookId(tag.getName()));
                        hm.put("state", null);
                        ss.insert("mappings.LogMapping.logsLogbooksEntry", hm);
                    }
                }
                if (log.getXmlProperties().size() > 0) {
                    for (XmlProperty property : log.getXmlProperties()) {
                        if (property.getName().isEmpty() || property.getName() == null) {
                            throw new CFException(Response.Status.NOT_FOUND,
                                    "Property name (key) can not be null ");
                        }

                        int propId;
                        if (property.getId() > 0) {
                            propId = property.getId();
                        }
                        else {
                            XmlProperty prop = (XmlProperty) ss.selectOne("mappings.PropertyMapping.getProperty", property.getName());
                            propId = prop.getId();
                        }
                        if (propId > 0) {
                            hm.clear();
                            hm.put("lid", logId);
                            hm.put("pid", propId);
                            Map<String, String> attributes = property.getAttributes();
                            Iterator attribute = attributes.entrySet().iterator();
                            while (attribute.hasNext()) {
                                Map.Entry e = (Map.Entry) attribute.next();
                                hm.put("attribute", e.getKey());
                                hm.put("value", e.getValue());
                                ss.insert("mappings.PropertyMapping.addAttributeToLog", hm);
                            }
                        } else {
                            throw new CFException(Response.Status.NOT_FOUND, "Property attributes could not be created");
                        }

                    }
                }

                ss.commit();

                // Get log object directly from db so all information is filled in and we can update the md5
                log = FindLogsQuery.findLogByIdNoMD5((long) logId);

                hm.clear();

                hm.put("md5entry", getmd5Entry((long) logId, log));
                hm.put("md5recent", getmd5Recent((long) logId));
                hm.put("id", (long) logId);

                ss.update("mappings.LogMapping.updateMD5", hm);
            } else {
                throw new CFException(Response.Status.NOT_FOUND,
                        "Log could not be created");
            }

            ss.commit();
        } catch (PersistenceException e) {
{'fraction_non_alphanumeric': 0.11014871711063899, 'fraction_numerical': 0.003350220624285014, 'mean_word_length': 1.4561509131045556, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 30, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6812829', 'n_tokens_mistral': 3198, 'n_tokens_neox': 2968, 'n_words': 677}