1
+ //document upload scripts
2
+ var path = '' ;
3
+ load ( path + "document.js" )
4
+ load ( path + 'uuid.js' ) ;
5
+
6
+ print ( "Loaded all dependent files..." ) ;
7
+
8
+ var hub_name = 0 ;
9
+ var projectId = hub_name + 1 ;
10
+ var title = projectId + 1 ;
11
+ var description = title + 1 ;
12
+ var documentUrl = description + 1 ;
13
+ var doiLink = documentUrl + 1 ;
14
+ var role = doiLink + 1 ;
15
+ var citation = role + 1 ;
16
+ var keywords = citation + 1 ;
17
+ var embeddedVideo = keywords + 1 ;
18
+
19
+ var csvData = cat ( path + 'NESP_MARINE_ARTEFACTS_FOR_TEST.txt' ) ;
20
+ print ( "Loaded csv file" ) ;
21
+ var csvRows = csvData . split ( '\r' ) ;
22
+ print ( "Total rows " + csvRows . length ) ;
23
+
24
+ for ( var i = 1 ; i < csvRows . length ; i ++ ) {
25
+ print ( "PRINT " + csvRows . length )
26
+
27
+ var row = csvRows [ i ] ;
28
+ var fields = row . split ( '\t' ) ;
29
+
30
+ if ( fields [ projectId ] ) {
31
+ document . projectId = fields [ projectId ]
32
+ document . documentId = UUID . generate ( )
33
+ document . description = ""
34
+ document . name = ""
35
+ document . filename = ""
36
+ document . contentType = ""
37
+ document . doiLink = ""
38
+ document . role = ""
39
+ document . citation = ""
40
+ document . labels = [ ]
41
+ document . embeddedVideo = ""
42
+
43
+ //When there are commas/ double quotations, tsv adds double quotes in the beginning and end of text, following string
44
+ //manipulations are done to avoid that
45
+
46
+ if ( fields [ title ] ) {
47
+ if ( ( fields [ title ] . indexOf ( ',' ) != - 1 ) || ( fields [ title ] . indexOf ( '"' ) != - 1 ) ) {
48
+ var tempTitle = fields [ title ] . replace ( / " " / g, '"' ) ;
49
+ document . name = tempTitle . substring ( 1 , tempTitle . length - 1 ) ;
50
+ }
51
+ else {
52
+ document . name = fields [ title ]
53
+ }
54
+ }
55
+
56
+ if ( fields [ description ] ) {
57
+ if ( ( fields [ description ] . indexOf ( ',' ) != - 1 ) || ( fields [ description ] . indexOf ( '"' ) != - 1 ) ) {
58
+ var tempDescription = fields [ description ] . replace ( / " " / g, '"' ) ;
59
+ document . description = tempDescription . substring ( 1 , tempDescription . length - 1 ) ;
60
+ }
61
+ else {
62
+ document . description = fields [ description ]
63
+ }
64
+ }
65
+
66
+ if ( fields [ documentUrl ] ) {
67
+ var docUrl = fields [ documentUrl ]
68
+ var urlSplitBySlash = docUrl . split ( '/' )
69
+ var fileName = urlSplitBySlash [ urlSplitBySlash . length - 1 ]
70
+
71
+ var contentType = ""
72
+
73
+ if ( fileName . endsWith ( ".pdf" ) ) {
74
+ contentType = "application/pdf"
75
+ } else if ( fileName . endsWith ( ".doc" ) || fileName . endsWith ( ".docx" ) ) {
76
+ contentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document" ;
77
+ }
78
+
79
+ document . filename = fileName
80
+ document . contentType = contentType
81
+ }
82
+
83
+ if ( fields [ doiLink ] ) {
84
+ if ( ( fields [ doiLink ] . indexOf ( ',' ) != - 1 ) || ( fields [ doiLink ] . indexOf ( '"' ) != - 1 ) ) {
85
+ var tempDoi = fields [ doiLink ] . replace ( / " " / g, '"' ) ;
86
+ document . doiLink = tempDoi . substring ( 1 , tempDoi . length - 1 ) ;
87
+ }
88
+ else {
89
+ document . doiLink = fields [ doiLink ]
90
+ }
91
+ }
92
+
93
+ if ( fields [ role ] ) {
94
+ document . role = fields [ role ]
95
+ }
96
+
97
+ if ( fields [ citation ] ) {
98
+ if ( ( fields [ citation ] . indexOf ( ',' ) != - 1 ) || ( fields [ citation ] . indexOf ( '"' ) != - 1 ) ) {
99
+ var tempCitation = fields [ citation ] . replace ( / " " / g, '"' ) ;
100
+ document . citation = tempCitation . substring ( 1 , tempCitation . length - 1 ) ;
101
+ }
102
+ else {
103
+ document . citation = fields [ citation ]
104
+ }
105
+ }
106
+
107
+ if ( fields [ keywords ] ) {
108
+ if ( ( fields [ keywords ] . indexOf ( ',' ) != - 1 ) || ( fields [ keywords ] . indexOf ( '"' ) != - 1 ) ) {
109
+ var tempKeywords = fields [ keywords ] . replace ( / " " / g, '"' ) ;
110
+ var tempKeywordsStr = tempKeywords . substring ( 1 , tempKeywords . length - 1 ) ;
111
+ var tempKeywordsArr = tempKeywordsStr . split ( ',' ) ;
112
+
113
+ document . labels = tempKeywordsArr ;
114
+ }
115
+ else {
116
+ document . labels . push ( fields [ keywords ] ) ;
117
+ }
118
+ }
119
+
120
+ if ( fields [ embeddedVideo ] ) {
121
+ if ( ( fields [ embeddedVideo ] . indexOf ( ',' ) != - 1 ) || ( fields [ embeddedVideo ] . indexOf ( '"' ) != - 1 ) ) {
122
+ var tempEmbeddedVideo = fields [ embeddedVideo ] . replace ( / " " / g, '"' ) ;
123
+ document . embeddedVideo = tempEmbeddedVideo . substring ( 1 , tempEmbeddedVideo . length - 1 ) ;
124
+ }
125
+ else {
126
+ document . embeddedVideo = fields [ embeddedVideo ]
127
+ }
128
+ }
129
+
130
+ var today = new ISODate ( ) ;
131
+ var dd = today . getDate ( ) ;
132
+ var mm = today . getMonth ( ) + 1 ; //January is 0!
133
+ var yyyy = today . getFullYear ( ) ;
134
+ var dateUTC = yyyy + "-" + ( "0" + mm ) . slice ( - 2 ) + "-" + ( "0" + dd ) . slice ( - 2 ) + "T00:00:00Z" ;
135
+
136
+ document . dateCreated = ISODate ( dateUTC ) ;
137
+ document . lastUpdated = ISODate ( dateUTC ) ;
138
+
139
+ var documentResult = db . document . insert ( document ) ;
140
+
141
+ print ( "PROJECT ID: " + document . projectId )
142
+ print ( "DOCUMENT ID: " + document . documentId )
143
+ print ( "FILE NAME: " + document . filename )
144
+ print ( "documentResult " + documentResult )
145
+ }
146
+ }
147
+
148
+ print ( "Created " + ( i - 1 ) + " documents" ) ;
149
+ print ( "<<<<<<<<<<<<<<<<<<<" )
0 commit comments