1
1
package org .edu_sharing .repository .server .importer ;
2
2
3
- import java .io .InputStream ;
4
- import java .io .Serializable ;
5
- import java .net .MalformedURLException ;
6
- import java .net .URL ;
7
- import java .util .*;
8
- import java .util .stream .Stream ;
9
-
10
3
import org .alfresco .model .ContentModel ;
11
4
import org .alfresco .service .ServiceRegistry ;
12
5
import org .alfresco .service .cmr .dictionary .DictionaryService ;
28
21
import org .edu_sharing .service .clientutils .WebsiteInformation ;
29
22
import org .edu_sharing .service .collection .CollectionServiceFactory ;
30
23
24
+ import java .io .InputStream ;
25
+ import java .io .Serializable ;
26
+ import java .net .MalformedURLException ;
27
+ import java .net .URL ;
28
+ import java .util .*;
29
+ import java .util .stream .Stream ;
30
+
31
31
32
32
public class ExcelLOMImporter {
33
33
@@ -40,9 +40,9 @@ public class ExcelLOMImporter {
40
40
DictionaryService dictionaryService = serviceRegistry .getDictionaryService ();
41
41
NodeService nodeService = serviceRegistry .getNodeService ();
42
42
43
- String targetFolder = null ;
43
+ String targetFolder ;
44
44
45
- InputStream is = null ;
45
+ InputStream is ;
46
46
47
47
int maxNodesInFolder = 100 ;
48
48
@@ -68,7 +68,7 @@ public ExcelLOMImporter(String targetFolder, InputStream is, Boolean addToCollec
68
68
this .is = is ;
69
69
70
70
71
- HashMap <Integer ,String > IdxColumnMap = new HashMap <Integer , String >();
71
+ HashMap <Integer ,String > IdxColumnMap = new HashMap <>();
72
72
73
73
try {
74
74
Workbook workbook = WorkbookFactory .create (this .is );
@@ -85,7 +85,7 @@ public ExcelLOMImporter(String targetFolder, InputStream is, Boolean addToCollec
85
85
NodeRef currentFolder = nodeService .getChildByName (targetFolderNodeRef , assocTypeContains , folderName );
86
86
87
87
if (currentFolder == null ){
88
- Map <QName ,Serializable > folderProps = new HashMap <QName , Serializable >();
88
+ Map <QName ,Serializable > folderProps = new HashMap <>();
89
89
folderProps .put (QName .createQName (CCConstants .CM_NAME ), folderName );
90
90
folderProps .put (QName .createQName (CCConstants .CM_PROP_C_TITLE ), folderName );
91
91
parentFolder = nodeService .createNode (targetFolderNodeRef ,assocTypeContains , QName .createQName (folderName ), QName .createQName (CCConstants .CCM_TYPE_MAP ),folderProps ).getChildRef ().getId ();
@@ -94,15 +94,15 @@ public ExcelLOMImporter(String targetFolder, InputStream is, Boolean addToCollec
94
94
try {
95
95
currentLevelObjects = apiClient .getChildren (parentFolder );
96
96
}catch (Throwable e ){
97
- e . printStackTrace ( );
97
+ logger . error ( e . getMessage (), e );
98
98
}
99
99
100
- if (IdxColumnMap .size () > 0 ){
100
+ if (! IdxColumnMap .isEmpty () ){
101
101
//we got the headers
102
- HashMap <QName ,Serializable > toSafe = new HashMap <QName , Serializable >();
102
+ HashMap <QName ,Serializable > toSafe = new HashMap <>();
103
103
104
104
String contentUrl = null ;
105
- LinkedHashSet <String > collectionsToImportTo = new LinkedHashSet <String >();
105
+ LinkedHashSet <String > collectionsToImportTo = new LinkedHashSet <>();
106
106
for (Cell cell : row ){
107
107
108
108
int colIdxIdx = cell .getColumnIndex ();
@@ -129,35 +129,32 @@ public ExcelLOMImporter(String targetFolder, InputStream is, Boolean addToCollec
129
129
String value = cell .getStringCellValue ();
130
130
if (value == null ) continue ;
131
131
value = value .trim ();
132
- if (value .equals ( "" )) continue ;
132
+ if (value .isEmpty ( )) continue ;
133
133
134
134
if (columnName != null ){
135
135
alfrescoProperty = getExcelAlfMap ().get (columnName );
136
136
}
137
137
138
138
if (alfrescoProperty != null ){
139
- if (alfrescoProperty != null &&
140
- alfrescoProperty .equals (CCConstants .CM_PROP_CONTENT )){
139
+ if (alfrescoProperty .equals (CCConstants .CM_PROP_CONTENT )){
141
140
contentUrl = value ;
142
141
}else {
143
142
PropertyDefinition propDef = dictionaryService .getProperty (QName .createQName (alfrescoProperty ));
144
143
145
144
if (propDef != null ) {
146
145
if (propDef .isMultiValued () && !alfrescoProperty .contains ("contributer" )){
147
- ArrayList <String > multival = new ArrayList <String >();
148
-
149
- //String[] vals = value.split(","); StringTool.escape(CCConstants.MULTIVALUE_SEPARATOR)
146
+
147
+ //String[] vals = value.split(","); StringTool.escape(CCConstants.MULTIVALUE_SEPARATOR)
150
148
String [] vals = value .split (StringTool .escape (CCConstants .MULTIVALUE_SEPARATOR ));
151
- multival . addAll (Arrays .asList (vals ));
149
+ ArrayList < String > multival = new ArrayList <> (Arrays .asList (vals ));
152
150
153
151
toSafe .put (QName .createQName (alfrescoProperty ), multival );
154
152
}else {
155
153
toSafe .put (QName .createQName (alfrescoProperty ), value );
156
154
}
157
155
}else {
158
156
logger .error ("unkown property: " + alfrescoProperty );
159
- continue ;
160
- }
157
+ }
161
158
}
162
159
}
163
160
@@ -169,20 +166,20 @@ public ExcelLOMImporter(String targetFolder, InputStream is, Boolean addToCollec
169
166
String nodeName = addName (toSafe , wwwUrl );
170
167
addThumbnail (toSafe , wwwUrl );
171
168
172
- if (toSafe .size () > 0 && nodeName != null && !nodeName .trim ().equals ( "" )){
169
+ if (! toSafe .isEmpty () && nodeName != null && !nodeName .trim ().isEmpty ( )){
173
170
174
171
//check for valid thumbnail url
175
172
boolean createNode = true ;
176
173
String thumbUrl = (String )toSafe .get (qnameThumbnail );
177
174
178
- if ((thumbUrl == null || !thumbUrl .startsWith ("http" )) && (contentUrl == null || contentUrl .trim ().equals ( "" ))) {
175
+ if ((thumbUrl == null || !thumbUrl .startsWith ("http" )) && (contentUrl == null || contentUrl .trim ().isEmpty ( ))) {
179
176
logger .error ("invalid thumbnail url:" + thumbUrl +" for:" +toSafe .get (QName .createQName (CCConstants .CM_NAME ))+" will not safe object" );
180
177
createNode = false ;
181
178
}
182
179
if (createNode ) {
183
180
ChildAssociationRef newNode = nodeService .createNode (new NodeRef (MCAlfrescoAPIClient .storeRef ,parentFolder ),QName .createQName (CCConstants .CM_ASSOC_FOLDER_CONTAINS ), QName .createQName (nodeName ), QName .createQName (CCConstants .CCM_TYPE_IO ),toSafe );
184
181
185
- if (contentUrl != null && !contentUrl .trim ().equals ( "" )){
182
+ if (contentUrl != null && !contentUrl .trim ().isEmpty ( )){
186
183
String mimetype = MimeTypes .guessMimetype (contentUrl );
187
184
try {
188
185
InputStream inputStream = new URL (contentUrl ).openConnection ().getInputStream ();
@@ -277,7 +274,7 @@ private String addName(HashMap<QName, Serializable> toSafe, String wwwUrl) {
277
274
return null ;
278
275
}
279
276
280
- HashMap <String ,Object > eduProps = new HashMap <String , Object >();
277
+ HashMap <String ,Object > eduProps = new HashMap <>();
281
278
eduProps .put (CCConstants .CM_NAME , nodeName );
282
279
eduProps .put (CCConstants .LOM_PROP_GENERAL_TITLE , nodeName );
283
280
new DuplicateFinder ().transformToSafeName (currentLevelObjects , eduProps );
@@ -288,10 +285,12 @@ private String addName(HashMap<QName, Serializable> toSafe, String wwwUrl) {
288
285
private void addToCollections (ChildAssociationRef newNode , LinkedHashSet <String > collectionsForNode , Boolean addToCollection ){
289
286
String wwwUrl = (String )serviceRegistry .getNodeService ().getProperty (newNode .getChildRef (),QName .createQName (CCConstants .CCM_PROP_IO_WWWURL ));
290
287
String nodeName = (String )serviceRegistry .getNodeService ().getProperty (newNode .getChildRef (), ContentModel .PROP_NAME );
291
- if (collectionsForNode != null && collectionsForNode .size () > 1 ){
288
+ if (collectionsForNode != null && ! collectionsForNode .isEmpty () ){
292
289
293
290
String parentCollection = collectionsForNode .stream ().findFirst ().get ();
294
- String targetCollection = collectionsForNode .stream ().skip (collectionsForNode .size () -1 ).findFirst ().get ();
291
+ String targetCollection = (collectionsForNode .size () == 1 )
292
+ ? parentCollection
293
+ : collectionsForNode .stream ().skip (collectionsForNode .size () -1 ).findFirst ().get ();
295
294
logger .info ("collections for node " + String .join ("/" ,collectionsForNode ) +" p:" +parentCollection +" c:" +targetCollection );
296
295
297
296
SearchParameters searchParameters = new SearchParameters ();
@@ -306,22 +305,25 @@ private void addToCollections(ChildAssociationRef newNode, LinkedHashSet<String>
306
305
//check if there is a parent
307
306
NodeRef pathMatchesNodeRef = null ;
308
307
LinkedHashSet <String > pathsMatch = new LinkedHashSet <>();
309
- for (NodeRef nodeRef : rs .getNodeRefs ()){
310
- Path path = serviceRegistry .getNodeService ().getPath (nodeRef );
308
+ for (NodeRef targetCollectionNodeRef : rs .getNodeRefs ()){
309
+
310
+ Path path = serviceRegistry .getNodeService ().getPath (targetCollectionNodeRef );
311
311
String displayPath = path .toDisplayPath (serviceRegistry .getNodeService (),serviceRegistry .getPermissionService ());
312
- logger .info ("checking path for parent collection;\" " + parentCollection + "\" ;path:" +displayPath +"/" +nodeService .getProperty (nodeRef ,ContentModel .PROP_NAME ));
313
- if (displayPath .contains (parentCollection )){
312
+ String targetCollectionName = (String )nodeService .getProperty (targetCollectionNodeRef ,ContentModel .PROP_NAME );
313
+ displayPath += "/" +targetCollectionName ;
314
+ logger .info ("checking path for parent collection;\" " + parentCollection + "\" ;path:" +displayPath );
315
+ if (displayPath .contains (parentCollection ) && displayPath .endsWith (targetCollection )){
314
316
pathsMatch .add (displayPath );
315
- pathMatchesNodeRef = nodeRef ;
317
+ pathMatchesNodeRef = targetCollectionNodeRef ;
316
318
}
317
319
}
318
320
319
321
if (pathsMatch .size () > 1 ){
320
322
logger .error ("more than one path matches;" +nodeName +";" +newNode .getChildRef ()+";" + String .join (";" ,pathsMatch ));
321
- }else if (pathsMatch .size () == 0 ){
323
+ }else if (pathsMatch .isEmpty () ){
322
324
logger .error ("no path matches;" +nodeName +";" +newNode .getChildRef ());
323
325
}else {
324
- logger .info ("adding;" + nodeName +";" +newNode .getChildRef () +";TO;" + pathsMatch .iterator ().next () + "/" + nodeService . getProperty ( pathMatchesNodeRef , ContentModel . PROP_NAME ) );
326
+ logger .info ("adding;" + nodeName +";" +newNode .getChildRef () +";TO;" + pathsMatch .iterator ().next ());
325
327
try {
326
328
if (addToCollection ) {
327
329
CollectionServiceFactory .getLocalService ().addToCollection (pathMatchesNodeRef .getId (), newNode .getChildRef ().getId (), null , false );
@@ -332,7 +334,7 @@ private void addToCollections(ChildAssociationRef newNode, LinkedHashSet<String>
332
334
}
333
335
}
334
336
}else {
335
- this .logger .info ("addToCollections expects two collection to be defined in excelsheet;" + wwwUrl );
337
+ this .logger .info ("addToCollections expects minimum one collection to be defined in excelsheet;" + wwwUrl );
336
338
}
337
339
}
338
340
@@ -354,10 +356,9 @@ private static String getYoutubeId(String wwwUrl){
354
356
355
357
URL url = new URL (wwwUrl );
356
358
String queryString = url .getQuery ();
357
- String id = Stream .of (queryString .split ("&" )).map (kv -> kv .split ("=" )).filter (kv -> "v" .equalsIgnoreCase (kv [0 ])).map (kv -> kv [1 ])
359
+ return Stream .of (queryString .split ("&" )).map (kv -> kv .split ("=" )).filter (kv -> "v" .equalsIgnoreCase (kv [0 ])).map (kv -> kv [1 ])
358
360
.findFirst ()
359
361
.orElse ("" );
360
- return id ;
361
362
}else {
362
363
return null ;
363
364
}
@@ -369,7 +370,7 @@ private static String getYoutubeId(String wwwUrl){
369
370
370
371
public HashMap <String , String > getExcelAlfMap () {
371
372
if (excelAlfMap == null ){
372
- excelAlfMap = new HashMap <String , String >();
373
+ excelAlfMap = new HashMap <>();
373
374
excelAlfMap .put ("catalog" , CCConstants .CCM_PROP_IO_REPLICATIONSOURCE );
374
375
excelAlfMap .put ("identifier" , CCConstants .CCM_PROP_IO_REPLICATIONSOURCEID );
375
376
excelAlfMap .put ("datestamp" , CCConstants .CCM_PROP_IO_REPLICATIONSOURCETIMESTAMP );
@@ -385,6 +386,7 @@ public HashMap<String, String> getExcelAlfMap() {
385
386
excelAlfMap .put ("educationalTypicalAgeRange" , CCConstants .CCM_PROP_IO_REPL_EDUCATIONAL_TYPICALAGERANGE );
386
387
excelAlfMap .put ("lifeCycleContributerAuthor" , CCConstants .CCM_PROP_IO_REPL_LIFECYCLECONTRIBUTER_AUTHOR );
387
388
excelAlfMap .put ("lifeCycleContributerPublisher" , CCConstants .CCM_PROP_IO_REPL_LIFECYCLECONTRIBUTER_PUBLISHER );
389
+ excelAlfMap .put ("lifeCycleContributerSubjectMatterExpert" , CCConstants .CCM_PROP_IO_REPL_LIFECYCLECONTRIBUTER_SUBJECT_MATTER_EXPERT );
388
390
excelAlfMap .put ("metadataContributerProvider" , CCConstants .CCM_PROP_IO_REPL_METADATACONTRIBUTER_PROVIDER );
389
391
excelAlfMap .put ("metadataContributerCreator" , CCConstants .CCM_PROP_IO_REPL_METADATACONTRIBUTER_CREATOR );
390
392
excelAlfMap .put ("technicalFormat" , CCConstants .LOM_PROP_TECHNICAL_FORMAT );
@@ -406,6 +408,7 @@ public HashMap<String, String> getExcelAlfMap() {
406
408
excelAlfMap .put ("licenseValid" ,CCConstants .CCM_PROP_IO_LICENSE_VALID );
407
409
excelAlfMap .put ("originUniversity" ,CCConstants .CCM_PROP_IO_UNIVERSITY );
408
410
excelAlfMap .put ("metadataset" ,CCConstants .CM_PROP_METADATASET_EDU_METADATASET );
411
+ excelAlfMap .put ("authorFreetext" ,CCConstants .CCM_PROP_AUTHOR_FREETEXT );
409
412
excelAlfMap .put ("oeh_widgets" ,"{" + CCConstants .NAMESPACE_CCM +"}oeh_widgets" );
410
413
}
411
414
return excelAlfMap ;
0 commit comments