Skip to content

Commit 21f1c75

Browse files
committed
Bug #5329 - adding a treatment to purge empty WYSIWYG contents
(adding JCR unit tests ...) (adding a treatment in order to rename physical WYSIWYG file name according to the language)
1 parent 5635708 commit 21f1c75

27 files changed

+4892
-65
lines changed

pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,11 @@
100100
<groupId>org.springframework</groupId>
101101
<artifactId>spring-context</artifactId>
102102
</dependency>
103+
<dependency>
104+
<groupId>org.mockito</groupId>
105+
<artifactId>mockito-core</artifactId>
106+
<scope>compile</scope>
107+
</dependency>
103108
<dependency>
104109
<groupId>commons-dbcp</groupId>
105110
<artifactId>commons-dbcp</artifactId>

src/main/java/org/silverpeas/migration/jcr/service/RepositoryManager.java

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,35 +23,43 @@
2323
*/
2424
package org.silverpeas.migration.jcr.service;
2525

26-
import java.io.IOException;
27-
import java.io.InputStreamReader;
28-
import java.io.Reader;
29-
import java.util.HashMap;
30-
import java.util.Map;
31-
import javax.jcr.RepositoryException;
32-
import javax.jcr.Session;
33-
import javax.jcr.SimpleCredentials;
34-
import javax.jcr.UnsupportedRepositoryOperationException;
35-
import javax.jcr.nodetype.InvalidNodeTypeDefinitionException;
36-
import javax.jcr.nodetype.NodeTypeExistsException;
3726
import org.apache.commons.io.Charsets;
3827
import org.apache.commons.io.IOUtils;
3928
import org.apache.jackrabbit.api.JackrabbitRepository;
4029
import org.apache.jackrabbit.commons.JcrUtils;
4130
import org.apache.jackrabbit.commons.cnd.CndImporter;
4231
import org.apache.jackrabbit.commons.cnd.ParseException;
4332
import org.apache.jackrabbit.core.RepositoryFactoryImpl;
33+
import org.apache.jackrabbit.core.RepositoryImpl;
34+
import org.apache.jackrabbit.core.config.RepositoryConfig;
4435
import org.silverpeas.util.ConfigurationHolder;
4536
import org.slf4j.Logger;
4637
import org.slf4j.LoggerFactory;
4738

