Skip to content

Commit

Permalink
Merge pull request #208 from ONLYOFFICE/feature/forcesave-history
Browse files Browse the repository at this point in the history
Feature/forcesave history
  • Loading branch information
LinneyS authored Apr 18, 2024
2 parents 05f6e73 + f6d47de commit 8cc5110
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 34 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@
- docs cloud banner on settings page
- filling pdf
- wait dialog for convert action
- comment "ONLYOFFICE (forcesave)" for forcesave versions

## Changed
- Alfresco v6.* and earlier is no longer supported
- default conversion format (from docxf to pdf instead oform)
- remove filling for oform
- forcesave versions don't show in editor history

## 6.1.0
## Added
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import org.alfresco.service.cmr.repository.ContentService;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.repository.NodeService;
import org.alfresco.service.cmr.version.Version;
import org.alfresco.service.cmr.version.VersionService;
import org.alfresco.service.cmr.version.VersionType;
import org.apache.http.HttpEntity;
import org.slf4j.Logger;
Expand Down Expand Up @@ -54,6 +56,8 @@ public class CallbackServiceImpl extends DefaultCallbackService {
ConvertService convertService;
@Autowired
DocumentManager documentManager;
@Autowired
VersionService versionService;

private Logger logger = LoggerFactory.getLogger(this.getClass());

Expand All @@ -71,6 +75,7 @@ public void handlerSave(final Callback callback, final String fileId) throws Exc
NodeRef nodeRef = new NodeRef(fileId);
NodeRef wc = cociService.getWorkingCopy(nodeRef);
Map<String, Serializable> versionProperties = new HashMap<String, Serializable>();
Version oldVersion = versionService.getCurrentVersion(nodeRef);

logger.debug("Document Updated, changing content");
updateNode(wc, callback.getUrl(), callback.getFiletype());
Expand Down Expand Up @@ -100,6 +105,16 @@ public void handlerSave(final Callback callback, final String fileId) throws Exc
}
}

// Delete history(changes.json and diff.zip) for previous forcesave version if exists.
if (oldVersion.getVersionProperty(Util.ForcesaveAspect.getLocalName()) != null
&& (Boolean) oldVersion.getVersionProperty(Util.ForcesaveAspect.getLocalName())) {
try {
historyManager.deleteHistory(nodeRef, oldVersion);
} catch (Exception e) {
logger.error(e.getMessage(), e);
}
}

util.postActivity(nodeRef, false);

logger.debug("Save complete");
Expand Down Expand Up @@ -133,6 +148,7 @@ public void handlerForcesave(Callback callback, String fileId) throws Exception
NodeRef nodeRef = new NodeRef(fileId);
NodeRef wc = cociService.getWorkingCopy(nodeRef);
Map<String, Serializable> versionProperties = new HashMap<String, Serializable>();
Version oldVersion = versionService.getCurrentVersion(nodeRef);

logger.debug("Forcesave request (type: " + callback.getForcesavetype() + ")");
updateNode(wc, callback.getUrl(), callback.getFiletype());
Expand All @@ -144,6 +160,7 @@ public void handlerForcesave(Callback callback, String fileId) throws Exception
nodeService.removeProperty(wc, Util.EditingKeyAspect);

versionProperties.put(VersionModel.PROP_VERSION_TYPE, VersionType.MINOR);
versionProperties.put(VersionModel.PROP_DESCRIPTION, "ONLYOFFICE (forcesave)");
versionProperties.put(Util.ForcesaveAspect.getLocalName(), true);
cociService.checkin(wc, versionProperties, null, true);

Expand All @@ -163,6 +180,16 @@ public void handlerForcesave(Callback callback, String fileId) throws Exception
}
}

// Delete history(changes.json and diff.zip) for previous forcesave version if exists.
if (oldVersion.getVersionProperty(Util.ForcesaveAspect.getLocalName()) != null
&& (Boolean) oldVersion.getVersionProperty(Util.ForcesaveAspect.getLocalName())) {
try {
historyManager.deleteHistory(nodeRef, oldVersion);
} catch (Exception e) {
logger.error(e.getMessage(), e);
}
}

util.postActivity(nodeRef, false);

logger.debug("Forcesave complete");
Expand Down
104 changes: 70 additions & 34 deletions repo/src/main/java/com/parashift/onlyoffice/util/HistoryManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,24 @@ public void saveHistory(NodeRef nodeRef, History history, String changesUrl) thr
logger.debug("History saved successfully.");
}

public void deleteHistory(NodeRef nodeRef, Version version) {
NodeRef changesNodeRef = util.getChildNodeByName(nodeRef, "changes.json");
if (changesNodeRef != null) {
Version changesVersion = getHistoryNodeVersionForVersion(version, "changes.json");
if (changesVersion != null) {
versionService.deleteVersion(changesNodeRef, changesVersion);
}
}

NodeRef diffZipNodeRef = util.getChildNodeByName(nodeRef, "diff.zip");
if (diffZipNodeRef != null) {
Version diffZipVersion = getHistoryNodeVersionForVersion(version, "diff.zip");
if (diffZipVersion != null) {
versionService.deleteVersion(diffZipNodeRef, diffZipVersion);
}
}
}

