diff --git a/notes-service/src/main/java/org/exoplatform/wiki/service/ExportThread.java b/notes-service/src/main/java/org/exoplatform/wiki/service/ExportThread.java index 80dcf335a..91b527448 100644 --- a/notes-service/src/main/java/org/exoplatform/wiki/service/ExportThread.java +++ b/notes-service/src/main/java/org/exoplatform/wiki/service/ExportThread.java @@ -20,7 +20,16 @@ package org.exoplatform.wiki.service; -import java.io.*; +import java.io.BufferedWriter; +import java.io.ByteArrayInputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.FileWriter; +import java.io.IOException; +import java.io.InputStream; +import java.io.InterruptedIOException; import java.nio.file.Files; import java.nio.file.StandardCopyOption; import java.text.SimpleDateFormat; @@ -34,14 +43,15 @@ import java.util.List; import java.util.Map; import java.util.Optional; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import java.util.stream.Collectors; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; -import io.meeds.notes.model.NoteFeaturedImage; -import io.meeds.notes.model.NotePageProperties; import org.apache.commons.io.FileUtils; import org.apache.commons.lang3.StringUtils; +import org.springframework.util.MimeTypeUtils; import com.fasterxml.jackson.databind.ObjectMapper; @@ -59,9 +69,10 @@ import org.exoplatform.wiki.model.ExportList; import org.exoplatform.wiki.model.NoteToExport; import org.exoplatform.wiki.model.Page; -import org.exoplatform.wiki.model.Wiki; import org.exoplatform.wiki.model.WikiType; -import org.springframework.util.MimeTypeUtils; + +import io.meeds.notes.model.NoteFeaturedImage; +import io.meeds.notes.model.NotePageProperties; public class ExportThread implements Runnable { @@ -444,12 +455,22 @@ public NoteToExport getNoteToExport(NoteToExport note, int exportId) throws Wiki public String processNotesLinkForExport(NoteToExport note) throws WikiException { String content = note.getContent(); - String noteLinkprefix = "class=\"noteLink\" href=\""; +// String noteLinkprefix = "class=\"noteLink\" href=\""; +// String noteLinkprefix = "class=\"noteLink\" href=\"(\\d+)|.*?/([^/\"]+)"; + String noteLinkprefix = "class=\"noteLink\" href=\"(?:.*?/|)(\\d+)"; String contentUpdated = content; Map urlToReplaces = new HashMap<>(); - while (contentUpdated.contains("noteLink")) { - String checkContent = contentUpdated; - String noteId = contentUpdated.split(noteLinkprefix)[1].split("\"")[0]; + Pattern pattern = Pattern.compile(noteLinkprefix); + Matcher matcher = pattern.matcher(contentUpdated); + while (matcher.find()) { + String matchedLink = matcher.group(0); + String noteId; + if (matcher.group(1) != null) { + noteId = matcher.group(1); + } else { + noteId = matcher.group(2); + } + Page linkedNote = null; try { linkedNote = noteService.getNoteById(noteId); @@ -461,11 +482,7 @@ public String processNotesLinkForExport(NoteToExport note) throws WikiException String noteParams = IMAGE_URL_REPLACEMENT_PREFIX + linkedNote.getWikiType() + IMAGE_URL_REPLACEMENT_SUFFIX + IMAGE_URL_REPLACEMENT_PREFIX + linkedNote.getWikiOwner() + IMAGE_URL_REPLACEMENT_SUFFIX + IMAGE_URL_REPLACEMENT_PREFIX + linkedNote.getName() + IMAGE_URL_REPLACEMENT_SUFFIX; - urlToReplaces.put(noteLinkprefix + linkedNote.getId() + "\"", noteLinkprefix + noteParams + "\""); - } - contentUpdated = contentUpdated.replace(noteLinkprefix + noteId + "\"", ""); - if (contentUpdated.equals(checkContent)) { - break; + urlToReplaces.put(matchedLink + "\"", "class=\"noteLink\" href=\"" + noteParams + "\""); } } if (!urlToReplaces.isEmpty()) { @@ -525,38 +542,6 @@ public String processImagesForExport(Page note) throws WikiException, IOExceptio return htmlUploadImageProcessor.processImagesForExport(content); } - private void replaceIncludedPages(Page note, Wiki wiki) throws WikiException { - Page note_ = noteService.getNoteOfNoteBookByName(wiki.getType(), wiki.getOwner(), note.getName()); - if (note_ != null) { - String content = note_.getContent(); - if (content.contains("class=\"noteLink\" href=\"//-")) { - while (content.contains("class=\"noteLink\" href=\"//-")) { - String linkedParams = content.split("class=\"noteLink\" href=\"//-")[1].split("-//\"")[0]; - String NoteName = linkedParams.split("-////-")[2]; - Page linkedNote = null; - linkedNote = noteService.getNoteOfNoteBookByName(wiki.getType(), wiki.getOwner(), NoteName); - if (linkedNote != null) { - content = content.replace("\"noteLink\" href=\"//-" + linkedParams + "-//", - "\"noteLink\" href=\"" + linkedNote.getId()); - } else { - content = content.replace("\"noteLink\" href=\"//-" + linkedParams + "-//", "\"noteLink\" href=\"" + NoteName); - } - if (content.equals(note_.getContent())) - break; - } - if (!content.equals(note_.getContent())) { - note_.setContent(content); - noteService.updateNote(note_); - } - } - } - if (note.getChildren() != null) { - for (Page child : note.getChildren()) { - replaceIncludedPages(child, wiki); - } - } - } - private String replaceUrl(String body, Map urlToReplaces) { for (String url : urlToReplaces.keySet()) { while (body.contains(url)) { diff --git a/notes-service/src/main/java/org/exoplatform/wiki/service/NoteService.java b/notes-service/src/main/java/org/exoplatform/wiki/service/NoteService.java index ab915df29..65f504fbb 100644 --- a/notes-service/src/main/java/org/exoplatform/wiki/service/NoteService.java +++ b/notes-service/src/main/java/org/exoplatform/wiki/service/NoteService.java @@ -22,10 +22,7 @@ import java.io.IOException; import java.util.List; -import java.util.Map; -import io.meeds.notes.model.NoteFeaturedImage; -import io.meeds.notes.model.NotePageProperties; import org.gatein.api.EntityNotFoundException; import org.exoplatform.commons.utils.PageList; @@ -41,6 +38,9 @@ import org.exoplatform.wiki.service.search.SearchResult; import org.exoplatform.wiki.service.search.WikiSearchData; +import io.meeds.notes.model.NoteFeaturedImage; +import io.meeds.notes.model.NotePageProperties; + /** * Provides functions for processing database with notes, including: adding, * editing, removing and searching for data. @@ -72,6 +72,22 @@ public interface NoteService { */ Page createNote(Wiki noteBook, String parentNoteName, Page note, Identity userIdentity) throws WikiException, IllegalAccessException; + + /** + * Create a new note in the given notebook, under the given parent note. + * + * @param noteBook Notebook object. + * @param parentNoteName parent note name. + * @param note the note object to create. + * @param userIdentity user Identity. + * @param importMode true if the creation is without timestamp for import mode. + * @return The new note. + * @throws WikiException if an error occured + * @throws IllegalAccessException if the user don't have edit rights to the + * parent note + */ + Page createNote(Wiki noteBook, String parentNoteName, Page note, Identity userIdentity, boolean importMode) throws WikiException, + IllegalAccessException; /** * Deletes a note. diff --git a/notes-service/src/main/java/org/exoplatform/wiki/service/impl/NoteServiceImpl.java b/notes-service/src/main/java/org/exoplatform/wiki/service/impl/NoteServiceImpl.java index dd6b7f397..be28a29eb 100644 --- a/notes-service/src/main/java/org/exoplatform/wiki/service/impl/NoteServiceImpl.java +++ b/notes-service/src/main/java/org/exoplatform/wiki/service/impl/NoteServiceImpl.java @@ -20,7 +20,10 @@ package org.exoplatform.wiki.service.impl; -import java.io.*; +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; import java.net.MalformedURLException; import java.net.URL; import java.nio.file.Files; @@ -38,7 +41,6 @@ import java.util.concurrent.ConcurrentHashMap; import java.util.regex.Matcher; -import lombok.Getter; import org.apache.commons.collections4.MapUtils; import org.apache.commons.lang3.StringUtils; import org.gatein.api.EntityNotFoundException; @@ -59,6 +61,7 @@ import org.exoplatform.services.log.Log; import org.exoplatform.services.security.Identity; import org.exoplatform.services.security.IdentityConstants; +import org.exoplatform.services.thumbnail.ImageThumbnailService; import org.exoplatform.social.common.service.HTMLUploadImageProcessor; import org.exoplatform.social.core.identity.provider.OrganizationIdentityProvider; import org.exoplatform.social.core.manager.IdentityManager; @@ -69,7 +72,6 @@ import org.exoplatform.social.metadata.model.MetadataKey; import org.exoplatform.social.metadata.model.MetadataObject; import org.exoplatform.social.metadata.model.MetadataType; -import org.exoplatform.services.thumbnail.ImageThumbnailService; import org.exoplatform.upload.UploadResource; import org.exoplatform.upload.UploadService; import org.exoplatform.wiki.WikiException; @@ -103,6 +105,7 @@ import io.meeds.notes.model.NotePageProperties; import io.meeds.notes.service.NotePageViewService; import io.meeds.social.cms.service.CMSService; +import lombok.Getter; import lombok.SneakyThrows; public class NoteServiceImpl implements NoteService { @@ -226,22 +229,22 @@ public NoteServiceImpl(DataStorage dataStorage, this.imageThumbnailService = imageThumbnailService; this.htmlUploadImageProcessor = htmlUploadImageProcessor; } - + /** * {@inheritDoc} */ @Override - public Page createNote(Wiki noteBook, String parentNoteName, Page note, Identity userIdentity) throws WikiException, + public Page createNote(Wiki noteBook, String parentNoteName, Page note, Identity userIdentity, boolean importMode) throws WikiException, IllegalAccessException { - - String pageName = TitleResolver.getId(note.getName(), false); - if (pageName == null) { - pageName = TitleResolver.getId(note.getTitle(), false); + if (importMode) { + String pageName = TitleResolver.getId(note.getName(), false); + if (pageName == null) { + pageName = TitleResolver.getId(note.getTitle(), false); + } + note.setName(pageName); } - note.setName(pageName); - - if (isExisting(noteBook.getType(), noteBook.getOwner(), pageName)) { - throw new WikiException("Page " + noteBook.getType() + ":" + noteBook.getOwner() + ":" + pageName + if (isExisting(noteBook.getType(), noteBook.getOwner(), note.getName())) { + throw new WikiException("Page " + noteBook.getType() + ":" + noteBook.getOwner() + ":" + note.getName() + " already exists, cannot create it."); } @@ -276,6 +279,15 @@ public Page createNote(Wiki noteBook, String parentNoteName, Page note, Identity } } + /** + * {@inheritDoc} + */ + @Override + public Page createNote(Wiki noteBook, String parentNoteName, Page note, Identity userIdentity) throws WikiException, + IllegalAccessException { + return createNote(noteBook, parentNoteName, note, userIdentity, true); + } + /** * {@inheritDoc} */ @@ -415,40 +427,34 @@ public boolean deleteNote(String noteType, String noteOwner, String noteName, Id throw new EntityNotFoundException("Note to delete not found"); } Space space = spaceService.getSpaceByGroupId(note.getWikiOwner()); - if (note != null) { - if (!Utils.canManageNotes(userIdentity.getUserId(), space, note)) { - log.error("Can't delete note '" + noteName + "'. does not have edit permission on it."); - throw new IllegalAccessException("User does not have edit permissions on the note."); - } - - invalidateCachesOfPageTree(note); - invalidateAttachmentCache(note); - - // Store all children to launch post deletion listeners - List allChrildrenPages = new ArrayList<>(); - Queue queue = new LinkedList<>(); - queue.add(note); - Page tempPage; - while (!queue.isEmpty()) { - tempPage = queue.poll(); - List childrenPages = getChildrenNoteOf(tempPage, false, false); - for (Page childPage : childrenPages) { - queue.add(childPage); - allChrildrenPages.add(childPage); - } - } + if (!Utils.canManageNotes(userIdentity.getUserId(), space, note)) { + log.error("Can't delete note '" + noteName + "'. does not have edit permission on it."); + throw new IllegalAccessException("User does not have edit permissions on the note."); + } - deleteNote(noteType, noteOwner, noteName); - postDeletePage(noteType, noteOwner, noteName, note); + invalidateCachesOfPageTree(note); + invalidateAttachmentCache(note); - // Post delete activity for all children pages - for (Page childNote : allChrildrenPages) { - postDeletePage(childNote.getWikiType(), childNote.getWikiOwner(), childNote.getName(), childNote); + // Store all children to launch post deletion listeners + List allChrildrenPages = new ArrayList<>(); + Queue queue = new LinkedList<>(); + queue.add(note); + Page tempPage; + while (!queue.isEmpty()) { + tempPage = queue.poll(); + List childrenPages = getChildrenNoteOf(tempPage, false, false); + for (Page childPage : childrenPages) { + queue.add(childPage); + allChrildrenPages.add(childPage); } + } - } else { - log.error("Can't delete note '" + noteName + "'. This note does not exist."); - return false; + deleteNote(noteType, noteOwner, noteName); + postDeletePage(noteType, noteOwner, noteName, note); + + // Post delete activity for all children pages + for (Page childNote : allChrildrenPages) { + postDeletePage(childNote.getWikiType(), childNote.getWikiOwner(), childNote.getName(), childNote); } } catch (WikiException e) { log.error("Can't delete note '" + noteName + "' ", e); @@ -1340,7 +1346,7 @@ public void importNotes(List files, Page parent, String conflict, Identi userIdentity); } for (Page note : notes.getNotes()) { - replaceIncludedPages(note, wiki); + replaceIncludedPages(note, wiki, userIdentity); } cleanUp(notesFile); } @@ -1951,7 +1957,7 @@ private void importNote(Page note, wiki.getOwner(), imagesSubLocationPath); note.setContent(processedContent); - note_ = createNote(wiki, parent_.getName(), note, userIdentity); + note_ = createNote(wiki, parent_.getName(), note, userIdentity, false); } else { if (StringUtils.isNotEmpty(conflict)) { if (conflict.equals("overwrite") || conflict.equals("replaceAll")) { @@ -1960,25 +1966,10 @@ private void importNote(Page note, wiki.getOwner(), imagesSubLocationPath); note.setContent(processedContent); - note_ = createNote(wiki, parent_.getName(), note, userIdentity); + note_ = createNote(wiki, parent_.getName(), note, userIdentity, false); } if (conflict.equals("duplicate")) { - String title = note.getTitle(); - int i; - try { - i = title.lastIndexOf("_") != -1 ? Integer.valueOf(title.substring(title.lastIndexOf("_") + 1)) + 1 : 1; - } catch (NumberFormatException e) { - i = 1; - } - String newTitle = note.getTitle() + "_" + i; - while (getNoteOfNoteBookByName(wiki.getType(), wiki.getOwner(), newTitle) != null - || isExisting(wiki.getType(), wiki.getOwner(), TitleResolver.getId(newTitle, false))) { - i++; - newTitle = note.getTitle() + "_" + i; - } - note.setName(newTitle); - note.setTitle(newTitle); String processedContent = htmlUploadImageProcessor.processSpaceImages(note.getContent(), wiki.getOwner(), imagesSubLocationPath); @@ -2134,41 +2125,17 @@ private String getNoteTitleWithTraduction(Page note, Identity userIdentity, Stri return note.getTitle(); } - private LinkedList getNoteAncestorsIds(String noteId) throws WikiException { - return getNoteAncestorsIds(null, noteId); - } - - private LinkedList getNoteAncestorsIds(LinkedList ancestorsIds, String noteId) throws WikiException { - if (ancestorsIds == null) { - ancestorsIds = new LinkedList<>(); - } - if (noteId == null) { - return ancestorsIds; - } - Page note = getNoteById(noteId); - String parentId = note.getParentPageId(); - - if (parentId != null) { - ancestorsIds.push(parentId); - getNoteAncestorsIds(ancestorsIds, parentId); - } - - return ancestorsIds; - } - private String getDraftNameSuffix(long clientTime) { return new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date(clientTime)); } - private void replaceIncludedPages(Page note, Wiki wiki) throws WikiException { + private void replaceIncludedPages(Page note, Wiki wiki, Identity userIdentity) throws WikiException { Page note_ = getNoteOfNoteBookByName(wiki.getType(), wiki.getOwner(), note.getName()); if (note_ != null) { String content = note_.getContent(); if (content.contains("class=\"noteLink\" href=\"//-")) { while (content.contains("class=\"noteLink\" href=\"//-")) { String linkedParams = content.split("class=\"noteLink\" href=\"//-")[1].split("-//\"")[0]; - String noteBookType = linkedParams.split("-////-")[0]; - String noteBookOwner = linkedParams.split("-////-")[1]; String NoteName = linkedParams.split("-////-")[2]; Page linkedNote = null; linkedNote = getNoteOfNoteBookByName(wiki.getType(), wiki.getOwner(), NoteName); @@ -2184,23 +2151,15 @@ private void replaceIncludedPages(Page note, Wiki wiki) throws WikiException { if (!content.equals(note_.getContent())) { note_.setContent(content); updateNote(note_); + createVersionOfNote(note_, userIdentity.getUserId()); } } } if (note.getChildren() != null) { for (Page child : note.getChildren()) { - replaceIncludedPages(child, wiki); - } - } - } - - private String replaceUrl(String body, Map urlToReplaces) { - for (String url : urlToReplaces.keySet()) { - while (body.contains(url)) { - body = body.replace(url, urlToReplaces.get(url)); + replaceIncludedPages(child, wiki, userIdentity); } } - return body; } private void addAllNodes(Page note, List listOfNotes) throws WikiException {