39+
import javax.jcr.RepositoryException;
40+
import javax.jcr.Session;
41+
import javax.jcr.SimpleCredentials;
42+
import javax.jcr.UnsupportedRepositoryOperationException;
43+
import javax.jcr.nodetype.InvalidNodeTypeDefinitionException;
44+
import javax.jcr.nodetype.NodeTypeExistsException;
45+
import java.io.IOException;
46+
import java.io.InputStreamReader;
47+
import java.io.Reader;
48+
import java.util.HashMap;
49+
import java.util.Map;
50+
4851
/**
4952
* @author ehugonnet
5053
*/
5154
public class RepositoryManager {
5255

5356
private static final Logger logger = LoggerFactory.getLogger(RepositoryManager.class);
5457
private JackrabbitRepository repository;
58+
private static boolean isJcrTestEnv = false;
59+
60+
public static void setIsJcrTestEnv() {
61+
isJcrTestEnv = true;
62+
}
5563

5664
public RepositoryManager() {
5765
try {
@@ -79,7 +87,11 @@ private void initRepository(String repositoryHome, String conf) throws Repositor
7987
Map<String, String> parameters = new HashMap<String, String>(2);
8088
parameters.put(RepositoryFactoryImpl.REPOSITORY_HOME, repositoryHome);
8189
parameters.put(RepositoryFactoryImpl.REPOSITORY_CONF, conf);
82-
repository = (JackrabbitRepository) JcrUtils.getRepository(parameters);
90+
if (!isJcrTestEnv) {
91+
repository = (JackrabbitRepository) JcrUtils.getRepository(parameters);
92+
} else {
93+
repository = RepositoryImpl.create(RepositoryConfig.create(conf, repositoryHome));
94+
}
8395
Reader reader = new InputStreamReader(
8496
this.getClass().getClassLoader().getResourceAsStream("silverpeas-jcr.txt"), Charsets.UTF_8);
8597
try {
@@ -115,4 +127,8 @@ public void logout(Session session) {
115127
public void shutdown() {
116128
this.repository.shutdown();
117129
}
130+
131+
public static boolean isIsJcrTestEnv() {
132+
return isJcrTestEnv;
133+
}
118134
}

src/main/java/org/silverpeas/migration/jcr/service/SimpleDocumentService.java

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,14 @@
2323
*/
2424
package org.silverpeas.migration.jcr.service;
2525

26+
import org.apache.commons.io.FileUtils;
2627
import org.apache.commons.io.IOUtils;
28+
import org.silverpeas.migration.jcr.service.model.DocumentType;
2729
import org.silverpeas.migration.jcr.service.model.SimpleDocument;
2830
import org.silverpeas.migration.jcr.service.model.SimpleDocumentPK;
2931
import org.silverpeas.migration.jcr.service.repository.DocumentRepository;
3032
import org.silverpeas.util.StringUtil;
33+
import org.silverpeas.util.file.FileUtil;
3134

3235
import javax.jcr.RepositoryException;
3336
import javax.jcr.Session;
@@ -37,6 +40,7 @@
3740
import java.io.FileNotFoundException;
3841
import java.io.IOException;
3942
import java.io.InputStream;
43+
import java.io.OutputStream;
4044
import java.util.ArrayList;
4145
import java.util.List;
4246

@@ -142,6 +146,63 @@ public SimpleDocument mergeDocument(SimpleDocument document, SimpleDocument merg
142146
return searchDocumentById(document.getPk(), document.getLanguage());
143147
}
144148

149+
public void removeContent(SimpleDocument document, String lang) {
150+
Session session = null;
151+
try {
152+
session = repositoryManager.getSession();
153+
boolean requireLock = repository.lock(session, document, document.getEditedBy());
154+
boolean existsOtherContents = repository.removeContent(session, document.getPk(), lang);
155+
session.save();
156+
SimpleDocument finalDocument = document;
157+
if (requireLock) {
158+
finalDocument = repository.unlockFromContentDeletion(session, document);
159+
if (existsOtherContents) {
160+
repository.duplicateContent(document, finalDocument);
161+
}
162+
}
163+
finalDocument.setLanguage(lang);
164+
final File fileToDelete;
165+
if (!existsOtherContents) {
166+
fileToDelete =
167+
new File(finalDocument.getDirectoryPath(null)).getParentFile().getParentFile();
168+
} else {
169+
fileToDelete = new File(finalDocument.getAttachmentPath());
170+
}
171+
FileUtils.deleteQuietly(fileToDelete);
172+
FileUtil.deleteEmptyDir(fileToDelete.getParentFile());
173+
} catch (RepositoryException ex) {
174+
throw new AttachmentException(ex);
175+
} catch (IOException ex) {
176+
throw new AttachmentException(ex);
177+
} finally {
178+
repositoryManager.logout(session);
179+
}
180+
}
181+
182+
public void getBinaryContent(OutputStream output, SimpleDocumentPK pk, String lang) {
183+
getBinaryContent(output, pk, lang, 0, -1);
184+
}
185+
186+
public void getBinaryContent(final OutputStream output, final SimpleDocumentPK pk,
187+
final String lang, final long contentOffset, final long contentLength) {
188+
Session session = null;
189+
InputStream in = null;
190+
try {
191+
session = repositoryManager.getSession();
192+
in = repository.getContent(session, pk, lang);
193+
if (in != null) {
194+
IOUtils.copyLarge(in, output, contentOffset, contentLength);
195+
}
196+
} catch (IOException ex) {
197+
throw new AttachmentException(ex);
198+
} catch (RepositoryException ex) {
199+
throw new AttachmentException(ex);
200+
} finally {
201+
IOUtils.closeQuietly(in);
202+
repositoryManager.logout(session);
203+
}
204+
}
205+
145206
public List<String> listBasenames(String instanceId) {
146207
Session session = null;
147208
try {
@@ -154,6 +215,43 @@ public List<String> listBasenames(String instanceId) {
154215
}
155216
}
156217

218+
public List<String> listForeignIdsWithWysiwyg(String instanceId) {
219+
Session session = null;
220+
try {
221+
session = repositoryManager.getSession();
222+
return new ArrayList<String>(
223+
repository.listForeignIdsByType(session, instanceId, DocumentType.wysiwyg));
224+
} catch (RepositoryException ex) {
225+
throw new AttachmentException(ex);
226+
} finally {
227+
repositoryManager.logout(session);
228+
}
229+
}
230+
231+
/**
232+
* Gets the list of attachments of WYSIWYG type for the given instance identifier and
233+
* foreign identifier. For each document represented by a master JCR Node, the number of
234+
* SimpleDocument returned depends on the number of languages registered for the JCR Node.
235+
* For a document, if it exists one version in "fr" and an other one in "en" (for example), two
236+
* SimpleDocument are returned, one for the "fr" language and an other one for "en" language.
237+
* @param instanceId the identifier of the component instance limitation.
238+
* @param foreignId the identifier of object limitation.
239+
* @return
240+
* @throws RepositoryException
241+
*/
242+
public List<SimpleDocument> listWysiwygByForeignId(String instanceId, String foreignId) {
243+
Session session = null;
244+
try {
245+
session = repositoryManager.getSession();
246+
return repository.listAttachmentsByForeignIdAndDocumentType(session, instanceId, foreignId,
247+
DocumentType.wysiwyg);
248+
} catch (RepositoryException ex) {
249+
throw new AttachmentException(ex);
250+
} finally {
251+
repositoryManager.logout(session);
252+
}
253+
}
254+
157255
public List<SimpleDocument> listWysiwygForBasename(String basename, String instanceId) {
158256
Session session = null;
159257
try {

src/main/java/org/silverpeas/migration/jcr/service/model/SimpleAttachment.java

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,16 @@
2323
*/
2424
package org.silverpeas.migration.jcr.service.model;
2525

26+
import org.silverpeas.migration.jcr.service.ConverterUtil;
27+
2628
import java.io.File;
2729
import java.io.Serializable;
2830
import java.util.Date;
29-
import org.silverpeas.migration.jcr.service.ConverterUtil;
3031

3132
/**
3233
* @author ehugonnet
3334
*/
34-
public class SimpleAttachment implements Serializable {
35+
public class SimpleAttachment implements Serializable, Cloneable {
3536

3637
private static final long serialVersionUID = -6153003608158238503L;
3738
private String filename;
@@ -60,6 +61,24 @@ public SimpleAttachment(String filename, String language, String title, String d
6061
this.xmlFormId = xmlFormId;
6162
}
6263

64+
public SimpleAttachment(final String filename, final String language, final String title,
65+
final String description, final long size, final String contentType, final String createdBy,
66+
final Date created, final String updatedBy, final Date updated, final String xmlFormId,
67+
final File file) {
68+
this.filename = filename;
69+
this.language = language;
70+
this.title = title;
71+
this.description = description;
72+
this.size = size;
73+
this.contentType = contentType;
74+
this.createdBy = createdBy;
75+
this.created = created;
76+
this.updatedBy = updatedBy;
77+
this.updated = updated;
78+
this.xmlFormId = xmlFormId;
79+
this.file = file;
80+
}
81+
6382
public SimpleAttachment() {
6483
}
6584

@@ -253,4 +272,14 @@ public String toString() {
253272
+ ", createdBy=" + createdBy + ", created=" + created + ", updatedBy=" + updatedBy
254273
+ ", updated=" + updated + ", xmlFormId=" + xmlFormId + '}';
255274
}
275+
276+
@SuppressWarnings("CloneDoesntDeclareCloneNotSupportedException")
277+
@Override
278+
public SimpleAttachment clone() {
279+
try {
280+
return (SimpleAttachment) super.clone();
281+
} catch (CloneNotSupportedException e) {
282+
return null;
283+
}
284+
}
256285
}

src/main/java/org/silverpeas/migration/jcr/service/model/SimpleDocument.java

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
*
3737
* @author ehugonnet
3838
*/
39-
public class SimpleDocument implements Serializable {
39+
public class SimpleDocument implements Serializable, Cloneable {
4040

4141
private static final long serialVersionUID = 8778738762037114180L;
4242
public static final String WEBDAV_FOLDER = "webdav";
@@ -147,6 +147,32 @@ public SimpleDocument(SimpleDocumentPK pk, String foreignId, int order, boolean
147147
this.file = file;
148148
}
149149

150+
public SimpleDocument(final SimpleDocumentPK pk, final String foreignId, final int order,
151+
final boolean versioned, final String editedBy, final Date reservation, final Date alert,
152+
final Date expiry, final String status, final String cloneId, final int minorVersion,
153+
final int majorVersion, final boolean publicDocument, final String nodeName,
154+
final String comment, final DocumentType documentType, final String oldContext,
155+
final SimpleAttachment file) {
156+
this.pk = pk;
157+
this.foreignId = foreignId;
158+
this.order = order;
159+
this.versioned = versioned;
160+
this.editedBy = editedBy;
161+
this.reservation = reservation;
162+
this.alert = alert;
163+
this.expiry = expiry;
164+
this.status = status;
165+
this.cloneId = cloneId;
166+
this.minorVersion = minorVersion;
167+
this.majorVersion = majorVersion;
168+
this.publicDocument = publicDocument;
169+
this.nodeName = nodeName;
170+
this.comment = comment;
171+
this.documentType = documentType;
172+
this.oldContext = oldContext;
173+
this.file = file;
174+
}
175+
150176
public SimpleDocument() {
151177
}
152178

@@ -513,4 +539,17 @@ public SimpleDocument getLastPublicVersion() {
513539
public String getFolder() {
514540
return documentType.getFolderName();
515541
}
542+
543+
@SuppressWarnings("CloneDoesntDeclareCloneNotSupportedException")
544+
@Override
545+
public SimpleDocument clone() {
546+
try {
547+
SimpleDocument clone = (SimpleDocument) super.clone();
548+
clone.setPK(clone.getPk().clone());
549+
clone.setAttachment(clone.getAttachment().clone());
550+
return clone;
551+
} catch (CloneNotSupportedException e) {
552+
return null;
553+
}
554+
}
516555
}

src/main/java/org/silverpeas/migration/jcr/service/model/SimpleDocumentPK.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,4 +98,9 @@ public String toString() {
9898
append(getComponentName()).append(", oldSilverpeasId=").append(oldSilverpeasId).append('}');
9999
return buffer.toString();
100100
}
101+
102+
@Override
103+
public SimpleDocumentPK clone() {
104+
return (SimpleDocumentPK) super.clone();
105+
}
101106
}

src/main/java/org/silverpeas/migration/jcr/service/model/WAPrimaryKey.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
* @author Nicolas Eysseric
3737
* @version 1.0
3838
*/
39-
public abstract class WAPrimaryKey implements Serializable {
39+
public abstract class WAPrimaryKey implements Serializable, Cloneable {
4040

4141
private static final long serialVersionUID = -2456912022917180222L;
4242

@@ -228,4 +228,13 @@ public String getSpaceId() {
228228
public String getInstanceId() {
229229
return getComponentName();
230230
}
231+
232+
@Override
233+
public WAPrimaryKey clone() {
234+
try {
235+
return (WAPrimaryKey) super.clone();
236+
} catch (CloneNotSupportedException e) {
237+
return null;
238+
}
239+
}
231240
}

src/main/java/org/silverpeas/migration/jcr/service/repository/DocumentConverter.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,8 +182,13 @@ protected SimpleAttachment getAttachment(Node node, String language) throws Repo
182182
}
183183

184184
public void fillNode(SimpleDocument document, Node documentNode) throws RepositoryException {
185+
fillNode(document, documentNode, false);
186+
}
187+
188+
public void fillNode(SimpleDocument document, Node documentNode, boolean skipAttachmentContent)
189+
throws RepositoryException {
185190
setDocumentNodeProperties(document, documentNode);
186-
if (document.getAttachment() != null) {
191+
if (!skipAttachmentContent && document.getAttachment() != null) {
187192
Node attachmentNode = getAttachmentNode(document.getAttachment().getNodeName(), documentNode);
188193
attachmentConverter.fillNode(document.getAttachment(), attachmentNode);
189194
}

0 commit comments

Comments
 (0)