Skip to content

Bug #5329 - adding a treatment to purge empty WYSIWYG contents #17

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 22, 2014
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -100,6 +100,11 @@
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
Original file line number Diff line number Diff line change
@@ -23,35 +23,43 @@
*/
package org.silverpeas.migration.jcr.service;

import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.HashMap;
import java.util.Map;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.SimpleCredentials;
import javax.jcr.UnsupportedRepositoryOperationException;
import javax.jcr.nodetype.InvalidNodeTypeDefinitionException;
import javax.jcr.nodetype.NodeTypeExistsException;
import org.apache.commons.io.Charsets;
import org.apache.commons.io.IOUtils;
import org.apache.jackrabbit.api.JackrabbitRepository;
import org.apache.jackrabbit.commons.JcrUtils;
import org.apache.jackrabbit.commons.cnd.CndImporter;
import org.apache.jackrabbit.commons.cnd.ParseException;
import org.apache.jackrabbit.core.RepositoryFactoryImpl;
import org.apache.jackrabbit.core.RepositoryImpl;
import org.apache.jackrabbit.core.config.RepositoryConfig;
import org.silverpeas.util.ConfigurationHolder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.SimpleCredentials;
import javax.jcr.UnsupportedRepositoryOperationException;
import javax.jcr.nodetype.InvalidNodeTypeDefinitionException;
import javax.jcr.nodetype.NodeTypeExistsException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.HashMap;
import java.util.Map;

