9
9
10
10
import static org .dspace .content .Item .ANY ;
11
11
import static org .dspace .content .MetadataSchemaEnum .CRIS ;
12
+ import static org .dspace .content .authority .Choices .CF_UNSET ;
12
13
import static org .dspace .content .service .DSpaceObjectService .MD_COPYRIGHT_TEXT ;
13
14
import static org .dspace .content .service .DSpaceObjectService .MD_INTRODUCTORY_TEXT ;
14
15
import static org .dspace .content .service .DSpaceObjectService .MD_LICENSE ;
49
50
import org .dspace .content .Collection ;
50
51
import org .dspace .content .Community ;
51
52
import org .dspace .content .Item ;
53
+ import org .dspace .content .MetadataField ;
52
54
import org .dspace .content .MetadataFieldName ;
53
55
import org .dspace .content .MetadataSchemaEnum ;
54
56
import org .dspace .content .MetadataValue ;
55
57
import org .dspace .content .factory .ContentServiceFactory ;
56
58
import org .dspace .content .service .CollectionService ;
57
59
import org .dspace .content .service .CommunityService ;
60
+ import org .dspace .content .service .ItemService ;
58
61
import org .dspace .core .Context ;
59
62
import org .dspace .core .CrisConstants ;
60
63
import org .dspace .eperson .factory .EPersonServiceFactory ;
@@ -122,7 +125,8 @@ public class StructBuilder {
122
125
= EPersonServiceFactory .getInstance ().getEPersonService ();
123
126
protected static final HandleService handleService
124
127
= HandleServiceFactory .getInstance ().getHandleService ();
125
-
128
+ protected static final ItemService itemService
129
+ = ContentServiceFactory .getInstance ().getItemService ();
126
130
/**
127
131
* Default constructor
128
132
*/
@@ -407,6 +411,9 @@ private static Element exportACollection(Collection collection) {
407
411
Element element = new Element ("collection" );
408
412
element .setAttribute ("identifier" , collection .getHandle ());
409
413
element .addContent (new Element ("name" ).setText (collection .getName ()));
414
+
415
+ buildTemplateItem (collection , element );
416
+
410
417
element .addContent (new Element ("description" )
411
418
.setText (collectionService .getMetadataFirstValue (collection ,
412
419
MetadataSchemaEnum .DC .getName (), "description" , "abstract" , Item .ANY )));
@@ -833,6 +840,8 @@ private static Element[] handleCollections(Context context,
833
840
collectionService .setMetadataSingleValue (context , collection ,
834
841
MD_SHORT_DESCRIPTION , Item .ANY , " " );
835
842
843
+ handleTemplateItem (context , collection , tn );
844
+
836
845
// import the rest of the metadata
837
846
for (Map .Entry <String , MetadataFieldName > entry : collectionMap .entrySet ()) {
838
847
NodeList nl = (NodeList ) xPath .compile (entry .getKey ()).evaluate (tn , XPathConstants .NODESET );
@@ -854,6 +863,8 @@ private static Element[] handleCollections(Context context,
854
863
855
864
String fieldValue ;
856
865
866
+ buildTemplateItem (collection , element );
867
+
857
868
fieldValue = collectionService .getMetadataFirstValue (collection ,
858
869
CollectionService .MD_SHORT_DESCRIPTION , Item .ANY );
859
870
if (fieldValue != null ) {
@@ -930,4 +941,93 @@ private static Element[] handleCollections(Context context,
930
941
931
942
return elements ;
932
943
}
944
+
945
+ private static void handleTemplateItem (Context context , Collection collection , Node tn )
946
+ throws XPathExpressionException , SQLException , AuthorizeException {
947
+
948
+ XPath xPath = XPathFactory .newInstance ().newXPath ();
949
+ Node node = (Node ) xPath .compile ("templateItem" ).evaluate (tn , XPathConstants .NODE );
950
+
951
+ if (node == null ) {
952
+ return ;
953
+ }
954
+
955
+ Item templateItem = itemService .createTemplateItem (context , collection );
956
+
957
+ NodeList metadataNodes = (NodeList ) xPath .compile ("metadata" ).evaluate (node , XPathConstants .NODESET );
958
+
959
+ for (int i = 0 ; i < metadataNodes .getLength (); i ++) {
960
+ Node metadataNode = metadataNodes .item (i );
961
+ MetadataFieldName metadataFieldName = buildMetadataFieldName (metadataNode );
962
+
963
+ Node valueAttribute = (Node ) xPath .compile ("value" ).evaluate (metadataNode , XPathConstants .NODE );
964
+ Node authorityAttribute = (Node ) xPath .compile ("authority" ).evaluate (metadataNode , XPathConstants .NODE );
965
+ Node confidenceAttribute = (Node ) xPath .compile ("confidence" ).evaluate (metadataNode , XPathConstants .NODE );
966
+
967
+ String authority = null ;
968
+ int confidence = CF_UNSET ;
969
+
970
+ if (authorityAttribute != null ) {
971
+ authority = authorityAttribute .getTextContent ();
972
+ confidence = confidenceAttribute != null ? Integer .parseInt (confidenceAttribute .getTextContent ()) : 600 ;
973
+ }
974
+
975
+ itemService .addMetadata (context , templateItem , metadataFieldName .schema , metadataFieldName .element ,
976
+ metadataFieldName .qualifier , ANY , valueAttribute .getTextContent (), authority , confidence );
977
+ itemService .update (context , templateItem );
978
+ }
979
+ }
980
+
981
+ private static MetadataFieldName buildMetadataFieldName (Node node ) {
982
+ Node schemaAttribute = node .getAttributes ().getNamedItem ("schema" );
983
+ Node elementAttribute = node .getAttributes ().getNamedItem ("element" );
984
+ Node qualifierAttribute = node .getAttributes ().getNamedItem ("qualifier" );
985
+
986
+ if (qualifierAttribute == null ) {
987
+ return new MetadataFieldName (schemaAttribute .getTextContent (), elementAttribute .getTextContent ());
988
+ } else {
989
+ return new MetadataFieldName (schemaAttribute .getTextContent (),
990
+ elementAttribute .getTextContent (), qualifierAttribute .getTextContent ());
991
+ }
992
+ }
993
+
994
+ private static void buildTemplateItem (Collection collection , Element element ) {
995
+
996
+ try {
997
+ Item templateItem = collection .getTemplateItem ();
998
+
999
+ if (templateItem == null ) {
1000
+ return ;
1001
+ }
1002
+
1003
+ Element templateItemElement = new Element ("templateItem" );
1004
+
1005
+ for (MetadataValue metadataValue : templateItem .getMetadata ()) {
1006
+ MetadataField metadataField = metadataValue .getMetadataField ();
1007
+ Element metadata = new Element ("metadata" );
1008
+ metadata .setAttribute ("schema" , metadataField .getMetadataSchema ().getName ());
1009
+ metadata .setAttribute ("element" , metadataField .getElement ());
1010
+
1011
+ if (metadataField .getQualifier () != null ) {
1012
+ metadata .setAttribute ("qualifier" , metadataField .getQualifier ());
1013
+ }
1014
+
1015
+ metadata .addContent (new Element ("value" ).setText (metadataValue .getValue ()));
1016
+
1017
+ if (metadataValue .getAuthority () != null ) {
1018
+ metadata .addContent (new Element ("authority" ).setText (metadataValue .getAuthority ()));
1019
+ metadata .addContent (new Element ("confidence" ).setText (
1020
+ String .valueOf (metadataValue .getConfidence ())
1021
+ ));
1022
+ }
1023
+
1024
+ templateItemElement .addContent (metadata );
1025
+ }
1026
+
1027
+ element .addContent (templateItemElement );
1028
+ } catch (SQLException e ) {
1029
+ throw new RuntimeException (e );
1030
+ }
1031
+ }
1032
+
933
1033
}
0 commit comments