1+ //document upload scripts
2+ var path = './' ;
3+ load ( path + "templates/documentTemplate.js" )
4+ load ( '../../utils/uuid.js' ) ;
5+ load ( path + 'data/documents.js' ) ;
6+ // load("/Users/var03f/Documents/ala/NESP/NESP 2 Products/NESP2 Product Register - for csiro.js");
7+ var projectIdField = "Project ID" ,
8+ projectTitleField = "Project name/title" ,
9+ titleField = "Product title" ,
10+ fileNameField = "Sharepoint file name" ,
11+ citationField = "Citation" ,
12+ descriptionField = "Synopsis" ,
13+ keywordsField = "Keywords" ,
14+ productType = "Type of research product " ,
15+ web = "web link" ,
16+ doiField = "DOI" ,
17+ associatedProgramField = "Hub full name" ;
18+ print ( "Loaded all dependent files..." ) ;
19+ var today = new ISODate ( ) ;
20+ var dd = today . getDate ( ) ;
21+ var mm = today . getMonth ( ) + 1 ; //January is 0!
22+ var yyyy = today . getFullYear ( ) ;
23+ var dateUTC = yyyy + "-" + ( "0" + mm ) . slice ( - 2 ) + "-" + ( "0" + dd ) . slice ( - 2 ) + "T00:00:00Z" ;
24+ var date = ISODate ( dateUTC ) ;
25+ var filepath = yyyy + "-" + ( "0" + mm ) . slice ( - 2 ) ;
26+ var projectsNotFound = [ ] , projectsFound = [ ] ;
27+ print ( "filepath: " + filepath ) ;
28+ print ( "date: " + dateUTC ) ;
29+ print ( "Loaded csv file" ) ;
30+
31+ for ( var i = 0 ; i < documents . length ; i ++ ) {
32+ print ( "Loading document " + ( i + 1 ) + " of " + documents . length ) ;
33+ var fields = documents [ i ] ;
34+ fields [ projectIdField ] = fields [ projectIdField ] . trim ( ) ;
35+ fields [ projectTitleField ] = fields [ projectTitleField ] . trim ( ) ;
36+ var project = db . project . findOne ( { externalId : fields [ projectIdField ] , associatedProgram : fields [ associatedProgramField ] , organisationName : / N E S P 2 / , status : { $ne : 'deleted' } } )
37+ if ( ! project ) {
38+ // throw new Error("Project with externalId: " + fields[projectIdField] + " and name: " + fields[projectTitleField] + " not found.");
39+ if ( projectsNotFound . indexOf ( fields [ projectIdField ] + " - " + fields [ projectTitleField ] ) == - 1 )
40+ projectsNotFound . push ( fields [ projectIdField ] + " - " + fields [ projectTitleField ] ) ;
41+ continue ;
42+ }
43+
44+ if ( db . document . findOne ( { projectId : project . projectId , name : fields [ titleField ] , status : { $ne : 'deleted' } } ) ) {
45+ db . document . deleteMany ( { projectId : project . projectId , name : fields [ titleField ] , status : { $ne : 'deleted' } } ) ;
46+ }
47+
48+
49+ if ( project ) {
50+ projectsFound . push ( project . projectId ) ;
51+ document = Object . assign ( { } , documentTemplate ) ;
52+ document . projectId = project . projectId
53+ document . documentId = UUID . generate ( )
54+ document . description = ""
55+ document . name = ""
56+ document . filename = ""
57+ document . filepath = filepath
58+ document . contentType = ""
59+ document . doiLink = ""
60+ document . role = ""
61+ document . citation = ""
62+ document . labels = [ ]
63+ document . embeddedVideo = ""
64+ document . status = "active"
65+ document . dateCreated = date ;
66+ document . lastUpdated = date ;
67+
68+ //When there are commas/ double quotations, tsv adds double quotes in the beginning and end of text, following string
69+ //manipulations are done to avoid that
70+
71+ if ( fields [ titleField ] ) {
72+ document . name = fields [ titleField ] ;
73+ }
74+
75+ if ( fields [ descriptionField ] ) {
76+ document . description = fields [ descriptionField ] ;
77+ }
78+
79+ if ( fields [ fileNameField ] != "use URL Link" ) {
80+ var fileName = fields [ fileNameField ] ;
81+ var contentType = ""
82+
83+ if ( fileName . endsWith ( ".pdf" ) ) {
84+ contentType = "application/pdf"
85+ } else if ( fileName . endsWith ( ".doc" ) || fileName . endsWith ( ".docx" ) ) {
86+ contentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document" ;
87+ } else if ( fileName . endsWith ( ".pptx" ) ) {
88+ contentType = "application/vnd.openxmlformats-officedocument.presentationml.presentation" ;
89+ }
90+
91+ document . filename = fileName
92+ document . contentType = contentType
93+ }
94+
95+ if ( fields [ doiField ] ) {
96+ document . doiLink = fields [ doiField ] ;
97+ }
98+
99+ if ( fields [ productType ] ) {
100+ switch ( fields [ productType ] ) {
101+ case "video" :
102+ case "Video" :
103+ document . role = "embeddedVideo" ;
104+ document . embeddedVideo = embedInIframe ( fields [ web ] ) ;
105+ break ;
106+ case "Journal article" :
107+ document . role = "journalArticles" ;
108+ break ;
109+ case "Report" :
110+ document . role = "reports" ;
111+ break ;
112+ case "webinar" :
113+ document . role = "webinars" ;
114+ break ;
115+ case "Website" :
116+ document . role = "webPages" ;
117+ document . externalUrl = fields [ web ] ;
118+ break ;
119+ case "Presentation" :
120+ document . role = "presentations" ;
121+ break ;
122+ case "Poster" :
123+ document . role = "postersBanners" ;
124+ break ;
125+ case "newsletter" :
126+ document . role = "magazines" ;
127+ break ;
128+ case "Fact sheet" :
129+ document . role = "factsheets" ;
130+ break ;
131+ case "calendar" :
132+ document . role = "documents" ;
133+ break ;
134+ case "Book or book chapter" :
135+ document . role = "bookChapters" ;
136+ break ;
137+ }
138+ }
139+
140+ if ( fields [ citationField ] ) {
141+ document . citation = fields [ citationField ] ;
142+ }
143+
144+ if ( fields [ keywordsField ] ) {
145+ if ( fields [ keywordsField ] . indexOf ( ',' ) != - 1 ) {
146+ var tempKeywords = fields [ keywordsField ] ;
147+ document . labels = tempKeywords . split ( ',' ) ;
148+ }
149+ else if ( fields [ keywordsField ] ) {
150+ document . labels . push ( fields . keywords ) ;
151+ }
152+ }
153+
154+ if ( fields [ doiField ] ) {
155+ document . doiLink = fields [ doiField ]
156+ }
157+
158+ var documentResult = db . document . insertOne ( document ) ;
159+
160+ print ( "PROJECT ID: " + document . projectId )
161+ print ( "DOCUMENT ID: " + document . documentId )
162+ print ( "FILE NAME: " + document . filename )
163+ print ( "insert result: " + JSON . stringify ( documentResult ) ) ;
164+ }
165+ }
166+
167+ function embedInIframe ( url ) {
168+ if ( ! url ) {
169+ return ;
170+ }
171+
172+ if ( url . indexOf ( "youtube" ) != - 1 ) {
173+ return '<iframe width="560" height="315" src="' + url + '" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" allowfullscreen></iframe>'
174+ }
175+ else if ( url . indexOf ( "vimeo" ) != - 1 ) {
176+ return '<iframe title="Vimeo player" src="' + url + '" width="640" height="360" frameborder="0" allow="autoplay; fullscreen; picture-in-picture" allowfullscreen></iframe>'
177+ }
178+
179+ }
180+ print ( ">>>>>>>>>>>>>>>>> Projects" ) ;
181+ printjson ( projectsFound ) ;
182+ print ( "Created " + i + " documents" ) ;
183+ print ( "<<<<<<<<<<<<<<<<<<<" ) ;
184+ print ( "Projects not found: " + projectsNotFound . length ) ;
185+ printjson ( projectsNotFound ) ;
0 commit comments