/**
* @author ehugonnet
*/
public class RepositoryManager {

private static final Logger logger = LoggerFactory.getLogger(RepositoryManager.class);
private JackrabbitRepository repository;
private static boolean isJcrTestEnv = false;

public static void setIsJcrTestEnv() {
isJcrTestEnv = true;
}

public RepositoryManager() {
try {
@@ -79,7 +87,11 @@ private void initRepository(String repositoryHome, String conf) throws Repositor
Map<String, String> parameters = new HashMap<String, String>(2);
parameters.put(RepositoryFactoryImpl.REPOSITORY_HOME, repositoryHome);
parameters.put(RepositoryFactoryImpl.REPOSITORY_CONF, conf);
repository = (JackrabbitRepository) JcrUtils.getRepository(parameters);
if (!isJcrTestEnv) {
repository = (JackrabbitRepository) JcrUtils.getRepository(parameters);
} else {
repository = RepositoryImpl.create(RepositoryConfig.create(conf, repositoryHome));
}
Reader reader = new InputStreamReader(
this.getClass().getClassLoader().getResourceAsStream("silverpeas-jcr.txt"), Charsets.UTF_8);
try {
@@ -115,4 +127,8 @@ public void logout(Session session) {
public void shutdown() {
this.repository.shutdown();
}

public static boolean isIsJcrTestEnv() {
return isJcrTestEnv;
}
}
Original file line number Diff line number Diff line change
@@ -23,11 +23,14 @@
*/
package org.silverpeas.migration.jcr.service;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.silverpeas.migration.jcr.service.model.DocumentType;
import org.silverpeas.migration.jcr.service.model.SimpleDocument;
import org.silverpeas.migration.jcr.service.model.SimpleDocumentPK;
import org.silverpeas.migration.jcr.service.repository.DocumentRepository;
import org.silverpeas.util.StringUtil;
import org.silverpeas.util.file.FileUtil;

import javax.jcr.RepositoryException;
import javax.jcr.Session;
@@ -37,6 +40,7 @@
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

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

public void removeContent(SimpleDocument document, String lang) {
Session session = null;
try {
session = repositoryManager.getSession();
boolean requireLock = repository.lock(session, document, document.getEditedBy());
boolean existsOtherContents = repository.removeContent(session, document.getPk(), lang);
session.save();
SimpleDocument finalDocument = document;
if (requireLock) {
finalDocument = repository.unlockFromContentDeletion(session, document);
if (existsOtherContents) {
repository.duplicateContent(document, finalDocument);
}
}
finalDocument.setLanguage(lang);
final File fileToDelete;
if (!existsOtherContents) {
fileToDelete =
new File(finalDocument.getDirectoryPath(null)).getParentFile().getParentFile();
} else {
fileToDelete = new File(finalDocument.getAttachmentPath());
}
FileUtils.deleteQuietly(fileToDelete);
FileUtil.deleteEmptyDir(fileToDelete.getParentFile());
} catch (RepositoryException ex) {
throw new AttachmentException(ex);
} catch (IOException ex) {
throw new AttachmentException(ex);
} finally {
repositoryManager.logout(session);
}
}

public void getBinaryContent(OutputStream output, SimpleDocumentPK pk, String lang) {
getBinaryContent(output, pk, lang, 0, -1);
}

public void getBinaryContent(final OutputStream output, final SimpleDocumentPK pk,
final String lang, final long contentOffset, final long contentLength) {
Session session = null;
InputStream in = null;
try {
session = repositoryManager.getSession();
in = repository.getContent(session, pk, lang);
if (in != null) {
IOUtils.copyLarge(in, output, contentOffset, contentLength);
}
} catch (IOException ex) {
throw new AttachmentException(ex);
} catch (RepositoryException ex) {
throw new AttachmentException(ex);
} finally {
IOUtils.closeQuietly(in);
repositoryManager.logout(session);
}
}

public List<String> listBasenames(String instanceId) {
Session session = null;
try {
@@ -154,6 +215,43 @@ public List<String> listBasenames(String instanceId) {
}
}

public List<String> listForeignIdsWithWysiwyg(String instanceId) {
Session session = null;
try {
session = repositoryManager.getSession();
return new ArrayList<String>(
repository.listForeignIdsByType(session, instanceId, DocumentType.wysiwyg));
} catch (RepositoryException ex) {
throw new AttachmentException(ex);
} finally {
repositoryManager.logout(session);
}
}

/**
* Gets the list of attachments of WYSIWYG type for the given instance identifier and
* foreign identifier. For each document represented by a master JCR Node, the number of
* SimpleDocument returned depends on the number of languages registered for the JCR Node.
* For a document, if it exists one version in "fr" and an other one in "en" (for example), two
* SimpleDocument are returned, one for the "fr" language and an other one for "en" language.
* @param instanceId the identifier of the component instance limitation.
* @param foreignId the identifier of object limitation.
* @return
* @throws RepositoryException
*/
public List<SimpleDocument> listWysiwygByForeignId(String instanceId, String foreignId) {
Session session = null;
try {
session = repositoryManager.getSession();
return repository.listAttachmentsByForeignIdAndDocumentType(session, instanceId, foreignId,
DocumentType.wysiwyg);
} catch (RepositoryException ex) {
throw new AttachmentException(ex);
} finally {
repositoryManager.logout(session);
}
}

public List<SimpleDocument> listWysiwygForBasename(String basename, String instanceId) {
Session session = null;
try {
Original file line number Diff line number Diff line change
@@ -23,15 +23,16 @@
*/
package org.silverpeas.migration.jcr.service.model;

import org.silverpeas.migration.jcr.service.ConverterUtil;

import java.io.File;
import java.io.Serializable;
import java.util.Date;
import org.silverpeas.migration.jcr.service.ConverterUtil;

/**
* @author ehugonnet
*/
public class SimpleAttachment implements Serializable {
public class SimpleAttachment implements Serializable, Cloneable {

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

public SimpleAttachment(final String filename, final String language, final String title,
final String description, final long size, final String contentType, final String createdBy,
final Date created, final String updatedBy, final Date updated, final String xmlFormId,
final File file) {
this.filename = filename;
this.language = language;
this.title = title;
this.description = description;
this.size = size;
this.contentType = contentType;
this.createdBy = createdBy;
this.created = created;
this.updatedBy = updatedBy;
this.updated = updated;
this.xmlFormId = xmlFormId;
this.file = file;
}

public SimpleAttachment() {
}

@@ -253,4 +272,14 @@ public String toString() {
+ ", createdBy=" + createdBy + ", created=" + created + ", updatedBy=" + updatedBy
+ ", updated=" + updated + ", xmlFormId=" + xmlFormId + '}';
}

@SuppressWarnings("CloneDoesntDeclareCloneNotSupportedException")
@Override
public SimpleAttachment clone() {
try {
return (SimpleAttachment) super.clone();
} catch (CloneNotSupportedException e) {
return null;
}
}
}
Original file line number Diff line number Diff line change
@@ -36,7 +36,7 @@
*
* @author ehugonnet
*/
public class SimpleDocument implements Serializable {
public class SimpleDocument implements Serializable, Cloneable {

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

public SimpleDocument(final SimpleDocumentPK pk, final String foreignId, final int order,
final boolean versioned, final String editedBy, final Date reservation, final Date alert,
final Date expiry, final String status, final String cloneId, final int minorVersion,
final int majorVersion, final boolean publicDocument, final String nodeName,
final String comment, final DocumentType documentType, final String oldContext,
final SimpleAttachment file) {
this.pk = pk;
this.foreignId = foreignId;
this.order = order;
this.versioned = versioned;
this.editedBy = editedBy;
this.reservation = reservation;
this.alert = alert;
this.expiry = expiry;
this.status = status;
this.cloneId = cloneId;
this.minorVersion = minorVersion;
this.majorVersion = majorVersion;
this.publicDocument = publicDocument;
this.nodeName = nodeName;
this.comment = comment;
this.documentType = documentType;
this.oldContext = oldContext;
this.file = file;
}

public SimpleDocument() {
}

@@ -513,4 +539,17 @@ public SimpleDocument getLastPublicVersion() {
public String getFolder() {
return documentType.getFolderName();
}

@SuppressWarnings("CloneDoesntDeclareCloneNotSupportedException")
@Override
public SimpleDocument clone() {
try {
SimpleDocument clone = (SimpleDocument) super.clone();
clone.setPK(clone.getPk().clone());
clone.setAttachment(clone.getAttachment().clone());
return clone;
} catch (CloneNotSupportedException e) {
return null;
}
}
}
Original file line number Diff line number Diff line change
@@ -98,4 +98,9 @@ public String toString() {
append(getComponentName()).append(", oldSilverpeasId=").append(oldSilverpeasId).append('}');
return buffer.toString();
}

@Override
public SimpleDocumentPK clone() {
return (SimpleDocumentPK) super.clone();
}
}
Original file line number Diff line number Diff line change
@@ -36,7 +36,7 @@
* @author Nicolas Eysseric
* @version 1.0
*/
public abstract class WAPrimaryKey implements Serializable {
public abstract class WAPrimaryKey implements Serializable, Cloneable {

private static final long serialVersionUID = -2456912022917180222L;

@@ -228,4 +228,13 @@ public String getSpaceId() {
public String getInstanceId() {
return getComponentName();
}

@Override
public WAPrimaryKey clone() {
try {
return (WAPrimaryKey) super.clone();
} catch (CloneNotSupportedException e) {
return null;
}
}
}
Original file line number Diff line number Diff line change
@@ -182,8 +182,13 @@ protected SimpleAttachment getAttachment(Node node, String language) throws Repo
}

public void fillNode(SimpleDocument document, Node documentNode) throws RepositoryException {
fillNode(document, documentNode, false);
}

public void fillNode(SimpleDocument document, Node documentNode, boolean skipAttachmentContent)
throws RepositoryException {
setDocumentNodeProperties(document, documentNode);
if (document.getAttachment() != null) {
if (!skipAttachmentContent && document.getAttachment() != null) {
Node attachmentNode = getAttachmentNode(document.getAttachment().getNodeName(), documentNode);
attachmentConverter.fillNode(document.getAttachment(), attachmentNode);
}
Loading
Oops, something went wrong.