<commit_before>/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.harvard.iq.dataverse;

import edu.harvard.iq.dataverse.engine.command.exception.CommandException;
import edu.harvard.iq.dataverse.engine.command.impl.DeleteGuestbookCommand;
import edu.harvard.iq.dataverse.engine.command.impl.UpdateDataverseCommand;
import edu.harvard.iq.dataverse.engine.command.impl.UpdateDataverseGuestbookRootCommand;
import edu.harvard.iq.dataverse.util.JsfHelper;
import static edu.harvard.iq.dataverse.util.JsfHelper.JH;
import java.util.LinkedList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.view.ViewScoped;
import javax.inject.Inject;
import javax.inject.Named;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author skraffmiller
 */
@ViewScoped
@Named
public class ManageGuestbooksPage implements java.io.Serializable {

    @EJB
    DataverseServiceBean dvService;

    @EJB
    GuestbookResponseServiceBean guestbookResponseService;
    
    @EJB
    GuestbookServiceBean guestbookService;
    
    @EJB
    EjbDataverseEngine engineService;

    @PersistenceContext(unitName = "VDCNet-ejbPU")
    EntityManager em;

    @Inject
    DataversePage dvpage;

    @Inject
    GuestbookPage guestbookPage;

    @Inject
    DataverseSession session;
    
    @Inject
    DataverseRequestServiceBean dvRequestService;

    private List<Guestbook> guestbooks;
    private List<GuestbookResponse> responses;
    private Dataverse dataverse;
    private Long dataverseId;
    private boolean inheritGuestbooksValue;

    private Guestbook selectedGuestbook = null;

    public void init() {
        dataverse = dvService.find(dataverseId);
        dvpage.setDataverse(dataverse);

        guestbooks = new LinkedList<>();
        setInheritGuestbooksValue(!dataverse.isGuestbookRoot());
        if (inheritGuestbooksValue && dataverse.getOwner() != null) {
            for (Guestbook pg : dataverse.getParentGuestbooks()) {
                pg.setUsageCount(guestbookService.findCountUsages(pg.getId(), dataverseId));
                pg.setResponseCount(guestbookResponseService.findCountByGuestbookId(pg.getId(), dataverseId));
                guestbooks.add(pg);
            }
        }
        for (Guestbook cg : dataverse.getGuestbooks()) {
            cg.setDeletable(true);
            cg.setUsageCount(guestbookService.findCountUsages(cg.getId(), dataverseId));
            if (!(guestbookService.findCountUsages(cg.getId(), null) == 0)) {
                cg.setDeletable(false);
            }
            cg.setResponseCount(guestbookResponseService.findCountByGuestbookId(cg.getId() , dataverseId));
            if (!(guestbookResponseService.findCountByGuestbookId(cg.getId() , null) == 0)) {
                cg.setDeletable(false);
            }
            cg.setDataverse(dataverse);
            guestbooks.add(cg);
        }
    }
    
    public void downloadResponsesByDataverse(){
        FacesContext ctx = FacesContext.getCurrentInstance();
        HttpServletResponse response = (HttpServletResponse) ctx.getExternalContext().getResponse();
        response.setContentType("text/comma-separated-values");
        String fileNameString = "attachment;filename=" + getFileName();
        response.setHeader("Content-Disposition", fileNameString);
        String converted = convertResponsesToTabDelimited(guestbookResponseService.findArrayByDataverseId(dataverseId));
        try {
            ServletOutputStream out = response.getOutputStream();
            out.write(converted.getBytes());                                                                                                                                                                                                                                                                                                                     
            out.flush();
            ctx.responseComplete();
        } catch (Exception e) {

        }
    }
    
    public void downloadResponsesByDataverseAndGuestbook(){
        FacesContext ctx = FacesContext.getCurrentInstance();
        HttpServletResponse response = (HttpServletResponse) ctx.getExternalContext().getResponse();
        response.setContentType("text/comma-separated-values");
        String fileNameString = "attachment;filename=" + getFileName();
        response.setHeader("Content-Disposition", fileNameString);
        //selectedGuestbook
        String converted = convertResponsesToTabDelimited(guestbookResponseService.findArrayByDataverseIdAndGuestbookId(dataverseId, selectedGuestbook.getId()));
        try {
            ServletOutputStream out = response.getOutputStream();
            out.write(converted.getBytes());                                                                                                                                                                                                                                                                                                                     
            out.flush();
            ctx.responseComplete();
        } catch (Exception e) {

        }
    }
    
