/
ASpaceMapper.java
1880 lines (1487 loc) · 61.4 KB
/
ASpaceMapper.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package org.nyu.edu.dlts.utils;
import org.apache.commons.lang.WordUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
/**
* Created by IntelliJ IDEA.
* User: nathan
* Date: 9/5/12
* Time: 1:41 PM
*
* Class to map AT data model to ASPace JSON data model
*/
public class ASpaceMapper {
// String used when mapping access class to groups
public static final String ACCESS_CLASS_PREFIX = "_AccessClass_";
// The utility class used to map to ASpace Enums
private ASpaceEnumUtil enumUtil = new ASpaceEnumUtil();
// used to map vocabularies to ASpace vocabularies
public String vocabularyURI = "/vocabularies/1";
// these store the ids of all accessions, resources, and digital objects loaded so we can
// check for uniqueness before copying them to the ASpace backend
private ArrayList<String> digitalObjectIDs = new ArrayList<String>();
private ArrayList<String> accessionIDs = new ArrayList<String>();
private ArrayList<String> resourceIDs = new ArrayList<String>();
private ArrayList<String> eadIDs = new ArrayList<String>();
// variable to keep track of filenames and their ids to make sure we have unique names
private ArrayList<String> digitalObjectFilenames = new ArrayList<String>();
private HashMap<String, String> fileIDsToFilenamesMap = new HashMap<String, String>();
// some code used for testing
private boolean makeUnique = false;
// initialize the random string generators for use when unique ids are needed
private RandomString randomString = new RandomString(3);
private RandomString randomStringLong = new RandomString(6);
// used to store errors
private ASpaceCopyUtil aspaceCopyUtil;
// used when generating errors
private String currentCollectionRecordIdentifier;
// date formatter used to convert date string to date object
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("yyyyddMM");
SimpleDateFormat humanDateFormat = new SimpleDateFormat("MM/dd/yyyy");
// booleans used to convert some bbcode to html or blanks
private boolean bbcodeToHTML = false;
private boolean bbcodeToBlank = true;
// boolean to specify whether to publish the notes
private Boolean publishRecord = false;
// variable to store the base uri for digital objects
private String digitalObjectBaseURI = "";
/**
* Main constructor
*/
public ASpaceMapper() { }
/**
* Constructor that takes an aspace copy util object
* @param aspaceCopyUtil
*/
public ASpaceMapper(ASpaceCopyUtil aspaceCopyUtil) {
this.aspaceCopyUtil = aspaceCopyUtil;
}
/**
* Set the option for either converting some bbcode to html
*
* @param option
*/
public void setBBCodeOption(String option) {
if (option.equals("-bbcode_html")) {
bbcodeToHTML = true;
bbcodeToBlank = false;
} else {
bbcodeToHTML = false;
bbcodeToBlank = true;
}
}
/**
* Method to set the hash map that holds the dynamic enums
*
* @param dynamicEnums
*/
public void setASpaceDynamicEnums(HashMap<String, JSONObject> dynamicEnums) {
enumUtil.setASpaceDynamicEnums(dynamicEnums);
}
/**
* Method to set the base URI for digital objects
*
* @param baseURI
*/
public void setDigitalObjectBaseURI(String baseURI) {
digitalObjectBaseURI = baseURI;
}
/**
* Method to return the enum util
* @return
*/
public ASpaceEnumUtil getEnumUtil() {
return enumUtil;
}
/**
* This method is used to map AR lookup list values into a dynamic enum.
*
* @param enumList
* @return
*/
public JSONObject mapEnumList(JSONObject enumList, String endpoint) throws Exception {
// first we get the correct dynamic enum based on list. If it null then we just return null
JSONObject dynamicEnumJS = enumUtil.getDynamicEnum(endpoint);
if(dynamicEnumJS == null) return null;
// add the values return from aspace enum an arraylist to make lookup easier
JSONArray valuesJA = dynamicEnumJS.getJSONArray("values");
ArrayList<String> valuesList = new ArrayList<String>();
for(int i = 0; i < valuesJA.length(); i++) {
valuesList.add(valuesJA.getString(i));
}
// now see if all the archon values are in the ASpace list already
// if they are not then add them
String valueKey = dynamicEnumJS.getString("valueKey");
String idPrefix = dynamicEnumJS.getString("idPrefix");
boolean toLowerCase = true;
if(dynamicEnumJS.has("keepValueCase")) {
toLowerCase = false;
}
int count = 0;
Iterator<String> keys = enumList.keys();
while(keys.hasNext()) {
JSONObject enumJS = enumList.getJSONObject(keys.next());
String value = enumJS.getString(valueKey);
// most values in ASpace are lower case so normalize
if(toLowerCase) {
value = value.toLowerCase();
}
// some values have spaces which space normally uses underscore for
value = value.replace(" ", "_");
// map the id to value
String id = idPrefix + "_" + enumJS.get("ID");
enumUtil.addIdAndValueToEnumList(id, value);
// see if to add this to aspace
if(!valuesList.contains(value)) {
valuesJA.put(value);
count++;
System.out.println("Adding value " + value);
}
}
// need to add other to extent unit type enum list
if(endpoint.contains("extentunits")) {
valuesJA.put(ASpaceEnumUtil.UNMAPPED);
}
if(count != 0) {
return dynamicEnumJS;
} else {
return null;
}
}
/**
* Method to get the corporate agent object from a repository
*
* @param repository
* @return
*/
public String getCorporateAgent(JSONObject repository) throws JSONException {
// Main json object, agent_person.rb schema
JSONObject agentJS = new JSONObject();
agentJS.put("agent_type", "agent_corporate_entity");
// hold name information
JSONArray namesJA = new JSONArray();
JSONObject namesJS = new JSONObject();
//add the contact information
JSONArray contactsJA = new JSONArray();
JSONObject contactsJS = new JSONObject();
contactsJS.put("name", repository.get("Name"));
contactsJS.put("address_1", repository.get("Address"));
contactsJS.put("address_2", repository.get("Address2"));
contactsJS.put("city", repository.get("City"));
// add the country and country code together
contactsJS.put("country", enumUtil.getASpaceCountryCode(repository.getInt("CountryID")));
String postCode = repository.get("ZIPCode") + "-" + repository.get("ZIPPlusFour");
contactsJS.put("post_code", postCode);
addPhoneNumbers(contactsJS, repository.getString("Phone"), repository.getString("PhoneExtension"),
repository.getString("Fax"));
contactsJS.put("email", repository.get("Email"));
contactsJS.put("email_signature", repository.get("EmailSignature"));
contactsJA.put(contactsJS);
agentJS.put("agent_contacts", contactsJA);
// add the names object
String primaryName = repository.getString("Name");
namesJS.put("source", "local");
namesJS.put("primary_name", primaryName);
namesJS.put("sort_name", primaryName);
namesJA.put(namesJS);
agentJS.put("names", namesJA);
return agentJS.toString();
}
/**
* Method to add the telephone and fax numbers to an agent contact information
*
* @param contactsJS
* @param telephone
* @param fax
*/
private void addPhoneNumbers(JSONObject contactsJS, String telephone, String ext, String fax) throws JSONException {
JSONArray telephonesJA = new JSONArray();
if (telephone != null && !telephone.isEmpty()) {
JSONObject phoneJS = new JSONObject();
phoneJS.put("number", telephone);
phoneJS.put("ext", ext);
phoneJS.put("number_type", "business");
telephonesJA.put(phoneJS);
}
if (fax != null && !fax.isEmpty()) {
JSONObject phoneJS = new JSONObject();
phoneJS.put("number", fax);
phoneJS.put("number_type", "fax");
telephonesJA.put(phoneJS);
}
contactsJS.put("telephones", telephonesJA);
}
/**
* Method to convert an Archon repository record
*
* @param record
* @return
* @throws Exception
*/
public String convertRepository(JSONObject record, String agentURI) throws Exception {
// Main json object
JSONObject json = new JSONObject();
// add the Archon database Id as an external ID
addExternalId(record, json, "repository");
// get the repo code
json.put("repo_code", record.get("Name"));
json.put("name", fixEmptyString(record.getString("Name")));
json.put("org_code", record.get("Code"));
json.put("url", fixUrl(record.getString("URL")));
if(agentURI != null) {
json.put("agent_representation", getReferenceObject(agentURI));
}
return json.toString();
}
/**
* Method to convert an AT subject record to
*
* @param record
* @return
* @throws Exception
*/
public String convertUser(JSONObject record) throws Exception {
// Main json object
JSONObject json = new JSONObject();
// add the AT database Id as an external ID
addExternalId(record, json, "user");
// get the username replacing spaces with underscores
String username = record.getString("Login");
json.put("username", username);
// get the full name
String name = fixEmptyString(record.getString("DisplayName"), "full name not entered");
json.put("name", name);
json.put("first_name", record.get("FirstName"));
json.put("last_name", record.get("LastName"));
json.put("email", record.get("Email"));
return json.toString();
}
/**
* Method to convert a subject record
*
* @param record
* @return
* @throws Exception
*/
public JSONObject convertSubject(JSONObject record) throws Exception {
// Main json object
JSONObject json = new JSONObject();
// add the AT database Id as an external ID
addExternalId(record, json, "subject");
json.put("vocabulary", vocabularyURI);
json.put("publish", true);
json.put("source", enumUtil.getASpaceSubjectSource(record.getInt("SubjectSourceID")));
// add the terms
JSONArray termsJA = new JSONArray();
try {
addSubjectTerms(record, termsJA);
json.put("terms", termsJA);
} catch(Exception e) {
String message = "Invalid subject terms for " + record + "\n";
aspaceCopyUtil.addErrorMessage(message);
return null;
}
return json;
}
/**
* Method to add terms to a subject
*
* @param record
* @param termsJA
* @throws Exception
*/
private void addSubjectTerms(JSONObject record, JSONArray termsJA) throws Exception {
if(record.get("Parent") != null && record.getInt("ParentID") != 0) {
addSubjectTerms(record.getJSONObject("Parent"), termsJA);
}
JSONObject termJS = new JSONObject();
termJS.put("term", record.get("Subject"));
termJS.put("term_type", enumUtil.getASpaceTermType(record.getInt("SubjectTypeID")));
termJS.put("vocabulary", vocabularyURI);
termsJA.put(termJS);
}
/**
* Method to convert a name aka creator record to an ASpace agent record
*
* @param record
* @param creatorTypeId
* @return
* @throws Exception
*/
public JSONObject convertCreator(JSONObject record, int creatorTypeId) throws Exception {
// Main json object
JSONObject agentJS = new JSONObject();
// add the AR database Id as an external ID
addExternalId(record, agentJS, "creator");
agentJS.put("vocabulary", vocabularyURI);
agentJS.put("publish", true);
// hold name information
JSONArray namesJA = new JSONArray();
JSONObject namesJS = new JSONObject();
// add the biog-history note to the agent object
if(record.has("BiogHist") && !record.getString("BiogHist").isEmpty()) {
addBiologicalHistoryNote(agentJS, record, creatorTypeId);
}
// add the agent date
if(record.has("Dates") && !record.getString("Dates").isEmpty()) {
JSONArray datesJA = new JSONArray();
JSONObject dateJS = new JSONObject();
dateJS.put("date_type", "single");
dateJS.put("label", "existence");
dateJS.put("expression", record.get("Dates"));
datesJA.put(dateJS);
agentJS.put("dates_of_existence", datesJA);
}
// add the source for the name
namesJS.put("source", enumUtil.getASpaceNameSource(record.getInt("CreatorSourceID")));
// add basic information to the names record
String sortName = record.getString("Name");
namesJS.put("sort_name", sortName);
namesJS.put("name_order", "direct");
switch (creatorTypeId) {
case 19:
case 21:
case 23:
agentJS.put("agent_type", "agent_person");
namesJS.put("primary_name", sortName);
if(record.has("NameFullerForm")) {
namesJS.put("fuller_form", record.get("NameFullerForm"));
}
break;
case 20:
agentJS.put("agent_type", "agent_family");
namesJS.put("family_name", sortName);
break;
case 22:
agentJS.put("agent_type", "agent_corporate_entity");
namesJS.put("primary_name", sortName);
break;
default:
String message = sortName + ":: Unknown name type: " + creatorTypeId + "\n";
aspaceCopyUtil.addErrorMessage(message);
return null;
}
// add the names array and names json objects to main record, including any alternative name
namesJA.put(namesJS);
if(record.has("NameVariants") && !record.getString("NameVariants").isEmpty()) {
String nameVariant = record.getString("NameVariants");
JSONObject namesVariantJS = new JSONObject();
namesVariantJS.put("name_order", "direct");
namesVariantJS.put("source", namesJS.get("source"));
namesVariantJS.put("sort_name", nameVariant);
if(namesJS.has("primary_name")) {
namesVariantJS.put("primary_name", nameVariant);
} else {
namesVariantJS.put("family_name", nameVariant);
}
namesJA.put(namesVariantJS);
}
agentJS.put("names", namesJA);
return agentJS;
}
/**
* Method to convert the donor information in an accession record to
* an agent
*
* @param record the accession record
* @return
* @throws Exception
*/
public JSONObject convertAccessionDonor(JSONObject record) throws Exception {
// Main json object
JSONObject agentJS = new JSONObject();
// add the AR database Id as an external ID
addExternalId(record, agentJS, "donor");
agentJS.put("vocabulary", vocabularyURI);
agentJS.put("agent_type", "agent_person");
agentJS.put("publish", false);
// hold name information
JSONArray namesJA = new JSONArray();
JSONObject namesJS = new JSONObject();
// add the source for the name
namesJS.put("source", "local");
// add basic information to the names record
String sortName = record.getString("Donor");
namesJS.put("sort_name", sortName);
namesJS.put("name_order", "direct");
agentJS.put("agent_type", "agent_person");
namesJS.put("primary_name", sortName);
// add the names array and names json objects to main record
namesJA.put(namesJS);
agentJS.put("names", namesJA);
// add the contact information
if(record.has("DonorContactInformation")) {
JSONArray contactsJA = new JSONArray();
JSONObject contactsJS = new JSONObject();
contactsJS.put("name", sortName);
contactsJS.put("address_1", record.get("DonorContactInformation"));
contactsJS.put("note", record.get("DonorNotes"));
contactsJA.put(contactsJS);
agentJS.put("agent_contacts", contactsJA);
}
return agentJS;
}
/**
* Method to convert the classification record
*
* @param record
* @return
* @throws Exception
*/
public JSONObject convertClassification(JSONObject record) throws Exception {
// Main json object
JSONObject json = new JSONObject();
// set the model type
if(record.getString("ParentID").equals("0")) {
json.put("jsonmodel_type", "classification");
} else {
json.put("jsonmodel_type", "classification_term");
/*TODO 10/8/2015 Below code causes bug in ASpace v1.4.0*/
//json.put("position", record.getInt("Position"));
}
json.put("identifier", record.get("ClassificationIdentifier"));
json.put("title", record.get("Title"));
json.put("description", record.get("Description"));
return json;
}
/**
* Method to convert an accession record to json ASpace JSON
*
* @param record
* @return
* @throws Exception
*/
public JSONObject convertAccession(JSONObject record) throws Exception {
// Main json object
JSONObject json = new JSONObject();
// add the AR database Id as an external ID
addExternalId(record, json, "accession");
json.put("publish", convertToBoolean(record.getInt("Enabled")));
// check to make sure we have a title
String title = fixEmptyString(record.getString("Title"), null);
String id_0 = record.getString("Identifier");
String id_1 = getUniqueID(ASpaceClient.ACCESSION_ENDPOINT, id_0, null, title);
if (makeUnique) {
id_0 = randomStringLong.nextString();
}
Date date = getDate(record.getString("AccessionDate"));
if (date == null) {
// use the default date of 01/01/9999
date = getDate("99990101");
// add an error message about this
String message = "Invalid Accession Date for" + id_0 + "\n";
aspaceCopyUtil.addErrorMessage(message);
}
json.put("title", title);
json.put("accession_date", date);
json.put("id_0", id_0);
json.put("id_1", id_1); // This is only used to make sure the ids are unique
json.put("content_description", record.get("ScopeContent"));
json.put("condition_description", record.get("PhysicalDescription"));
json.put("general_note", record.get("Comments"));
if(record.has("MaterialTypeID")) {
json.put("resource_type", enumUtil.getASpaceResourceType(record.getString("MaterialTypeID")));
}
/* add linked records (extents, dates, rights statement)*/
// add the extent array containing one object or many depending if we using multiple extents
if(record.has("ReceivedExtent") && record.getDouble("ReceivedExtent") != 0) {
JSONArray extentJA = new JSONArray();
JSONObject extentJS = new JSONObject();
extentJS.put("extent_type", enumUtil.getASpaceExtentType(record.getInt("ReceivedExtentUnitID")));
extentJS.put("number", record.getString("ReceivedExtent"));
extentJS.put("portion", "whole");
extentJS.put("container_summary", "Received Extent");
extentJA.put(extentJS);
json.put("extents", extentJA);
}
// add the inclusive dates
addDate(record.getString("InclusiveDates"), json, "inclusive", "other");
// add the collection management record now
if(record.has("ExpectedCompletionDate") && !record.getString("ExpectedCompletionDate").isEmpty()) {
addCollectionManagementRecord(record, json);
}
/*
json.put("suppressed", record.getInternalOnly());
json.put("acquisition_type", enumUtil.getASpaceAcquisitionType(record.getAcquisitionType()));
json.put("resource_type", enumUtil.getASpaceAccessionResourceType(record.getResourceType()));
json.put("restrictions_apply", record.getRestrictionsApply());
json.put("retention_rule", record.getRetentionRule());
json.put("general_note", record.getGeneralAccessionNote());
json.put("access_restrictions", record.getAccessRestrictions());
json.put("access_restrictions_note", record.getAccessRestrictionsNote());
json.put("use_restrictions_note", record.getUseRestrictionsNote());
json.put("use_restrictions", record.getUseRestrictions());
*/
return json;
}
/**
* Method to return a collection management record object from an accession
*
* @param record
* @param recordJS
* @return
* @throws Exception
*/
public void addCollectionManagementRecord(JSONObject record, JSONObject recordJS) throws Exception {
// Main json object
JSONObject json = new JSONObject();
json.put("processing_plan", "Expected Completion Date: " + record.get("ExpectedCompletionDate"));
if (record.has("ProcessingPriorityID")) {
json.put("processing_priority", enumUtil.getASpaceCollectionManagementRecordProcessingPriority(record.getInt("ProcessingPriorityID")));
}
recordJS.put("collection_management", json);
}
/**
* Method to add a bioghist note agent object
*
*
* @param agentJS
* @param record
* @param creatorTypeId
* @throws Exception
*/
public void addBiologicalHistoryNote(JSONObject agentJS, JSONObject record, int creatorTypeId) throws Exception {
JSONArray notesJA = new JSONArray();
JSONObject noteJS = new JSONObject();
noteJS.put("jsonmodel_type", "note_bioghist");
noteJS.put("label", "biographical statement");
JSONArray subnotesJA = new JSONArray();
JSONObject textNoteJS = new JSONObject();
addTextNote(textNoteJS, record.getString("BiogHist"));
subnotesJA.put(textNoteJS);
// add a subnote which holds the citation information
if(!record.getString("BiogHistAuthor").isEmpty()) {
JSONObject citationJS = new JSONObject();
citationJS.put("jsonmodel_type", "note_citation");
JSONArray contentJA = new JSONArray();
contentJA.put("Author: " + record.get("BiogHistAuthor"));
citationJS.put("content", contentJA);
subnotesJA.put(citationJS);
}
// add the subnote which hold the source information
if(!record.getString("Sources").isEmpty()) {
String noteType = "note_citation";
if(creatorTypeId == 22) {
noteType = "note_abstract";
}
JSONObject subnoteJS = new JSONObject();
subnoteJS.put("jsonmodel_type", noteType);
JSONArray contentJA = new JSONArray();
contentJA.put(record.get("Sources"));
subnoteJS.put("content", contentJA);
subnotesJA.put(subnoteJS);
}
noteJS.put("subnotes", subnotesJA);
notesJA.put(noteJS);
agentJS.put("notes", notesJA);
}
/**
* Method to convert a digital object record
*
* @param record
* @return
*/
public JSONObject convertDigitalObject(JSONObject record) throws Exception {
// Main json object
JSONObject json = new JSONObject();
// add the AT database Id as an external ID
addExternalId(record, json, "digital_object");
/* add the fields required for abstract_archival_object.rb */
String title = record.getString("Title");
json.put("title", title);
boolean dateAdded = addDate(record.getString("Date"), json, null, "digitized");
// need to add title if no date or title
if(title.isEmpty() && !dateAdded) {
json.put("title", "Digital Object " + record.get("ID"));
}
/* add the fields required digital_object.rb */
JSONArray fileVersionsJA = new JSONArray();
addFileVersion(fileVersionsJA, record, "Digital Object");
json.put("file_versions", fileVersionsJA);
json.put("digital_object_id", getUniqueID(ASpaceClient.DIGITAL_OBJECT_ENDPOINT, record.getString("Identifier"), null, title));
// set the digital object type
json.put("digital_object_type", "mixed_materials");
// set weather to publish
publishRecord = convertToBoolean(record.getInt("Browsable"));
json.put("publish", publishRecord);
// add the notes
addDigitalObjectNotes(record, json);
return json;
}
/**
* Method to convert a digital object record into a aspace digital object component
*
* @param record
* @return
*/
public JSONObject convertToDigitalObjectComponent(JSONObject record) throws Exception {
// Main json object
JSONObject json = new JSONObject();
json.put("publish", publishRecord);
/* add the fields required for abstract_archival_object.rb */
String title = record.getString("Title");
json.put("title", fixEmptyString(title));
/* add fields required for digital object component*/
JSONArray fileVersionsJA = new JSONArray();
addFileVersion(fileVersionsJA, record, "Digital Object Component");
json.put("file_versions", fileVersionsJA);
String label = title;
json.put("label", label);
json.put("position", record.getInt("DisplayOrder"));
json.put("component_id", fixEmptyString(record.getString("ID"), "ID_" + randomString.nextString()));
return json;
}
/**
* Method to add a file version object to the digital object
*
* @param fileVersionsJA
*/
public void addFileVersion(JSONArray fileVersionsJA, JSONObject record, String type) throws JSONException {
if(record.has("ContentURL") && !record.getString("ContentURL").isEmpty()) {
JSONObject fileVersionJS = new JSONObject();
fileVersionJS.put("file_uri", record.getString("ContentURL"));
fileVersionJS.put("use_statement", "image-master");
fileVersionJS.put("xlink_actuate_attribute", "none");
fileVersionJS.put("xlink_show_attribute", "none");
fileVersionsJA.put(fileVersionJS);
} else if(record.has("Filename") && !record.getString("Filename").isEmpty()) {
String filename = verifyFilename(record.getString("ID"), record.getString("Filename"));
JSONObject fileVersionJS = new JSONObject();
fileVersionJS.put("file_uri", digitalObjectBaseURI + filename);
fileVersionJS.put("use_statement", "image-master");
fileVersionJS.put("xlink_actuate_attribute", "none");
fileVersionJS.put("xlink_show_attribute", "none");
fileVersionJS.put("file_format_name", record.get("FileTypeID"));
fileVersionJS.put("file_size_bytes", NumberUtils.toInt((String) record.get("Bytes"), 0));
fileVersionsJA.put(fileVersionJS);
} else {
//System.out.println("No file version found for " + type + ": " + record.get("Title"));
}
}
/**
* Method to sanitize filenames and make them unique
* @param id
* @param filename
* @return
*/
private String verifyFilename(String id, String filename) {
// first remove all none valid characters and replace with "_"
filename = filename.replaceAll("[^a-zA-Z0-9.-]", "_");
// now check to see if we have a unique name
if(!digitalObjectFilenames.contains(filename)) {
digitalObjectFilenames.add(filename);
} else {
// create a unique filename by appending the -0 + id to name
String uniqueFilename;
int i = filename.lastIndexOf('.');
if (i > 0) {
uniqueFilename = filename.substring(0, i) + "-0" + id + "." + filename.substring(i + 1);
} else {
uniqueFilename = filename + "-0" + id;
}
String message = "Duplicate Digital Object Filename: " + filename + " Changed To: " + uniqueFilename + "\n";
aspaceCopyUtil.addErrorMessage(message);
filename = uniqueFilename;
digitalObjectFilenames.add(filename);
}
// add the filename and ID so we can save it later
fileIDsToFilenamesMap.put(id, filename);
return filename;
}
/**
* Method to return the hashmap containing the file ids and the files names for saving
* to the download directory
* @return
*/
public HashMap<String, String> getFileIDsToFilenamesMap() {
return fileIDsToFilenamesMap;
}
/**
* Method to convert an collection record to json ASpace JSON
*
* @param record
* @param classificationIdPartsMap
* @return
* @throws Exception
*/
public JSONObject convertCollection(JSONObject record, HashMap<String, String> classificationIdPartsMap) throws Exception {
// Main json object
JSONObject json = new JSONObject();
// add the AR database Id as an external ID
addExternalId(record, json, "collection");
/* Add fields needed for abstract_archival_object.rb */
// check to make sure we have a title
String title = cleanTitle(fixEmptyString(record.getString("Title")));
json.put("title", title);
// add the language code
json.put("language", getLanguageCode(null, "eng"));
// add the extent array containing one object or many depending if we using multiple extents
addResourceExtent(record, json);
// add the date array containing the dates json objects
addResourceDates(record, json, "Resource: " + currentCollectionRecordIdentifier);
// add external documents
String otherURL = record.getString("OtherURL");
if(!otherURL.isEmpty()) {
addExternalDocument(json, "Other URL", otherURL);
}
/* Add fields needed for resource.rb */
// get the ids and make them unique if we in DEBUG mode
String id = record.getString("CollectionIdentifier");
String classificationID = record.getString("ClassificationID");
String[] idParts = new String[]{"", "", "", ""};
if(!classificationID.equals("0") && classificationIdPartsMap.get(classificationID) != null) {
String[] sa = classificationIdPartsMap.get(classificationID).split("/");
// this can be placed in a loop but lets keep it nice an clear?
if(sa.length == 1) {
idParts[0] = sa[0];
idParts[1] = id;
} else if(sa.length == 2) {
idParts[0] = sa[0];
idParts[1] = sa[1];
idParts[2] = id;
} else if (sa.length == 3) {
idParts[0] = sa[0];
idParts[1] = sa[1];
idParts[2] = sa[2];
idParts[3] = id;
}
} else {
idParts[0] = id;
}
// make sure the id is unique
getUniqueID(ASpaceClient.RESOURCE_ENDPOINT, "", idParts, title);
// debug code to generate random ids for copying over the same collection records
if(makeUnique) {
idParts[0] = randomString.nextString();
idParts[1] = randomString.nextString();
idParts[2] = randomString.nextString();
idParts[3] = randomString.nextString();
}
json.put("id_0", idParts[0]);
json.put("id_1", idParts[1]);
json.put("id_2", idParts[2]);
json.put("id_3", idParts[3]);
// get the level
json.put("level", "collection");
if(record.has("MaterialTypeID")) {
json.put("resource_type", enumUtil.getASpaceResourceType(record.getString("MaterialTypeID")));
}
// set the publish, restrictions, processing note, container summary
publishRecord = convertToBoolean(record.getInt("Enabled"));
json.put("publish", publishRecord);
json.put("container_summary", "Archon Container Summary");
// add fields for EAD
json.put("ead_id", concatIdParts(idParts));
json.put("ead_location", "Archon Finding Aid location");
json.put("finding_aid_title", "Archon Finding Aid Title");
json.put("finding_aid_date", getHumanReadableDate(record.getString("PublicationDate")));
json.put("finding_aid_author", record.get("FindingAidAuthor"));
Integer descriptiveRulesID = record.getInt("DescriptiveRulesID");
if(descriptiveRulesID != null) {
json.put("finding_aid_description_rules", enumUtil.getASpaceFindingAidDescriptionRule(descriptiveRulesID));
}
json.put("finding_aid_language", enumUtil.getASpaceLanguageCode(record.getString("FindingLanguageID")));
json.put("finding_aid_note", record.get("PublicationNote"));
// add any reversion statements