Skip to content

Commit

Permalink
feat: Add multilangue backend services - EXO-65935 - Meeds-io/MIPs#70 (
Browse files Browse the repository at this point in the history
…#711)

Add multilangue backend services
  • Loading branch information
hakermi authored and exo-swf committed Oct 5, 2023
1 parent 4c0aadf commit a17ff76
Show file tree
Hide file tree
Showing 15 changed files with 513 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,12 @@
import org.exoplatform.commons.file.model.FileInfo;
import org.exoplatform.commons.file.model.FileItem;
import org.exoplatform.commons.file.services.FileService;
import org.exoplatform.commons.utils.CommonsUtils;
import org.exoplatform.services.log.ExoLogger;
import org.exoplatform.services.log.Log;
import org.exoplatform.social.core.identity.model.Identity;
import org.exoplatform.social.core.identity.provider.OrganizationIdentityProvider;
import org.exoplatform.social.core.manager.IdentityManager;
import org.exoplatform.wiki.WikiException;
import org.exoplatform.wiki.jpa.dao.PageDAO;
import org.exoplatform.wiki.jpa.dao.WikiDAO;
Expand Down Expand Up @@ -374,6 +378,7 @@ public static DraftPage convertDraftPageEntityToDraftPage(DraftPageEntity draftP
draftPage.setAuthor(draftPageEntity.getAuthor());
draftPage.setOwner(draftPageEntity.getAuthor());
draftPage.setContent(draftPageEntity.getContent());
draftPage.setLang(draftPageEntity.getLang());
draftPage.setSyntax(draftPageEntity.getSyntax());
draftPage.setCreatedDate(draftPageEntity.getCreatedDate());
draftPage.setUpdatedDate(draftPageEntity.getUpdatedDate());
Expand Down Expand Up @@ -415,6 +420,7 @@ public static DraftPageEntity convertDraftPageToDraftPageEntity(DraftPage draftP
draftPageEntity.setTitle(draftPage.getTitle());
draftPageEntity.setAuthor(draftPage.getAuthor());
draftPageEntity.setContent(draftPage.getContent());
draftPageEntity.setLang(draftPage.getLang());
draftPageEntity.setSyntax(draftPage.getSyntax());
draftPageEntity.setCreatedDate(draftPage.getCreatedDate());
draftPageEntity.setUpdatedDate(draftPage.getUpdatedDate());
Expand All @@ -441,6 +447,7 @@ public static PageVersion convertPageVersionEntityToPageVersion(PageVersionEntit
pageVersion.setContent(pageVersionEntity.getContent());
pageVersion.setCreatedDate(pageVersionEntity.getCreatedDate());
pageVersion.setUpdatedDate(pageVersionEntity.getUpdatedDate());
pageVersion.setLang(pageVersionEntity.getLang());
}
return pageVersion;
}
Expand All @@ -455,6 +462,7 @@ public static PageHistory convertPageVersionEntityToPageHistory(PageVersionEntit
pageHistory.setContent(pageVersionEntity.getContent());
pageHistory.setCreatedDate(pageVersionEntity.getCreatedDate());
pageHistory.setUpdatedDate(pageVersionEntity.getUpdatedDate());
pageHistory.setLang(pageVersionEntity.getLang());
}
return pageHistory;
}
Expand All @@ -470,4 +478,20 @@ public static PermissionEntry convertPermissionEntityToPermissionEntry(Permissio
return permissionEntry;
}

public static List<PageHistory> toPageHistoryVersions(List<PageVersionEntity> pageVersionEntities) {
IdentityManager identityManager = CommonsUtils.getService(IdentityManager.class);

return pageVersionEntities.stream().map(EntityConverter::convertPageVersionEntityToPageHistory)
.peek(pageHistory -> {
Identity identity = identityManager.getOrCreateIdentity(OrganizationIdentityProvider.NAME,
pageHistory.getAuthor());
if (identity != null && identity.getProfile() != null
&& identity.getProfile().getFullName() != null) {
pageHistory.setAuthorFullName(identity.getProfile().getFullName());
}}).toList();
}

