package edu.harvard.iq.dataverse.api.datadeposit;

import edu.harvard.iq.dataverse.ControlledVocabularyValue;
import edu.harvard.iq.dataverse.DatasetField;
import edu.harvard.iq.dataverse.DatasetFieldConstant;
import edu.harvard.iq.dataverse.DatasetFieldServiceBean;
import edu.harvard.iq.dataverse.DatasetFieldType;
import edu.harvard.iq.dataverse.DatasetFieldValue;
import edu.harvard.iq.dataverse.DatasetVersion;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.inject.Named;
import org.swordapp.server.SwordError;
import org.swordapp.server.UriRegistry;

@Stateless
@Named
public class SwordServiceBean {

    private static final Logger logger = Logger.getLogger(SwordServiceBean.class.getCanonicalName());

    @EJB
    DatasetFieldServiceBean datasetFieldService;

    /**
     * Mutate the dataset version, adding a datasetContact (email address) from
     * the dataverse that will own the dataset.
     */
    public void addDatasetContact(DatasetVersion newDatasetVersion) {
        DatasetFieldType emailDatasetFieldType = datasetFieldService.findByNameOpt(DatasetFieldConstant.datasetContact);
        DatasetField emailDatasetField = DatasetField.createNewEmptyDatasetField(emailDatasetFieldType, newDatasetVersion);

        for (DatasetField childField : emailDatasetField.getDatasetFieldCompoundValues().get(0).getChildDatasetFields()) {
            if (DatasetFieldConstant.datasetContactEmail.equals(childField.getDatasetFieldType().getName())) {
                // set the value to that of the owning dataverse
                childField.getSingleValue().setValue(newDatasetVersion.getDataset().getOwner().getContactEmail());
            }
        }

        newDatasetVersion.getDatasetFields().add(emailDatasetField);

    }

    public void addDatasetSubject(DatasetVersion datasetVersion) {
        DatasetFieldType subjectDatasetFieldType = datasetFieldService.findByNameOpt(DatasetFieldConstant.subject);
        DatasetField subjectDatasetField = DatasetField.createNewEmptyDatasetField(subjectDatasetFieldType, datasetVersion);

        /**
         * @todo Rather than hard coding "Other" here, inherit the Subject from
         * the parent dataverse, when it's available once
         * https://github.com/IQSS/dataverse/issues/769 has been implemented
         */
        String subjectOther = "Other";
        ControlledVocabularyValue cvv = datasetFieldService.findControlledVocabularyValueByDatasetFieldTypeAndStrValue(subjectDatasetFieldType, subjectOther);
        if (cvv != null) {
            subjectDatasetField.setSingleControlledVocabularyValue(cvv);
            
            datasetVersion.getDatasetFields().add(subjectDatasetField);

        } else {
            logger.info(subjectDatasetFieldType.getName() + "could not be populated with '" + subjectOther + "': null returned from lookup.");
        }
    }
}

@@ -2181,256 +2181,178 @@
 odo 
-Rather than hard coding %22Other%22 here, inherit the Subject from%0A         * the parent dataverse, when it's available once%0A         * https://github.com/IQSS/dataverse/issues/769 has been implemented%0A         */%0A        String subjectOther = %22Other%22;
+Once dataverse has subject (https://github.com/IQSS/dataverse/issues/769), we should get subject from there %0A         * for now, we'll use the global NA value%0A         */
 %0A   
@@ -2412,16 +2412,18 @@
 ice.find
+NA
 Controll
@@ -2443,109 +2443,12 @@
 alue
-ByDatasetFieldTypeAndStrValue(subjectDatasetFieldType, subjectOther);%0A        if (cvv != null) %7B%0A    
+();%0A
     
@@ -2533,20 +2533,16 @@
         
-    
 datasetV
@@ -2596,179 +2596,8 @@
 ld);
-%0A%0A        %7D else %7B%0A            logger.info(subjectDatasetFieldType.getName() + %22could not be populated with '%22 + subjectOther + %22': null returned from lookup.%22);%0A        %7D
 %0A   

set subject to NA rather than Other