    private String getFileName(){
       return  dataverse.getName() + "_GuestbookReponses.csv";
    }

    private final String SEPARATOR = ",";
    private final String END_OF_LINE = "\n";

    
    private String convertResponsesToTabDelimited(List<Object[]> guestbookResponses) {

        StringBuilder sb = new StringBuilder();
        sb.append("Dataset, Time, Type, File Name, User Name, Email, Institution, Position, Custom Questions");
        sb.append(END_OF_LINE);
        for (Object[] array : guestbookResponses) {
            sb.append(array[0]);
            sb.append(SEPARATOR);
            sb.append(array[1]);
            sb.append(SEPARATOR);
            sb.append(array[2]);
            sb.append(SEPARATOR);
            sb.append(array[3]);
            sb.append(SEPARATOR);
            sb.append(array[4]);
            sb.append(SEPARATOR);
            sb.append(array[5] == null ? "" : array[5]);
            sb.append(SEPARATOR);
            sb.append(array[6] == null ? "" : array[6]);
            sb.append(SEPARATOR);
            sb.append(array[7] == null ? "" : array[7]);
            if(array[8] != null){
                sb.append(SEPARATOR);
                List <CustomQuestionResponse> responses = (List<CustomQuestionResponse>) array[8];
                for (CustomQuestionResponse response: responses){
                    sb.append(response.getCustomQuestion().getQuestionString());
                    sb.append(SEPARATOR);
                    sb.append(response.getResponse() == null ? "" : response.getResponse());
                }
            }
            sb.append(END_OF_LINE);
        }
        return sb.toString();
    }

<commit_msg>#3064 Fix Column headers and method names<commit_after>/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.harvard.iq.dataverse;

import edu.harvard.iq.dataverse.engine.command.exception.CommandException;
import edu.harvard.iq.dataverse.engine.command.impl.DeleteGuestbookCommand;
import edu.harvard.iq.dataverse.engine.command.impl.UpdateDataverseCommand;
import edu.harvard.iq.dataverse.engine.command.impl.UpdateDataverseGuestbookRootCommand;
import edu.harvard.iq.dataverse.util.JsfHelper;
import static edu.harvard.iq.dataverse.util.JsfHelper.JH;
import java.util.LinkedList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.view.ViewScoped;
import javax.inject.Inject;
import javax.inject.Named;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author skraffmiller
 */
@ViewScoped
@Named
public class ManageGuestbooksPage implements java.io.Serializable {

    @EJB
    DataverseServiceBean dvService;

    @EJB
    GuestbookResponseServiceBean guestbookResponseService;
    
    @EJB
    GuestbookServiceBean guestbookService;
    
    @EJB
    EjbDataverseEngine engineService;

    @PersistenceContext(unitName = "VDCNet-ejbPU")
    EntityManager em;

    @Inject
    DataversePage dvpage;

    @Inject
    GuestbookPage guestbookPage;

    @Inject
    DataverseSession session;
    
    @Inject
    DataverseRequestServiceBean dvRequestService;

    private List<Guestbook> guestbooks;
    private List<GuestbookResponse> responses;
    private Dataverse dataverse;
    private Long dataverseId;
    private boolean inheritGuestbooksValue;

    private Guestbook selectedGuestbook = null;

