Source: themes/ess-dive/models/metadata/eml211/EML211.js

/* global define */
define(['jquery', 'underscore', 'backbone', 'uuid',
        'collections/Units',
        'models/metadata/ScienceMetadata',
        'models/DataONEObject',
        'models/metadata/eml211/EMLGeoCoverage', 
        'models/metadata/eml211/EMLKeywordSet', 
        'models/metadata/eml211/EMLTaxonCoverage', 
        'models/metadata/eml211/EMLTemporalCoverage', 
        'models/metadata/eml211/EMLDistribution', 
        'models/metadata/eml211/EMLEntity',
        'models/metadata/eml211/EMLDataTable',
        'models/metadata/eml211/EMLOtherEntity',
        'models/metadata/eml211/EMLParty', 
        'models/metadata/eml211/EMLProject',
        'models/metadata/eml211/EMLText',
		'models/metadata/eml211/EMLMethods',
        'models/metadata/eml211/BaseEML211'
],
    function($, _, Backbone, uuid, Units, ScienceMetadata, DataONEObject,
    		EMLGeoCoverage, EMLKeywordSet, EMLTaxonCoverage, EMLTemporalCoverage, 
    		EMLDistribution, EMLEntity, EMLDataTable, EMLOtherEntity, EMLParty, 
            EMLProject, EMLText, EMLMethods, BaseEML211) {

        /**
         * Overrides BaseEML211.createXML() to set 'system' to 'ess-dive'
         *
         * An EML211 object represents an Ecological Metadata Language
         * document, version 2.1.1
         * @type {void|*}
         */
        var EML211 = BaseEML211.extend({
            /**
             * Override defaults
             * @returns {*}
             */
            defaults: function(){
        		return _.extend(BaseEML211.prototype.defaults(), {
                    id: DataONEObject.generateEssDiveId(),
                    intellectualRights:"This work is licensed under the Creative Commons Attribution 4.0 International License. To view a copy of this license, visit http://creativecommons.org/licenses/by/4.0/."
        		});
            
            },
            createXML: function(){

                // Generate the xml string from the base class
                var xml = BaseEML211.prototype.createXML.apply(this);

                // Create the EML html object
                var eml = $($.parseHTML(xml));

                // Override the system value
                eml.attr("system","ess-dive")

                // Generate the new EML string
                var emlString = $(document.createElement("div")).append(eml.clone()).html();
                return emlString;


            },

            /*
            * Cleans up the given text so that it is XML-valid by escaping reserved characters, trimming white space, etc.
            *
            * @param {string} textString - The string to clean up
            * @return {string} - The cleaned up string
            */
            cleanXMLText: function(textString){

              if( typeof textString != "string" )
                return;

              textString = textString.trim();

              return textString;

            },

            /**
             * Extend save function to add default publication year
             * 
             * Saves the EML document to the server using the DataONE API
             * 
             * @param attributes
             * @param options
             * @returns {boolean}
             */
            save: function(attributes, options){

                // Set missing publication Date before saving
                if ( ! this.get("pubDate") ) {
                    this.set("pubDate",(new Date()).getFullYear().toString())
                }

                // Make sure there is only one additionaInfo/Reference Papers section
                if ( this.get("additionalInfo") &&
                    _.isArray(this.get("additionalInfo")) && this.get("additionalInfo").length >= 1)
                {
                    var additionalInfo = this.get("additionalInfo");
                    if (_.isArray(this.get("additionalInfo")) && this.get("additionalInfo").length >= 1){
                        additionalInfo = this.get("additionalInfo")[0]
                    }

                    if (additionalInfo.get("section") && _.isArray(additionalInfo.get("section")) &&
                        additionalInfo.get("section").length >0)
                    {
                        this.set("additionalInfo", new EMLText({
                            text:[],
                            section: new EMLText({
                                type: "section",
                                title: "Related References",
                                text: additionalInfo.get("section")[0].text
                            })
                        }));
                    }

                }

                return BaseEML211.prototype.save.apply(this, [attributes, options]);
            },
            /**
             * Overrides base class to add Funding Organization Validation
             * Checks if this EML model has all the required values necessary to save to the server
             * 
             * @returns {*|{formatString}|{name}|string}
             */
            validate: function() {

                var errors = BaseEML211.prototype.validate.apply(this);
                if (!errors){
                    errors={};
                }

                // get the target from the EML Model
                var keywordSet = _.find(this.get("keywordSets"),
                        function(keywordSet){
                            return keywordSet.get("thesaurus").indexOf("CATEGORICAL") >=0
                        });

                if (typeof keywordSet == "undefined")
                {
                    errors.keywordCategoricalSets = "Provide at least one keyword or variable.";
                }

                //Validate the Funding Organizations models
            	var fundingOrganizations = _.filter(this.get("associatedParty"),
                        function(party){ return party.get("role") == "fundingOrganization" });

                if(!fundingOrganizations.length){
            		errors.fundingOrganization = "At least one funding organization is required.";
            	}

                //Validate the Project
            	if(!this.get("project") || !this.get("project").get("title")){
            		errors.project = "Please enter a project title.";
            	}
                
                if( Object.keys(errors).length )
                {
            		return errors;
                }
            	else{
            		return;
            	}
            }
        });

        return EML211;
    }
);