private void saveHistoryData(final NodeRef nodeRef, final String data, final String name, final boolean fromString) {
if (data == null || data.isEmpty()) {
logger.error("Error saving history " + name + "History data is null!");
Expand Down Expand Up @@ -164,6 +182,16 @@ private NodeRef createHistoryNode(NodeRef parentNodeRef, String name) {
}

private NodeRef getHistoryNodeForVersion(Version version, String name) {
Version historyNodeVersion = getHistoryNodeVersionForVersion(version, name);

if (historyNodeVersion != null) {
return historyNodeVersion.getFrozenStateNodeRef();
}

return null;
}

private Version getHistoryNodeVersionForVersion(Version version, String name) {
NodeRef historyNodeRef = util.getChildNodeByName(version.getVersionedNodeRef(), name);

if (historyNodeRef != null) {
Expand All @@ -172,7 +200,7 @@ private NodeRef getHistoryNodeForVersion(Version version, String name) {
String contentVersionUUID = (String) nodeService.getProperty(versionHistory.getFrozenStateNodeRef(), ContentVersionUUID);

if (contentVersionUUID.equals(version.getFrozenStateNodeRef().getId())) {
return versionHistory.getFrozenStateNodeRef();
return versionHistory;
}
}
}
Expand Down Expand Up @@ -212,49 +240,58 @@ public Map<String, Object> getHistoryInfo(NodeRef nodeRef) throws IOException {
ObjectMapper objectMapper = new ObjectMapper();
List<Version> versions = (List<Version>) versionService.getVersionHistory(nodeRef).getAllVersions();
List<com.onlyoffice.model.documenteditor.history.Version> history = new ArrayList<>();
Version latestVersion = versions.get(0);

Collections.reverse(versions);

for (Version internalVersion : versions) {
Date created = (Date) internalVersion.getVersionProperty(Version2Model.PROP_FROZEN_MODIFIED);

com.onlyoffice.model.documenteditor.history.Version version = com.onlyoffice.model.documenteditor.history.Version.builder()
.version(internalVersion.getVersionLabel())
.key(
documentManager.getDocumentKey(
internalVersion.getFrozenStateNodeRef().toString(),
false
)
)
.created(ISO8601DateFormat.format(created))
.build();

NodeRef person = personService.getPersonOrNull(internalVersion.getVersionProperty("modifier").toString());

PersonService.PersonInfo personInfo = null;
if (person != null) {
personInfo = personService.getPerson(person);
if (personInfo != null) {
User user = User.builder()
.id(personInfo.getUserName())
.name(personInfo.getFirstName() + " " + personInfo.getLastName())
.build();

version.setUser(user);
if ((internalVersion.getVersionProperty(Util.ForcesaveAspect.getLocalName()) == null
|| !(Boolean) internalVersion.getVersionProperty(Util.ForcesaveAspect.getLocalName()))
|| internalVersion.equals(latestVersion)) {


Date created = (Date) internalVersion.getVersionProperty(Version2Model.PROP_FROZEN_MODIFIED);

com.onlyoffice.model.documenteditor.history.Version version =
com.onlyoffice.model.documenteditor.history.Version.builder()
.version(internalVersion.getVersionLabel())
.key(
documentManager.getDocumentKey(
internalVersion.getFrozenStateNodeRef().toString(),
false
)
)
.created(ISO8601DateFormat.format(created))
.build();

NodeRef person =
personService.getPersonOrNull(internalVersion.getVersionProperty("modifier").toString());

PersonService.PersonInfo personInfo = null;
if (person != null) {
personInfo = personService.getPerson(person);
if (personInfo != null) {
User user = User.builder()
.id(personInfo.getUserName())
.name(personInfo.getFirstName() + " " + personInfo.getLastName())
.build();

version.setUser(user);
}
}
}

NodeRef changesNodeRef = getHistoryNodeForVersion(internalVersion, "changes.json");
NodeRef changesNodeRef = getHistoryNodeForVersion(internalVersion, "changes.json");

if (changesNodeRef != null) {
ContentReader reader = contentService.getReader(changesNodeRef, ContentModel.PROP_CONTENT);
History changes = objectMapper.readValue(reader.getContentInputStream(), History.class);
if (changesNodeRef != null) {
ContentReader reader = contentService.getReader(changesNodeRef, ContentModel.PROP_CONTENT);
History changes = objectMapper.readValue(reader.getContentInputStream(), History.class);

version.setChanges(changes.getChanges());
version.setServerVersion(changes.getServerVersion());
}
}

history.add(version);
history.add(version);
}
}

Map<String, Object> historyInfo = new HashMap<>();
Expand All @@ -274,7 +311,6 @@ public HistoryData getHistoryData(NodeRef nodeRef, String versionLabel) throws I
Collections.reverse(versions);

for (Version version : versions) {
VersionType versionType = (VersionType) version.getVersionProperty(VersionModel.PROP_VERSION_TYPE);
if (version.getVersionLabel().equals(versionLabel)) {
String versionFileName = documentManager.getDocumentName(version.getFrozenStateNodeRef().toString());

Expand Down

0 comments on commit 8cc5110

Please sign in to comment.