30
30
import java .sql .Statement ;
31
31
import java .util .ArrayList ;
32
32
import java .util .Collection ;
33
-
33
+ import java .util .Locale ;
34
+ import javax .jcr .RepositoryException ;
35
+ import javax .jcr .Session ;
34
36
import org .silverpeas .dbbuilder .dbbuilder_dl .DbBuilderDynamicPart ;
35
37
import org .silverpeas .migration .jcr .service .AttachmentException ;
36
- import org .silverpeas .migration .jcr .service .SimpleDocumentService ;
38
+ import org .silverpeas .migration .jcr .service .RepositoryManager ;
37
39
import org .silverpeas .migration .jcr .service .model .DocumentType ;
40
+ import org .silverpeas .migration .jcr .service .model .SimpleDocument ;
38
41
import org .silverpeas .migration .jcr .service .model .SimpleDocumentPK ;
42
+ import org .silverpeas .migration .jcr .service .repository .DocumentRepository ;
43
+ import org .silverpeas .util .Console ;
39
44
import org .silverpeas .util .StringUtil ;
40
45
41
-
42
46
/**
43
47
* @author cbonin
44
48
*/
45
49
public class XmlFormImagesToAttachment extends DbBuilderDynamicPart {
46
-
47
- private final SimpleDocumentService service ;
48
-
50
+
51
+ private final RepositoryManager repositoryManager ;
52
+ private final DocumentRepository documentRepository ;
53
+
49
54
public XmlFormImagesToAttachment () {
50
- this .service = new SimpleDocumentService ();
55
+ this .repositoryManager = new RepositoryManager ();
56
+ this .documentRepository = new DocumentRepository (repositoryManager );
51
57
}
52
58
53
59
public void migrate () throws IOException , SQLException {
60
+ Session session = openJCRSession ();
54
61
try {
55
-
56
62
getConsole ()
57
- .printMessage ("Migrate Classified Images in XML Form To Attachment and Classified Description in XML Form to Classified DB" );
63
+ .printMessage (
64
+ "Migrate Classified Images in XML Form To Attachment and Classified Description in XML Form to Classified DB" );
58
65
getConsole ().printMessage ("" );
66
+ long migrationStart = System .currentTimeMillis ();
59
67
60
68
Collection <String > listInstanceId = getListClassifiedInstance ();
61
-
62
69
for (String instanceId : listInstanceId ) {
70
+ long instanceMigrationStart = System .currentTimeMillis ();
63
71
int instanceIntId = Integer .parseInt (instanceId );
64
72
getConsole ().printMessage ("Migrate Classified instance id = " + instanceId );
65
73
String xmlFormName = getXMLFormName (instanceIntId );
66
- getConsole ().printTrace ("Migrate Classified XML Form = " + xmlFormName );
74
+ getConsole ().printTrace ("Migrate Classified XML Form = " + xmlFormName );
67
75
Collection <Integer > listTemplateId = getListTemplate (xmlFormName , instanceId );
68
76
for (Integer templateId : listTemplateId ) {
69
77
getConsole ().printTrace ("TemplateId = " + templateId );
@@ -72,76 +80,74 @@ public void migrate() throws IOException, SQLException {
72
80
int recordId = record .getRecordId ();
73
81
String classifiedId = record .getExternalId ();
74
82
int classifiedIntId = Integer .parseInt (classifiedId );
75
- getConsole ().printMessage ("Record [recordId = " + recordId + ", externalId = " +
76
- classifiedId + "]" );
83
+ getConsole ().printMessage ("Record [recordId = " + recordId + ", externalId = "
84
+ + classifiedId + "]" );
77
85
Collection <FieldTemplate > listValue = getListValue (recordId );
78
86
for (FieldTemplate fieldTemplate : listValue ) {
79
- getConsole ().printTrace ("Field Name = " + fieldTemplate .getFieldName ());//category | type | description | photo
87
+ getConsole ().printTrace ("Field Name = " + fieldTemplate .getFieldName ());//category | type | description | photo
80
88
if (fieldTemplate .getFieldName ().startsWith ("photo" )) {
81
89
if (StringUtil .isDefined (fieldTemplate .getFieldValue ())) {
82
90
getConsole ().printTrace ("Photo field value = " + fieldTemplate .getFieldValue ());
83
-
84
- getConsole ().printTrace ("Delete field template recordId = " + recordId +
85
- ", fieldName = '" + fieldTemplate .getFieldName () +
86
- "', fieldValue = " + fieldTemplate .getFieldValue ());
91
+
92
+ getConsole ().printTrace ("Delete field template recordId = " + recordId
93
+ + ", fieldName = '" + fieldTemplate .getFieldName () + "', fieldValue = "
94
+ + fieldTemplate .getFieldValue ());
87
95
try {
88
96
deletePhotoValue (recordId , fieldTemplate .getFieldName ());
89
97
} catch (SQLException e ) {
90
98
getConsole ()
91
- .printError ("ERROR when Deleting field template recordId = " +
92
- recordId + ", fieldName = '" + fieldTemplate .getFieldName () +
93
- "', fieldValue = " + fieldTemplate .getFieldValue () +
94
- ", error = " + e .getMessage (), e );
99
+ .printError ("ERROR when Deleting field template recordId = " + recordId
100
+ + ", fieldName = '" + fieldTemplate .getFieldName () + "', fieldValue = "
101
+ + fieldTemplate .getFieldValue () + ", error = " + e .getMessage (), e );
95
102
}
96
103
97
- if (StringUtil .isLong (fieldTemplate .getFieldValue ())) {
98
- SimpleDocumentPK simpleDocumentPk = new SimpleDocumentPK (null , "classifieds" +instanceId );
104
+ if (StringUtil .isLong (fieldTemplate .getFieldValue ())) {
105
+ SimpleDocumentPK simpleDocumentPk = new SimpleDocumentPK (null , "classifieds"
106
+ + instanceId );
99
107
simpleDocumentPk .setOldSilverpeasId (Long .valueOf (fieldTemplate .getFieldValue ()));
100
108
try {
101
- this . service . moveImageContext (simpleDocumentPk , getConsole () );
109
+ moveImageContext (session , simpleDocumentPk );
102
110
} catch (AttachmentException e ) {
103
111
getConsole ()
104
- .printError ("ERROR when Moving attachment image id = " +fieldTemplate .getFieldValue ()+
105
- ", " +
106
- e .getMessage ());
112
+ .printError ("ERROR when Moving attachment image id = " + fieldTemplate .
113
+ getFieldValue () + ", " + e .getMessage ());
107
114
}
108
- }
115
+ }
109
116
} else {
110
117
getConsole ().printTrace ("Photo field value = null" );
111
- getConsole ().printTrace ("Delete field template recordId = " + recordId +
112
- ", fieldName = '" + fieldTemplate .getFieldName () + "', fieldValue = null" );
118
+ getConsole ().printTrace ("Delete field template recordId = " + recordId
119
+ + ", fieldName = '" + fieldTemplate .getFieldName () + "', fieldValue = null" );
113
120
try {
114
121
deletePhotoValue (recordId , fieldTemplate .getFieldName ());
115
122
} catch (SQLException e ) {
116
- getConsole ().printError ("ERROR when Deleting field template recordId = " +
117
- recordId + ", fieldName = '" + fieldTemplate .getFieldName () +
118
- "', fieldValue = null, error = " +
119
- e .getMessage (), e );
123
+ getConsole ().printError ("ERROR when Deleting field template recordId = "
124
+ + recordId + ", fieldName = '" + fieldTemplate .getFieldName ()
125
+ + "', fieldValue = null, error = " + e .getMessage (), e );
120
126
}
121
127
}
122
128
} else if ("description" .equals (fieldTemplate .getFieldName ())) {
123
129
getConsole ()
124
130
.printTrace ("Description field value = " + fieldTemplate .getFieldValue ());
125
- getConsole ().printTrace ("Update classified instanceId = " + instanceId +
126
- ", classifiedId = " + classifiedId + ", description = " +
127
- fieldTemplate . getFieldValue ());
131
+ getConsole ().printTrace ("Update classified instanceId = " + instanceId
132
+ + ", classifiedId = " + classifiedId + ", description = " + fieldTemplate .
133
+ getFieldValue ());
128
134
boolean updated = false ;
129
135
try {
130
136
updateClassified (instanceId , classifiedIntId , fieldTemplate .getFieldValue ());
131
137
updated = true ;
132
138
} catch (SQLException e ) {
133
- getConsole ().printError ("ERROR when Updating classified instanceId = " +
134
- instanceId + ", classifiedId = " + classifiedId + ", description = " +
135
- fieldTemplate .getFieldValue () + ", error = " + e .getMessage (), e );
139
+ getConsole ().printError ("ERROR when Updating classified instanceId = "
140
+ + instanceId + ", classifiedId = " + classifiedId + ", description = "
141
+ + fieldTemplate .getFieldValue () + ", error = " + e .getMessage (), e );
136
142
}
137
143
if (updated ) {
138
- getConsole ().printTrace ("Delete field template recordId = " + recordId +
139
- ", fieldName = 'description'" );
144
+ getConsole ().printTrace ("Delete field template recordId = " + recordId
145
+ + ", fieldName = 'description'" );
140
146
try {
141
147
deleteDescriptionValue (recordId );
142
148
} catch (SQLException e ) {
143
- getConsole ().printError ("ERROR when Deleting field template recordId = " +
144
- recordId + ", fieldName = 'description', error = " + e .getMessage (), e );
149
+ getConsole ().printError ("ERROR when Deleting field template recordId = "
150
+ + recordId + ", fieldName = 'description', error = " + e .getMessage (), e );
145
151
}
146
152
}
147
153
}
@@ -151,10 +157,18 @@ public void migrate() throws IOException, SQLException {
151
157
getConsole ().printTrace ("---------------------" );
152
158
}
153
159
160
+ long instanceMigrationEnd = System .currentTimeMillis ();
161
+ getConsole ().printMessage ("Classified instance " + instanceId + " has been migrated in "
162
+ + (instanceMigrationEnd - instanceMigrationStart ) + " ms" );
154
163
getConsole ()
155
164
.printMessage ("--------------------------------------------------------------------" );
156
165
}
166
+ long migrationEnd = System .currentTimeMillis ();
167
+ getConsole ().printMessage ("Classified components have been migrated in "
168
+ + (migrationEnd - migrationStart ) + " ms" );
157
169
} finally {
170
+ repositoryManager .logout (session );
171
+ repositoryManager .shutdown ();
158
172
getConsole ().close ();
159
173
}
160
174
}
@@ -215,8 +229,8 @@ public Collection<Integer> getListTemplate(String xmlFormName, String instanceId
215
229
try {
216
230
pstmt =
217
231
getConnection ()
218
- .prepareStatement (
219
- "SELECT templateId FROM sb_formtemplate_template where templateName=? and externalId like ?" );
232
+ .prepareStatement (
233
+ "SELECT templateId FROM sb_formtemplate_template where templateName=? and externalId like ?" );
220
234
pstmt .setString (1 , xmlFormName );
221
235
pstmt .setString (2 , "classifieds" + instanceId + "%" );
222
236
rs = pstmt .executeQuery ();
@@ -298,8 +312,8 @@ public void updateClassified(String instanceId, int classifiedId, String descrip
298
312
try {
299
313
pstmt =
300
314
getConnection ()
301
- .prepareStatement (
302
- "UPDATE SC_Classifieds_Classifieds set description = ? where instanceId = ? and classifiedId = ? " );
315
+ .prepareStatement (
316
+ "UPDATE SC_Classifieds_Classifieds set description = ? where instanceId = ? and classifiedId = ? " );
303
317
pstmt .setString (1 , description );
304
318
pstmt .setString (2 , "classifieds" + instanceId );
305
319
pstmt .setInt (3 , classifiedId );
@@ -339,4 +353,65 @@ public void deletePhotoValue(int recordId, String fieldPhotoName) throws SQLExce
339
353
}
340
354
}
341
355
}
356
+
357
+ private void moveImageContext (Session session , SimpleDocumentPK simpleDocumentPk ) {
358
+ try {
359
+ Console console = getConsole ();
360
+ session = repositoryManager .getSession ();
361
+ SimpleDocument simpleDocument = documentRepository .findDocumentByOldSilverpeasId (session ,
362
+ simpleDocumentPk .getComponentName (),
363
+ simpleDocumentPk .getOldSilverpeasId (), false , null );
364
+ if (simpleDocument != null ) {
365
+ boolean verifFormatImage = verifFormatImage (simpleDocument .getFilename ());
366
+ if (verifFormatImage ) {
367
+ console .printTrace ("Delete attachment with attachmentId = " + simpleDocument .getId ()
368
+ + ", oldSilverpeasId = " + simpleDocument .getOldSilverpeasId ());
369
+ String sourcePath = simpleDocument .getFullJcrPath ();
370
+ simpleDocument .setDocumentType (DocumentType .attachment );
371
+ String destinationPath = simpleDocument .getFullJcrPath ();
372
+ console .printTrace ("Moving " + sourcePath + " to " + destinationPath );
373
+ if (!session .nodeExists (destinationPath )) {
374
+ documentRepository .
375
+ prepareComponentAttachments (session , simpleDocumentPk .getInstanceId (),
376
+ DocumentType .attachment .getFolderName ());
377
+ }
378
+ session .move (sourcePath , destinationPath );
379
+ } else {// format Image not correct
380
+ console .printTrace ("Format Image not correct, delete attachment attachmentId = "
381
+ + simpleDocument .getId () + ", oldSilverpeasId = " + simpleDocument
382
+ .getOldSilverpeasId ());
383
+ documentRepository .deleteDocument (session , simpleDocument .getPk ());
384
+ }
385
+ session .save ();
386
+ } else {
387
+ throw new AttachmentException ("ERROR Simple Document with oldSilverpeasId "
388
+ + simpleDocumentPk .getOldSilverpeasId () + " not found" );
389
+ }
390
+ } catch (RepositoryException ex ) {
391
+ throw new AttachmentException (ex );
392
+ }
393
+ }
394
+
395
+ private Session openJCRSession () {
396
+ try {
397
+ return repositoryManager .getSession ();
398
+ } catch (RepositoryException ex ) {
399
+ throw new AttachmentException (ex );
400
+ }
401
+ }
402
+
403
+ private boolean verifFormatImage (String filename ) {
404
+ int indexPoint = filename .lastIndexOf ('.' );
405
+ if (indexPoint != -1 ) {
406
+ // le fichier contient une extension. On recupere l'extension
407
+ String extension = filename .substring (indexPoint + 1 );
408
+ extension = extension .toLowerCase (Locale .FRANCE );
409
+ if ("jpg" .equals (extension ) || "gif" .equals (extension ) || "bmp" .equals (extension ) || "tiff"
410
+ .equals (extension ) || "tif" .equals (extension ) || "jpeg" .equals (extension ) || "png" .equals (
411
+ extension )) {
412
+ return true ;
413
+ }
414
+ }
415
+ return false ;
416
+ }
342
417
}
0 commit comments