Skip to content

Commit 0fbd1ec

Browse files
committed
Clean up unused codes and group common code in reusable methods of the DocumentRepository class
1 parent cca14fa commit 0fbd1ec

13 files changed

+567
-1816
lines changed

src/main/java/org/silverpeas/migration/classified/XmlFormImagesToAttachment.java

Lines changed: 124 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -30,40 +30,48 @@
3030
import java.sql.Statement;
3131
import java.util.ArrayList;
3232
import java.util.Collection;
33-
33+
import java.util.Locale;
34+
import javax.jcr.RepositoryException;
35+
import javax.jcr.Session;
3436
import org.silverpeas.dbbuilder.dbbuilder_dl.DbBuilderDynamicPart;
3537
import org.silverpeas.migration.jcr.service.AttachmentException;
36-
import org.silverpeas.migration.jcr.service.SimpleDocumentService;
38+
import org.silverpeas.migration.jcr.service.RepositoryManager;
3739
import org.silverpeas.migration.jcr.service.model.DocumentType;
40+
import org.silverpeas.migration.jcr.service.model.SimpleDocument;
3841
import org.silverpeas.migration.jcr.service.model.SimpleDocumentPK;
42+
import org.silverpeas.migration.jcr.service.repository.DocumentRepository;
43+
import org.silverpeas.util.Console;
3944
import org.silverpeas.util.StringUtil;
4045

41-
4246
/**
4347
* @author cbonin
4448
*/
4549
public class XmlFormImagesToAttachment extends DbBuilderDynamicPart {
46-
47-
private final SimpleDocumentService service;
48-
50+
51+
private final RepositoryManager repositoryManager;
52+
private final DocumentRepository documentRepository;
53+
4954
public XmlFormImagesToAttachment() {
50-
this.service = new SimpleDocumentService();
55+
this.repositoryManager = new RepositoryManager();
56+
this.documentRepository = new DocumentRepository(repositoryManager);
5157
}
5258

5359
public void migrate() throws IOException, SQLException {
60+
Session session = openJCRSession();
5461
try {
55-
5662
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");
5865
getConsole().printMessage("");
66+
long migrationStart = System.currentTimeMillis();
5967

6068
Collection<String> listInstanceId = getListClassifiedInstance();
61-
6269
for (String instanceId : listInstanceId) {
70+
long instanceMigrationStart = System.currentTimeMillis();
6371
int instanceIntId = Integer.parseInt(instanceId);
6472
getConsole().printMessage("Migrate Classified instance id = " + instanceId);
6573
String xmlFormName = getXMLFormName(instanceIntId);
66-
getConsole().printTrace("Migrate Classified XML Form = "+xmlFormName);
74+
getConsole().printTrace("Migrate Classified XML Form = " + xmlFormName);
6775
Collection<Integer> listTemplateId = getListTemplate(xmlFormName, instanceId);
6876
for (Integer templateId : listTemplateId) {
6977
getConsole().printTrace("TemplateId = " + templateId);
@@ -72,76 +80,74 @@ public void migrate() throws IOException, SQLException {
7280
int recordId = record.getRecordId();
7381
String classifiedId = record.getExternalId();
7482
int classifiedIntId = Integer.parseInt(classifiedId);
75-
getConsole().printMessage("Record [recordId = " + recordId + ", externalId = " +
76-
classifiedId + "]");
83+
getConsole().printMessage("Record [recordId = " + recordId + ", externalId = "
84+
+ classifiedId + "]");
7785
Collection<FieldTemplate> listValue = getListValue(recordId);
7886
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
8088
if (fieldTemplate.getFieldName().startsWith("photo")) {
8189
if (StringUtil.isDefined(fieldTemplate.getFieldValue())) {
8290
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());
8795
try {
8896
deletePhotoValue(recordId, fieldTemplate.getFieldName());
8997
} catch (SQLException e) {
9098
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);
95102
}
96103

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);
99107
simpleDocumentPk.setOldSilverpeasId(Long.valueOf(fieldTemplate.getFieldValue()));
100108
try {
101-
this.service.moveImageContext(simpleDocumentPk, getConsole());
109+
moveImageContext(session, simpleDocumentPk);
102110
} catch (AttachmentException e) {
103111
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());
107114
}
108-
}
115+
}
109116
} else {
110117
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");
113120
try {
114121
deletePhotoValue(recordId, fieldTemplate.getFieldName());
115122
} 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);
120126
}
121127
}
122128
} else if ("description".equals(fieldTemplate.getFieldName())) {
123129
getConsole()
124130
.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());
128134
boolean updated = false;
129135
try {
130136
updateClassified(instanceId, classifiedIntId, fieldTemplate.getFieldValue());
131137
updated = true;
132138
} 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);
136142
}
137143
if (updated) {
138-
getConsole().printTrace("Delete field template recordId = " + recordId +
139-
", fieldName = 'description'");
144+
getConsole().printTrace("Delete field template recordId = " + recordId
145+
+ ", fieldName = 'description'");
140146
try {
141147
deleteDescriptionValue(recordId);
142148
} 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);
145151
}
146152
}
147153
}
@@ -151,10 +157,18 @@ public void migrate() throws IOException, SQLException {
151157
getConsole().printTrace("---------------------");
152158
}
153159

160+
long instanceMigrationEnd = System.currentTimeMillis();
161+
getConsole().printMessage("Classified instance " + instanceId + " has been migrated in "
162+
+ (instanceMigrationEnd - instanceMigrationStart) + " ms");
154163
getConsole()
155164
.printMessage("--------------------------------------------------------------------");
156165
}
166+
long migrationEnd = System.currentTimeMillis();
167+
getConsole().printMessage("Classified components have been migrated in "
168+
+ (migrationEnd - migrationStart) + " ms");
157169
} finally {
170+
repositoryManager.logout(session);
171+
repositoryManager.shutdown();
158172
getConsole().close();
159173
}
160174
}
@@ -215,8 +229,8 @@ public Collection<Integer> getListTemplate(String xmlFormName, String instanceId
215229
try {
216230
pstmt =
217231
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 ?");
220234
pstmt.setString(1, xmlFormName);
221235
pstmt.setString(2, "classifieds" + instanceId + "%");
222236
rs = pstmt.executeQuery();
@@ -298,8 +312,8 @@ public void updateClassified(String instanceId, int classifiedId, String descrip
298312
try {
299313
pstmt =
300314
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 = ? ");
303317
pstmt.setString(1, description);
304318
pstmt.setString(2, "classifieds" + instanceId);
305319
pstmt.setInt(3, classifiedId);
@@ -339,4 +353,65 @@ public void deletePhotoValue(int recordId, String fieldPhotoName) throws SQLExce
339353
}
340354
}
341355
}
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+
}
342417
}

0 commit comments

Comments
 (0)