<commit_before>/**
 * 
 */
package com.intuit.tank.script;

/*
 * #%L
 * JSF Support Beans
 * %%
 * Copyright (C) 2011 - 2015 Intuit Inc.
 * %%
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * #L%
 */

import java.io.InputStream;
import java.io.Serializable;

import javax.enterprise.context.RequestScoped;
import javax.enterprise.event.Event;
import javax.inject.Inject;
import javax.inject.Named;

import com.intuit.tank.auth.TankSecurityContext;
import com.intuit.tank.script.util.ScriptServiceUtil;
import org.apache.commons.io.IOUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import com.intuit.tank.util.Messages;
import org.primefaces.event.FileUploadEvent;
import org.primefaces.model.file.UploadedFile;

import com.intuit.tank.ModifiedScriptMessage;
import com.intuit.tank.api.model.v1.script.ScriptTO;
import com.intuit.tank.auth.Security;
import com.intuit.tank.dao.ScriptDao;
import com.intuit.tank.project.Script;
import com.intuit.tank.qualifier.Modified;
import com.intuit.tank.util.UploadedFileIterator;
import com.intuit.tank.wrapper.FileInputStreamWrapper;

@Named
@RequestScoped
public class TankXmlUploadBean implements Serializable {
    private static final Logger LOG = LogManager.getLogger(TankXmlUploadBean.class);

    private static final long serialVersionUID = 1L;

    private boolean useFlash = true;

    @Inject
    @Modified
    private Event<ModifiedScriptMessage> scriptEvent;

    @Inject
    private TankSecurityContext securityContext;

    @Inject
    private Messages messages;

    @Inject
    private Security security;

    public TankXmlUploadBean() {
    }

    public void handleFileUpload(FileUploadEvent event) throws Exception {
        LOG.info("Success! " + event.getFile().getFileName() + " is uploaded.");
        messages.info("Success! " + event.getFile().getFileName() + " is uploaded.");

        UploadedFile item = event.getFile();

        if (item != null) {
            try {
                UploadedFileIterator uploadedFileIterator = new UploadedFileIterator(item, "xml");
                FileInputStreamWrapper w = uploadedFileIterator.getNext();
                while (w != null) {
                    processScript(w.getInputStream(), w.getFileName());
                    w = uploadedFileIterator.getNext();
                }
            } catch (Exception e) {
                LOG.error("Error extracting zip file: " + e.toString());
                messages.error("Error extracting zip file: " + e.toString());
                throw new RuntimeException(e);
            } finally {
                IOUtils.closeQuietly(item.getInputStream());
            }
        }
    }

    public void clearDialog() {
        messages.clear();
    }