    public void init() {
        dataverse = dvService.find(dataverseId);
        dvpage.setDataverse(dataverse);

        guestbooks = new LinkedList<>();
        setInheritGuestbooksValue(!dataverse.isGuestbookRoot());
        if (inheritGuestbooksValue && dataverse.getOwner() != null) {
            for (Guestbook pg : dataverse.getParentGuestbooks()) {
                pg.setUsageCount(guestbookService.findCountUsages(pg.getId(), dataverseId));
                pg.setResponseCount(guestbookResponseService.findCountByGuestbookId(pg.getId(), dataverseId));
                guestbooks.add(pg);
            }
        }
        for (Guestbook cg : dataverse.getGuestbooks()) {
            cg.setDeletable(true);
            cg.setUsageCount(guestbookService.findCountUsages(cg.getId(), dataverseId));
            if (!(guestbookService.findCountUsages(cg.getId(), null) == 0)) {
                cg.setDeletable(false);
            }
            cg.setResponseCount(guestbookResponseService.findCountByGuestbookId(cg.getId() , dataverseId));
            if (!(guestbookResponseService.findCountByGuestbookId(cg.getId() , null) == 0)) {
                cg.setDeletable(false);
            }
            cg.setDataverse(dataverse);
            guestbooks.add(cg);
        }
    }
    
    public void downloadResponsesByDataverse(){
        FacesContext ctx = FacesContext.getCurrentInstance();
        HttpServletResponse response = (HttpServletResponse) ctx.getExternalContext().getResponse();
        response.setContentType("text/comma-separated-values");
        String fileNameString = "attachment;filename=" + getFileName();
        response.setHeader("Content-Disposition", fileNameString);
        String converted = convertResponsesToCommaDelimited(guestbookResponseService.findArrayByDataverseId(dataverseId));
        try {
            ServletOutputStream out = response.getOutputStream();
            out.write(converted.getBytes());                                                                                                                                                                                                                                                                                                                     
            out.flush();
            ctx.responseComplete();
        } catch (Exception e) {

        }
    }
    
    public void downloadResponsesByDataverseAndGuestbook(){
        FacesContext ctx = FacesContext.getCurrentInstance();
        HttpServletResponse response = (HttpServletResponse) ctx.getExternalContext().getResponse();
        response.setContentType("text/comma-separated-values");
        String fileNameString = "attachment;filename=" + getFileName();
        response.setHeader("Content-Disposition", fileNameString);
        //selectedGuestbook
        String converted = convertResponsesToCommaDelimited(guestbookResponseService.findArrayByDataverseIdAndGuestbookId(dataverseId, selectedGuestbook.getId()));
        try {
            ServletOutputStream out = response.getOutputStream();
            out.write(converted.getBytes());                                                                                                                                                                                                                                                                                                                     
            out.flush();
            ctx.responseComplete();
        } catch (Exception e) {

        }
    }
    
    private String getFileName(){
       return  dataverse.getName() + "_GuestbookReponses.csv";
    }

    private final String SEPARATOR = ",";
    private final String END_OF_LINE = "\n";

    
    private String convertResponsesToCommaDelimited(List<Object[]> guestbookResponses) {

        StringBuilder sb = new StringBuilder();
        sb.append("Dataset, Date, Type, File Name, User Name, Email, Institution, Position, Custom Questions");
        sb.append(END_OF_LINE);
        for (Object[] array : guestbookResponses) {
            sb.append(array[0]);
            sb.append(SEPARATOR);
            sb.append(array[1]);
            sb.append(SEPARATOR);
            sb.append(array[2]);
            sb.append(SEPARATOR);
            sb.append(array[3]);
            sb.append(SEPARATOR);
            sb.append(array[4]);
            sb.append(SEPARATOR);
            sb.append(array[5] == null ? "" : array[5]);
            sb.append(SEPARATOR);
            sb.append(array[6] == null ? "" : array[6]);
            sb.append(SEPARATOR);
            sb.append(array[7] == null ? "" : array[7]);
            if(array[8] != null){
                List <CustomQuestionResponse> responses = (List<CustomQuestionResponse>) array[8];               
                for (CustomQuestionResponse response: responses){
                    sb.append(SEPARATOR);
                    sb.append(response.getCustomQuestion().getQuestionString());
                    sb.append(SEPARATOR);
                    sb.append(response.getResponse() == null ? "" : response.getResponse());
                }
            }
            sb.append(END_OF_LINE);
        }
        return sb.toString();
    }


{'fraction_non_alphanumeric': 0.096521918090142, 'fraction_numerical': 0.0023324415174590107, 'mean_word_length': 2.120291095890411, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 60, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '374451', 'n_tokens_mistral': 3740, 'n_tokens_neox': 3643, 'n_words': 636}