public static List<DraftPage> toDraftPages(List<DraftPageEntity> draftPageEntities) {
return draftPageEntities.stream().map(EntityConverter::convertDraftPageEntityToDraftPage).toList();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
package org.exoplatform.wiki.jpa;

import org.apache.commons.lang.StringUtils;
import org.exoplatform.commons.api.persistence.DataInitializer;
import org.exoplatform.commons.api.persistence.ExoTransactional;
import org.exoplatform.commons.file.services.FileService;
import org.exoplatform.commons.utils.ObjectPageList;
Expand Down Expand Up @@ -1121,10 +1120,10 @@ public void addPageVersion(Page page , String userName) throws WikiException {
pageVersionEntity.setVersionNumber(versionNumber);
pageVersionEntity.setName(pageEntity.getName());
pageVersionEntity.setTitle(pageEntity.getTitle());
if(StringUtils.isNotEmpty(userName)){
if (StringUtils.isNotEmpty(userName)) {
pageVersionEntity.setAuthor(userName);
} else{
pageVersionEntity.setAuthor(pageEntity.getAuthor());
} else {
pageVersionEntity.setAuthor(pageEntity.getAuthor());
}
pageVersionEntity.setContent(pageEntity.getContent());
pageVersionEntity.setSyntax(pageEntity.getSyntax());
Expand All @@ -1133,6 +1132,7 @@ public void addPageVersion(Page page , String userName) throws WikiException {
Date now = Calendar.getInstance().getTime();
pageVersionEntity.setCreatedDate(now);
pageVersionEntity.setUpdatedDate(now);
pageVersionEntity.setLang(page.getLang());

// attachment must be saved here because of Hibernate bug HHH-6776
pageVersionDAO.create(pageVersionEntity);
Expand Down Expand Up @@ -1445,4 +1445,65 @@ private String getDownloadURL(String wikiType, String wikiOwner, String pageName

return sb.toString();
}


/**
* {@inheritDoc}
*/
@Override
public List<PageHistory> getPageHistoryVersionsByPageIdAndLang(Long pageId, String lang) {
if (pageId == null) {
throw new IllegalArgumentException("pageId argument is null");
}
return EntityConverter.toPageHistoryVersions(pageVersionDAO.findPageVersionsByPageIdAndLang(pageId, lang));
}

/**
* {@inheritDoc}
*/
@Override
public DraftPage getLatestDraftPageByUserAndTargetPageAndLang(Long targetPageId, String username, String lang) {
if (username == null) {
throw new IllegalArgumentException("username argument is null");
}
if (targetPageId == null) {
throw new IllegalArgumentException("targetPageId argument is null");
}
return EntityConverter.convertDraftPageEntityToDraftPage(draftPageDAO.findLatestDraftPageByUserAndTargetPageAndLang(targetPageId,
username,
lang));
}

/**
* {@inheritDoc}
*/
@Override
public PageVersion getPublishedVersionByPageIdAndLang(Long pageId, String lang) {
if (pageId == null) {
throw new IllegalArgumentException("targetPageId argument is null");
}
return convertPageVersionEntityToPageVersion(pageVersionDAO.findLatestVersionByPageIdAndLang(pageId, lang));
}

/**
* {@inheritDoc}
*/
@Override
public List<String> getPageAvailableTranslationLanguages(Long pageId) {
if (pageId == null) {
throw new IllegalArgumentException("pageId argument is null");
}
return pageVersionDAO.findPageAvailableTranslationLanguages(pageId);
}

/**
* {@inheritDoc}
*/
@Override
public DraftPage getDraftPageByIdAndLang(Long draftNoteId, String lang) {
if (draftNoteId == null) {
throw new IllegalArgumentException("draftNoteId argument is null");
}
return EntityConverter.convertDraftPageEntityToDraftPage(draftPageDAO.findDraftNoteByIdAndLang(draftNoteId, lang));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,9 @@
package org.exoplatform.wiki.jpa.dao;

import org.exoplatform.commons.api.persistence.ExoTransactional;
import org.exoplatform.commons.persistence.impl.GenericDAOJPAImpl;
import org.exoplatform.wiki.jpa.entity.DraftPageEntity;

import javax.persistence.EntityTransaction;
import javax.persistence.NoResultException;
import javax.persistence.Query;
import javax.persistence.TypedQuery;
import java.util.List;

Expand Down Expand Up @@ -103,4 +100,33 @@ public DraftPageEntity findLatestDraftPageByUserAndTargetPage(Long targetPageId,
return null;
}
}

public DraftPageEntity findLatestDraftPageByUserAndTargetPageAndLang(Long targetPageId, String username, String lang) {
TypedQuery<DraftPageEntity> query =
getEntityManager().createNamedQuery("wikiDraftPage.findLatestDraftPageByUserAndTargetPageAndLang",
DraftPageEntity.class)
.setParameter("username", username)
.setParameter("targetPageId", targetPageId)
.setParameter("lang", lang);
query.setMaxResults(1);
try {
return query.getSingleResult();
} catch (NoResultException e) {
return null;
}
}

public DraftPageEntity findDraftNoteByIdAndLang(Long draftNoteId, String lang) {
TypedQuery<DraftPageEntity> query = getEntityManager()
.createNamedQuery("wikiDraftPage.findDraftByIdAndLang",
DraftPageEntity.class)
.setParameter("id", draftNoteId)
.setParameter("lang", lang);

try {
return query.getSingleResult();
} catch (NoResultException e) {
return null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import javax.persistence.NoResultException;
import javax.persistence.Query;
import javax.persistence.TypedQuery;
import java.util.Collections;
import java.util.List;

public class PageVersionDAO extends WikiBaseDAO<PageVersionEntity, Long> {
Expand Down Expand Up @@ -62,4 +63,42 @@ public Long countPagesVersionsBySyntax(String syntax) {
.setParameter("syntax", syntax)
.getSingleResult();
}

public List<PageVersionEntity> findPageVersionsByPageIdAndLang(Long pageId, String lang) {
TypedQuery<PageVersionEntity> query = getEntityManager()
.createNamedQuery("wikiPageVersion.getPageVersionsByPageIdAndLang",
PageVersionEntity.class)
.setParameter("pageId", pageId)
.setParameter("lang", lang);
try {
return query.getResultList();
} catch (NoResultException e) {
return Collections.emptyList();
}
}

public PageVersionEntity findLatestVersionByPageIdAndLang(Long pageId, String lang) {
TypedQuery<PageVersionEntity> query = getEntityManager()
.createNamedQuery("wikiPageVersion.getLatestPageVersionsByPageIdAndLang",
PageVersionEntity.class)
.setParameter("pageId", pageId)
.setParameter("lang", lang);
query.setMaxResults(1);
try {
return query.getSingleResult();
} catch (NoResultException e) {
return null;
}
}

public List<String> findPageAvailableTranslationLanguages(Long pageId) {
Query query = getEntityManager().createNamedQuery("wikiPageVersion.getPageAvailableTranslationLanguages")
.setParameter("pageId", pageId);
try {
return query.getResultList();
} catch (NoResultException e) {
return Collections.emptyList();
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,10 @@
@NamedQuery(name = "wikiDraftPage.findDraftPageByUserAndName", query = "SELECT d FROM WikiDraftPageEntity d WHERE d.author = :username AND d.name = :draftPageName ORDER BY d.updatedDate DESC"),
@NamedQuery(name = "wikiDraftPage.findLatestDraftPageByUserAndTargetPage", query = "SELECT d FROM WikiDraftPageEntity d WHERE d.author = :username AND d.targetPage.id = :targetPageId ORDER BY d.updatedDate DESC"),
@NamedQuery(name = "wikiDraftPage.findDraftPageByUserAndTargetPage", query = "SELECT d FROM WikiDraftPageEntity d WHERE d.author = :username AND d.targetPage.id = :targetPageId"),
@NamedQuery(name = "wikiDraftPage.findDraftPagesByUserAndParentPage", query = "SELECT d FROM WikiDraftPageEntity d WHERE d.author = :username AND d.parentPage.id = :parentPageId")
})
@NamedQuery(name = "wikiDraftPage.findDraftPagesByUserAndParentPage", query = "SELECT d FROM WikiDraftPageEntity d WHERE d.author = :username AND d.parentPage.id = :parentPageId"),
@NamedQuery(name = "wikiDraftPage.findLatestDraftPageByUserAndTargetPageAndLang", query = "SELECT d FROM WikiDraftPageEntity d WHERE d.author = :username AND d.targetPage.id = :targetPageId AND " +
"((:lang IS NULL AND d.lang IS NULL) OR (:lang IS NOT NULL AND d.lang = :lang)) ORDER BY d.updatedDate DESC"),
@NamedQuery(name = "wikiDraftPage.findDraftByIdAndLang", query = "SELECT d FROM WikiDraftPageEntity d WHERE d.id = :id AND ((:lang IS NULL AND d.lang IS NULL) OR (:lang IS NOT NULL AND d.lang = :lang))") })
public class DraftPageEntity extends BasePageEntity {

@Id
Expand All @@ -62,6 +64,9 @@ public class DraftPageEntity extends BasePageEntity {
@OneToMany(cascade = CascadeType.ALL, mappedBy = "draftPage")
private List<DraftPageAttachmentEntity> attachments;

@Column(name = "LANG")
private String lang;

public PageEntity getTargetPage() {
return targetPage;
}
Expand Down Expand Up @@ -109,4 +114,12 @@ public List<DraftPageAttachmentEntity> getAttachments() {
public void setAttachments(List<DraftPageAttachmentEntity> attachments) {
this.attachments = attachments;
}

public String getLang() {
return lang;
}

public void setLang(String lang) {
this.lang = lang;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,11 @@
@NamedQuery(name = "wikiPageVersion.getLastversionNumberOfPage", query = "SELECT max(p.versionNumber) FROM WikiPageVersionEntity p WHERE p.page.id = :pageId"),
@NamedQuery(name = "wikiPageVersion.getPageversionByPageIdAndVersion", query = "SELECT p FROM WikiPageVersionEntity p WHERE p.page.id = :pageId AND p.versionNumber = :versionNumber"),
@NamedQuery(name = "wikiPageVersion.getAllPagesVersionsBySyntax", query = "SELECT p FROM WikiPageVersionEntity p WHERE p.syntax = :syntax OR p.syntax IS NULL ORDER BY p.updatedDate DESC"),
@NamedQuery(name = "wikiPageVersion.countAllPagesVersionsBySyntax", query = "SELECT COUNT(p) FROM WikiPageVersionEntity p WHERE p.syntax = :syntax OR p.syntax IS NULL")
@NamedQuery(name = "wikiPageVersion.countAllPagesVersionsBySyntax", query = "SELECT COUNT(p) FROM WikiPageVersionEntity p WHERE p.syntax = :syntax OR p.syntax IS NULL"),
@NamedQuery(name = "wikiPageVersion.getPageVersionsByPageIdAndLang", query = "SELECT p FROM WikiPageVersionEntity p WHERE p.page.id = :pageId AND ((:lang IS NULL AND p.lang IS NULL) OR (:lang IS NOT NULL AND p.lang = :lang))"),
@NamedQuery(name = "wikiPageVersion.getLatestPageVersionsByPageIdAndLang", query = "SELECT p FROM WikiPageVersionEntity p WHERE p.page.id = :pageId AND ((:lang IS NULL AND p.lang IS NULL) OR (:lang IS NOT NULL AND p.lang = :lang)) ORDER BY p.versionNumber DESC"),
@NamedQuery(name = "wikiPageVersion.getPageAvailableTranslationLanguages", query = "SELECT DISTINCT p.lang FROM WikiPageVersionEntity p WHERE p.page.id = :pageId AND p.lang IS NOT NULL")

})
public class PageVersionEntity extends BasePageEntity {
@Id
Expand All @@ -58,6 +62,9 @@ public class PageVersionEntity extends BasePageEntity {
@Column(name = "MINOR_EDIT")
private boolean minorEdit;

@Column(name = "LANG")
private String lang;

public long getId() {
return id;
}
Expand Down Expand Up @@ -93,4 +100,12 @@ public boolean isMinorEdit() {
public void setMinorEdit(boolean minorEdit) {
this.minorEdit = minorEdit;
}

public String getLang() {
return lang;
}

public void setLang(String lang) {
this.lang = lang;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ public class Page {

private Map<String, List<MetadataItem>> metadatas;

private String lang;

public Page(String name) {
this.name = name;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,6 @@ public class PageHistory {
private Date updatedDate;

private String content;

private String lang;
}
Original file line number Diff line number Diff line change
Expand Up @@ -207,4 +207,50 @@ public default List<Attachment> getAttachmentsOfPage(Page page, boolean loadCont
* @return the List pf pages of the wiki
*/
public List<Page> getPagesOfWiki(String wikiType, String wikiOwner);

/**
* Retrieves list of page versions by page id and lang
*
* @param pageId page id
* @param lang content language
* @return {@link List} of {@link PageHistory}
*/
List<PageHistory> getPageHistoryVersionsByPageIdAndLang(Long pageId, String lang);

/**
* Retrieves latest draft of a specific page by target page id and content language
* and owner username
*
* @param targetPageId target page id
* @param username owner username
* @param lang content language
* @return {@link DraftPage}
*/
DraftPage getLatestDraftPageByUserAndTargetPageAndLang(Long targetPageId, String username, String lang);

/**
* Retrieves the published note page version by its id and content language
* @param pageId page id
* @param lang content language
* @return {@link PageVersion}
*/
PageVersion getPublishedVersionByPageIdAndLang(Long pageId, String lang);

/**
* Retrieves list of available translations languages of a page
*
* @param pageId page id
* @return {@link List} of {@link String}
*/
List<String> getPageAvailableTranslationLanguages(Long pageId);

/**
* Retrieves a draft note by its id and content language
*
* @param draftNoteId draft note id
* @param lang content language
* @return {@link DraftPage}
*/
DraftPage getDraftPageByIdAndLang(Long draftNoteId, String lang);

}
Loading

0 comments on commit a17ff76

Please sign in to comment.