    private void processScript(InputStream inputStream, String fileName) {
        ScriptDao dao = new ScriptDao();

        LOG.info("Parsing xml script " + fileName + "to TO");
        ScriptTO scriptTo = ScriptServiceUtil.parseXMLtoScriptTO(inputStream);
        LOG.info("Parsed xml script " + fileName + "to TO");
        Script script = ScriptServiceUtil.transferObjectToScript(scriptTo);
        LOG.info("Parsing TO to script " + fileName);

        if (script.getId() > 0) {
            Script existing = dao.findById(script.getId());
            if (existing == null) {
                LOG.error("Error updating script: Script passed with unknown id.");
                messages.error("Script " + fileName + " passed with unknown id.");
                return;
            }
            if (!existing.getName().equals(script.getName())) {
                LOG.error("Error updating script: Cannot change the name of an existing Script.");
                messages.error("Cannot change the name of an existing script.");
                return;
            }
            if (!security.isAdmin() && !security.isOwner(script)) {
                LOG.error("Error updating script: Cannot change the name of an existing Script.");
                messages.error("You do not have rights to modify " + script.getName() + ".");
                return;
            }
            script.setSerializedScriptStepId(existing.getSerializedScriptStepId());
        } else {
            script.setCreator(securityContext.getCallerPrincipal().getName());
        }
        script = dao.saveOrUpdate(script);
        LOG.info("Script " + script.getName() + " from file " + fileName + " has been added.");<commit_msg>SRE-19610 more debug messages<commit_after>/**
 * 
 */
package com.intuit.tank.script;

/*
 * #%L
 * JSF Support Beans
 * %%
 * Copyright (C) 2011 - 2015 Intuit Inc.
 * %%
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * #L%
 */

import java.io.InputStream;
import java.io.Serializable;

import javax.enterprise.context.RequestScoped;
import javax.enterprise.event.Event;
import javax.inject.Inject;
import javax.inject.Named;

import com.intuit.tank.auth.TankSecurityContext;
import com.intuit.tank.script.util.ScriptServiceUtil;
import org.apache.commons.io.IOUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import com.intuit.tank.util.Messages;
import org.primefaces.event.FileUploadEvent;
import org.primefaces.model.file.UploadedFile;

import com.intuit.tank.ModifiedScriptMessage;
import com.intuit.tank.api.model.v1.script.ScriptTO;
import com.intuit.tank.auth.Security;
import com.intuit.tank.dao.ScriptDao;
import com.intuit.tank.project.Script;
import com.intuit.tank.qualifier.Modified;
import com.intuit.tank.util.UploadedFileIterator;
import com.intuit.tank.wrapper.FileInputStreamWrapper;

@Named
@RequestScoped
public class TankXmlUploadBean implements Serializable {
    private static final Logger LOG = LogManager.getLogger(TankXmlUploadBean.class);

    private static final long serialVersionUID = 1L;

    private boolean useFlash = true;

    @Inject
    @Modified
    private Event<ModifiedScriptMessage> scriptEvent;

    @Inject
    private TankSecurityContext securityContext;

    @Inject
    private Messages messages;

    @Inject
    private Security security;

    public TankXmlUploadBean() {
    }

    public void handleFileUpload(FileUploadEvent event) throws Exception {
        LOG.info("Success! " + event.getFile().getFileName() + " is uploaded.");
        messages.info("Success! " + event.getFile().getFileName() + " is uploaded.");

        UploadedFile item = event.getFile();

        if (item != null) {
            try {
                UploadedFileIterator uploadedFileIterator = new UploadedFileIterator(item, "xml");
                FileInputStreamWrapper w = uploadedFileIterator.getNext();
                while (w != null) {
                    processScript(w.getInputStream(), w.getFileName());
                    w = uploadedFileIterator.getNext();
                }
            } catch (Exception e) {
                LOG.error("Error extracting zip file: " + e.toString());
                messages.error("Error extracting zip file: " + e.toString());
                throw new RuntimeException(e);
            } finally {
                IOUtils.closeQuietly(item.getInputStream());
            }
        }
    }

    public void clearDialog() {
        messages.clear();
    }

    private void processScript(InputStream inputStream, String fileName) {
        ScriptDao dao = new ScriptDao();

        LOG.info("debug: Parsing xml script " + fileName + "to TO");
        ScriptTO scriptTo = ScriptServiceUtil.parseXMLtoScriptTO(inputStream);
        LOG.info("debug: Parsed xml script " + fileName + "to TO");
        Script script = ScriptServiceUtil.transferObjectToScript(scriptTo);
        LOG.info("debug: Parsing TO to script " + fileName);

        if (script.getId() > 0) {
            LOG.info("debug: finding script by id " + fileName);
            Script existing = dao.findById(script.getId());
            LOG.info("debug: found script by id " + script.getId() );

            if (existing == null) {
                LOG.error("Error updating script: Script passed with unknown id.");
                messages.error("Script " + fileName + " passed with unknown id.");
                return;
            }
            if (!existing.getName().equals(script.getName())) {
                LOG.error("Error updating script: Cannot change the name of an existing Script.");
                messages.error("Cannot change the name of an existing script.");
                return;
            }
            if (!security.isAdmin() && !security.isOwner(script)) {
                LOG.error("Error updating script: Cannot change the name of an existing Script.");
                messages.error("You do not have rights to modify " + script.getName() + ".");
                return;
            }
            script.setSerializedScriptStepId(existing.getSerializedScriptStepId());

        } else {
            script.setCreator(securityContext.getCallerPrincipal().getName());
        }
        LOG.info("debug: saving script " + script.getName());
        script = dao.saveOrUpdate(script);
        LOG.info("debug: saved script " + script.getName());

        LOG.info("Script " + script.getName() + " from file " + fileName + " has been added.");
{'fraction_non_alphanumeric': 0.11054492512479201, 'fraction_numerical': 0.004055740432612313, 'mean_word_length': 2.6608298439284357, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 6}, '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': '4444053', 'n_tokens_mistral': 2655, 'n_tokens_neox': 2562, 'n_words': 635}